1

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

talonmies
  • 70,661
  • 34
  • 192
  • 269
user1612986
  • 1,373
  • 3
  • 22
  • 38
  • also i realized that most probably i am doing something not good. say my vector is very big say 60000 element. ideally i will like to spawn 60000 kernel and copy the result of each kernel computation to the appropriate index of the vector. but i do not want to spawn 60000 vectors each with length 60000 (hopefully this is not what is happening in my code.) – user1612986 Jan 21 '13 at 16:41

1 Answers1

5

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

Community
  • 1
  • 1
kangshiyin
  • 9,681
  • 1
  • 17
  • 29