question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Creating a cupy device array from GPU Pointer

See original GitHub issue

Hi,

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:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
leofangcommented, Feb 9, 2021

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-L2514 ptr there is the pointer address (cast to intptr_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.

1reaction
Char-Aznablecommented, Aug 24, 2022

Thank @leofang for pointing out my mistakes! I updated my snippet and it works great!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cupy array construction from existing GPU pointer
Show activity on this post. I would like to construct a Cupy GPU array view of the array that already exists on the...
Read more >
cupy.ndarray — CuPy 11.4.0 documentation
Multi-dimensional array on a CUDA device. This class implements a subset of methods of numpy.ndarray . The difference is that this class allocates...
Read more >
cupy.cuda.MemoryPointer — CuPy 11.4.0 documentation
Pointer to a point on a device memory. An instance of this class holds a reference to the original memory buffer and a...
Read more >
Basics of CuPy — CuPy 11.4.0 documentation
CuPy has a concept of a current device, which is the default GPU device on which the allocation, manipulation, calculation, etc., of arrays...
Read more >
Interoperability — CuPy 11.4.0 documentation
For using CUDA streams created in foreign libraries in CuPy, ... that is, passing CUDA device pointers directly to MPI calls to avoid...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found