我有Nvidia geforce 310M,当我写这段代码时
int main()
{
int dev;
cudaDeviceProp prop;
cudaGetDevice(&dev);
cudaGetDeviceProperties(&prop,dev);
cout << "Device name " << prop.name << endl;
cout << "warp size = " << prop.warpSize << endl;
cout << "mutiprocesosrs = " << prop.multiProcessorCount << endl;
return 0;
}
它写道:
Device name ♀
warp size = -2144521342
mutiprocesosrs = 0
Press any key to continue . . .
每次运行此程序时都会生成不同的warp大小。 我该如何解决这个问题?
(p.s。我正在使用visual studio 2010)
答案 0 :(得分:3)
首先检查函数调用是否成功!
cudaDeviceProp prop;
int dev;
if (cudaGetDevice(&dev) != cudaSuccess)
{
/* error */
abort();
}
if (cudaGetDeviceProperties(&prop, dev) != cudaSuccess)
{
/* error */
abort();
}
我创建了一个用于调用所有CUDA函数的宏,它负责检查并抛出自定义异常:
#define CUDACALL(F, ARGS...) do { e = F(ARGS); if (e != cudaSuccess) throw cudaException(#F, e); } while (false)
有了这个,我只想说,
try
{
int dev;
cudaDeviceProp prop;
CUDACALL(cudaGetDevice, &dev);
CUDACALL(cudaGetDeviceProperties, &prop, dev);
// ...
}
catch (cudeException const & e)
{
std::cerr << "Cuda error: " << e.what() << "\n";
}
异常类定义如下:
struct cudaException : public std::exception
{
cudaException(const std::string & str, cudaError_t err) : s(str), e(err) { }
~cudaException() throw() { }
virtual const char * what() const throw()
{
const std::string r = s + ": \"" + cudaGetErrorString(e) + "\" (" + (char)(e + '0') + ')';
return r.c_str();
}
private:
std::string s;
cudaError_t e;
};
答案 1 :(得分:1)
使用此代码,您可以检查您的cuda设置是否有误。 (取自cuda deviceQuery样本)
if (cudaGetDeviceCount((int*)&_deviceCount) != cudaSuccess)
{
printf("ERROR: CUDA Driver and Runtime version may be mismatched.\n");
return false;
}
// This function call returns 0 if there are no CUDA capable devices.
if (_deviceCount == 0)
{
printf("ERROR: There is no device supporting CUDA!\n");
return false;
}
_deviceProperties = SAVE_ALLOCATE(cudaDeviceProp, _deviceCount * sizeof(cudaDeviceProp));
for (unsigned int dev = 0; dev < _deviceCount; ++dev)
{
cudaGetDeviceProperties(&_deviceProperties[dev], dev);
printf("\nDevice %d: \"%s\"\n", dev, _deviceProperties[dev].name);
#if CUDART_VERSION >= 2020
// Console log
cudaDriverGetVersion(&_driverVersion);
printf(" CUDA Driver Version: %d.%d\n", _driverVersion/1000, _driverVersion%100);
cudaRuntimeGetVersion(&_runtimeVersion);
printf(" CUDA Runtime Version: %d.%d\n", _runtimeVersion/1000, _runtimeVersion%100);
#endif
printf(" CUDA Capability revision number: %d.%d\n",
_deviceProperties[dev].major,_deviceProperties[dev].minor);
}
你应该看看是否有问题;)