【问题标题】:using std::bind2nd with thrust使用带有推力的 std::bind2nd
【发布时间】:2013-03-01 14:32:45
【问题描述】:

我正在尝试将 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

是否有另一种方法可以将适配器用于推力中的二进制函数?我在网络上的示例中看到有人使用“thrust::bind2nd”,但我在我们的任何头文件中都找不到。

【问题讨论】:

  • 对这个操作使用占位符表达式thrust::placeholders::_1 == 1,而不是尝试使用bind2nd之类的东西。
  • 这也是个好主意。谢谢。

标签: cuda thrust


【解决方案1】:

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

简而言之,如果你想在GPU上使用适配器功能,你需要自己写一个,标准库的(bind1stbind2ndbind)不能使用。

【讨论】:

  • 谢谢。由于我认为标准 stl 没有 hostdevice 标识符,它只是默认为 host 吗?
  • @SolArnu:是的。未修饰的代码(因此没有 __device__ 或 __host__)默认为主机编译轨迹。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-28
  • 1970-01-01
相关资源
最近更新 更多