我想发送大小size
的在每一维的3D阵列src
,展平为大小length = size * size * size
的一维数组,为一个内核,计算结果并将其存储在dst
。但是,最后,dst
不正确地包含全部0。这里是我的代码:正确使用cudaMalloc3D与cudaMemcpy
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()
的正确用法是什么?
请让我知道我是否应该发布内核的最小测试用例,或者如果问题可以在上面的代码中找到。
考虑看看您可能会感兴趣[这个提问/回答(http://stackoverflow.com/questions/16119943/how-and-当我应该使用pitched指针与cuda-api) –
谢谢,我已经偶然发现,这是非常有益的。 –
现在可以在[从cuda 3D内存复制到线性内存:复制数据不在我预期的地方](http:// stackoverflow。COM /问题/ 16107480 /复制从 - CUDA-3D-存储器到线性存储器复制的数据 - 是 - 不其中-I-人口会/ 23052768#23052768)。 – JackOLantern