Skip to content

Commit 11e245c

Browse files
authored
Merge pull request #1239 from IntelPython/experimental/memory_enum_classes
Adds memory enum classes to the experimental module
2 parents f562bd6 + d175a3b commit 11e245c

File tree

4 files changed

+173
-0
lines changed

4 files changed

+173
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# SPDX-FileCopyrightText: 2023 Intel Corporation
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
"""The kernel_iface provides a set of Python classes and functions that are
6+
analogous to the C++ SYCL API. The kernel_iface API is meant to allow
7+
prototyping SYCL-like kernels in pure Python before compiling them using
8+
numba_dpex.kernel.
9+
"""
10+
11+
from .memory_enums import AddressSpace, MemoryOrder, MemoryScope
12+
13+
__all__ = ["AddressSpace", "MemoryOrder", "MemoryScope"]
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# SPDX-FileCopyrightText: 2023 Intel Corporation
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
"""A collection of FlagEnum classes that syntactically represents the SYCL
6+
memory enum classes.
7+
"""
8+
9+
from numba_dpex.experimental.flag_enum import FlagEnum
10+
11+
12+
class MemoryOrder(FlagEnum):
13+
"""
14+
An enumeration of the supported ``sycl::memory_order`` values in dpcpp. The
15+
integer values of the enums is kept consistent with the corresponding
16+
implementation in dpcpp.
17+
18+
===================== ============
19+
Order Enum value
20+
===================== ============
21+
RELAXED 0
22+
ACQUIRE 1
23+
CONSUME_UNSUPPORTED 2
24+
RELEASE 3
25+
ACQ_REL 4
26+
SEQ_CST 5
27+
===================== ============
28+
"""
29+
30+
RELAXED = 0
31+
ACQUIRE = 1
32+
CONSUME_UNSUPPORTED = 2
33+
RELEASE = 3
34+
ACQ_REL = 4
35+
SEQ_CST = 5
36+
37+
38+
class MemoryScope(FlagEnum):
39+
"""
40+
An enumeration of SYCL memory scope. For more details please refer to
41+
SYCL 2020 specification, section 3.8.3.2
42+
43+
=============== ============
44+
Memory Scope Enum value
45+
=============== ============
46+
WORK_ITEM 0
47+
SUB_GROUP 1
48+
WORK_GROUP 2
49+
DEVICE 3
50+
SYSTEM 4
51+
=============== ============
52+
"""
53+
54+
WORK_ITEM = 0
55+
SUB_GROUP = 1
56+
WORK_GROUP = 2
57+
DEVICE = 3
58+
SYSTEM = 4
59+
60+
61+
class AddressSpace(FlagEnum):
62+
"""The address space values supported by numba_dpex.
63+
64+
================== ============
65+
Address space Value
66+
================== ============
67+
PRIVATE 0
68+
GLOBAL 1
69+
CONSTANT 2
70+
LOCAL 3
71+
GENERIC 4
72+
================== ============
73+
"""
74+
75+
PRIVATE = 0
76+
GLOBAL = 1
77+
CONSTANT = 2
78+
LOCAL = 3
79+
GENERIC = 4
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-FileCopyrightText: 2023 Intel Corporation
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# SPDX-FileCopyrightText: 2023 Intel Corporation
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
import dpnp
6+
7+
import numba_dpex.experimental as exp_dpex
8+
from numba_dpex import Range
9+
from numba_dpex.experimental.kernel_iface import (
10+
AddressSpace,
11+
MemoryOrder,
12+
MemoryScope,
13+
)
14+
15+
16+
def test_compilation_of_memory_order():
17+
"""Tests if a MemoryOrder flags can be used inside a kernel function."""
18+
19+
@exp_dpex.kernel
20+
def store_memory_order_flag(a):
21+
a[0] = MemoryOrder.RELAXED
22+
a[1] = MemoryOrder.CONSUME_UNSUPPORTED
23+
a[2] = MemoryOrder.ACQ_REL
24+
a[3] = MemoryOrder.ACQUIRE
25+
a[4] = MemoryOrder.RELEASE
26+
a[5] = MemoryOrder.SEQ_CST
27+
28+
a = dpnp.ones(10, dtype=dpnp.int64)
29+
exp_dpex.call_kernel(store_memory_order_flag, Range(10), a)
30+
31+
assert a[0] == MemoryOrder.RELAXED
32+
assert a[1] == MemoryOrder.CONSUME_UNSUPPORTED
33+
assert a[2] == MemoryOrder.ACQ_REL
34+
assert a[3] == MemoryOrder.ACQUIRE
35+
assert a[4] == MemoryOrder.RELEASE
36+
assert a[5] == MemoryOrder.SEQ_CST
37+
38+
39+
def test_compilation_of_memory_scope():
40+
"""Tests if a MemoryScope flags can be used inside a kernel function."""
41+
42+
@exp_dpex.kernel
43+
def store_memory_scope_flag(a):
44+
a[0] = MemoryScope.DEVICE
45+
a[1] = MemoryScope.SUB_GROUP
46+
a[2] = MemoryScope.WORK_GROUP
47+
a[3] = MemoryScope.SYSTEM
48+
a[4] = MemoryScope.WORK_ITEM
49+
50+
a = dpnp.ones(10, dtype=dpnp.int64)
51+
exp_dpex.call_kernel(store_memory_scope_flag, Range(10), a)
52+
53+
assert a[0] == MemoryScope.DEVICE
54+
assert a[1] == MemoryScope.SUB_GROUP
55+
assert a[2] == MemoryScope.WORK_GROUP
56+
assert a[3] == MemoryScope.SYSTEM
57+
assert a[4] == MemoryScope.WORK_ITEM
58+
59+
60+
def test_compilation_of_address_space():
61+
"""Tests if a AddressSpace flags can be used inside a kernel function."""
62+
63+
@exp_dpex.kernel
64+
def store_address_space_flag(a):
65+
a[0] = AddressSpace.CONSTANT
66+
a[1] = AddressSpace.GENERIC
67+
a[2] = AddressSpace.GLOBAL
68+
a[3] = AddressSpace.LOCAL
69+
a[4] = AddressSpace.PRIVATE
70+
71+
a = dpnp.ones(10, dtype=dpnp.int64)
72+
exp_dpex.call_kernel(store_address_space_flag, Range(10), a)
73+
74+
assert a[0] == AddressSpace.CONSTANT
75+
assert a[1] == AddressSpace.GENERIC
76+
assert a[2] == AddressSpace.GLOBAL
77+
assert a[3] == AddressSpace.LOCAL
78+
assert a[4] == AddressSpace.PRIVATE

0 commit comments

Comments
 (0)