Problem with 2Dmemcpy

I successfully copy the 2D array into kernel via cudaMallocPitch and cudaMemcpy2D, and finish the computation correctly. But when I want to copy them back, there arouses a problem. Because when copy into the kernel, the array was actually pitched. For example, the original width of array is 389, and pitched width is 512 automatically by the compiler. My question is when I copy them back. Are all the 512 data copied back or just the original 389 data? How can I access different rows?

You should copy the pitched memory back (or at least I generally find it easier to do so). Then, you can access it in a variety of ways, i.e.

int height, width //the un-pitched dimensions

int pitchHeight, pitchWdth //the pitched dimensions

float *dest; //host memory, dimensions height x width

float *src;   //memcpyd memory, dimensions pitchHeight x pitchWidth

for (int i = 0; i < height; i++){

  for (int j = 0; j < width; j++){

    dest[i*width+j] = src[i*pitchWidth+j];      



or, you could extract a given pitched row, and use that

Hope that made sense…