【发布时间】:2013-05-10 10:17:57
【问题描述】:
我想在每个维度上发送一个大小为 size 的 3D 数组 src,将其展平为大小为 length = size * size * size 的一维数组,然后发送到内核中,计算结果并将其存储在 dst 中。但是,最后,dst 不正确地包含全 0。这是我的代码:
int size = 256;
int length = size * size * size;
int bytes = length * sizeof(float);
// Allocate source and destination arrays on the host and initialize source array
float *src, *dst;
cudaMallocHost(&src, bytes);
cudaMallocHost(&dst, bytes);
for (int i = 0; i < length; i++) {
src[i] = i;
}
// Allocate source and destination arrays on the device
struct cudaPitchedPtr srcGPU, dstGPU;
struct cudaExtent extent = make_cudaExtent(size*sizeof(float), size, size);
cudaMalloc3D(&srcGPU, extent);
cudaMalloc3D(&dstGPU, extent);
// Copy to the device, execute kernel, and copy back to the host
cudaMemcpy(srcGPU.ptr, src, bytes, cudaMemcpyHostToDevice);
myKernel<<<numBlocks, blockSize>>>((float *)srcGPU.ptr, (float *)dstGPU.ptr);
cudaMemcpy(dst, dstGPU.ptr, bytes, cudaMemcpyDeviceToHost);
为了清楚起见,我省略了对cudaMallocHost()、cudaMalloc() 和cudaMemcpy() 的错误检查。在任何情况下,这段代码都不会触发错误。
cudaMalloc3D() 和 cudaMemcpy() 的正确用法是什么?
如果我也应该发布内核的最小测试用例,或者是否可以在上面的代码中找到问题,请告诉我。
【问题讨论】:
-
您可能对this question/answer感兴趣
-
谢谢,我已经偶然发现了它,它非常有用。
标签: cuda