我正在运行以下代码。但是,我得到的输出为
代码 38 出错
测试 = 0
设备计数= 0
代码 38 出错
测试2 = 0
我在 Ubuntu 上有一个 NVIDIA GTX 690 显卡。这是否意味着驱动程序未激活?
#include <iostream>
using namespace std;
__device__ __constant__ float* data;
template<class T> void allocOnly(T* deviceDest, size_t numElem)
{
cudaError_t errCode = cudaMalloc((void**)&deviceDest, numElem*sizeof(T));
if(errCode != cudaSuccess)
cout << "Got error with code " << errCode << endl;
}
int main()
{
float* test(0);
allocOnly<float>(test,10);
cout << "test = " << test << endl;
int deviceCount = 0;
cudaGetDeviceCount(&deviceCount);
cout << "deviceCount= " << deviceCount << endl;
float* test2(0);
cudaError_t errCode = cudaMalloc((void**)&test2, 10*sizeof(float));
if(errCode != cudaSuccess)
cout << "Got error with code " << errCode << endl;
cout << "test2 = " << test2 << endl;
return 0;
}
最佳答案
是的,您的系统设置有问题。尝试运行 nvidia-smi -a
从终端查看它的报告。
你应该这样做 proper cuda error checking调用cudaGetDeviceCount
(以及所有 cuda API 调用和内核调用)。 error returned from that API call也会很有启发性。
执行对 cudaGetDeviceCount
的调用对我来说也是有意义的。在任何其他 cuda API 调用之前(例如对 cudaMalloc
中的 allocOnly
的调用)。
关于cudaGetDeviceCount 返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17689335/