pycuda icon indicating copy to clipboard operation
pycuda copied to clipboard

Can pycuda.driver.memcpy_htod_async add a "size" parameter?

Open so2bin opened this issue 3 years ago • 2 comments

Hello, now "memcpy_htod_async" function only support paramter: pycuda.driver.memcpy_htod_async(dest, src, stream=None). Can you extends this api with additional paramter: size? Here size means how many bytes will be copyed. Or is there any other already exist function has the similay effect.

Thank you.

so2bin avatar Jul 10 '21 08:07 so2bin

I found the definition of memcpy_htod_async as here, you passed a fixed size of buf: buf_wrapper.m_buf.len: https://github.com/inducer/pycuda/blob/db6fb7edd8ed058f58df3d8b7e701a6843691a21/src/wrapper/wrap_cudadrv.cpp#L232

  void py_memcpy_dtoh_async(py::object dest, CUdeviceptr src, py::object stream_py)
  {
    py_buffer_wrapper buf_wrapper;
    buf_wrapper.get(dest.ptr(), PyBUF_ANY_CONTIGUOUS | PyBUF_WRITABLE);

    PYCUDA_PARSE_STREAM_PY;

    CUDAPP_CALL_GUARDED_THREADED(cuMemcpyDtoHAsync,
        (buf_wrapper.m_buf.buf, src, buf_wrapper.m_buf.len, s_handle));
  }

Cuda API has exported the parameter: size_t ByteCount, this paramter is very important in some scene as we may only want to transfer partial bytes from buffer: https://docs.nvidia.com/cuda/cuda-driver-api/group__CUDA__MEM.html#group__CUDA__MEM_1g56f30236c7c5247f8e061b59d3268362

Thank you.

so2bin avatar Jul 10 '21 08:07 so2bin

If you're copying to a numpy array, you can determine the transfer size simply by creating a view of the array, like a[0:100] to only copy the first 100 entries (assuming a 1D array).

inducer avatar Jul 11 '21 07:07 inducer