2013-03-01 81 views
1

我想使用std :: bind2nd与推力。我有用主机指针编译的代码,但没有使用设备指针。我认为他们是相同的,应该在两种情况下都能工作。使用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)); 

当我尝试调用第二copy_if与bind2nd我得到的错误如下:

/opt/cuda/include/thrust/detail/internal_functional.h(99): warning: calling a 
__host__ function from a __host__ __device__ function is not allowed 

是否有另一种方式来使用适配器推力二元函数?我看到一些人在网上的例子中使用“thrust :: bind2nd”,但我无法在我们的任何头文件中找到它。

+1

对此操作使用占位符表达式'thrust :: placeholders :: _ 1 == 1',而不是尝试使用类似'bind2nd'的东西。 – 2013-03-01 19:42:01

+0

这也是一个好主意。谢谢。 – 2013-03-01 21:40:00

回答

3

可以在推力中使用适配器。但是如果你想在GPU上使用它们,则适配器必须是__device__函数。这就是第一个编译器copy_if编译的原因,第二个编译器不支持 - 您的谓词是主机函数,而不是设备函数,使用device_vector意味着设备编译轨迹。简而言之,如果您想要在GPU上使用适配器功能,则需要自己编写一个适配器函数,但不能使用标准库函数(bind1st,bind2nd,)。

+0

谢谢。因为我想标准stl没有__host__或__device__标识符,它只是默认为__host__? – 2013-03-01 18:52:40

+1

@SolArnu:是的。未修饰的代码(所以不\ \ \ \设备\ _ \ _或\ _ \ _主机\ _ \ _)默认为主机编译轨迹。 – talonmies 2013-03-01 20:22:33