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:
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?
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
cp.empty
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.