cuda的错误结果

我尝试用cuda C编写一个简单的例子,我关注这个屏幕截图,但结果不对

这是一个例子:

#include "cuda_runtime.h" #include "device_launch_parameters.h" #include  #include #define SIZE 1024 __global__ void VectorAdd(int *a, int *b, int *c, int n) { int i = threadIdx.x; if (i < n){ c[i] = a[i] + b[i]; } } int main() { int *a, *b, *c; int *d_a, *d_b, *d_c; cudaError_t cudaStatus; cudaStatus = cudaSetDevice(0); if (cudaStatus != cudaSuccess) { fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?"); } a = (int *)malloc(SIZE*sizeof(int)); b = (int *)malloc(SIZE*sizeof(int)); c = (int *)malloc(SIZE*sizeof(int)); cudaMalloc(&d_a, SIZE*sizeof(int)); cudaMalloc(&d_b, SIZE*sizeof(int)); cudaMalloc(&d_c, SIZE*sizeof(int)); for (int i = 0; i < SIZE; i++) { a[i] = i; b[i] = i; c[i] = 0; } cudaMemcpy(d_a, a, SIZE*sizeof(int), cudaMemcpyHostToDevice); cudaMemcpy(d_b, b, SIZE*sizeof(int), cudaMemcpyHostToDevice); cudaMemcpy(d_c, c, SIZE*sizeof(int), cudaMemcpyHostToDevice); VectorAdd<<>>(d_a, d_b, d_c, SIZE); if (cudaStatus != cudaSuccess) { fprintf(stderr, "cudaMemcpy failed!"); } cudaMemcpy(c, d_c, SIZE*sizeof(int), cudaMemcpyDeviceToHost); for (int i = 0; i < 10; ++i) printf("c[%d] = %d\n", i, c[i]); free(a); free(b); free(c); enter code here cudaFree(d_a); cudaFree(d_b); cudaFree(d_c); return 0; } 

结果是:

 c[0]=0 c[1]=0 c[2]=0 c[3]=0 c[4]=0 c[5]=0 c[6]=0 c[7]=0 c[8]=0 c[9]=0 

但我期待这个结果:

 c[0]=0 c[1]=2 c[2]=4 c[3]=6 c[4]=8 c[5]=10 c[6]=12 c[7]=14 c[8]=16 c[9]=18 

请任何人可以帮忙解决这个问题!

我做了一些错误的评论,所以我会尝试修复我的错误并在这里给出正确的答案。 首先,请参加与正确的CUDA错误检查相关的评论。

其次,GT210(CC 1.2)的最大线程块大小是512,而不是256,因为我在一个混乱的时刻评论。

也就是说,通过执行上述错误检查,您应该得到以下错误:

 GPUassert: invalid device function 

在这种情况下,此错误表示您编译代码的体系结构高于运行该示例所使用的体系结构。 您正在为compute capability = 2.0或更高的设备(如您所评论的)编译示例,但随后您在GT210中执行具有compute capability = 1.2

因此,首先,重新编译相应体系结构的示例。 改变

 -gencode=arch=compute_20 TO -gencode=arch=compute_12 

成功编译体系结构的示例后,您将收到以下错误(因为您已经正在进行正确的错误检查 ;)

 GPUassert: invalid configuration argument 

在这种情况下,错误表明您使用的资源多于您的体系结构可用的资源(计算能力1.2),因为您尝试启动SIZE = 1024但最大线程块大小为512块,也就是说,您可以不配置超过512个线程的块。

因此,将SIZE调整为512,一切都应该按预期工作。 下面是您的示例,进行适当的CUDA错误检查 。

 #include "cuda_runtime.h" #include "device_launch_parameters.h" #include  #include #define SIZE 1024 #define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); } inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true) { if (code != cudaSuccess) { fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line); if (abort) exit(code); } } __global__ void VectorAdd(int *a, int *b, int *c, int n) { int i = threadIdx.x; if (i < n){ c[i] = a[i] + b[i]; } } int main() { int *a, *b, *c; int *d_a, *d_b, *d_c; cudaError_t cudaStatus; cudaStatus = cudaSetDevice(0); if (cudaStatus != cudaSuccess) { fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?"); } a = (int *)malloc(SIZE*sizeof(int)); b = (int *)malloc(SIZE*sizeof(int)); c = (int *)malloc(SIZE*sizeof(int)); gpuErrchk( cudaMalloc(&d_a, SIZE*sizeof(int)) ); gpuErrchk( cudaMalloc(&d_b, SIZE*sizeof(int)) ); gpuErrchk( cudaMalloc(&d_c, SIZE*sizeof(int)) ); for (int i = 0; i < SIZE; i++) { a[i] = i; b[i] = i; c[i] = 0; } gpuErrchk( cudaMemcpy(d_a, a, SIZE*sizeof(int), cudaMemcpyHostToDevice) ); gpuErrchk( cudaMemcpy(d_b, b, SIZE*sizeof(int), cudaMemcpyHostToDevice) ); gpuErrchk( cudaMemcpy(d_c, c, SIZE*sizeof(int), cudaMemcpyHostToDevice) ); VectorAdd<<< 1, SIZE >>>(d_a, d_b, d_c, SIZE); gpuErrchk( cudaPeekAtLastError() ); gpuErrchk( cudaDeviceSynchronize() ); gpuErrchk( cudaMemcpy(c, d_c, SIZE*sizeof(int), cudaMemcpyDeviceToHost) ); for (int i = 0; i < 10; ++i) printf("c[%d] = %d\n", i, c[i]); free(a); free(b); free(c); // enter code here cudaFree(d_a); cudaFree(d_b); cudaFree(d_c); return 0; }