2016-09-27 78 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

相关问题