|
| 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 | + |
| 16 | +from numba_dpex.core import types as dpex_types |
| 17 | + |
| 18 | + |
| 19 | +def populate_array(array, data, shape, strides, itemsize): |
| 20 | + """ |
| 21 | + Helper function for populating array structures. |
| 22 | + This avoids forgetting to set fields. |
| 23 | +
|
| 24 | + *shape* and *strides* can be Python tuples or LLVM arrays. |
| 25 | +
|
| 26 | + This is analog of numpy.np.arrayobj.populate_array without parent and |
| 27 | + meminfo fields, because they don't make sense on device. This function |
| 28 | + intended to be used only in kernel targets. |
| 29 | + """ |
| 30 | + context = array._context |
| 31 | + builder = array._builder |
| 32 | + datamodel = array._datamodel |
| 33 | + # doesn't matter what this array type instance is, it's just to get the |
| 34 | + # fields for the datamodel of the standard array type in this context |
| 35 | + standard_array = dpex_types.Array(types.float64, 1, "C") |
| 36 | + standard_array_type_datamodel = context.data_model_manager[standard_array] |
| 37 | + required_fields = set(standard_array_type_datamodel._fields) |
| 38 | + datamodel_fields = set(datamodel._fields) |
| 39 | + # Make sure that the presented array object has a data model that is close |
| 40 | + # enough to an array for this function to proceed. |
| 41 | + if (required_fields & datamodel_fields) != required_fields: |
| 42 | + missing = required_fields - datamodel_fields |
| 43 | + msg = ( |
| 44 | + f"The datamodel for type {array._fe_type} is missing " |
| 45 | + f"field{'s' if len(missing) > 1 else ''} {missing}." |
| 46 | + ) |
| 47 | + raise ValueError(msg) |
| 48 | + |
| 49 | + intp_t = context.get_value_type(types.intp) |
| 50 | + if isinstance(shape, (tuple, list)): |
| 51 | + shape = cgutils.pack_array(builder, shape, intp_t) |
| 52 | + if isinstance(strides, (tuple, list)): |
| 53 | + strides = cgutils.pack_array(builder, strides, intp_t) |
| 54 | + if isinstance(itemsize, int): |
| 55 | + itemsize = intp_t(itemsize) |
| 56 | + |
| 57 | + attrs = dict( |
| 58 | + shape=shape, |
| 59 | + strides=strides, |
| 60 | + data=data, |
| 61 | + itemsize=itemsize, |
| 62 | + ) |
| 63 | + |
| 64 | + # Calc num of items from shape |
| 65 | + nitems = context.get_constant(types.intp, 1) |
| 66 | + unpacked_shape = cgutils.unpack_tuple(builder, shape, shape.type.count) |
| 67 | + # (note empty shape => 0d array therefore nitems = 1) |
| 68 | + for axlen in unpacked_shape: |
| 69 | + nitems = builder.mul(nitems, axlen, flags=["nsw"]) |
| 70 | + attrs["nitems"] = nitems |
| 71 | + |
| 72 | + # Make sure that we have all the fields |
| 73 | + got_fields = set(attrs.keys()) |
| 74 | + if got_fields != required_fields: |
| 75 | + raise ValueError("missing {0}".format(required_fields - got_fields)) |
| 76 | + |
| 77 | + # Set field value |
| 78 | + for k, v in attrs.items(): |
| 79 | + setattr(array, k, v) |
| 80 | + |
| 81 | + return array |
0 commit comments