Skip to content

Commit e46d095

Browse files
committed
manual backport #1616
1 parent df3160f commit e46d095

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

cuda_bindings/cuda/bindings/_lib/utils.pxd.in

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ cimport cuda.bindings.driver as driver
55
cimport cuda.bindings.cydriver as cydriver
66
cimport cuda.bindings.cyruntime as cyruntime
77
from libcpp.vector cimport vector
8+
from cpython.buffer cimport PyBuffer_Release, Py_buffer
89

910
cdef class _HelperKernelParams:
1011
cdef Py_buffer _pybuffer
@@ -14,10 +15,19 @@ cdef class _HelperKernelParams:
1415
cdef int _length
1516
cdef bint _malloc_list_created
1617

18+
cdef struct _HelperInputVoidPtrStruct:
19+
Py_buffer _pybuffer
20+
1721
cdef class _HelperInputVoidPtr:
18-
cdef Py_buffer _pybuffer
22+
cdef _HelperInputVoidPtrStruct _helper
1923
cdef void* _cptr
20-
cdef bint _pyobj_acquired
24+
25+
cdef void * _helper_input_void_ptr(ptr, _HelperInputVoidPtrStruct *buffer)
26+
27+
cdef inline void * _helper_input_void_ptr_free(_HelperInputVoidPtrStruct *helper):
28+
if helper[0]._pybuffer.buf != NULL:
29+
PyBuffer_Release(&helper[0]._pybuffer)
30+
2131
{{if 'CUmemPool_attribute_enum' in found_types}}
2232

2333
cdef class _HelperCUmemPool_attribute:

cuda_bindings/cuda/bindings/_lib/utils.pxi.in

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,30 +130,32 @@ cdef class _HelperKernelParams:
130130
cdef class _HelperInputVoidPtr:
131131
def __cinit__(self, ptr):
132132
self._pyobj_acquired = False
133+
self._cptr = _helper_input_void_ptr(ptr, &self._helper)
134+
135+
def __dealloc__(self):
136+
_helper_input_void_ptr_free(&self._helper)
137+
138+
@property
139+
def cptr(self):
140+
return <void_ptr>self._cptr
141+
142+
143+
cdef void * _helper_input_void_ptr(ptr, _HelperInputVoidPtrStruct *helper):
144+
helper[0]._pybuffer.buf = NULL
145+
try:
146+
return <void *><void_ptr>ptr
147+
except:
133148
if ptr is None:
134-
self._cptr = NULL
135-
elif isinstance(ptr, (int)):
136-
# Easy run, user gave us an already configured void** address
137-
self._cptr = <void*><void_ptr>ptr
138-
elif isinstance(ptr, (_driver.CUdeviceptr)):
139-
self._cptr = <void*><void_ptr>int(ptr)
149+
return NULL
140150
elif PyObject_CheckBuffer(ptr):
141151
# Easy run, get address from Python Buffer Protocol
142-
err_buffer = PyObject_GetBuffer(ptr, &self._pybuffer, PyBUF_SIMPLE | PyBUF_ANY_CONTIGUOUS)
152+
err_buffer = PyObject_GetBuffer(ptr, &helper[0]._pybuffer, PyBUF_SIMPLE | PyBUF_ANY_CONTIGUOUS)
143153
if err_buffer == -1:
144154
raise RuntimeError("Failed to retrieve buffer through Buffer Protocol")
145-
self._pyobj_acquired = True
146-
self._cptr = <void*><void_ptr>self._pybuffer.buf
155+
return <void*><void_ptr>(helper[0]._pybuffer.buf)
147156
else:
148157
raise TypeError("Provided argument is of type {} but expected Type {}, {} or object with Buffer Protocol".format(type(ptr), type(None), type(int)))
149158

150-
def __dealloc__(self):
151-
if self._pyobj_acquired is True:
152-
PyBuffer_Release(&self._pybuffer)
153-
154-
@property
155-
def cptr(self):
156-
return <void_ptr>self._cptr
157159

158160
{{if 'CUmemPool_attribute_enum' in found_types}}
159161

0 commit comments

Comments
 (0)