From e9bee88c2967bd1d08c4ad9731000f4d266dafcd Mon Sep 17 00:00:00 2001 From: jakirkham Date: Mon, 21 Oct 2024 14:52:48 -0700 Subject: [PATCH 1/2] Mark all `nogil` `cdef` functions as `noexcept` (#306) These are all pure C functions implemented in Cython. They do not raise. Cython 3 adds checks for exceptions in these functions, which is unnecessary given none of these set exceptions. So add `noexcept` to turn off these Cython checks. xref: https://github.com/rapidsai/kvikio/pull/502 Authors: - https://github.com/jakirkham Approvers: - Peter Andreas Entschev (https://github.com/pentschev) URL: https://github.com/rapidsai/ucxx/pull/306 --- python/ucxx/ucxx/_lib/arr.pyx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python/ucxx/ucxx/_lib/arr.pyx b/python/ucxx/ucxx/_lib/arr.pyx index 6dfbd1c5..9fc8f806 100644 --- a/python/ucxx/ucxx/_lib/arr.pyx +++ b/python/ucxx/ucxx/_lib/arr.pyx @@ -245,7 +245,7 @@ cdef class Array: cdef inline bint _c_contiguous(Py_ssize_t itemsize, Py_ssize_t ndim, Py_ssize_t[::1] shape_mv, - Py_ssize_t[::1] strides_mv) nogil: + Py_ssize_t[::1] strides_mv) noexcept nogil: cdef Py_ssize_t i, s if strides_mv is not None: s = itemsize @@ -263,7 +263,7 @@ cdef inline bint _c_contiguous(Py_ssize_t itemsize, cdef inline bint _f_contiguous(Py_ssize_t itemsize, Py_ssize_t ndim, Py_ssize_t[::1] shape_mv, - Py_ssize_t[::1] strides_mv) nogil: + Py_ssize_t[::1] strides_mv) noexcept nogil: cdef Py_ssize_t i, s if strides_mv is not None: s = itemsize @@ -279,7 +279,7 @@ cdef inline bint _f_contiguous(Py_ssize_t itemsize, cdef inline bint _contiguous(Py_ssize_t itemsize, Py_ssize_t ndim, Py_ssize_t[::1] shape_mv, - Py_ssize_t[::1] strides_mv) nogil: + Py_ssize_t[::1] strides_mv) noexcept nogil: cdef bint r = _c_contiguous(itemsize, ndim, shape_mv, strides_mv) if not r: r = _f_contiguous(itemsize, ndim, shape_mv, strides_mv) @@ -292,7 +292,7 @@ cdef inline bint _contiguous(Py_ssize_t itemsize, @wraparound(False) cdef inline Py_ssize_t _nbytes(Py_ssize_t itemsize, Py_ssize_t ndim, - Py_ssize_t[::1] shape_mv) nogil: + Py_ssize_t[::1] shape_mv) noexcept nogil: cdef Py_ssize_t i, nbytes = itemsize for i in range(ndim): nbytes *= shape_mv[i] From cfe90080022b84aa35541097bb6ae8f276c515bb Mon Sep 17 00:00:00 2001 From: jakirkham Date: Tue, 22 Oct 2024 02:21:19 -0700 Subject: [PATCH 2/2] Use Cython's `array` to back `Py_ssize_t[::1]` (#307) In `Array`, `Py_ssize_t[::1]` objects are currently backed by [CPython `array`'s]( https://cython.readthedocs.io/en/latest/src/userguide/memoryviews.html#cpython-array-module ) with some internal bits expressed in Cython. However these are not compatible with [Python's Limited API and Stable ABI]( https://docs.python.org/3/c-api/stable.html#c-api-stability ). To address that, switch to [Cython's own `array` type]( https://cython.readthedocs.io/en/latest/src/userguide/memoryviews.html#cython-arrays ). As this is baked into Cython and doesn't use anything special, it is compatible with Python's Limited API and Stable ABI. xref: https://github.com/rapidsai/build-planning/issues/42 Authors: - https://github.com/jakirkham Approvers: - Mads R. B. Kristensen (https://github.com/madsbk) - Peter Andreas Entschev (https://github.com/pentschev) URL: https://github.com/rapidsai/ucxx/pull/307 --- python/ucxx/ucxx/_lib/arr.pyx | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/python/ucxx/ucxx/_lib/arr.pyx b/python/ucxx/ucxx/_lib/arr.pyx index 9fc8f806..f7b4d85f 100644 --- a/python/ucxx/ucxx/_lib/arr.pyx +++ b/python/ucxx/ucxx/_lib/arr.pyx @@ -4,13 +4,12 @@ # cython: language_level=3 -from cpython.array cimport array, newarrayobject from cpython.buffer cimport PyBuffer_IsContiguous +from cpython.mem cimport PyMem_Free, PyMem_Malloc from cpython.memoryview cimport ( PyMemoryView_FromObject, PyMemoryView_GET_BUFFER, ) -from cpython.object cimport PyObject from cpython.ref cimport Py_INCREF from cpython.tuple cimport PyTuple_New, PyTuple_SET_ITEM from cython cimport ( @@ -20,6 +19,7 @@ from cython cimport ( nonecheck, wraparound, ) +from cython.view cimport array from libc.stdint cimport uintptr_t from libc.string cimport memcpy @@ -62,13 +62,14 @@ cdef dict itemsize_mapping = { } -cdef array array_Py_ssize_t = array("q") +cdef sizeof_Py_ssize_t = sizeof(Py_ssize_t) -cdef inline Py_ssize_t[::1] new_Py_ssize_t_array(Py_ssize_t n): - return newarrayobject( - (array_Py_ssize_t).ob_type, n, array_Py_ssize_t.ob_descr - ) +cdef Py_ssize_t[::1] new_Py_ssize_t_array(Py_ssize_t n): + cdef array a = array((n,), sizeof_Py_ssize_t, b"q", "c", False) + a.data = PyMem_Malloc(n * sizeof(Py_ssize_t)) + a.callback_free_data = PyMem_Free + return a @auto_pickle(False)