pythongpucupy

Cupy array construction from existing GPU pointer


I would like to construct a Cupy GPU array view of the array that already exists on the GPU and I'm handed the following:

  1. Pointer to the array.
  2. I know the data type and the size of the data.
  3. I'm also given a pitch.

How one would construct an array view (avoiding copies preferably)? I tried the following:

import cupy as cp
import numpy as np 

shape = (w, h, c, b) # example
s = np.product(shape)*4 # this is 1D 

mem = cp.cuda.UnownedMemory(ptr=image_batch_ptr,
                            owner=None,
                            size=s)
memptr = cp.cuda.MemoryPointer(mem, 0)
d = cp.ndarray(shape=shape,
               dtype=np.float32,
               memptr=memptr)

But this does not seem to produce the correct alignment. Specifically, I'm having trouble with integrating pitch into the picture -- is it even possible?


Solution

  • I found a way to solve it. This is indeed possible with cupy but requires first moving (on device) 2D allocation to 1D allocation with copy.cuda.runtime.memcpy2D

    1. We initialise an empty cp.empty
    2. We copy the data from 2D allocation to that array using cupy.cuda.runtime.memcpy2D, there we can set the pitch and width. We use MemoryKind kind = 3 which is the device to device copy.

    This seems to be the optimal way to create a proper cp.ndarray without moving to host.