本文介绍了Cudamemcpy函数用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下cudaMemcpy函数将如何工作?

How will the cudaMemcpy function work in this case?

我已经声明了这样的矩阵

I have declared a matrix like this

float imagen[par->N][par->M];

,我想将其复制到cuda设备,所以我做到了

and I want to copy it to the cuda device so I did this

float *imagen_cuda;

int tam_cuda=par->M*par->N*sizeof(float);

cudaMalloc((void**) &imagen_cuda,tam_cuda); 
cudaMemcpy(imagen_cuda,imagen,tam_cuda,cudaMemcpyHostToDevice);

将2d数组复制到1d数组中可以吗?

Will this copy the 2d array into a 1d array fine?

我如何复制到另一个2d数组?

And how can I copy to another 2d array? can I change this and will it work?

float **imagen_cuda;


推荐答案

处理加倍下标的C数组并非易事在主机和设备之间复制数据时。在大多数情况下, cudaMemcpy (包括 cudaMemcpy2D )期望用于源和目标的普通指针,而不是指向

It's not trivial to handle a doubly-subscripted C array when copying data between host and device. For the most part, cudaMemcpy (including cudaMemcpy2D) expect an ordinary pointer for source and destination, not a pointer-to-pointer.

最简单的方法(我认为)是压平主机和设备上的2D数组,并使用索引算法来模拟2D坐标:

The simplest approach (I think) is to "flatten" the 2D arrays, both on host and device, and use index arithmetic to simulate 2D coordinates:

float imagen[par->N][par->M];
float *myimagen = &(imagen[0][0]);
float myval = myimagen[(rowsize*row) + col];

然后您可以使用普通的cudaMemcpy操作来处理转移(使用 myimagen 指针):

You can then use ordinary cudaMemcpy operations to handle the transfers (using the myimagen pointer):

float *d_myimagen;
cudaMalloc((void **)&d_myimagen, (par->N * par->M)*sizeof(float));
cudaMemcpy(d_myimagen, myimagen, (par->N * par->M)*sizeof(float), cudaMemcpyHostToDevice);

如果您真的想处理动态大小(即编译时未知)的双下标数组,您可以查看。

If you really want to handle dynamically sized (i.e. not known at compile time) doubly-subscripted arrays, you can review this question/answer.

这篇关于Cudamemcpy函数用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 15:12