Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion cuda_bindings/cuda/bindings/_lib/utils.pxd.in
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# SPDX-FileCopyrightText: Copyright (c) 2021-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-FileCopyrightText: Copyright (c) 2021-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE

cimport cuda.bindings.driver as driver
cimport cuda.bindings.cydriver as cydriver
cimport cuda.bindings.cyruntime as cyruntime
from libcpp.vector cimport vector
from cpython.buffer cimport PyBuffer_Release, Py_buffer

cdef class _HelperKernelParams:
cdef Py_buffer _pybuffer
Expand All @@ -18,6 +19,16 @@ cdef class _HelperInputVoidPtr:
cdef Py_buffer _pybuffer
cdef void* _cptr
cdef bint _pyobj_acquired

cdef struct _HelperInputVoidPtrStruct:
Py_buffer _pybuffer

cdef void * _helper_input_void_ptr(ptr, _HelperInputVoidPtrStruct *buffer)

cdef inline void * _helper_input_void_ptr_free(_HelperInputVoidPtrStruct *helper):
if helper[0]._pybuffer.buf != NULL:
Copy link
Member

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 helper is NULL?

PyBuffer_Release(&helper[0]._pybuffer)

{{if 'CUmemPool_attribute_enum' in found_types}}

cdef class _HelperCUmemPool_attribute:
Expand Down
48 changes: 32 additions & 16 deletions cuda_bindings/cuda/bindings/_lib/utils.pxi.in
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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}}

Expand Down
Loading