cudaMemcpy在简单复制期间抛出错误

时间:2012-01-13 15:23:13

标签: cuda gpgpu nvidia

下面是一小段代码,可将4个元素从数组复制到GPU。我真的不明白为什么cudaMemcpy在这种情况下会抛出错误。请帮忙

int size = 5;
float *a = (float*)malloc(size * sizeof(float));
a[0] = 1.0;
a[1] = 2.0;
a[2] = 3.0;
a[3] = 4.0;
a[4] = 5.0;

float *g;
cudaMalloc((void**)g, 4 * sizeof(float));
float *tem = a+2;
cudaError_t err = cudaMemcpy(g,a,4 * sizeof(float), cudaMemcpyHostToDevice);
if(err !=0){
    printf("Cudamemcpy threw error\n");
    getchar();  
}

1 个答案:

答案 0 :(得分:6)

我认为你错过了&符号:

cudaMalloc((void**)&g, 4 * sizeof(float));