首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用std::bind2nd和推力

使用std::bind2nd和推力
EN

Stack Overflow用户
提问于 2013-03-01 22:32:46
回答 1查看 483关注 0票数 1

我正在尝试使用std::bind2nd的推力。我的代码可以用主机指针编译,但不能用设备指针编译。我认为它们是相同的,应该在这两种情况下都有效。

代码语言:javascript
复制
// 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时,我得到以下错误:

代码语言:javascript
复制
 /opt/cuda/include/thrust/detail/internal_functional.h(99): warning: calling a
 __host__ function from a __host__ __device__ function is not allowed

有没有其他方法可以在推力中使用二进制函数的适配器?我在网上看到过一些人在示例中使用“find::bind2nd”,但我在我们的头文件中找不到它。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-02 01:22:55

在推力中使用适配器是可能的。但是如果你想在图形处理器上使用它们,适配器必须是一个__device__函数。这就是为什么第一个copy_if会编译,而第二个不会--您的谓词是主机函数,而不是设备函数,device_vector的使用暗示了设备编译轨迹。

简而言之,如果你想要一个适配器函数在图形处理器上使用,你需要自己编写一个,标准库函数(bind1stbind2ndbind)是不能使用的。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15160162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档