error using cudaMemcpy2D

Hello to all,

I have an array of float. Its size is (xdimydim3).

I want create a 2d matrix of size (xdim,ydim*3) on the device, using cudaMallocPitch and cudaMemcpy2d.

The code is:


void call_kernel(int xdim, int ydim, REAL* array, REAL* d_array, int argc, char** argv)


dim3 dBlock(BLOCK_X, BLOCK_Y, BLOCK_Z);

dim3 dGrid( (xdim)/dBlock.x,


CUT_DEVICE_INIT(argc, argv);

size_t pitch;

cudaMallocPitch((void**) &d_array, &pitch, xdimsizeof(REAL), 3ydim);

cudaMemcpy2D(d_array, pitch, array, xdimsizeof(REAL), xdimsizeof(REAL), 3*ydim, cudaMemcpyHostToDevice);



REAL* create3dArray(int xdim, int ydim)


REAL* ptr = (REAL*)malloc(xdimydim3*sizeof(REAL));

for (int i=0; i<xdim; i++)

  for (int j=0; j<ydim; j++)







int main(int argc, char** argv)


const int iDimX = 64;

const int iDimY = 64;

REAL* array3 = create3dArray(iDimX,iDimY);

REAL* d_array;

call_kernel(iDimX, iDimY, array3, d_array, argc, argv);



the code works correctly if the matrix is (xdim,ydim), and the array is of size (xdim*ydim)

but i get bus error on my mac-book if I try to set ydim to 3*ydim

can someone explain me why?

Thanks in advance


It looks like it would work. Did you try checking the return codes to see if allocation failed?

One other thing, in create3dArray your indices are backwards. But that shouldn’t cause the problem you’re seeing.

thank you very much for the answer:

I missed the “return ptr” in the function create3dArray.

could you explain me how to check the return code?

thank you again