根据现有 GPU 指针构建 Cupy 数组
我想构建 GPU 上已存在的数组的 Cupy GPU 数组视图,并且我收到以下内容:
- 指向数组的指针。
- 我知道数据类型和数据大小。
- 我也得到了一个推介。
如何构造一个数组视图(最好避免复制)?我尝试了以下操作:
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)
但这似乎没有产生正确的对齐方式。具体来说,我在将音高整合到图片中时遇到了困难——这可能吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了解决它的方法。这确实可以通过
cupy
实现,但需要首先使用copy.cuda.runtime.memcpy2D
MemoryKind kind = 3
这是设备到设备的复制。这似乎是在不移动到主机的情况下创建正确的 cp.ndarray 的最佳方法。
I found a way to solve it. This is indeed possible with
cupy
but requires first moving (on device) 2D allocation to 1D allocation withcopy.cuda.runtime.memcpy2D
cp.empty
cupy.cuda.runtime.memcpy2D
, there we can set the pitch and width. We useMemoryKind 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.