微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用推力 remove_if 的结果

如何解决如何使用推力 remove_if 的结果

我正在尝试使用推力 remove_if 并且我有一些问题。但是首先,documentation 中的示例无法正常工作

这里是代码(也包含文档代码

#include <thrust/host_vector.h>
#include <thrust/device_vector.h>

//#include <thrust/copy.h>
#include <thrust/remove.h>


#include <iostream>


template<typename T>
struct is_zero {
    __host__ __device__
    auto operator()(T x) const -> bool {
        return x == 0;
    }
};
struct is_even
{
  __host__ __device__
  bool operator()(const int x)
  {
    return (x % 2) == 0;
  }
};

int main(void){


int h_data[6] = {1,2,1,3};

const int N = 6;
int A[N] = {1,4,8,5,7};
int *new_end = thrust::remove_if(A,A + N,is_even());


int * d_data;

cudamalloc((void**)&d_data,6 * sizeof(int));

cudamemcpy(d_data,h_data,6 * sizeof(int),cudamemcpyHostToDevice);

thrust::device_ptr<int> dev_ptr(d_data);
thrust::device_vector<int> output;


thrust::remove_if(dev_ptr,dev_ptr+6,is_zero<int>());
//thrust::remove_if(d_data,d_data+6,is_zero<int>());  //--> segmentation fault



cudamemcpy(h_data,d_data,cudamemcpyDevicetoHost);

for(int i = 0; i < 6; i++)
        std::cout << "H[" << i << "] = " << h_data[i]<< std::endl;


for(int i = 0; i < 6; i++)
        std::cout << "new_end[" << i << "] = " << new_end[i]<< std::endl;

}

我运行这个,我得到了

H[0] = 1
H[1] = 2
H[2] = 1
H[3] = 3
H[4] = 1
H[5] = 3
new_end[0] = 8
new_end[1] = 5
new_end[2] = 7
new_end[3] = -491667200
new_end[4] = 541501445
new_end[5] = 2019959568

在文档中说

// The first three values of A are Now {1,7}
// Values beyond new_end are unspecified

零件编程的结果似乎有效(如果为零) 但是教程里的不是结果。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。