名称:
cudaMemcpyToArray – 在主机和设备间复制数据
概要:
cudaError_t cudaMemcpyToArray(struct cudaArray* dstArray,size_t dstX,size_t dstY,const void* src,size_t count,enum cudaMemcpyKind kind)
cudaError_t cudaMemcpyToArrayAsync(struct cudaArray* dstArray,size_t dstX,size_t dstY,const void* src,size_t count,enum cudaMemcpyKind kind,cudaStream_t stream)
说明
从src指向的存储器区域内将count个字节复制到一个CUDA数组dstArray,该数组的左上角从(dstX,dstY)开始,其中kind是cudaMemcpyHostToHost、cudaMemcpyHost-ToDevice、cudaMemcpyDeviceToHost或cudaMemcpyDeviceToDevice之一,用于指定复制的方向。
cudaMemcpyToArrayAsync()是异步的,可选择传入非零流参数,从而将其关联到一个流。它仅对分页锁定的主存储器有效,如果传入指向可分页存储器的指针,那么将返回一个错误。
返回值
相关返回值:
cudaSuccess
cudaErrorInvalidValue
cudaErrorInvalidDevicePointer cudaErrorInvalidMemcpyDirection
注意,如果之前是异步启动,该函数可能返回错误码。
注:
在《CUDA编程指导》中对,cudaMallocArray()函数的使用,个人觉得有错误。
enum cudaMemcpyKind kind ,应该是cudaMemcpyHostToHost、cudaMemcpyHost-ToDevice、cudaMemcpyDeviceToHost或cudaMemcpyDeviceToDevice之一。
在指导中使用的是cudaMemcpyToArray(cuArray,0,0,h_data,&channelDesc),channelDese为cudaChannelFormatDesc类型,不是cudaMemcpyKind.
/*********************************************************************/
/* This is a example of the CUDA program.*/
/*********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <cuda_runtime.h>
#include <cutil.h>
/************************************************************************/
/* myKernel */
/************************************************************************/
/************************************************************************/
/* Main CUDA */
/************************************************************************/
int main(int argc, char* argv[])
{
const int width=10;
const int height=10;
//初始化h_array
int h_array[width][height];
for (int i=0;i<width;i++)
for (int j=0;j<height;++j)
h_array[i][j]=j+i*64;
}
}
//以机构提channelDesc描述CUDA数组中的组件数量和数据类型
cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(32,0,0,0,cudaChannelFormatKindUnsigned);
cudaArray* cuArray;
cudaMallocArray(&cuArray,&channelDesc,width,height);
size_t sizeMem=width*height*sizeof(int);
size_t potX=0;
size_t potY=0;
cudaMemcpyToArray(cuArray,potX,potY,h_array,sizeMem,cudaMemcpyDeviceToHost);
cudaFreeArray(cuArray);
return 0;
}