首页 > 代码库 > CUDA三维数组

CUDA三维数组

http://hpcbbs.it168.com/forum.php?mod=viewthread&tid=1643

根据上面链接的帖子研究了下三维数组,就像他自己说的一样是有问题的,我自己修改了下,结果终于正确了。大家有兴趣的可以对照着看看。

整个过程关键参考了这篇文章http://www.xuebuyuan.com/685353.html

 1 #include <stdio.h> 2 //#include <cutil.h> 3 #include <helper_cuda.h>//替代cutil.h 4 #include <cuda_runtime.h> 5  6 #define Width  2 7 #define Height 2 8 #define Depth  2 9 int bmp1[Width][Height][Depth]={11,22,33,44,55,66,77,88};10 int bmp2[Width][Height][Depth];11 int main(){12 13     cudaPitchedPtr devPitchedPtr;14     cudaExtent extent = make_cudaExtent(Width * sizeof(int), Height, Depth);15     cudaMalloc3D(&devPitchedPtr, extent);16     cudaMemset3D(devPitchedPtr, 0, extent);17 18 19     cudaError status;20     cudaMemcpy3DParms HostToDev = { 0 };21     HostToDev.srcPtr = make_cudaPitchedPtr((void*)bmp1,Width * sizeof(int), Width, Height);22     HostToDev.dstPtr = devPitchedPtr;23     HostToDev.extent = extent;24     HostToDev.kind = cudaMemcpyHostToDevice;25     status = cudaMemcpy3D(&HostToDev);26     if (status != cudaSuccess){27         fprintf(stderr, "MemcpyHtD: %s\n", cudaGetErrorString(status));28     }29 30     cudaMemcpy3DParms DevToHost = { 0 };31     DevToHost.srcPtr = devPitchedPtr;32     DevToHost.dstPtr = make_cudaPitchedPtr((void*)bmp2, Width * sizeof(int), Width, Height);33     DevToHost.extent = extent;34     DevToHost.kind = cudaMemcpyDeviceToHost;35     status = cudaMemcpy3D(&DevToHost);36     if (status != cudaSuccess){37         fprintf(stderr, "MemcpyHtD: %s\n", cudaGetErrorString(status));38     }39     cudaFree(&devPitchedPtr);40 41     int i, j, k;42     for (i = 0; i<2; i++)43     for (j = 0; j<2; j++)44     for (k = 0; k<2; k++)45         printf("bmp2[%d][%d][%d]=%d\n", i, j, k, bmp2[i][j][k]);46 47     return 0;48 }

 

CUDA三维数组