Skip to content

Commit 92a7a7d

Browse files
authored
Merge pull request #1293 from IntelPython/feature/remove_unused_krenel_model_fields
Feature/remove unused kernel model fields
2 parents e615596 + 72f66dd commit 92a7a7d

File tree

14 files changed

+323
-183
lines changed

14 files changed

+323
-183
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.

numba_dpex/core/datamodel/models.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,12 @@ class USMArrayDeviceModel(StructModel):
8080
def __init__(self, dmm, fe_type):
8181
ndim = fe_type.ndim
8282
members = [
83-
# meminfo never used in kernel, so we don'te care about addrspace
84-
("meminfo", types.MemInfoPointer(fe_type.dtype)),
85-
# parent never used in kernel, so we don'te care about addrspace
86-
("parent", types.pyobject),
8783
("nitems", types.intp),
8884
("itemsize", types.intp),
8985
(
9086
"data",
9187
types.CPointer(fe_type.dtype, addrspace=fe_type.addrspace),
9288
),
93-
# sycl_queue never used in kernel, so we don'te care about addrspace
94-
("sycl_queue", types.voidptr),
9589
("shape", types.UniTuple(types.intp, ndim)),
9690
("strides", types.UniTuple(types.intp, ndim)),
9791
]

numba_dpex/core/kernel_interface/arg_pack_unpacker.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,9 @@ def _unpack_usm_array(self, val):
4747
strides = suai_attrs.strides
4848
ndim = suai_attrs.dimensions
4949

50-
# meminfo
51-
unpacked_array_attrs.append(ctypes.c_size_t(0))
52-
# parent
53-
unpacked_array_attrs.append(ctypes.c_size_t(0))
5450
unpacked_array_attrs.append(ctypes.c_longlong(size))
5551
unpacked_array_attrs.append(ctypes.c_longlong(itemsize))
5652
unpacked_array_attrs.append(buf)
57-
# queue: unused and passed as void*
58-
unpacked_array_attrs.append(ctypes.c_size_t(0))
5953
for ax in range(ndim):
6054
unpacked_array_attrs.append(ctypes.c_longlong(shape[ax]))
6155
for ax in range(ndim):
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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)

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.

0 commit comments

Comments
 (0)