cudaMemcpy2D invalid pitch argument

Hi !
I am trying to copy a device buffer into another device buffer. I tried to use cudaMemcpy2D because it allows a copy with different pitch: in my case, destination has dpitch = width, but the source spitch > width. It seems that cudaMemcpy2D refuses to copy data to a destination which has dpitch = width. I am new to using cuda, can someone explain why this is not possible? Using width-1 bytes works, but the rightmost pixels are incomplete (as expected). Using cuda 11.3 on Ubuntu 20.04.

In my scenario, the src is allocated by cv::cuda::GpuMat() and the dst is from cudaMalloc().
The src buffer is a RGB image.

cudaMemcpy2D(dst_ptr, dst_step, src_ptr, src_step, width * CV_ELEM_SIZE(cv_type), height, cudaMemcpyDeviceToDevice) );

When dst_step = width = 1800, src_step = 2048, I get the error invalid pitch argument.
If using width = dst_step = src_step = 2048, the error goes away and the image is copied without problems.

Does cudaMemcpy2D needs the pitch values to be a multiple of 32 / 64 ?
Either way, where can I find such limitations? I did not find it in the cuda reference doc.

Thanks

None of the limitations you are imagining are true, from my perspective. I’m not an expert on OpenCV, but if you want to concoct a (complete) CUDA example that doesn’t use OpenCV, I’m sure we can sort it out .

  • You can use cudaMemcpy2D to copy to a destination buffer where dpitch=width
  • cudaMemcpy2D does not need any particular pitch values (does not need pitch values that are multiples of 32/64).

In the case you have shown, if the amount of data to copy (width*CV_ELEM_SIZE(cv_type)) per line is larger than the destination width (1800), then you’re going to have an error.

Note that all pitches are specified in bytes, not in elements.

Thanks @Robert_Crovella for the quick response!

Yes, all pitches are in bytes. I have improved my code and I am not able to reproduce the problem. Thanks anyway!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.