Skip to content

Commit

Permalink
Override populate_array for kernel targets
Browse files Browse the repository at this point in the history
  • Loading branch information
ZzEeKkAa committed Jan 23, 2024
1 parent 65ed102 commit 753ba3a
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 1 deletion.
27 changes: 27 additions & 0 deletions LICENSES.third-party
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,30 @@ SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

numba
------------------------
Copyright (c) 2012, Anaconda, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
81 changes: 81 additions & 0 deletions numba_dpex/core/kernel_interface/arrayobj.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# SPDX-FileCopyrightText: 2012 - 2024 Anaconda Inc.
# SPDX-FileCopyrightText: 2024 Intel Corporation
#
# SPDX-License-Identifier: BSD-2-Clause

"""
This package contains implementation of some numpy.np.arrayobj functions without
parent and meminfo fields required, because they don't make sense on device.
These functions intended to be used only in kernel targets like local/private or
usm array view.
"""


from numba.core import cgutils, types

from numba_dpex.core import types as dpex_types


def populate_array(array, data, shape, strides, itemsize):
"""
Helper function for populating array structures.
This avoids forgetting to set fields.
*shape* and *strides* can be Python tuples or LLVM arrays.
This is analog of numpy.np.arrayobj.populate_array without parent and
meminfo fields, because they don't make sense on device. This function
intended to be used only in kernel targets.
"""
context = array._context
builder = array._builder
datamodel = array._datamodel
# doesn't matter what this array type instance is, it's just to get the
# fields for the datamodel of the standard array type in this context
standard_array = dpex_types.Array(types.float64, 1, "C")
standard_array_type_datamodel = context.data_model_manager[standard_array]
required_fields = set(standard_array_type_datamodel._fields)
datamodel_fields = set(datamodel._fields)
# Make sure that the presented array object has a data model that is close
# enough to an array for this function to proceed.
if (required_fields & datamodel_fields) != required_fields:
missing = required_fields - datamodel_fields
msg = (
f"The datamodel for type {array._fe_type} is missing "
f"field{'s' if len(missing) > 1 else ''} {missing}."
)
raise ValueError(msg)

intp_t = context.get_value_type(types.intp)
if isinstance(shape, (tuple, list)):
shape = cgutils.pack_array(builder, shape, intp_t)
if isinstance(strides, (tuple, list)):
strides = cgutils.pack_array(builder, strides, intp_t)
if isinstance(itemsize, int):
itemsize = intp_t(itemsize)

attrs = dict(
shape=shape,
strides=strides,
data=data,
itemsize=itemsize,
)

# Calc num of items from shape
nitems = context.get_constant(types.intp, 1)
unpacked_shape = cgutils.unpack_tuple(builder, shape, shape.type.count)
# (note empty shape => 0d array therefore nitems = 1)
for axlen in unpacked_shape:
nitems = builder.mul(nitems, axlen, flags=["nsw"])
attrs["nitems"] = nitems

# Make sure that we have all the fields
got_fields = set(attrs.keys())
if got_fields != required_fields:
raise ValueError("missing {0}".format(required_fields - got_fields))

# Set field value
for k, v in attrs.items():
setattr(array, k, v)

return array
8 changes: 8 additions & 0 deletions numba_dpex/core/targets/kernel_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,14 @@ def addrspacecast(self, builder, src, addrspace):
def get_ufunc_info(self, ufunc_key):
return self.ufunc_db[ufunc_key]

def populate_array(self, arr, **kwargs):
"""
Populate array structure.
"""
from numba_dpex.core.kernel_interface import arrayobj

return arrayobj.populate_array(arr, **kwargs)


class DpexCallConv(MinimalCallConv):
"""Custom calling convention class used by numba-dpex.
Expand Down
1 change: 0 additions & 1 deletion numba_dpex/ocl/oclimpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,6 @@ def _make_array(
shape=cgutils.pack_array(builder, kshape),
strides=cgutils.pack_array(builder, kstrides),
itemsize=context.get_constant(types.intp, itemsize),
meminfo=None,
)

return ary._getvalue()
Expand Down

0 comments on commit 753ba3a

Please sign in to comment.