第二章
讲了这么多又复杂又恶心的东西,我们来讲点轻松的。
我们在之前的例子中,有这么三句代码不知道有没有引起你的注意:
int dev = 0;
cudaDeviceProp deviceProp;
CHECK(cudaGetDeviceProperties(&deviceProp, 0));
这个cudaDeviceProp是个什么东西呢?根据运行结果,我们可以发现这个结构体里面的某个元素是GPU的型号,那么我们通过实际例子来看看cudaDeviceProp到底能返回什么信息:
Example 2-10
#include <stdlib.h>
#include <cuda_runtime.h>
#include <stdio.h>
/*
* Display a variety of information on the first CUDA device in this system,
* including driver version, runtime version, compute capability, bytes of
* global memory, etc.
*/
#define CHECK(call) \
{ \
const cudaError_t error=call; \
if(error!=cudaSuccess) \
{ \
printf("Error: %s:%d, ", __FILE__, __LINE__); \
printf("code:%d, reason: %s\n",error,cudaGetErrorString(error)); \
exit(-10*error); \
} \
}
int main(int argc, char **argv)
{
printf("%s Starting...\n", argv[0]);
int deviceCount = 0;
cudaGetDeviceCount(&deviceCount);
if (deviceCount == 0)
{
printf("There are no available device(s) that support CUDA\n");
}
else
{
printf("Detected %d CUDA Capable device(s)\n", deviceCount);
}
int dev = 0, driverVersion = 0, runtimeVersion = 0;
CHECK(cuda