-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;
}
“
谢谢!这真是一个愚蠢的错误!感谢您指出。你是对的,它与CUDA无关! :) – user6883610