Skip to content

Commit f863ab2

Browse files
committed
Emulate pycuda/pyopencl more closely for .gpudata
1 parent 73f3822 commit f863ab2

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

pygpu/gpuarray.pyx

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2229,13 +2229,40 @@ cdef class GpuArray:
22292229
def __get__(self):
22302230
return self.ga.offset
22312231

2232+
property data:
2233+
"""Return a pointer to the raw OpenCL buffer object.
2234+
2235+
This will fail for arrays that have an offset.
2236+
"""
2237+
def __get__(self):
2238+
if self.context.kind != b"opencl":
2239+
raise TypeError("This is for OpenCL arrays.")
2240+
if self.offset != 0:
2241+
raise ValueError("This array has an offset.")
2242+
# This wizadry grabs the actual backend pointer since it's
2243+
# guarenteed to be the first element of the gpudata
2244+
# structure.
2245+
return <size_t>((<void **>self.ga.data)[0])
2246+
2247+
property base_data:
2248+
"Return a pointer to the backing OpenCL object."
2249+
def __get__(self):
2250+
if self.context.kind != b"opencl":
2251+
raise TypeError("This is for OpenCL arrays.")
2252+
# This wizadry grabs the actual backend pointer since it's
2253+
# guarenteed to be the first element of the gpudata
2254+
# structure.
2255+
return <size_t>((<void **>self.ga.data)[0])
2256+
22322257
property gpudata:
22332258
"Return a pointer to the raw backend object."
22342259
def __get__(self):
2260+
if self.context.kind != b"cuda":
2261+
raise TypeError("This is for CUDA arrays.")
22352262
# This wizadry grabs the actual backend pointer since it's
22362263
# guarenteed to be the first element of the gpudata
22372264
# structure.
2238-
return <size_t>((<void **>self.ga.data)[0])
2265+
return <size_t>((<void **>self.ga.data)[0]) + self.offset
22392266

22402267
def __str__(self):
22412268
return str(numpy.asarray(self))

0 commit comments

Comments
 (0)