多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 5.5。支持的原子操作 > 原文: [http://numba.pydata.org/numba-doc/latest/roc/intrinsics.html](http://numba.pydata.org/numba-doc/latest/roc/intrinsics.html) Numba 提供了对`numba.roc.atomic`类中 HSA 支持的一些原子操作的访问。 ## 5.5.1。示例 下面的代码演示了如何使用`numba.roc.atomic.add`来并行计算输入数组中[0,32]中的每个数字: ```py 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 ```