/*
initCUDA.c
*/
#include <stdio.h>
#include <cuda_runtime.h>
int initCUDA()
{
int count;
int i;
cudaGetDeviceCount(&count);
if(count == 0)
{
fprintf(stderr, "There is no device.\n");
return 0; /* 0 present false */
}
for(i = 0; i < count; i++)
{
cudaDeviceProp prop;
if(cudaGetDeviceProperties(&prop, i) == cudaSuccess)
if(prop.major >= 1)
break;
}
if(i == count)
{
fprintf(stderr, "There is no device supporting CUDA 1.x.\n");
return 0;
}
cudaSetDevice(i);
return 1; /* 1 present true */
}
代码内容如上.
我将其保存为initCUDA.c, 用nvcc -c initCUDA.c方式编译, 错误提示如下.
initCUDA.c: In function ‘initCUDA’:
initCUDA.c:20: error: ‘cudaDeviceProp’ undeclared (first use in this function)
initCUDA.c:20: error: (Each undeclared identifier is reported only once
initCUDA.c:20: error: for each function it appears in.)
initCUDA.c:20: error: expected ‘;’ before ‘prop’
initCUDA.c:21: error: ‘prop’ undeclared (first use in this function)
我将其保存为initCUDA.cu, 用nvcc -c initCUDA.cu方式编译, 就是正确的.
我对第一种编译方式产生的错误不能理解. 为什么会说cudaDeviceProp没有声明?
它应该是在cuda_runtime.h中声明了的. 不然第二种方式编译应该也会产生同样的错误.