Skip to content

Commit 78470ab

Browse files
author
Diptorup Deb
committed
Adds a mock class for sycl::local_accessor
1 parent b903b09 commit 78470ab

File tree

2 files changed

+108
-3
lines changed

2 files changed

+108
-3
lines changed

numba_dpex/kernel_api/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,25 @@
1414
from .barrier import group_barrier
1515
from .index_space_ids import Group, Item, NdItem
1616
from .launcher import call_kernel
17+
from .local_accessor import LocalAccessor
1718
from .memory_enums import AddressSpace, MemoryOrder, MemoryScope
1819
from .private_array import PrivateArray
1920
from .ranges import NdRange, Range
2021

2122
__all__ = [
23+
"call_kernel",
24+
"group_barrier",
2225
"AddressSpace",
2326
"atomic_fence",
2427
"AtomicRef",
28+
"Group",
29+
"Item",
30+
"LocalAccessor",
2531
"MemoryOrder",
2632
"MemoryScope",
33+
"NdItem",
2734
"NdRange",
2835
"Range",
29-
"Group",
30-
"NdItem",
31-
"Item",
3236
"PrivateArray",
3337
"group_barrier",
3438
"call_kernel",
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# SPDX-FileCopyrightText: 2023 Intel Corporation
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
"""Implements a Python analogue to SYCL's local_accessor class. The class is
6+
intended to be used in pure Python code when prototyping a kernel function
7+
and to be passed to an actual kernel function for local memory allocation.
8+
"""
9+
import numpy
10+
11+
12+
class LocalAccessor:
13+
"""
14+
The ``LocalAccessor`` class is analogous to SYCL's ``local_accessor``
15+
class. The class acts a s proxy to allocating device local memory and
16+
accessing that memory from within a :func:`numba_dpex.kernel` decorated
17+
function.
18+
"""
19+
20+
def _verify_positive_integral_list(self, ls):
21+
"""Checks if all members of a list are positive integers."""
22+
23+
ret = False
24+
try:
25+
ret = all(int(val) > 0 for val in ls)
26+
except ValueError:
27+
pass
28+
29+
return ret
30+
31+
def __init__(self, shape, dtype) -> None:
32+
"""Creates a new LocalAccessor instance of the given shape and dtype."""
33+
34+
if not isinstance(shape, (list, tuple)):
35+
if hasattr(shape, "tolist"):
36+
fn = getattr(shape, "tolist")
37+
if callable(fn):
38+
self._shape = shape.tolist()
39+
else:
40+
try:
41+
self._shape = [
42+
shape,
43+
]
44+
except Exception as e:
45+
raise TypeError(
46+
"Argument shape must a non-negative integer, "
47+
"or a list/tuple of such integers."
48+
) from e
49+
else:
50+
self._shape = list(shape)
51+
52+
# Make sure shape is made up a supported types
53+
if not self._verify_positive_integral_list(self._shape):
54+
raise TypeError(
55+
"Argument shape must a non-negative integer, "
56+
"or a list/tuple of such integers."
57+
)
58+
59+
# Make sure shape has a rank between (1..3)
60+
if len(self._shape) < 1 or len(self._shape) > 3:
61+
raise TypeError("LocalAccessor can only have up to 3 dimensions.")
62+
63+
self._dtype = dtype
64+
65+
if self._dtype not in [
66+
numpy.float32,
67+
numpy.float64,
68+
numpy.int32,
69+
numpy.int64,
70+
]:
71+
raise TypeError(
72+
f"Argument dtype {dtype} is not supported. numpy.float32, "
73+
"numpy.float64, numpy.int32, numpy.int64 are the currently "
74+
"supported dtypes."
75+
)
76+
77+
self._data = numpy.empty(self._shape, dtype=self._dtype)
78+
79+
def __getitem__(self, idx_obj):
80+
"""Returns the value stored at the position represented by idx_obj in
81+
the self._data ndarray.
82+
"""
83+
84+
return self._data[idx_obj]
85+
86+
def __setitem__(self, idx_obj, val):
87+
"""Assigns a new value to the position represented by idx_obj in
88+
the self._data ndarray.
89+
"""
90+
91+
self._data[idx_obj] = val
92+
93+
@property
94+
def data(self):
95+
"""Returns the numpy.ndarray that is used as the internal storage for
96+
a LocalAccessor object.
97+
98+
Returns:
99+
numpy.ndarray: A numpy.ndarray object.
100+
"""
101+
return self._data

0 commit comments

Comments
 (0)