Skip to content

Commit 10baaa0

Browse files
ZzEeKkAaDiptorup Deb
authored andcommitted
Fix atomics example
1 parent b50a69c commit 10baaa0

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

numba_dpex/examples/kernel/atomic_op.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,28 @@
88

99

1010
@ndpx.kernel
11-
def atomic_reduction(a):
11+
def atomic_reduction(a, res):
12+
"""Summarize all the items in a and writes it into res using atomic.add.
13+
14+
:param a: array of values to get sum
15+
:param res: result where to add all the items from a array. It must be preset to 0.
16+
"""
1217
idx = ndpx.get_global_id(0)
13-
ndpx.atomic.add(a, 0, a[idx])
18+
ndpx.atomic.add(res, 0, a[idx])
1419

1520

1621
def main():
1722
N = 10
18-
a = np.arange(N)
23+
24+
# We are storing sum to the first element
25+
a = np.arange(0, N)
26+
res = np.zeros(1, dtype=a.dtype)
1927

2028
print("Using device ...")
2129
print(a.device)
2230

23-
atomic_reduction[ndpx.Range(N)](a)
24-
print("Reduction sum =", a[0])
31+
atomic_reduction[ndpx.Range(N)](a, res)
32+
print("Reduction sum =", res[0])
2533

2634
print("Done...")
2735

0 commit comments

Comments
 (0)