pythoncudagpucupy

Cupy copy numpy array to existing device array


I would like to copy a numpy array on an existing, pre-allocated, gpu array.

I've seen that cupy offers the functions copy and copyto, however the former does not allow to specify the destination array, while the latter works only between device arrays.

Is there any way to achieve copying a numpy array to the device making usage of pre-allocated memory?


Solution

  • What paleonix commented indeed works fine, i.e. something like this:

    cupy.cuda.runtime.memcpy(cupy_array.data.ptr, numpy_array.ctypes.data, numpy_array.nbytes, cupy.cuda.runtime.memcpyHostToDevice)