原文: http://numba.pydata.org/numba-doc/latest/roc/intrinsics.html
Numba 提供了对numba.roc.atomic
类中 HSA 支持的一些原子操作的访问。
下面的代码演示了如何使用numba.roc.atomic.add
来并行计算输入数组中[0,32]中的每个数字:
from numba import roc
import numpy as np
@roc.jit
def hsa_atomic_histogram(ary):
tid = roc.get_local_id(0)
sm = roc.shared.array(32, numba.uint32) # declare shared library
sm[tid] = 0 # init values to zero
roc.barrier(1) # synchronize (wait for init)
loc = ary[tid] % 32 # ensure we are in range
roc.atomic.add(sm, loc, 1) # atomic add
roc.barrier(1) # synchronize
ary[tid] = sm[tid] # store result inplace
ary = np.random.randint(0, 32, size=32).astype(np.uint32)
orig = ary.copy()
# HSA version
hsa_atomic_histogram[1, 32](ary)
# Expected behavior
gold = np.zeros_like(ary)
for i in range(orig.size):
gold[orig[i]] += 1
print(ary) # HSA kernel result
print(gold) # for comparison