thrust::device_vector in CUDA [duplicate]

雨燕双飞 提交于 2019-12-31 07:09:46

问题


i am new to CUDA and is trying to learn the usage. can someone please help. i have the following in the main function (i am in visual studio and my source and header files are .cu and .cuh respectively)

 thrust::device_vector<float> d_vec(100);
 kernel<<<100,1>>>(d_vec);

and then in the kernel i have

    template <typename T> __global__ kernel(thrust::device_vector<T> d_vec)
    {  int tid = threadIdx.x + blockIdx.x*blockDim.x;
       T xxx = 3.0;
       d_vec[tid] = xxx;
     }

my objective is to call the kernel once with float and once with double. Also note that in this simple example i have variable xxx (which in my real case is some computation producing double or float numbers).

and i get two error: 1> calling a __host__ function (operator =) from a __global__ function is not allowed 2> calling a __host__ function (operator []) from a __global__ function is not allowed

so i guess "[]" and "=" in "d_vec[tid] = .." is the problem. But my question is how do i access the device vector inside my kernel. Can someone please clarify what is the correct procedure and what i am doing wrong. thanks in advance


回答1:


thrust::device_vector objects/references can not be used as kernel parameters. You could use raw pointers to pass the device vector data.

thrust::device_vector<float> d_vec(100);
float* pd_vec = thrust::raw_pointer_cast(d_vec.data());
kernel<<<100,1>>>(pd_vec);

and here's the prototype of the kernel

template <typename T> __global__ kernel(T* pd_vec)

You Q is similar to this one. how to cast thrust::device_vector<int> to raw pointer



来源:https://stackoverflow.com/questions/14443001/thrustdevice-vector-in-cuda

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!