C语言 无法为 cufftComplex 数据类型分配 CUDA 设备内存



我正在尝试使用以下代码将 cufftComplex 数组分配到 CUDA 设备 (GEFORCE GTX 1080( 上的内存中:

cufftComplex *d_in, *d_out;
int ds = sizeof(cufftComplex) * width * height;
CUResult test_din = cuMemAlloc((void**)&d_in, ds);
CUResult test_dout = cuMemAlloc((void**)&d_out, ds);
printf("test_din:  %sn", cudaGetErrorString(test_din));
printf("test_dout:  %sn", cudaGetErrorString(test_dout));

当我运行此代码时,我得到的错误是:

test_din:初始化错误

test_dout:初始化错误

当我编译代码时,我确实收到有关使用 void** 的警告,但我看到的所有袖口示例,包括 Cuda 9.1 附带的代码示例,都包含 void** 类型转换。 警告的措辞如下:

/usr/local/cuda/

include/cuda.h:90:49:注意:预期的"CUdeviceptr *",但参数的类型为"void **">

我在这里做错了什么吗?

cuMemAlloc来自CUDA驱动程序API。

如果您研究任何适当的驱动程序 API 程序,您会发现您需要做的第一件事就是发布:

cuInit();

开始使用 CUDA。 也许您没有这样做(您应该提供MCVE(。 这是导致此特定错误的可能原因。

如果将 CUDA 驱动程序 API 和 CUDA 运行时 API 混合在一起,则会遇到其他断开连接。 对于大多数代码来说,它应该是不必要的,我不建议初学者使用它。

研究示例代码以了解如何使用其中一个。 例如,研究矢量添加示例代码以了解 CUDA 运行时 API 程序的基础知识。 研究相应的 vectorAddDrv 以了解 CUDA 驱动程序 API 程序的基础知识。

这里最简单的解决方法可能只是将您对cuMemAlloc的调用替换为cudaMalloc

cufftComplex *d_in, *d_out;
int ds = sizeof(cufftComplex) * width * height;
cudaError_t test_din = cudaMalloc((void**)&d_in, ds);
cudaError_t test_dout = cudaMalloc((void**)&d_out, ds);
printf("test_din:  %sn", cudaGetErrorString(test_din));
printf("test_dout:  %sn", cudaGetErrorString(test_dout));

最新更新