|
| 1 | +# SPDX-FileCopyrightText: 2012 - 2024 Anaconda Inc. |
| 2 | +# SPDX-FileCopyrightText: 2024 Intel Corporation |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: BSD-2-Clause |
| 5 | + |
| 6 | +""" |
| 7 | +This package contains implementation of some numpy.np.arrayobj functions without |
| 8 | +parent and meminfo fields required, because they don't make sense on device. |
| 9 | +These functions intended to be used only in kernel targets like local/private or |
| 10 | +usm array view. |
| 11 | +""" |
| 12 | + |
| 13 | + |
| 14 | +from numba.core import cgutils, types |
| 15 | +from numba.np import arrayobj as np_arrayobj |
| 16 | + |
| 17 | +from numba_dpex.core import types as dpex_types |
| 18 | + |
| 19 | + |
| 20 | +def populate_array(array, data, shape, strides, itemsize): |
| 21 | + """ |
| 22 | + Helper function for populating array structures. |
| 23 | + This avoids forgetting to set fields. |
| 24 | +
|
| 25 | + *shape* and *strides* can be Python tuples or LLVM arrays. |
| 26 | +
|
| 27 | + This is analog of numpy.np.arrayobj.populate_array without parent and |
| 28 | + meminfo fields, because they don't make sense on device. This function |
| 29 | + intended to be used only in kernel targets. |
| 30 | + """ |
| 31 | + context = array._context |
| 32 | + builder = array._builder |
| 33 | + datamodel = array._datamodel |
| 34 | + # doesn't matter what this array type instance is, it's just to get the |
| 35 | + # fields for the datamodel of the standard array type in this context |
| 36 | + standard_array = dpex_types.Array(types.float64, 1, "C") |
| 37 | + standard_array_type_datamodel = context.data_model_manager[standard_array] |
| 38 | + required_fields = set(standard_array_type_datamodel._fields) |
| 39 | + datamodel_fields = set(datamodel._fields) |
| 40 | + # Make sure that the presented array object has a data model that is close |
| 41 | + # enough to an array for this function to proceed. |
| 42 | + if (required_fields & datamodel_fields) != required_fields: |
| 43 | + missing = required_fields - datamodel_fields |
| 44 | + msg = ( |
| 45 | + f"The datamodel for type {array._fe_type} is missing " |
| 46 | + f"field{'s' if len(missing) > 1 else ''} {missing}." |
| 47 | + ) |
| 48 | + raise ValueError(msg) |
| 49 | + |
| 50 | + intp_t = context.get_value_type(types.intp) |
| 51 | + if isinstance(shape, (tuple, list)): |
| 52 | + shape = cgutils.pack_array(builder, shape, intp_t) |
| 53 | + if isinstance(strides, (tuple, list)): |
| 54 | + strides = cgutils.pack_array(builder, strides, intp_t) |
| 55 | + if isinstance(itemsize, int): |
| 56 | + itemsize = intp_t(itemsize) |
| 57 | + |
| 58 | + attrs = dict( |
| 59 | + shape=shape, |
| 60 | + strides=strides, |
| 61 | + data=data, |
| 62 | + itemsize=itemsize, |
| 63 | + ) |
| 64 | + |
| 65 | + # Calc num of items from shape |
| 66 | + nitems = context.get_constant(types.intp, 1) |
| 67 | + unpacked_shape = cgutils.unpack_tuple(builder, shape, shape.type.count) |
| 68 | + # (note empty shape => 0d array therefore nitems = 1) |
| 69 | + for axlen in unpacked_shape: |
| 70 | + nitems = builder.mul(nitems, axlen, flags=["nsw"]) |
| 71 | + attrs["nitems"] = nitems |
| 72 | + |
| 73 | + # Make sure that we have all the fields |
| 74 | + got_fields = set(attrs.keys()) |
| 75 | + if got_fields != required_fields: |
| 76 | + raise ValueError("missing {0}".format(required_fields - got_fields)) |
| 77 | + |
| 78 | + # Set field value |
| 79 | + for k, v in attrs.items(): |
| 80 | + setattr(array, k, v) |
| 81 | + |
| 82 | + return array |
| 83 | + |
| 84 | + |
| 85 | +def make_view(context, builder, aryty, ary, return_type, data, shapes, strides): |
| 86 | + """ |
| 87 | + Build a view over the given array with the given parameters. |
| 88 | +
|
| 89 | + This is analog of numpy.np.arrayobj.make_view without parent and |
| 90 | + meminfo fields, because they don't make sense on device. This function |
| 91 | + intended to be used only in kernel targets. |
| 92 | + """ |
| 93 | + retary = np_arrayobj.make_array(return_type)(context, builder) |
| 94 | + populate_array( |
| 95 | + retary, data=data, shape=shapes, strides=strides, itemsize=ary.itemsize |
| 96 | + ) |
| 97 | + return retary |
| 98 | + |
| 99 | + |
| 100 | +def _getitem_array_generic( |
| 101 | + context, builder, return_type, aryty, ary, index_types, indices |
| 102 | +): |
| 103 | + """ |
| 104 | + Return the result of indexing *ary* with the given *indices*, |
| 105 | + returning either a scalar or a view. |
| 106 | +
|
| 107 | + This is analog of numpy.np.arrayobj._getitem_array_generic without parent |
| 108 | + and meminfo fields, because they don't make sense on device. This function |
| 109 | + intended to be used only in kernel targets. |
| 110 | + """ |
| 111 | + dataptr, view_shapes, view_strides = np_arrayobj.basic_indexing( |
| 112 | + context, |
| 113 | + builder, |
| 114 | + aryty, |
| 115 | + ary, |
| 116 | + index_types, |
| 117 | + indices, |
| 118 | + boundscheck=context.enable_boundscheck, |
| 119 | + ) |
| 120 | + |
| 121 | + if isinstance(return_type, types.Buffer): |
| 122 | + # Build array view |
| 123 | + retary = make_view( |
| 124 | + context, |
| 125 | + builder, |
| 126 | + aryty, |
| 127 | + ary, |
| 128 | + return_type, |
| 129 | + dataptr, |
| 130 | + view_shapes, |
| 131 | + view_strides, |
| 132 | + ) |
| 133 | + return retary._getvalue() |
| 134 | + else: |
| 135 | + # Load scalar from 0-d result |
| 136 | + assert not view_shapes |
| 137 | + return np_arrayobj.load_item(context, builder, aryty, dataptr) |
0 commit comments