2011-11-03 101 views
3

我有一个计算能力2.0的GTX 570(费米架构)。我在我的电脑上有Cuda 4.0版本,我正在使用Ubuntu 10.10在Cuda 4.0中使用printf()编译错误

使用Cuda 4.0,可以在内核中使用printf()。下面是一个例子代码125页在CUDA 4.0 programming guide

#if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ < 200) 
#define printf(f, ...) ((void)(f, __VA_ARGS__),0) 
#endif 


__global__ void helloCUDA(float f) 
{ 
printf(“Hello thread %d, f=%f\n”, threadIdx.x, f); 
} 



void main() 
{ 
helloCUDA<<<1, 5>>>(1.2345f); 
cudaDeviceReset(); 
} 

我正在以下编译错误。

gaurish108 MyPractice: nvcc printf_inkernel.cu -o printf_inkernel 
printf_inkernel.cu(10): error: unrecognized token 

printf_inkernel.cu(10): error: expected an expression 

printf_inkernel.cu(10): error: unrecognized token 

printf_inkernel.cu(10): error: unrecognized token 

printf_inkernel.cu(10): error: unrecognized token 

printf_inkernel.cu(10): error: unrecognized token 

printf_inkernel.cu(10): error: unrecognized token 

printf_inkernel.cu(10): error: unrecognized token 

printf_inkernel.cu(15): warning: return type of function "main" must be "int" 

8 errors detected in the compilation of "/tmp/tmpxft_000014cd_00000000-4_printf_inkernel.cpp1.ii". 

为什么不识别printf?我试图加入国旗-arch=sm_20,但我得到了同样的错误。

回答

6

看起来你在printf的格式器字符串的任一末尾都有一个奇怪的引号字符。

如果您复制并粘贴此程序,它应该编译和运行没有错误:

#include <stdio.h> 

__global__ void helloCUDA(float f) 
{ 
    printf("Hello thread %d, f=%f\n", threadIdx.x, f); 
} 

int main() 
{ 
    helloCUDA<<<1, 5>>>(1.2345f); 
    cudaDeviceReset(); 
    return 0; 
} 

和输出:

$ nvcc -arch=sm_20 test.cu -run 
Hello thread 0, f=1.234500 
Hello thread 1, f=1.234500 
Hello thread 2, f=1.234500 
Hello thread 3, f=1.234500 
Hello thread 4, f=1.234500 

我不明白的怪异宏需要开始该程序。我会摆脱它。

+2

嘿非常感谢!代码现在正在工作。顺便说一句,你是来自斯坦福在线GPU视频讲座的Jared Hoberock吗?这些讲座很棒!非常感谢! – smilingbuddha