Creating a cupy device array from GPU Pointer
See original GitHub issueHi,
we develop a GPU-accelerated C++ code with Python bindings called WarpX.
If we are compiling for CPU, we currently expose our memory for user-side manipulation as numpy arrays, adopting the existing, C+±side managed memory directly via numpy.frombuffer
[code]:
PyBUF_WRITE = 0x200
buffer_from_memory = ctypes.pythonapi.PyMemoryView_FromMemory
buffer_from_memory.argtypes = (ctypes.c_void_p, ctypes.c_int, ctypes.c_int)
buffer_from_memory.restype = ctypes.py_object
buf = buffer_from_memory(pointer, dtype.itemsize*size, PyBUF_WRITE)
Looking at the cupy _generate
functions and cupy.ndarray
constructors, it is not immediately clear to us how we can create a non-owning cupy array in the same way. Do you have any hints/workflows on that? We found that there is no frombuffer equivalent in cupy.
We saw #3431 for mmaping a host-side binary into a device-side cupy array but are not fully sure if this is similar enough.
Alternatively, we currently considered creating a numpy array that points to a device-side pointer for its data, a strategy that worked for us for GPU-GPU code coupling as a wrapper before, but now wonder if we can make this a proper cupy array with functions like cupy.asarray(numpy_array_w_device_ptr)
. Would this adopt or copy out the data and should we add further flags to describe our GPU memory (ownership)?
Thank you already for your feedback!
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
There’s a
UnownedMemory
class that can wrap a foreign device pointer for creating a CuPy array on top of it, if it’s what you’re asking for. Check it out how CuPy talks to other libraries through the CUDA Array Interface: https://github.com/cupy/cupy/blob/a5b24f91d4d77fa03e6a4dd2ac954ff9a04e21f4/cupy/core/core.pyx#L2478-L2514ptr
there is the pointer address (cast tointptr_t
, which is also representable as a plain Python int). A nicer thing you can do is to support__cuda_array_interface__
in your Python layer 🙂 Let me know if this helps.Thank @leofang for pointing out my mistakes! I updated my snippet and it works great!