1 /*2 * Copyright Xu Hongzhi (northwest. School of Information Engineering). All rights reserved.3 * Data:2012-4-224 */5 //6 //This program is a demonstration of two-dimensional dynamic array of space applications and memory data with each other to copy both methods7#include <stdio.h>8 //#include <cutil_inline.h>9#include <iostream>Ten#include <cuda_runtime.h> One //#include <cutil.h> A using namespacestd; - - intMainintargcChar**argv) the { - - //Cut_device_init (argc, argv); //Start CUDA - #if1 + //Method 1. Progressive copy - float**cpu_orign, **cpu_ret;//host-side raw data, copy-back data + float**gpu;//device-side data A intwidth =5, height =3;//the width and height of the array atsize_t size =sizeof(float) *width;//width of data in bytes - intRow, col; - - //Request memory space, and initialize -Cpu_orign =New float*[height]; -Cpu_ret =New float*[height]; in for(row =0; Row < height; ++row) - { toCpu_orign[row] =New float[width]; +Cpu_ret[row] =New float[width]; - //Initializing Data the for(col =0; Col < width; ++Col) * { $Cpu_orign[row][col] = (float) (Row +col);Panax NotoginsengCpu_ret[row][col] =0.0f; - } the } + A //Request memory space and initialize theGPU =New float*[height]; + for(row =0; Row < height; ++row) - { $(Cudamalloc (void* *) &Gpu[row], size)); $(Cudamemset (Gpu[row),0, size)); - } - the //Copy host-side raw data to device side - for(row =0; Row < height; ++row)Wuyi (cudamemcpy (Gpu[row], Cpu_orign[row], size, cudamemcpyhosttodevice)); the - //copy device-side data to the host side to return data Wu for(row =0; Row < height; ++row) - (cudamemcpy (Cpu_ret[row], Gpu[row], size, cudamemcpydevicetohost)); About $ //print host side return data - for(row =0; Row < height; ++row) - { - for(col =0; Col < width; ++Col) Acout << Cpu_ret[row][col] <<" "; +cout <<Endl; the } - //free up memory and graphics space $ Free(cpu_orign); the Free(Cpu_ret); the for(row =0; Row < height; ++row) the (Cudafree (Gpu[row])); the #endif - in the #if0 the //Method 2: Overall copy About float**cpu_orign, **cpu_ret;//host-side raw data, copy-back data the float**gpu;//device-side data the intwidth =5, height =3;//the width and height of the array thesize_t size =sizeof(float) *width;//width of data in bytes + size_t pitch; - intRow, col; the Bayi //Request memory space, and initialize theCpu_orign =New float*[height]; theCpu_ret =New float*[height]; - for(row =0; Row < height; ++row) - { theCpu_orign[row] =New float[width]; theCpu_ret[row] =New float[width]; the //Initializing Data the for(col =0; Col < width; ++Col) - { theCpu_orign[row][col] = (float) (Row +col); theCpu_ret[row][col] =0.0f; the }94 } the the //Request memory space and initialize the(Cudamallocpitch (void* *) &GPU, &pitch, size, height));98(Cudamemset2d (GPU, Pitch,0, size, height)); About - //Copy host-side raw data to device side101 (Cudamemcpy2d (GPU, pitch, cpu_orign, size, size, height, cudamemcpyhosttodevice));102 103 //copy device-side data to the host side to return data104 (Cudamemcpy2d (cpu_ret, size, GPU, pitch, size, height, cudamemcpydevicetohost)); the 106 //print host side return data107 for(row =0; Row < height; ++row)108 {109 for(col =0; Col < width; ++Col) thecout << Cpu_ret[row][col] <<" ";111cout <<Endl; the }113 //free up memory and graphics space the Free(cpu_orign); the Free(Cpu_ret); the (Cudafree (GPU));117 #endif118System"Pause");119 //cut_exit (argc, argv); //Exit Cuda -}
http://blog.csdn.net/xhz1234/article/details/7487767
"Turn" cuda and two-dimensional dynamic array