question on copy a matrix, which copy function to use

For 2D array copy, which copy function should be use?

For cudaMemcpy2D(), what is the pitch value for host memory?


The typical use case for cudaMemcpy2D is for copy to/from a device allocation that was created with cudaMallocPitch (only). Other “2D” or simulated 2D matrix approaches probably don’t require cudaMemcpy2D. cudaMemcpy2D can also be used for strided accesses to otherwise flat allocations that may or may not be pitched, eg. to access a sub-matrix of a flat matrix.

Note that in either the cudaMemcpy case or the cudaMemcpy2D case, the data is expected to be “flat” ie. accessible via a single pointer (*) not a double pointer (**).

for the cudaMemcpy2D cases, normally host memory would be an unpitched allocation. Therefore the pitch is equal to the element size (in bytes) * the matrix width (in elements).