简体   繁体   English

cuda:如何将主机数据复制到3D cuda阵列并返回

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

The "cuda c programming guide" gives examples on using cuda arrays. “ cuda c编程指南”给出了有关使用cuda数组的示例。 Those examples are limited to 2D. 这些示例仅限于2D。 So in case of a 2D cuda array I would simply do the following to copy host data to device memory: 因此,在二维CUDA阵列的情况下,我只需执行以下操作即可将主机数据复制到设备内存中:

// 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. 我尝试将cuInputArray作为3D cuda数组使用相同的方法,但没有成功,但得到了无效的参数错误。

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: 为了将3D数据复制到GPU设备内存中,您需要执行以下操作:

  1. Allocate the memory space with cudaMalloc3D 使用cudaMalloc3D 分配内存空间
  2. Setup the input parameters with cudaMemcpy3DParms 使用cudaMemcpy3DParms 设置输入参数
  3. Copy input data from host to device with cudaMemcpy3D 使用cudaMemcpy3D 输入数据从主机复制到设备

Then, to get your data back to the host: 然后,将您的数据返回主机:

  1. Setup the output parameters with cudaMemcpy3DParms 使用cudaMemcpy3DParms 设置输出参数
  2. Copy output data from device to host with cudaMemcpy3D 使用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 . CUDA C编程指南的第3.2.2章设备存储器中有一个代码示例,该示例分配了一个宽×高×深3D浮点值数组,并显示了如何在设备代码中循环这些数组元素

Also the simpleTexture3D example of the CUDA SDK is a good starting point. 同样,CUDA SDK的simpleTexture3D示例也是一个很好的起点。

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. 只是一个建议 :准备您的代码以捕获CUDA错误并分析发生错误的情况,因为您可能会发现一些错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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