'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:
- Pointer to the array.
- I know the data type and the size of the data.
- 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 1:[1]
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
- We initialise an empty
cp.empty - We copy the data from 2D allocation to that array using
cupy.cuda.runtime.memcpy2D, there we can set the pitch and width. We useMemoryKind kind = 3which is the device to device copy.
This seems to be the optimal way to create a proper cp.ndarray without moving to host.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | LemurPwned |
