Cuda 如何提升推力函数中的迭代器

Cuda 如何提升推力函数中的迭代器,cuda,thrust,system-error,Cuda,Thrust,System Error,我正在研究推力。但我不知道如何获得迭代器指向的值 示例代码如下所示: #include <thrust/for_each.h> #include <thrust/device_vector.h> #include <iostream> #include <vector> using namespace std; class ADD { private: typedef typename thrust::device_vector<i

我正在研究推力。但我不知道如何获得迭代器指向的值

示例代码如下所示:

#include <thrust/for_each.h>
#include <thrust/device_vector.h>
#include <iostream>
#include <vector>
using namespace std;

class ADD
{
private:
    typedef typename thrust::device_vector<int>::iterator PTR;
public:
    ADD(){}
    ~ADD(){}
    void setPtr(PTR &ptr)
    {this->ptr=ptr;}
    __host__ __device__
    void operator()(int &x)
    {
            // note that using printf in a __device__ function requires
            // code compiled for a GPU with compute capability 2.0 or
            // higher (nvcc --arch=sm_20)
            x+=add();
    }
    __host__ __device__
    int add()
    {return *ptr++;}
private:
    PTR ptr;
};
int main()
{
    thrust::device_vector<int> d_vec(3);
    d_vec[0] = 0; d_vec[1] = 1; d_vec[2] = 2;
    thrust::device_vector<int>::iterator itr=d_vec.begin();
    ADD *addtest=new ADD();
    addtest->setPtr(itr);
    thrust::for_each(d_vec.begin(), d_vec.end(), *addtest);
    for(int i=0;i<3;i++)
            cout<<d_vec[i]<<endl;
    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
类添加
{
私人:
typedef typename推力::设备\向量::迭代器PTR;
公众:
添加(){}
~ADD(){}
无效设置PTR(PTR和PTR)
{this->ptr=ptr;}
__主机设备__
void运算符()(int&x)
{
//请注意,在设备函数中使用printf需要
//为计算能力为2.0或2.0的GPU编译的代码
//更高(nvcc--arch=sm_20)
x+=add();
}
__主机设备__
int add()
{return*ptr++;}
私人:
PTR-PTR;
};
int main()
{
推力:装置矢量d矢量(3);
d_vec[0]=0;d_vec[1]=1;d_vec[2]=2;
推力::设备向量::迭代器itr=d_向量.begin();
ADD*addtest=new ADD();
addtest->setPtr(itr);
推力::对于每个(d_vec.begin(),d_vec.end(),*addtest);

for(int i=0;i@Gang.Wang):我想你只是混淆了两种不同的东西:所有类似STL的功能,包括for\u、device\u向量迭代器等,只是一个仅存在于主机上的“facade”


While运算符()包含编译到CUDA内核并并行应用于向量的每个元素的实际GPU代码。因此,无法从函子访问device_vector::迭代器。

那么,如何执行类似操作?我想使用另一个向量更改向量的每个元素的值。您可以获得指向device me的实际指针如下所示:推力::原始指针\u转换(d_vec.data()),然后可以作为参数传递给你的函子。但我仍然不确定你到底想做什么,可能有更好的解决方案。如果你想对一对设备向量应用二进制操作,请看一下推力::转换()的第二个版本或推力::变换_if()
test.cu(28): warning: calling a host function("thrust::experimental::iterator_facade<thrust::detail::normal_iterator<thrust::device_ptr<int> > , thrust::device_ptr<int> , int, thrust::detail::cuda_device_space_tag, thrust::random_access_traversal_tag, thrust::device_reference<int> , long> ::operator *") from a __device__/__global__ function("printf_functor::add") is not allowed

test.cu(28): warning: calling a host function("thrust::experimental::iterator_facade<thrust::detail::normal_iterator<thrust::device_ptr<int> > , thrust::device_ptr<int> , int, thrust::detail::cuda_device_space_tag, thrust::random_access_traversal_tag, thrust::device_reference<int> , long> ::operator *") from a __device__/__global__ function("printf_functor::add") is not allowed