Search code examples
vectorvisual-studio-2015cudagputhrust

CUDA gpu vector


Recently, when I try to use CUDA programming, I want send a vector to GPU memory. Someone tells me that I can use thrust::device_vector and thrust::host_vector. I also read the help document, but still don't know how to send such a vector into the kernel function. My codes are as following:

thrust::device_vector<int> dev_firetime[1000];

__global__ void computeCurrent(thrust::device_vector<int> d_ftime)
{
    int idx = blockDim.x*blockIdx.x + threadIdx.x;
    printf("ftime = %d\n", d_ftime[idx]);   
}

In fact, I don't know how to send the vector to kernel function. If you know, please tell me something about this question, and are there any better way to complete the same function? Thanks very much!


Solution

  • Thrust device vectors cannot be passed directly to CUDA kernels. You need to pass a pointer to the underlying device memory to the kernel. This can be done like this:

    __global__ void computeCurrent(int* d_ftime)
    {
        int idx = blockDim.x*blockIdx.x + threadIdx.x;
        printf("ftime = %d\n", d_ftime[idx]);   
    }
    
    thrust::device_vector<int> dev_firetime(1000);
    int* d_ftime = thrust::raw_pointer_cast<int*>(dev_firetime.data());
    computeCurrent<<<....>>>(d_ftime);
    

    If you have an array of vectors, you need to do something like what is described here.