将一组结构从cpu复制到GPU

时间:2014-04-25 22:19:11

标签: c arrays struct cuda

我提到了这篇文章:loading an array of structs with arrays onto cuda

在上面的帖子中给出的代码中,cpuPointArray(结构数组)使用malloc在CPU上分配内存,而结构成员(float * c和float * d)使用cudamalloc分配内存。

有人可以解释为什么要这样做。

另外,我不明白为什么在以下循环中(来自上面的链接),cpuPointArray用于将数据从设备复制到主机。当我用gpuPointArray替换cpuPointArray时,我得到了分段错误。使用cuda-gdb,我发现gpuPointArray.c是NULL。有人可以解释为什么它应该是NULL:

for (int k=0; k<16; k++){
        printf("creating memory on cpu for array c\n");
        outPointArray[k].c = (float*)malloc(16*sizeof(float));
        printf("creating memory on cpu for array d\n");
        outPointArray[k].d = (float*)malloc(16*sizeof(float));
        printf("copying memory values onto cpu array c\n");
        err = cudaMemcpy(outPointArray[k].c, cpuPointArray[k].c, 16*sizeof(float), cudaMemcpyDeviceToHost);
        checkerror(err, "copy array c from gpu to cpu");
        printf("copying memory values onto cpu array c\n");
        err = cudaMemcpy(outPointArray[k].d, cpuPointArray[k].d, 16*sizeof(float), cudaMemcpyDeviceToHost);
        checkerror(err, "copy array d from gpu to cpu");
        printf("bottom of loop %d\n", k);
    }

0 个答案:

没有答案