我尝试用 cuda C 编写一个简单的示例, 我关注了有关此的截屏视频,但我得到了错误的结果
这是一个例子:
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include<windows.h>
#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<<< 1, SIZE >>>(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
请任何人帮忙解决这个问题!
最佳答案
我做了一些错误的评论,所以我会尝试修复我的错误并在这里给出正确的答案。首先,请参加与proper CUDA error checking相关的评论.
其次,GT210 (CC 1.2) 的最大线程 block 大小是 512,而不是我在一时的困惑中评论的 256。
也就是说,通过执行上述错误检查,您应该得到以下错误:
GPUassert: invalid device function
在这种情况下,此错误表明您为其编译代码的体系结构高于您用于运行示例的体系结构。您正在为 compute capability = 2.0
或更高版本的设备编译示例(正如您评论的那样),但随后您在具有 compute capability = 1.2
的 GT210 中执行代码.
因此,首先,针对相应的体系结构重新编译您的示例。改变
-gencode=arch=compute_20 TO -gencode=arch=compute_12
一旦您为您的架构成功编译示例,您将收到以下错误(因为您已经正在做 proper error checking ;)
GPUassert: invalid configuration argument
在这种情况下,错误表明您使用的资源多于您的体系结构(计算能力 1.2)可用的资源,因为您尝试启动 SIZE = 1024
的 block ,但最大线程Block Size 为512
,即不能配置超过 512 个线程的 block 。
因此,将 SIZE 调整为 512,一切都应该按预期工作。下面是你的例子,做 proper CUDA error checking .
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include<windows.h>
#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;
}
关于c - cuda 中的错误结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26790825/