c++ - 用推力调用手写的CUDA内核

标签 c++ cuda thrust

因为我需要使用 CUDA 对大量数字进行排序,所以我使用了 thrust。到目前为止,还不错……但是当我想调用一个包含数据的 thrust::host_vector 的“手写”内核时怎么办?

我的方法是(缺少备份):

int CUDA_CountAndAdd_Kernel(thrust::host_vector<float> *samples, thrust::host_vector<int> *counts, int n) {

 thrust::device_ptr<float> dSamples = thrust::device_malloc<float>(n);
 thrust::copy(samples->begin(), samples->end(), dSamples);

 thrust::device_ptr<int> dCounts = thrust::device_malloc<int>(n);
 thrust::copy(counts->begin(), counts->end(), dCounts);

 float *dSamples_raw = thrust::raw_pointer_cast(dSamples);
 int *dCounts_raw = thrust::raw_pointer_cast(dCounts);

 CUDA_CountAndAdd_Kernel<<<1, n>>>(dSamples_raw, dCounts_raw);

 thrust::device_free(dCounts);
 thrust::device_free(dSamples);
}

内核看起来像:

__global__ void CUDA_CountAndAdd_Kernel_Device(float *samples, int *counts) 

但是编译失败:

error: argument of type "float **" is incompatible with parameter of type "thrust::host_vector> *"

嗯?!我以为我在给 float 和 int 原始指针?还是我遗漏了什么?

最佳答案

您正在使用调用所在函数的名称调用内核,而不是内核的名称 - 因此参数不匹配。

改变:

CUDA_CountAndAdd_Kernel<<<1, n>>>(dSamples_raw, dCounts_raw);

CUDA_CountAndAdd_Kernel_Device<<<1, n>>>(dSamples_raw, dCounts_raw);

看看会发生什么。

关于c++ - 用推力调用手写的CUDA内核,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2398031/

相关文章:

c++ - Gitlab CI 运行程序

c++ - 如果在 C++ 中内联一个调用 self 的函数会发生什么

c++ - 矩阵的平均四分之一

c++ - 检查矩阵是否包含 CUDA 中的 nans 或无限值

cuda - 具有自定义数据类型的推力矢量

c++ - std::thread 成员函数。这个指针应该访问类字段吗?

c++ - 我无法让我的调试器在第一次出现异常时停止中断

python - PyCUDA:查询设备状态(特别是内存)

cuda - OSX 上的 NVIDIA Cuda 错误 "all CUDA-capable devices are busy or unavailable"

cuda - 多 GPU 与 CUDA Thrust 的使用