Supported Atomic Operations#

Numba-dpex supports some of the atomic operations supported in DPC++. Those that are presently implemented are as follows:

class atomic
add(ary, idx, val)

Performs atomic addition ary[idx] += val.

Parameters:
  • ary – An array on which the atomic operation is performed. Allowed types: int32, int64, float32, or float64

  • idx (int) – Index of the array element, on which atomic operation is performed

  • val – The value of an increment. Its type must match the type of array elements, ary[]

Returns:

The old value at the index location ary[idx] as if it is loaded atomically.

Raises:

None

sub(ary, idx, val)

Performs atomic subtraction ary[idx] -= val.

Parameters:
  • ary – An array on which the atomic operation is performed. Allowed types: int32, int64, float32, or float64

  • idx (int) – Index of the array element, on which atomic operation is performed

  • val – The value of a decrement. Its type must match the type of array elements, ary[]

Returns:

The old value at the index location ary[idx] as if it is loaded atomically.

Raises:

None

Example#

Example usage of atomic operations

def main():
    N = 10

    # We are storing sum to the first element
    a = np.arange(0, N)
    res = np.zeros(1, dtype=a.dtype)

    print("Using device ...")
    print(a.device)

    atomic_reduction[ndpx.Range(N)](a, res)
    print("Reduction sum =", res[0])

    print("Done...")

Note

The numba_dpex.atomic.add function is analogous to The numba.cuda.atomic.add provided by the numba.cuda backend.

Full examples#

  • numba_dpex/examples/atomic_op.py