我正在尝试使用以下代码将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: %s\n", cudaGetErrorString(test_din));
printf("test_dout: %s\n", cudaGetErrorString(test_dout));
运行此代码时,我得到的错误是:
test_din:初始化错误
test_dout:初始化错误
编译代码时,我确实收到有关使用void **的警告,但是我见过的所有cufft示例(包括Cuda 9.1随附的代码示例)都包含void **类型转换。警告的措词如下:
/usr/local/cuda/include/cuda.h:90:49:注意:预期为“ CUdeviceptr *”,但参数的类型为“ void **”
这里有明显的地方我做错了吗?
答案 0 :(得分:2)
cuMemAlloc
来自CUDA驱动程序API。
如果学习任何适当的驱动程序API程序,您将发现需要做的第一件事就是发出:
cuInit();
开始使用CUDA。也许您还没有这样做(应该提供MCVE)。这可能是导致此特定错误的原因。
如果将两者混合使用,则会在CUDA驱动程序API和CUDA运行时API之间遇到其他断开连接。对于大多数代码来说,它不是必需的,而且我不建议初学者使用。
研究示例代码以了解如何使用其中一种。例如,研究vectorAdd示例代码以了解CUDA runtime API程序的基础。学习相应的vectorAddDrv,以了解CUDA driver 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: %s\n", cudaGetErrorString(test_din));
printf("test_dout: %s\n", cudaGetErrorString(test_dout));