我是gpu编码的新手。我尝试在youtube上运行简单的代码,但无法运行。 当我尝试运行它时,它在定义数组a的值时停在了行 a [i] = i; 并显示消息:
first_sample.exe中0x00007FF63DF51284的未处理异常: 0xC0000005:访问冲突读取位置0xFFFFFFFFFFFFFFFFFF。
#include <stdio.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;
cudaMallocManaged(&a, 1024 * sizeof(int));
cudaMallocManaged(&b, SIZE * sizeof(int));
cudaMallocManaged(&c, SIZE * sizeof(int));
//for (int i = 0; i < SIZE; ++i)
for (int i = 0; i < SIZE; ++i)
{
a[i] = i;
b[i] = i;
c[i] = 0;
}
VectorAdd <<< 1, SIZE>>> (a, b, c, SIZE);
cudaDeviceSynchronize();
for (int i = 0; i < 10; ++i)
printf("c[%d] = %d\n", i, c[i]);
cudaFree(a);
cudaFree(b);
cudaFree(c);
return 0;
}