|
| 1 | +import numpy as np |
| 2 | +from numpy cimport ( |
| 3 | + import_array, ndarray, npy_intp, |
| 4 | + PyArray_SimpleNewFromData, PyArray_New, |
| 5 | + NPY_ARRAY_OWNDATA, NPY_ARRAY_WRITEABLE, NPY_ARRAY_F_CONTIGUOUS, |
| 6 | +) |
| 7 | +from libc.stdint cimport uintptr_t |
| 8 | + |
| 9 | +import_array() |
| 10 | + |
| 11 | +cpdef int call_gxb_init(ffi, lib, int mode): |
| 12 | + # We need to call `GxB_init`, but we didn't compile Cython against GraphBLAS. So, we get it from cffi. |
| 13 | + # Step 1: ffi.addressof(lib, "GxB_init") |
| 14 | + # Return type: cffi.cdata object of a function pointer. Can't cast to int. |
| 15 | + # Step 2: ffi.cast("uintptr_t", ...) |
| 16 | + # Return type: cffi.cdata object of a uintptr_t type, an unsigned pointer. Can cast to int. |
| 17 | + # Step 3: int(...) |
| 18 | + # Return type: int. The physical address of the function. |
| 19 | + # Step 4: <uintptr_t>(...) |
| 20 | + # Return type: uintptr_t in Cython. Cast Python int to Cython integer for pointers. |
| 21 | + # Step 5: <GsB_init>(...) |
| 22 | + # Return: function pointer in Cython! |
| 23 | + |
| 24 | + cdef GxB_init func = <GxB_init><uintptr_t>int(ffi.cast("uintptr_t", ffi.addressof(lib, "GxB_init"))) |
| 25 | + return func(<GrB_Mode>mode, PyDataMem_NEW, PyDataMem_NEW_ZEROED, PyDataMem_RENEW, PyDataMem_FREE, True) |
| 26 | + |
| 27 | + |
| 28 | +cpdef ndarray claim_buffer(ffi, cdata, size_t size, dtype): |
| 29 | + cdef: |
| 30 | + npy_intp dims = size |
| 31 | + uintptr_t ptr = int(ffi.cast("uintptr_t", cdata)) |
| 32 | + ndarray array = PyArray_SimpleNewFromData(1, &dims, dtype.num, <void*>ptr) |
| 33 | + PyArray_ENABLEFLAGS(array, NPY_ARRAY_OWNDATA) |
| 34 | + return array |
| 35 | + |
| 36 | + |
| 37 | +cpdef ndarray claim_buffer_2d(ffi, cdata, size_t cdata_size, size_t nrows, size_t ncols, dtype, bint is_c_order): |
| 38 | + cdef: |
| 39 | + size_t size = nrows * ncols |
| 40 | + ndarray array |
| 41 | + uintptr_t ptr |
| 42 | + npy_intp dims[2] |
| 43 | + if cdata_size == size: |
| 44 | + ptr = int(ffi.cast("uintptr_t", cdata)) |
| 45 | + dims[0] = nrows |
| 46 | + dims[1] = ncols |
| 47 | + if is_c_order: |
| 48 | + array = PyArray_SimpleNewFromData(2, dims, dtype.num, <void*>ptr) |
| 49 | + else: |
| 50 | + array = PyArray_New( |
| 51 | + ndarray, 2, dims, dtype.num, NULL, <void*>ptr, -1, |
| 52 | + NPY_ARRAY_F_CONTIGUOUS | NPY_ARRAY_WRITEABLE, <object>NULL |
| 53 | + ) |
| 54 | + PyArray_ENABLEFLAGS(array, NPY_ARRAY_OWNDATA) |
| 55 | + elif cdata_size > size: # pragma: no cover |
| 56 | + array = claim_buffer(ffi, cdata, cdata_size, dtype) |
| 57 | + if is_c_order: |
| 58 | + array = array[:size].reshape((nrows, ncols)) |
| 59 | + else: |
| 60 | + array = array[:size].reshape((ncols, nrows)).T |
| 61 | + else: # pragma: no cover |
| 62 | + raise ValueError( |
| 63 | + f"Buffer size too small: {cdata_size}. " |
| 64 | + f"Unable to create matrix of size {nrows}x{ncols} = {size}" |
| 65 | + ) |
| 66 | + return array |
| 67 | + |
| 68 | + |
| 69 | +cpdef unclaim_buffer(ndarray array): |
| 70 | + PyArray_CLEARFLAGS(array, NPY_ARRAY_OWNDATA | NPY_ARRAY_WRITEABLE) |
0 commit comments