[CUDA]检查系统是否存在支持CUDA的GPU

参照cuda官方sample:1_Utilities\deviceQuery,使用的函数是cudaGetDeviceCount

	int dev_num = 0;
	cudaError_t error_id = cudaGetDeviceCount(&dev_num);
	if (error_id != cudaSuccess)
	{
		printf("cudaGetDeviceCount returned %d\n-> %s\n", (int)error_id, cudaGetErrorString(error_id));
		printf("Result = FAIL\n");
        exit(EXIT_FAILURE);
	}
	
	// This function call returns 0 if there are no CUDA capable devices.
	if (dev_num == 0)
	{
		printf("There are no available device(s) that support CUDA\n");
	}
	else
	{
		printf("Detected %d CUDA Capable device(s)\n", dev_num);
	}

该代码可以运行在一个没有gpu、没有安装NVIDIA驱动的系统上,此时cudaGetDeviceCount将会返回错误代码35(驱动错误)

不要用opencv的接口来做,它内部也是调用cudaGetDeviceCount,但如果像上面返回错误代码了,程序将会直接退出,非常不好

int dev_num = cv::cuda::getCudaEnabledDeviceCount();  //如果内部cudaGetDeviceCount返回错误了程序将直接退出

你可能感兴趣的:(cuda)