IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /get-started.md).

Mojo struct

NamedBarrierSemaphore

struct NamedBarrierSemaphore[origin: MutOrigin, //, thread_count: Int32, id_offset: Int32, max_num_barriers: Int32]

A device-wide semaphore implementation for NVIDIA GPUs with named barriers.

It's using an acquire-release logic instead of atomic instructions for inter-CTA synchronization with a shared lock variable. Please note that the memory barrier is for syncing warp groups within in a CTA. Cutlass reference implementation: https://github.com/NVIDIA/cutlass/blob/a1aaf2300a8fc3a8106a05436e1a2abad0930443/include/cutlass/arch/barrier.h.

Parameters​

  • ​origin (MutOrigin): Origin of the shared lock pointer in global memory.
  • ​thread_count (Int32): Number of threads participating in the barrier.
  • ​id_offset (Int32): Offset for the barrier ID.
  • ​max_num_barriers (Int32): Maximum number of named barriers to use.

Implemented traits​

AnyType, Copyable, Deinitable, ImplicitlyCopyable, Movable, RegisterPassable, TrivialRegisterPassable

Methods​

__init__​

def __init__(lock: Pointer[Int32, origin], thread_id: Int) -> Self

Initialize a new Semaphore instance.

Args:

  • ​lock (Pointer[Int32, origin]): Pointer to shared lock variable in global memory.
  • ​thread_id (Int): Thread ID within the CTA, used to determine if this thread should perform atomic operations.

state​

def state(self) -> Int32

Get the current state of the semaphore.

Returns:

Int32: The current state value of the semaphore.

wait_eq​

def wait_eq(mut self, id: Int32, status: Int32 = Int32(0))

Waits until the semaphore state equals the specified status.

Args:

  • ​id (Int32): Barrier ID to use for synchronization.
  • ​status (Int32): Expected status value to wait for.

wait_lt​

def wait_lt(mut self, id: Int32, count: Int32 = Int32(0))

Waits until the semaphore state is less than the specified count.

Args:

  • ​id (Int32): Barrier ID to use for synchronization.
  • ​count (Int32): Count value to compare against.

arrive_set​

def arrive_set(self, id: Int32, status: Int32 = Int32(0))

Arrives at the barrier and sets the semaphore status.

Args:

  • ​id (Int32): Barrier ID to use for synchronization.
  • ​status (Int32): Status value to set after arriving.