2016-09-27 81 views
-1

我正在通過「CUDA by Example」書籍的例子。下面的代碼並沒有給我一個答案,而是按照它應該做的那樣工作。錯誤在哪裏?爲什麼這不是從設備複製到Cuda主機?

感謝您的幫助和解答。

我得到一個輸出,其內容 計算的GPU做得到了答案:& d 按ENTER鍵停止

#include "cuda_runtime.h" 
#include "device_launch_parameters.h" 
#include <iostream> 
#include <stdio.h> 

using namespace std; 

__global__ void add_integers_cuda(int a, int b, int *c) 
{ 
    *c = a + b; 
} 

int main(void) 
{ 
    int c; 
    int *dev_ptr; 

    cudaMalloc((void **)&dev_ptr, sizeof(int)); //allocate sizeof(int) bytes of contiguous memory in the gpu device and return the address of first byte to dev_ptr. 

// call the kernel 
    add_integers_cuda <<<1,1>>>(2,7,dev_ptr); 

    cudaMemcpy(&c, dev_ptr, sizeof(int), cudaMemcpyDeviceToHost); 

    printf("Calculation done on GPU yields the answer: &d\n",c); 

    cudaFree(dev_ptr); 

    printf("Press enter to stop."); 
    cin.ignore(255, '\n'); 

    return 0; 

} 

回答

2

&d不是一個正確的printf格式化字符的位置:

printf("Calculation done on GPU yields the answer: &d\n",c); 

你不會得到你所得到的輸出電視機。

您應該使用%d代替:

printf("Calculation done on GPU yields the answer: %d\n",c); 

這一具體問題有沒有關係,當然CUDA。

如果您剛學習並遇到問題,您可能還需要使用cuda-memcheck和/或使用proper CUDA error checking來運行CUDA代碼。但是,這些人都不會指出上述錯誤。

+0

謝謝!這真是一個愚蠢的錯誤!感謝您指出。你是對的,它與CUDA無關! :) – user6883610

相關問題