我正在尝试使用std::bind2nd的推力。我的代码可以用主机指针编译,但不能用设备指针编译。我认为它们是相同的,应该在这两种情况下都有效。
// this compiles fine
thrust::host_vector<unsigned int> h_vec(nwords);
thrust::host_vector<unsigned int> h_map(nwords);
thrust::host_vector<unsigned int> h_out1(nwords);
thrust::copy_if(h_vec.begin(), h_vec.end(), h_map.begin(), h_out1.begin(),
std::bind2nd(thrust::equal_to<int>(),1));
// this compilation fails with the error below
thrust::device_vector<unsigned int> d_map(nwords);
thrust::device_vector<unsigned int> d_vec(nwords);
thrust::device_vector<unsigned int> d_out1(nwords);
thrust::copy_if(d_vec.begin(), d_vec.end(), d_map.begin(), d_out1.begin(),
std::bind2nd(thrust::equal_to<int>(),1));当我尝试用bind2nd调用第二个copy_if时,我得到以下错误:
/opt/cuda/include/thrust/detail/internal_functional.h(99): warning: calling a
__host__ function from a __host__ __device__ function is not allowed有没有其他方法可以在推力中使用二进制函数的适配器?我在网上看到过一些人在示例中使用“find::bind2nd”,但我在我们的头文件中找不到它。
发布于 2013-03-02 01:22:55
在推力中使用适配器是可能的。但是如果你想在图形处理器上使用它们,适配器必须是一个__device__函数。这就是为什么第一个copy_if会编译,而第二个不会--您的谓词是主机函数,而不是设备函数,device_vector的使用暗示了设备编译轨迹。
简而言之,如果你想要一个适配器函数在图形处理器上使用,你需要自己编写一个,标准库函数(bind1st,bind2nd,bind)是不能使用的。
https://stackoverflow.com/questions/15160162
复制相似问题