首页 > 代码库 > 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三维数组
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。