-
Notifications
You must be signed in to change notification settings - Fork 245
[PERF]: Faster void * conversion #1616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -129,30 +129,46 @@ cdef class _HelperKernelParams: | |
| cdef class _HelperInputVoidPtr: | ||
| def __cinit__(self, ptr): | ||
| self._pyobj_acquired = False | ||
| if ptr is None: | ||
| self._cptr = NULL | ||
| elif isinstance(ptr, (int)): | ||
| # Easy run, user gave us an already configured void** address | ||
| try: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems we can avoid code duplication by replacing the try-except block with a call to the new helper like this? self._cptr = _helper_input_void_ptr(ptr, <_HelperInputVoidPtrStruct*><PyObject*>self)(I'm not so sure about the self casting, I think it's correct because they share the same layout.) |
||
| self._cptr = <void*><void_ptr>ptr | ||
| elif isinstance(ptr, (_driver["CUdeviceptr"])): | ||
| self._cptr = <void*><void_ptr>int(ptr) | ||
|
Comment on lines
-137
to
-138
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q: This path seems to be gone? |
||
| elif PyObject_CheckBuffer(ptr): | ||
| # Easy run, get address from Python Buffer Protocol | ||
| err_buffer = PyObject_GetBuffer(ptr, &self._pybuffer, PyBUF_SIMPLE | PyBUF_ANY_CONTIGUOUS) | ||
| if err_buffer == -1: | ||
| raise RuntimeError("Failed to retrieve buffer through Buffer Protocol") | ||
| self._pyobj_acquired = True | ||
| self._cptr = <void*><void_ptr>self._pybuffer.buf | ||
| else: | ||
| raise TypeError("Provided argument is of type {} but expected Type {}, {} or object with Buffer Protocol".format(type(ptr), type(None), type(int))) | ||
| except: | ||
| if ptr is None: | ||
| self._cptr = NULL | ||
| elif PyObject_CheckBuffer(ptr): | ||
| # Easy run, get address from Python Buffer Protocol | ||
| err_buffer = PyObject_GetBuffer(ptr, &self._pybuffer, PyBUF_SIMPLE | PyBUF_ANY_CONTIGUOUS) | ||
| if err_buffer == -1: | ||
| raise RuntimeError("Failed to retrieve buffer through Buffer Protocol") | ||
| self._pyobj_acquired = True | ||
| self._cptr = <void*><void_ptr>self._pybuffer.buf | ||
| else: | ||
| raise TypeError("Provided argument is of type {} but expected Type {}, {} or object with Buffer Protocol".format(type(ptr), type(None), type(int))) | ||
|
|
||
| def __dealloc__(self): | ||
| if self._pyobj_acquired is True: | ||
| PyBuffer_Release(&self._pybuffer) | ||
|
|
||
| @property | ||
| def cptr(self): | ||
| return <void_ptr>self._cptr | ||
| return <void_ptr>self._cptr | ||
|
|
||
|
|
||
| cdef void * _helper_input_void_ptr(ptr, _HelperInputVoidPtrStruct *helper): | ||
| helper[0]._pybuffer.buf = NULL | ||
| try: | ||
| return <void *><void_ptr>ptr | ||
| except: | ||
| if ptr is None: | ||
| return NULL | ||
| elif PyObject_CheckBuffer(ptr): | ||
| # Easy run, get address from Python Buffer Protocol | ||
| err_buffer = PyObject_GetBuffer(ptr, &helper[0]._pybuffer, PyBUF_SIMPLE | PyBUF_ANY_CONTIGUOUS) | ||
| if err_buffer == -1: | ||
| raise RuntimeError("Failed to retrieve buffer through Buffer Protocol") | ||
| return <void*><void_ptr>(helper[0]._pybuffer.buf) | ||
| else: | ||
| raise TypeError("Provided argument is of type {} but expected Type {}, {} or object with Buffer Protocol".format(type(ptr), type(None), type(int))) | ||
|
|
||
|
|
||
| {{if 'CUmemPool_attribute_enum' in found_types}} | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: Should we check first if
helperis NULL?