CUDA gpu vector



最近,当我尝试使用 CUDA 编程时,我想向 GPU 内存发送一个向量。有人告诉我,我可以使用 thrust::d evice_vector 和 thrust::host_vector。我也阅读了帮助文档,但仍然不知道如何将这样的向量发送到内核函数中。我的代码如下:

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 = %dn", d_ftime[idx]);   
}

事实上,我不知道如何将向量发送到内核函数。如果你知道,请告诉我一些关于这个问题的事情,有没有更好的方法来完成相同的功能?非常感谢!

推力设备向量不能直接传递给 CUDA 内核。您需要将指向底层设备内存的指针传递给内核。这可以像这样完成:

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

如果你有一个向量数组,你需要做一些类似于这里描述的事情。

最新更新