Skip to main content
Log in

Mojo struct

Atomic

struct Atomic[dtype: DType, *, scope: StringSlice[StaticConstantOrigin] = __init__[__mlir_type.!kgen.string]("")]

Represents a value with atomic operations.

The class provides atomic add and sub methods for mutating the value.

Parameters

  • dtype (DType): DType of the value.
  • scope (StringSlice[StaticConstantOrigin]): The memory synchronization scope.

Fields

  • value (SIMD[dtype, 1]): The atomic value. This is the underlying value of the atomic. Access to the value can only occur through atomic primitive operations.

Implemented traits

AnyType, UnknownDestructibility

Methods

__init__

@implicit __init__(out self, value: SIMD[dtype, 1])

Constructs a new atomic value.

Args:

  • value (SIMD[dtype, 1]): Initial value represented as Scalar[dtype] type.

__iadd__

__iadd__(mut self, rhs: SIMD[dtype, 1])

Performs atomic in-place add.

Atomically replaces the current value with the result of arithmetic addition of the value and arg. That is, it performs atomic post-increment. The operation is a read-modify-write operation. Memory is affected according to the value of order which is sequentially consistent.

Args:

  • rhs (SIMD[dtype, 1]): Value to add.

__isub__

__isub__(mut self, rhs: SIMD[dtype, 1])

Performs atomic in-place sub.

Atomically replaces the current value with the result of arithmetic subtraction of the value and arg. That is, it performs atomic post-decrement. The operation is a read-modify-write operation. Memory is affected according to the value of order which is sequentially consistent.

Args:

  • rhs (SIMD[dtype, 1]): Value to subtract.

load

load(mut self) -> SIMD[dtype, 1]

Loads the current value from the atomic.

Returns:

The current value of the atomic.

fetch_add

static fetch_add[*, ordering: Consistency = Consistency(__init__[__mlir_type.!pop.int_literal](6))](ptr: UnsafePointer[SIMD[dtype, 1], address_space=address_space, alignment=alignment, origin=origin], rhs: SIMD[dtype, 1]) -> SIMD[dtype, 1]

Performs atomic in-place add.

Atomically replaces the current value with the result of arithmetic addition of the value and arg. That is, it performs atomic post-increment. The operation is a read-modify-write operation. Memory is affected according to the value of order which is sequentially consistent.

Parameters:

  • ordering (Consistency): The memory ordering.

Args:

  • ptr (UnsafePointer[SIMD[dtype, 1], address_space=address_space, alignment=alignment, origin=origin]): The source pointer.
  • rhs (SIMD[dtype, 1]): Value to add.

Returns:

The original value before addition.

fetch_add[*, ordering: Consistency = Consistency(__init__[__mlir_type.!pop.int_literal](6))](mut self, rhs: SIMD[dtype, 1]) -> SIMD[dtype, 1]

Performs atomic in-place add.

Atomically replaces the current value with the result of arithmetic addition of the value and arg. That is, it performs atomic post-increment. The operation is a read-modify-write operation. Memory is affected according to the value of order which is sequentially consistent.

Parameters:

  • ordering (Consistency): The memory ordering.

Args:

  • rhs (SIMD[dtype, 1]): Value to add.

Returns:

The original value before addition.

store

static store[*, ordering: Consistency = Consistency(__init__[__mlir_type.!pop.int_literal](6))](ptr: UnsafePointer[SIMD[dtype, 1], address_space=address_space, alignment=alignment, origin=origin], value: SIMD[dtype, 1])

Performs atomic store. The operation is a read-modify-write operation. Memory is affected according to the value of order which is sequentially consistent.

Parameters:

  • ordering (Consistency): The memory ordering.

Args:

  • ptr (UnsafePointer[SIMD[dtype, 1], address_space=address_space, alignment=alignment, origin=origin]): The source pointer.
  • value (SIMD[dtype, 1]): The value to store.

fetch_sub

fetch_sub[*, ordering: Consistency = Consistency(__init__[__mlir_type.!pop.int_literal](6))](mut self, rhs: SIMD[dtype, 1]) -> SIMD[dtype, 1]

Performs atomic in-place sub.

Atomically replaces the current value with the result of arithmetic subtraction of the value and arg. That is, it performs atomic post-decrement. The operation is a read-modify-write operation. Memory is affected according to the value of order which is sequentially consistent.

Parameters:

  • ordering (Consistency): The memory ordering.

Args:

  • rhs (SIMD[dtype, 1]): Value to subtract.

Returns:

The original value before subtraction.

compare_exchange_weak

compare_exchange_weak[*, failure_ordering: Consistency = Consistency(__init__[__mlir_type.!pop.int_literal](6)), success_ordering: Consistency = Consistency(__init__[__mlir_type.!pop.int_literal](6))](self, mut expected: SIMD[dtype, 1], desired: SIMD[dtype, 1]) -> Bool

Atomically compares the self value with that of the expected value. If the values are equal, then the self value is replaced with the desired value and True is returned. Otherwise, False is returned the the expected value is rewritten with the self value.

Parameters:

  • failure_ordering (Consistency): The memory ordering for the failure case.
  • success_ordering (Consistency): The memory ordering for the success case.

Args:

  • expected (SIMD[dtype, 1]): The expected value.
  • desired (SIMD[dtype, 1]): The desired value.

Returns:

True if self == expected and False otherwise.

max

static max[*, ordering: Consistency = Consistency(__init__[__mlir_type.!pop.int_literal](6))](ptr: UnsafePointer[SIMD[dtype, 1], address_space=address_space, alignment=alignment, mut=mut, origin=origin], rhs: SIMD[dtype, 1])

Performs atomic in-place max on the pointer.

Atomically replaces the current value pointer to by ptr by the result of max of the value and arg. The operation is a read-modify-write operation. The operation is a read-modify-write operation perform according to sequential consistency semantics.

Constraints:

The input type must be either integral or floating-point type.

Parameters:

  • ordering (Consistency): The memory ordering.

Args:

  • ptr (UnsafePointer[SIMD[dtype, 1], address_space=address_space, alignment=alignment, mut=mut, origin=origin]): The source pointer.
  • rhs (SIMD[dtype, 1]): Value to max.

max[*, ordering: Consistency = Consistency(__init__[__mlir_type.!pop.int_literal](6))](self, rhs: SIMD[dtype, 1])

Performs atomic in-place max.

Atomically replaces the current value with the result of max of the value and arg. The operation is a read-modify-write operation perform according to sequential consistency semantics.

Constraints:

The input type must be either integral or floating-point type.

Parameters:

  • ordering (Consistency): The memory ordering.

Args:

  • rhs (SIMD[dtype, 1]): Value to max.

min

static min[*, ordering: Consistency = Consistency(__init__[__mlir_type.!pop.int_literal](6))](ptr: UnsafePointer[SIMD[dtype, 1], address_space=address_space, alignment=alignment, mut=mut, origin=origin], rhs: SIMD[dtype, 1])

Performs atomic in-place min on the pointer.

Atomically replaces the current value pointer to by ptr by the result of min of the value and arg. The operation is a read-modify-write operation. The operation is a read-modify-write operation perform according to sequential consistency semantics.

Constraints:

The input type must be either integral or floating-point type.

Parameters:

  • ordering (Consistency): The memory ordering.

Args:

  • ptr (UnsafePointer[SIMD[dtype, 1], address_space=address_space, alignment=alignment, mut=mut, origin=origin]): The source pointer.
  • rhs (SIMD[dtype, 1]): Value to min.

min[*, ordering: Consistency = Consistency(__init__[__mlir_type.!pop.int_literal](6))](self, rhs: SIMD[dtype, 1])

Performs atomic in-place min.

Atomically replaces the current value with the result of min of the value and arg. The operation is a read-modify-write operation. The operation is a read-modify-write operation perform according to sequential consistency semantics.

Constraints:

The input type must be either integral or floating-point type.

Parameters:

  • ordering (Consistency): The memory ordering.

Args:

  • rhs (SIMD[dtype, 1]): Value to min.