Cuda Memory Copy

Source: Internet
Author: User

Original link
1, cudamemcpy () <--> cudamalloc ()//Linear memory copy

1//Linear memory Copy 2 Cudamalloc ((void**) &dev_a, data_size); 3 cudamemcpy (Dev_a, Host_a, Data_size, Cudamemcpyhosttodevice);

2, Cudamemcpy2d () <-->cudamallocpitch ()//Linear memory copy

cudaerror_t cudamemcpy2d (        void *     DST,    size_t     dpitch,    const void *     src,    size_t     Spitch,    size_t     width,    size_t     height,    enum cudamemcpykind     kind     )    

Cases:

1 Cudamallocpitch ((void**) &devptr, &pitch, Width * sizeof (float), height);  2 cudamemcpy2d (void* dst,size_t dpitch,const void* src,size_t spitch,size_t width,size_t height,enum cudaMemcpyKind Kind )

3, Cudamemcpy2dtoarray () <-->cudamallocarray ()//(two-dimensional) linear memory to a copy of a 2-D array

1 cudaerror_t cudamemcpy2dtoarray    (     2     struct Cudaarray *     DST, 3     size_t     woffset, 4     size_t     Hoffset, 5     const void *     SRC, 6     size_t     spitch, 7     size_t     width, 8     size_t     Height, 9     enum Cudamemcpykind     kind     10)    

Cases:

 1 void MV (float *y, float *a, float *x, int m, int n) 2 {3 int blknum = (M >> 4) + ((M & 15)? 1:0); 4 int height = Blknum << 4; 5 int width = (n & 255)? (((n >> 8) + 1) << 8): N; 6 DIM3 Threads (16, 16); 7 dim3 Grid (Blknum, 1); 8 Cudaarray *d_a; 9 float *d_x, *d_y;10 cudachannelformatdesc channeldesc = cudacreatechanneldesc<float4> (); CudaMall Ocarray (&d_a, &channeldesc, Width >> 2, height), Cudamemcpy2dtoarray (d_a, 0, 0, A, n * sizeof (float), n * sizeof (float), M, Cudamemcpyhosttodevice); Cudabindtexturetoarray (Texrefa, d_a); Cudamalloc (void *) &amp ;d _x, n * sizeof (float)), cudamalloc (void * *) &d_y, M * sizeof (float)), and cudamemcpy (d_x, x, n * sizeof ( float), cudamemcpyhosttodevice), mv_kernel<<< grid, Threads >>> (d_y, D_a, d_x, M, n); cudame mcpy (y, d_y, M * sizeof (float), cudamemcpydevicetohost); Cudafree (d_y); 23 Cudafree (d_x); Cudaunbindtexture (TEXREFA); Cudafreearray (d_a); 26} 

4, Cudamemcpytoarray () <-->cudamallocarray ()//(1 D) linear memory to 2-d array copy

1 cudaerror_t cudamemcpytoarray (    2     struct Cudaarray *     dst,3     size_t     woffset,4     size_t     hoffset,5     const void *     src,6     size_t     count,7     enum cudamemcpykind     kind     8)    

Cases:

1 void initcudatexture (float *h_volume, float2 *velocity) 2 {3     cudachannelformatdesc desc = Cudacreatechanneldesc (32 , 0, 0, 0, cudachannelformatkindfloat); 4  5     cudamallocarray (&d_volumearray, &desc, 6  7     cudamemcpytoarray (D_volumearray, 0, 0, h_volume, sizeof (float) *128*128, cudamemcpydevicetodevice); 8  9     tex.normalized = true;10     tex.filtermode = cudafiltermodelinear;11     tex.addressmode[0] = Cudaaddressmodewrap;12     tex.addressmode[1] = cudaaddressmodewrap;13     cutilsafecall ( Cudabindtexturetoarray (Tex, D_volumearray)); 15 16}

5, Cudamemcpy3d () <-->cudamalloc3darray ()//(1 D) linear memory to 3-D array copy

1 cudaerror_t cudamemcpy3d (const struct cudamemcpy3dparms *     p)      2  3 struct Cudaextent {4   size_t width; 5< c4/>size_t height; 6   size_t depth; 7}; 8 struct Cudaextent make_cudaextent (size_t W, size_t H, size_t D); 9 struct Cudapos {   size_t x;12   size_t y;13 size_t z;14   };15 struct Cudapos make_cudapos (size_t x, size_t Y, size_t z); cudamemcpy3dparms struct {   cudaarray     *srcarray;19   struct Cudapos        srcpos ; the   struct cudapitchedptr srcptr;21   struct cudaarray     *dstarray;22   struct cudapos        dstpos;   cudapitchedptr dstptr;24   struct cudaextent     extent;25   enum cudamemcpykind   kind; 26};

Cases:

1 void initcudatexture (const uchar *h_volume, Cudaextent volumesize) 2 {3     cudachannelformatdesc Channeldesc = cudaCre Atechanneldesc<uchar> (); 4  5     cutilsafecall (Cudamalloc3darray (&d_volumearray, &channeldesc, Volumesize)); 6  7     Cudamemcpy3dparms Copyparams = {0}; 8     copyparams.srcptr = Make_cudapitchedptr ((void*) H_volume, volumesize.width*sizeof (Uchar), Volumesize.width, Volumesize.height); 9     Copyparams.dstarray = d_volumearray;10     copyparams.extent   = volumesize;11     copyparams.kind     = Cudamemcpyhosttodevice;12     Cutilsafecall (Cudamemcpy3d (&copyparams));     tex.normalized = true;15     Tex.filtermode = cudafiltermodelinear;16     tex.addressmode[0] = cudaaddressmodewrap;17     TEX.ADDRESSMODE[1] = cudaaddressmodewrap;18     tex.addressmode[2] = cudaaddressmodewrap;19     CutilSafeCall ( Cudabindtexturetoarray (Tex, D_volumearray, Channeldesc)); 21}

6, Cudamemcpytosymbol ()//copy to constant memory

1 __constant__ float constdata[256];2 float data[256];3 cudamemcpytosymbol (constdata, data, sizeof (data)); 4 Cudamemcpyfromsymbol (data, constdata, sizeof); 5 __device__ float devdata; Float value = 3.14f;6 cudamemcpytosymbol (devdata, &value, sizeof (float)); 7 __device__ float* Devpointer; float* ptr;8 Cudamalloc (&ptr, N. sizeof (float)), 9 Cudamemcpytosymbol (Devpointer, &ptr, sizeof (PTR));

Cuda Memory Copy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.