我正在使用GeForce 210,計算能力1.2和CUDA 6.5。cudaPrintfInit和cudaPrintfDisplay失敗
我希望從我的CUDA內核中打印浮點值,我在項目目錄中包含了「cuPrintf.cu」和「cuPrintf.cuh」,並將它們包含在我的代碼中。它編譯良好,運行沒有錯誤,但沒有打印任何內容。這是我編譯我的代碼:
$ nvcc -arch=compute_12 test.cu
我讀到類似question然後包圍我的內核與cudaPrintfInit()和cudaPrintfDisplay()。
if(cudaPrintfInit() != cudaSuccess)
printf("cudaPrintfInit failed\n");
test_kernel<<<grid, block>>>(val);
if(cudaPrintfDisplay(stdout, true) != cudaSuccess)
printf("cudaPrintfDisplay failed\n");
cudaPrintfEnd();
我的內核是這樣的:
__global__ void test_kernel (float val){
i = blockIdx.x*BLOCK_X + threadIdx.x;
j = blockIdx.y*BLOCK_Y + threadIdx.y;
if(j == 20)
cuPrintf("%f is value, %d is j", val, j);
}
在編譯和運行,輸出爲:
cudaPrintfInit failed
cudaPrintfDisplay failed
我猜有可能是與我編譯的方式有問題,或cuPrintf不允許浮動打印?根據類似question的附加鏈接,問題是每個塊的線程超過最大值,但我的塊大小是16 x 16(所以不應該是問題)。 cudaPrintfInit和cudaPrintfDisplay顯示失敗!
我也運行CUDA安裝附帶的CUDA示例代碼「simplePrintf」。這是完美的。幫幫我!
您的問題中缺少塊的數量,即「網格」值。只是猜測是一個,所以j總是在'[0..15]'的範圍內,因此你永遠不會打印這個值。 – pQB 2015-03-13 12:52:10
@pQB不能解釋爲什麼會打印'cudaPrintfInit失敗'消息。對於此類問題,您[提供](http://stackoverflow.com/help/on-topic)提供了[MCVE](http://stackoverflow.com/help/mcve)。提供一個簡短的完整代碼,顯示你在做什麼。此外,確保你正在做[適當的cuda錯誤檢查](http://stackoverflow.com/questions/14038589/what-is-the-canonical-way-to-check-for-errors-using-the-cuda- runtime-api) – 2015-03-13 14:19:08
沒有總的網格是256 x 256,所以16 x 16塊來掩蓋整個網格。無論如何,我可以解決這個問題,我在下面添加了另一個答案。 – user2774555 2015-03-19 20:34:33