2012-11-28 99 views
3

我們需要做些什麼來使用cuPrintf()? (設備計算能力爲1.2,Ubuntu的12)我找不到「cuPrintf.cu」和「cudaPrintf.cuh」,於是我下載了他們的代碼,並將它們包括:我們如何使用cuPrintf()?

#include "cuPrintf.cuh" 
#include "cuPrintf.cu" 

順便說一句,這是其他地區驗證碼:

__global__ void hello_kernel (float f) { 
printf ("Thread number %d. f = %d\n", threadIdx.x, f); 
} 

    int main() { 
    dim3 gridSize = dim3 (1); 
    dim3 blockSize = dim3 (16); 
    cudaPrintfInit(); 
    hello_kernel <<< gridSize, blockSize >>> (1.2345f); 
    cudaPrintfDisplay (stdout, true); 
    cudaPrintfEnd(); 
    return (0); 
} 

但NVCC還是給出了一個錯誤:

[email protected]:~/CUDA/matrixMult$ nvcc printfTest.cu -o printfTest 

printfTest.cu(5): error: calling a __host__ function("printf") from a __global__ 
function("hello_kernel") is not allowed 

謝謝!

+0

調用'cuPrintf'在內核中沒有'printf' – talonmies

回答

3

在你的內核,而不是這樣的:

printf ("Thread number %d. f = %d\n", threadIdx.x, f); 

你應該這樣做:

cuPrintf ("Thread number %d. f = %d\n", threadIdx.x, f); 

其他,我相信你的代碼是正確的(我的作品)。

This SO question/answer給出了關於正確使用cuPrintf的更多提示。

+0

糟糕!首先我嘗試使用printf(),但後來發現計算能力低於2.0。最後我忘了將printf()更改爲cuPrintf()。對不起,愚蠢的問題:( – Max

-1

包括<stdio.h>並與-arch=sm_20編譯。

詳細

代碼:

#include <stdio.h> 
__global__ void hello_kernel (float f) { 
    printf ("Thread number %d. f = %d\n", threadIdx.x, f); 
} 

int main(){ 
    return 0; 
} 

編譯:

nvcc -arch=sm_20 -o printfTest printfTest.cu 
+2

由於設備是計算能力1.2,這不會工作。 –

+0

這是內核printf,而不是cuPrintf – talonmies

+0

糟糕!我錯過了計算能力。 – ahmad