假设我编译了一个使用CUDA Toolkit的程序,并且我在不支持所需计算能力的硬件上运行程序,或者甚至没有支持CUDA接口的NVIDIA GPU,我怎么知道编程水平?为了回退CPU程序或显示错误消息。
答案 0 :(得分:3)
如果您已经安装了样本的CUDA工具包,我建议您查看deviceQuery项目。这显示了有关如何在设备中查询属性(如Capability Major / Minor版本号)的示例。
附加短片段:
cudaSetDevice(dev);
cudaDeviceProp deviceProp;
cudaGetDeviceProperties(&deviceProp, dev);
printf("\nDevice %d: \"%s\"\n", dev, deviceProp.name);
// Console log
cudaDriverGetVersion(&driverVersion);
cudaRuntimeGetVersion(&runtimeVersion);
printf(" CUDA Driver Version / Runtime Version %d.%d / %d.%d\n", driverVersion/1000, (driverVersion%100)/10, runtimeVersion/1000, (runtimeVersion%100)/10);
printf(" CUDA Capability Major/Minor version number: %d.%d\n", deviceProp.major, deviceProp.minor);
至于系统是否没有GPU,你可以使用下面的代码片段,但我相信你需要在那时拥有静态库。
int deviceCount = 0;
cudaError_t error_id = cudaGetDeviceCount(&deviceCount);
if (error_id != cudaSuccess)
{
printf("cudaGetDeviceCount returned %d\n-> %s\n", (int)error_id, cudaGetErrorString(error_id));
exit(EXIT_FAILURE);
}
// This function call returns 0 if there are no CUDA capable devices.
if (deviceCount == 0)
{
printf("There are no available device(s) that support CUDA\n");
}
else
{
printf("Detected %d CUDA Capable device(s)\n", deviceCount);
}