|
| 1 | +# distutils: language = c++ |
| 2 | +# cython: language_level=3 |
| 3 | + |
| 4 | +from cpython.mem cimport PyMem_Malloc |
| 5 | +from cpython.ref cimport Py_INCREF |
| 6 | +from cpython.tuple cimport PyTuple_New, PyTuple_SetItem |
| 7 | + |
| 8 | + |
| 9 | +cdef int ERROR_MALLOC = 1 |
| 10 | +cdef int ERROR_INTERNAL = -1 |
| 11 | +cdef int ERROR_INCORRECT_ORDER = 2 |
| 12 | +cdef int ERROR_UNEXPECTED_STRIDES = 3 |
| 13 | + |
| 14 | +cdef int USM_ARRAY_C_CONTIGUOUS = 1 |
| 15 | +cdef int USM_ARRAY_F_CONTIGUOUS = 2 |
| 16 | +cdef int USM_ARRAY_WRITEABLE = 4 |
| 17 | + |
| 18 | + |
| 19 | +cdef Py_ssize_t shape_to_elem_count(int nd, Py_ssize_t *shape_arr): |
| 20 | + """ |
| 21 | + Computes number of elements in an array. |
| 22 | + """ |
| 23 | + cdef Py_ssize_t count = 1 |
| 24 | + for i in range(nd): |
| 25 | + count *= shape_arr[i] |
| 26 | + return count |
| 27 | + |
| 28 | + |
| 29 | +cdef int _from_input_shape_strides( |
| 30 | + int nd, object shape, object strides, int itemsize, char order, |
| 31 | + Py_ssize_t **shape_ptr, Py_ssize_t **strides_ptr, |
| 32 | + Py_ssize_t *nelems, Py_ssize_t *min_disp, Py_ssize_t *max_disp, |
| 33 | + int *contig): |
| 34 | + """ |
| 35 | + Arguments: nd, shape, strides, itemsize, order |
| 36 | + Modifies: |
| 37 | + shape_ptr - pointer to C array for shape values |
| 38 | + stride_ptr - pointer to C array for strides values |
| 39 | + nelems - Number of elements in array |
| 40 | + min_disp = min( dot(strides, index), index for shape) |
| 41 | + max_disp = max( dor(strides, index), index for shape) |
| 42 | + contig = enumation for array contiguity |
| 43 | + Returns: 0 on success, error code otherwise. |
| 44 | + On success pointers point to allocated arrays, |
| 45 | + Otherwise they are set to NULL |
| 46 | + """ |
| 47 | + cdef int i |
| 48 | + cdef int all_incr = 1 |
| 49 | + cdef int all_decr = 1 |
| 50 | + cdef Py_ssize_t elem_count = 1 |
| 51 | + cdef Py_ssize_t min_shift = 0 |
| 52 | + cdef Py_ssize_t max_shift = 0 |
| 53 | + cdef Py_ssize_t str_i |
| 54 | + cdef Py_ssize_t* shape_arr |
| 55 | + cdef Py_ssize_t* strides_arr |
| 56 | + |
| 57 | + # 0-d array |
| 58 | + if (nd == 0): |
| 59 | + contig[0] = USM_ARRAY_C_CONTIGUOUS |
| 60 | + nelems[0] = 1 |
| 61 | + min_disp[0] = 0 |
| 62 | + max_disp[0] = 0 |
| 63 | + shape_ptr[0] = <Py_ssize_t *>(<size_t>0) |
| 64 | + strides_ptr[0] = <Py_ssize_t *>(<size_t>0) |
| 65 | + return 0 |
| 66 | + |
| 67 | + shape_arr = <Py_ssize_t*>PyMem_Malloc(nd * sizeof(Py_ssize_t)) |
| 68 | + if (not shape_arr): |
| 69 | + return ERROR_MALLOC |
| 70 | + shape_ptr[0] = shape_arr |
| 71 | + for i in range(0, nd): |
| 72 | + shape_arr[i] = <Py_ssize_t> shape[i] |
| 73 | + elem_count *= shape_arr[i] |
| 74 | + if elem_count == 0: |
| 75 | + contig[0] = USM_ARRAY_C_CONTIGUOUS |
| 76 | + nelems[0] = 1 |
| 77 | + min_disp[0] = 0 |
| 78 | + max_disp[0] = 0 |
| 79 | + strides_ptr[0] = <Py_ssize_t *>(<size_t>0) |
| 80 | + return 0 |
| 81 | + nelems[0] = elem_count |
| 82 | + |
| 83 | + if (strides is None): |
| 84 | + # no need to allocate and populate strides |
| 85 | + if (int(order) not in [ord('C'), ord('F'), ord('c'), ord('f')]): |
| 86 | + return ERROR_INCORRECT_ORDER |
| 87 | + if order == <char> ord('C') or order == <char> ord('c'): |
| 88 | + contig[0] = USM_ARRAY_C_CONTIGUOUS |
| 89 | + else: |
| 90 | + contig[0] = USM_ARRAY_F_CONTIGUOUS |
| 91 | + min_disp[0] = 0 |
| 92 | + max_disp[0] = (elem_count - 1) |
| 93 | + strides_ptr[0] = <Py_ssize_t *>(<size_t>0) |
| 94 | + return 0 |
| 95 | + elif ((isinstance(strides, (list, tuple)) or hasattr(strides, 'tolist')) |
| 96 | + and len(strides) == nd): |
| 97 | + strides_arr = <Py_ssize_t*>PyMem_Malloc(nd * sizeof(Py_ssize_t)) |
| 98 | + if (not strides_arr): |
| 99 | + return ERROR_MALLOC |
| 100 | + strides_ptr[0] = strides_arr |
| 101 | + for i in range(0, nd): |
| 102 | + str_i = <Py_ssize_t> strides[i] |
| 103 | + strides_arr[i] = str_i |
| 104 | + if str_i > 0: |
| 105 | + max_shift += strides_arr[i] * (shape_arr[i] - 1) |
| 106 | + else: |
| 107 | + min_shift += strides_arr[i] * (shape_arr[i] - 1) |
| 108 | + min_disp[0] = min_shift |
| 109 | + max_disp[0] = max_shift |
| 110 | + if max_shift == min_shift + (elem_count - 1): |
| 111 | + if nd == 1: |
| 112 | + contig[0] = USM_ARRAY_C_CONTIGUOUS |
| 113 | + return 0 |
| 114 | + for i in range(0, nd - 1): |
| 115 | + if all_incr: |
| 116 | + all_incr = strides_arr[i] < strides_arr[i + 1] |
| 117 | + if all_decr: |
| 118 | + all_decr = strides_arr[i] > strides_arr[i + 1] |
| 119 | + if all_incr: |
| 120 | + contig[0] = USM_ARRAY_C_CONTIGUOUS |
| 121 | + elif all_decr: |
| 122 | + contig[0] = USM_ARRAY_F_CONTIGUOUS |
| 123 | + else: |
| 124 | + contig[0] = 0 |
| 125 | + return 0 |
| 126 | + else: |
| 127 | + contig[0] = 0 # non-contiguous |
| 128 | + return 0 |
| 129 | + else: |
| 130 | + return ERROR_UNEXPECTED_STRIDES |
| 131 | + # return ERROR_INTERNAL |
| 132 | + |
| 133 | + |
| 134 | +cdef object _make_int_tuple(int nd, Py_ssize_t *ary): |
| 135 | + """ |
| 136 | + Makes Python tuple from C array |
| 137 | + """ |
| 138 | + cdef tuple res |
| 139 | + cdef object tmp |
| 140 | + if (ary): |
| 141 | + res = PyTuple_New(nd) |
| 142 | + for i in range(nd): |
| 143 | + tmp = <object>ary[i] |
| 144 | + Py_INCREF(tmp) # SetItem steals the reference |
| 145 | + PyTuple_SetItem(res, i, tmp) |
| 146 | + return res |
| 147 | + else: |
| 148 | + return None |
| 149 | + |
| 150 | + |
| 151 | +cdef object _make_reversed_int_tuple(int nd, Py_ssize_t *ary): |
| 152 | + """ |
| 153 | + Makes Python reversed tuple from C array |
| 154 | + """ |
| 155 | + cdef tuple res |
| 156 | + cdef object tmp |
| 157 | + cdef int i |
| 158 | + cdef int nd_1 |
| 159 | + if (ary): |
| 160 | + res = PyTuple_New(nd) |
| 161 | + nd_1 = nd - 1 |
| 162 | + for i in range(nd): |
| 163 | + tmp = <object>ary[i] |
| 164 | + Py_INCREF(tmp) # SetItem steals the reference |
| 165 | + PyTuple_SetItem(res, nd_1 - i, tmp) |
| 166 | + return res |
| 167 | + else: |
| 168 | + return None |
| 169 | + |
| 170 | + |
| 171 | +cdef object _c_contig_strides(int nd, Py_ssize_t *shape): |
| 172 | + """ |
| 173 | + Makes Python tuple for C-contiguous array |
| 174 | + """ |
| 175 | + cdef tuple cc_strides = PyTuple_New(nd) |
| 176 | + cdef object si = 1 |
| 177 | + cdef int i |
| 178 | + cdef int nd_1 = nd - 1 |
| 179 | + for i in range(0, nd): |
| 180 | + Py_INCREF(si) # SetItem steals the reference |
| 181 | + PyTuple_SetItem(cc_strides, nd_1 - i, si) |
| 182 | + si = si * shape[nd_1 - i] |
| 183 | + return cc_strides |
| 184 | + |
| 185 | + |
| 186 | +cdef object _f_contig_strides(int nd, Py_ssize_t *shape): |
| 187 | + """ |
| 188 | + Makes Python t |
| 189 | + """ |
| 190 | + cdef tuple fc_strides = PyTuple_New(nd) |
| 191 | + cdef object si = 1 |
| 192 | + for i in range(0, nd): |
| 193 | + Py_INCREF(si) # SetItem steals the reference |
| 194 | + PyTuple_SetItem(fc_strides, i, si) |
| 195 | + si = si * shape[i] |
| 196 | + return fc_strides |
0 commit comments