简体   繁体   中英

cuda: how to copy host data to 3D cuda array and back

The "cuda c programming guide" gives examples on using cuda arrays. Those examples are limited to 2D. So in case of a 2D cuda array I would simply do the following to copy host data to device memory:

// Copy to device memory some data located at address h_data in host memory  
// cuInputArray is a 2D cuda array
cudaMemcpyToArray(cuInputArray, 0, 0, host_data, size_bytes, cudaMemcpyHostToDevice);
// with e.g. size_bytes := size_arr_x * size_arr_y * sizeof(float)

I tried the same approach with cuInputArray being a 3D cuda array without much success, getting invalid argument errors.

So how would get my host data to device memory AND back?

In order to copy 3D data into the GPU device memory, you need to do the following:

  1. Allocate the memory space with cudaMalloc3D
  2. Setup the input parameters with cudaMemcpy3DParms
  3. Copy input data from host to device with cudaMemcpy3D

Then, to get your data back to the host:

  1. Setup the output parameters with cudaMemcpy3DParms
  2. Copy output data from device to host with cudaMemcpy3D

The Chapter 3.2.2 Device Memory of the CUDA C Programming Guide has a code sample that allocates a width×height×depth 3D array of floating-point values and shows how to loop over the array elements in device code .

Also the simpleTexture3D example of the CUDA SDK is a good starting point.

Just a recommendation : Prepare your code to catch CUDA error and analyse what is happening in case of error because you would probably find a few.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM