Skip to content

Commit 753ba3a

Browse files
committed
Override populate_array for kernel targets
1 parent 65ed102 commit 753ba3a

File tree

4 files changed

+116
-1
lines changed

4 files changed

+116
-1
lines changed

LICENSES.third-party

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,30 @@ SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
2828
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
2929
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
3030
DEALINGS IN THE SOFTWARE.
31+
32+
numba
33+
------------------------
34+
Copyright (c) 2012, Anaconda, Inc.
35+
All rights reserved.
36+
37+
Redistribution and use in source and binary forms, with or without
38+
modification, are permitted provided that the following conditions are
39+
met:
40+
41+
Redistributions of source code must retain the above copyright notice,
42+
this list of conditions and the following disclaimer.
43+
44+
Redistributions in binary form must reproduce the above copyright
45+
notice, this list of conditions and the following disclaimer in the
46+
documentation and/or other materials provided with the distribution.
47+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
48+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
49+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
50+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
51+
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
54+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
55+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
57+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

numba_dpex/core/targets/kernel_target.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,14 @@ def addrspacecast(self, builder, src, addrspace):
435435
def get_ufunc_info(self, ufunc_key):
436436
return self.ufunc_db[ufunc_key]
437437

438+
def populate_array(self, arr, **kwargs):
439+
"""
440+
Populate array structure.
441+
"""
442+
from numba_dpex.core.kernel_interface import arrayobj
443+
444+
return arrayobj.populate_array(arr, **kwargs)
445+
438446

439447
class DpexCallConv(MinimalCallConv):
440448
"""Custom calling convention class used by numba-dpex.

numba_dpex/ocl/oclimpl.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,6 @@ def _make_array(
421421
shape=cgutils.pack_array(builder, kshape),
422422
strides=cgutils.pack_array(builder, kstrides),
423423
itemsize=context.get_constant(types.intp, itemsize),
424-
meminfo=None,
425424
)
426425

427426
return ary._getvalue()

0 commit comments

Comments
 (0)