|
| 1 | + |
| 2 | + |
| 3 | +cdef bint _valid_usm_ptr_and_context(DPCTLSyclUSMRef ptr, SyclContext ctx): |
| 4 | + usm_type = _Memory.get_pointer_type(ptr, ctx) |
| 5 | + return usm_type in (b'shared', b'device', b'host') |
| 6 | + |
| 7 | + |
| 8 | +cdef DPCTLSyclQueueRef _queue_ref_copy_from_SyclQueue( |
| 9 | + DPCTLSyclUSMRef ptr, SyclQueue q): |
| 10 | + """ Check that USM ptr is consistent with SYCL context in the queue, |
| 11 | + and return a copy of QueueRef if so, or NULL otherwise. |
| 12 | + """ |
| 13 | + cdef SyclContext ctx = q.get_sycl_context() |
| 14 | + if (_valid_usm_ptr_and_context(ptr, ctx)): |
| 15 | + return DPCTLQueue_Copy(q.get_queue_ref()) |
| 16 | + else: |
| 17 | + return NULL |
| 18 | + |
| 19 | + |
| 20 | +cdef DPCTLSyclQueueRef _queue_ref_copy_from_USMRef_and_SyclContext( |
| 21 | + DPCTLSyclUSMRef ptr, SyclContext ctx): |
| 22 | + """ Obtain device from pointer and sycl context, use |
| 23 | + context and device to create a queue from which this memory |
| 24 | + can be accessible. |
| 25 | + """ |
| 26 | + cdef SyclDevice dev = _Memory.get_pointer_device(ptr, ctx) |
| 27 | + cdef DPCTLSyclContextRef CRef = ctx.get_context_ref() |
| 28 | + cdef DPCTLSyclDeviceRef DRef = dev.get_device_ref() |
| 29 | + return DPCTLQueue_Create(CRef, DRef, NULL, 0) |
| 30 | + |
| 31 | + |
| 32 | +cdef DPCTLSyclQueueRef get_queue_ref_from_ptr_and_syclobj( |
| 33 | + DPCTLSyclUSMRef ptr, object syclobj): |
| 34 | + """ Constructs queue from pointer and syclobject from |
| 35 | + __sycl_usm_array_interface__ |
| 36 | + """ |
| 37 | + cdef SyclContext ctx |
| 38 | + if type(syclobj) is SyclQueue: |
| 39 | + return _queue_ref_copy_from_SyclQueue(ptr, <SyclQueue> syclobj) |
| 40 | + elif type(syclobj) is SyclContext: |
| 41 | + ctx = <SyclContext>syclobj |
| 42 | + return _queue_ref_copy_from_USMRef_and_SyclContext(ptr, ctx) |
| 43 | + elif type(syclobj) is str: |
| 44 | + q = SyclQueue(syclobj) |
| 45 | + return _queue_ref_copy_from_SyclQueue(ptr, <SyclQueue> q) |
| 46 | + elif pycapsule.PyCapsule_IsValid(syclobj, "SyclQueueRef"): |
| 47 | + q = SyclQueue(syclobj) |
| 48 | + return _queue_ref_copy_from_SyclQueue(ptr, <SyclQueue> q) |
| 49 | + elif pycapsule.PyCapsule_IsValid(syclobj, "SyclContextRef"): |
| 50 | + ctx = <SyclContext>SyclContext(syclobj) |
| 51 | + return _queue_ref_copy_from_USMRef_and_SyclContext(ptr, ctx) |
| 52 | + elif hasattr(syclobj, '_get_capsule'): |
| 53 | + cap = syclobj._get_capsule() |
| 54 | + if pycapsule.PyCapsule_IsValid(cap, "SyclQueueRef"): |
| 55 | + q = SyclQueue(cap) |
| 56 | + return _queue_ref_copy_from_SyclQueue(ptr, <SyclQueue> q) |
| 57 | + elif pycapsule.PyCapsule_IsValid(cap, "SyclContexRef"): |
| 58 | + ctx = <SyclContext>SyclContext(cap) |
| 59 | + return _queue_ref_copy_from_USMRef_and_SyclContext(ptr, ctx) |
| 60 | + else: |
| 61 | + return NULL |
| 62 | + else: |
| 63 | + return NULL |
| 64 | + |
| 65 | + |
| 66 | +cdef object _pointers_from_shape_and_stride( |
| 67 | + int nd, object ary_shape, Py_ssize_t itemsize, Py_ssize_t ary_offset, |
| 68 | + object ary_strides): |
| 69 | + """ |
| 70 | + Internal utility: for given array data about shape/layout/element |
| 71 | + compute left-most displacement when enumerating all elements of the array |
| 72 | + and the number of bytes of memory between the left-most and right-most |
| 73 | + displacements. |
| 74 | +
|
| 75 | + Returns: tuple(min_disp, nbytes) |
| 76 | + """ |
| 77 | + if (nd > 0): |
| 78 | + if (ary_strides is None): |
| 79 | + nelems = 1 |
| 80 | + for si in ary_shape: |
| 81 | + sh_i = int(si) |
| 82 | + if (sh_i <= 0): |
| 83 | + raise ValueError("Array shape elements need to be positive") |
| 84 | + nelems = nelems * sh_i |
| 85 | + return (ary_offset, nelems * itemsize) |
| 86 | + else: |
| 87 | + min_disp = ary_offset |
| 88 | + max_disp = ary_offset |
| 89 | + for i in range(nd): |
| 90 | + str_i = int(ary_strides[i]) |
| 91 | + sh_i = int(ary_shape[i]) |
| 92 | + if (str_i > 0): |
| 93 | + max_disp += str_i * (sh_i - 1) |
| 94 | + else: |
| 95 | + min_disp += str_i * (sh_i - 1); |
| 96 | + return (min_disp, (max_disp - min_disp + 1) * itemsize) |
| 97 | + elif (nd == 0): |
| 98 | + return (ary_offset, itemsize) |
| 99 | + else: |
| 100 | + raise ValueError("Array dimensions can not be negative") |
| 101 | + |
| 102 | + |
| 103 | +cdef class _USMBufferData: |
| 104 | + """ |
| 105 | + Internal data struct populated from parsing |
| 106 | + `__sycl_usm_array_interface__` dictionary |
| 107 | + """ |
| 108 | + cdef DPCTLSyclUSMRef p |
| 109 | + cdef int writeable |
| 110 | + cdef object dt |
| 111 | + cdef Py_ssize_t itemsize |
| 112 | + cdef Py_ssize_t nbytes |
| 113 | + cdef SyclQueue queue |
| 114 | + |
| 115 | + @staticmethod |
| 116 | + cdef _USMBufferData from_sycl_usm_ary_iface(dict ary_iface): |
| 117 | + cdef object ary_data_tuple = ary_iface.get('data', None) |
| 118 | + cdef object ary_typestr = ary_iface.get('typestr', None) |
| 119 | + cdef object ary_shape = ary_iface.get('shape', None) |
| 120 | + cdef object ary_strides = ary_iface.get('strides', None) |
| 121 | + cdef object ary_syclobj = ary_iface.get('syclobj', None) |
| 122 | + cdef Py_ssize_t ary_offset = ary_iface.get('offset', 0) |
| 123 | + cdef int ary_version = ary_iface.get('version', 0) |
| 124 | + cdef Py_ssize_t arr_data_ptr = 0 |
| 125 | + cdef DPCTLSyclUSMRef memRef = NULL |
| 126 | + cdef Py_ssize_t itemsize = -1 |
| 127 | + cdef int writeable = -1 |
| 128 | + cdef int nd = -1 |
| 129 | + cdef DPCTLSyclQueueRef QRef = NULL |
| 130 | + cdef object dt |
| 131 | + cdef _USMBufferData buf |
| 132 | + cdef SyclDevice dev |
| 133 | + cdef SyclContext ctx |
| 134 | + |
| 135 | + if ary_version != 1: |
| 136 | + raise ValueError(("__sycl_usm_array_interface__ is malformed:" |
| 137 | + " dict('version': {}) is unexpected." |
| 138 | + " The only recognized version is 1.").format( |
| 139 | + ary_version)) |
| 140 | + if not ary_data_tuple or len(ary_data_tuple) != 2: |
| 141 | + raise ValueError("__sycl_usm_array_interface__ is malformed:" |
| 142 | + " 'data' field is required, and must be a tuple" |
| 143 | + " (usm_pointer, is_writeable_boolean).") |
| 144 | + arr_data_ptr = <Py_ssize_t>ary_data_tuple[0] |
| 145 | + writeable = 1 if ary_data_tuple[1] else 0 |
| 146 | + # Check that memory and syclobj are consistent: |
| 147 | + # (USM pointer is bound to this sycl context) |
| 148 | + memRef = <DPCTLSyclUSMRef>arr_data_ptr |
| 149 | + QRef = get_queue_ref_from_ptr_and_syclobj(memRef, ary_syclobj) |
| 150 | + if (QRef is NULL): |
| 151 | + raise ValueError("__sycl_usm_array_interface__ is malformed:" |
| 152 | + " 'data' field is not consistent with 'syclobj'" |
| 153 | + " field, the pointer {} is not bound to" |
| 154 | + " SyclContext derived from" |
| 155 | + " dict('syclobj': {}).".format( |
| 156 | + hex(arr_data_ptr), ary_syclobj)) |
| 157 | + # shape must be present |
| 158 | + if ary_shape is None or not ( |
| 159 | + isinstance(ary_shape, collections.abc.Sized) and |
| 160 | + isinstance(ary_shape, collections.abc.Iterable)): |
| 161 | + DPCTLQueue_Delete(QRef) |
| 162 | + raise ValueError("Shape entry is a required element of " |
| 163 | + "`__sycl_usm_array_interface__` dictionary") |
| 164 | + nd = len(ary_shape) |
| 165 | + try: |
| 166 | + dt = np.dtype(ary_typestr) |
| 167 | + if (dt.hasobject or not (np.issubdtype(dt.type, np.integer) or |
| 168 | + np.issubdtype(dt.type, np.inexact))): |
| 169 | + DPCTLQueue_Delete(QRef) |
| 170 | + raise TypeError("Only integer types, floating and complex " |
| 171 | + "floating types are supported.") |
| 172 | + itemsize = <Py_ssize_t> (dt.itemsize) |
| 173 | + except TypeError as e: |
| 174 | + raise ValueError( |
| 175 | + "__sycl_usm_array_interface__ is malformed:" |
| 176 | + " dict('typestr': {}) is unexpected. ".format(ary_typestr) |
| 177 | + ) from e |
| 178 | + |
| 179 | + if (ary_strides is None or ( |
| 180 | + isinstance(ary_strides, collections.abc.Sized) and |
| 181 | + isinstance(ary_strides, collections.abc.Iterable) and |
| 182 | + len(ary_strides) == nd)): |
| 183 | + min_disp, nbytes = _pointers_from_shape_and_stride( |
| 184 | + nd, ary_shape, itemsize, ary_offset, ary_strides) |
| 185 | + else: |
| 186 | + DPCTLQueue_Delete(QRef) |
| 187 | + raise ValueError("__sycl_usm_array_interface__ is malformed: " |
| 188 | + "'strides' must be a tuple or " |
| 189 | + "list of the same length as shape") |
| 190 | + |
| 191 | + buf = _USMBufferData.__new__(_USMBufferData) |
| 192 | + buf.p = <DPCTLSyclUSMRef>( |
| 193 | + arr_data_ptr + (<Py_ssize_t>min_disp) * itemsize) |
| 194 | + buf.writeable = writeable |
| 195 | + buf.itemsize = itemsize |
| 196 | + buf.nbytes = <Py_ssize_t> nbytes |
| 197 | + |
| 198 | + buf.queue = SyclQueue._create(QRef) |
| 199 | + |
| 200 | + return buf |
0 commit comments