Cuda 使用带推力的std::BindSecond

Cuda 使用带推力的std::BindSecond,cuda,thrust,Cuda,Thrust,我试着用std::bindSecond加上推力。我有使用主机指针而不是设备指针编译的代码。我认为它们是相同的,应该在两种情况下都有效 // 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(nwor

我试着用std::bindSecond加上推力。我有使用主机指针而不是设备指针编译的代码。我认为它们是相同的,应该在两种情况下都有效

// 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”,但我在我们的任何头文件中都找不到它。

可以在推力中使用适配器。但是如果你想在GPU上使用它们,适配器必须是一个设备功能。这就是为什么第一个
copy_if
编译而第二个不编译的原因-您的谓词是一个宿主函数,而不是一个设备函数,并且
device_vector
的使用意味着设备编译轨迹


简而言之,如果您想要在GPU上使用适配器功能,您需要自己编写一个,标准库函数(
bind1st
bind2nd
bind
)不能使用。

谢谢。既然我想象标准stl既没有主机标识符也没有设备标识符,它只是默认为主机吗?@SolArnu:是的。未修饰的代码(因此没有设备或主机)默认为主机编译轨迹。使用占位符表达式
推力::占位符::\u 1==1
,而不是尝试使用类似
bind2nd
。这也是一个好主意。非常感谢。
 /opt/cuda/include/thrust/detail/internal_functional.h(99): warning: calling a
 __host__ function from a __host__ __device__ function is not allowed