使用std :: bind2nd与推力 [英] using std::bind2nd with thrust

查看:181
本文介绍了使用std :: bind2nd与推力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用std :: bind2nd与推力。我有一个与主机指针,但不与设备指针编译的代码。我认为他们是相同的,应该在这两种情况下工作。

I'm trying to use std::bind2nd with thrust. I have code that compiles with a host pointer, but not with a device pointer. I think they are identical and should work in both cases.

// 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时,我得到以下错误:

When I try to call the second copy_if with the bind2nd I get the error below:

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

有没有另一种方法使用适配器的二进制函数推力?我看到一些人在网上的例子中使用thrust :: bind2nd,但我在任何头文件中找不到。

Is there another way to using adapters for binary functions in thrust? I have seen some people using "thrust::bind2nd" in examples on the web but I can't find that in any of our header files.

推荐答案

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

It is possible to use adaptors in thrust. But if you want to use them on the GPU, the adaptor must be a __device__ function. This is why the first copy_if compiles and the second doesn't - your predicate is a host function, not a device function and the use of device_vector implies a device compilation trajectory.

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

In short, if you want an adaptor function for use on the GPU, you will need to write one yourself, the standard library ones (bind1st, bind2nd, bind) can't be used.

这篇关于使用std :: bind2nd与推力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆