Home Knowledge Base Barrier Synchronization

Barrier Synchronization is the fundamental parallel coordination primitive where all participating threads or processes must arrive at a designated point before any can proceed past it — ensuring a consistent global state at synchronization points, implemented through algorithms ranging from simple centralized counters to sophisticated tree-based and butterfly barriers that scale to thousands of threads while minimizing contention and latency.

Why Barriers

Centralized Barrier (Simple Counter)

// Simplest barrier: atomic counter + spinning
typedef struct {
    atomic_int count;
    atomic_int sense;
    int num_threads;
} barrier_t;

void barrier_wait(barrier_t *b, int *local_sense) {
    *local_sense = !(*local_sense);  // Flip local sense
    if (atomic_fetch_add(&b->count, 1) == b->num_threads - 1) {
        // Last thread to arrive → release all
        atomic_store(&b->count, 0);
        atomic_store(&b->sense, *local_sense);
    } else {
        // Spin until sense flips
        while (atomic_load(&b->sense) != *local_sense) { }
    }
}

Tree Barrier (Logarithmic)

          [Root]
         /      \
      [N0]      [N1]
      /  \      /  \
    [T0] [T1] [T2] [T3]

 Arrival (up): T0→N0, T1→N0, T2→N1, T3→N1 → N0→Root, N1→Root
 Release (down): Root→N0,N1 → N0→T0,T1 → N1→T2,T3

Butterfly (Tournament) Barrier

 Step 0: T0↔T1, T2↔T3    (pairs at distance 1)
 Step 1: T0↔T2, T1↔T3    (pairs at distance 2)

 After log₂(P) steps: All threads know everyone has arrived.

GPU Barriers

ScopeMechanismLatency
Warp (32 threads)__syncwarp()~1 cycle (implicit in SIMT)
Thread block (up to 1024)__syncthreads()~20-40 cycles
Grid (all blocks)cooperative_groups::grid_group::sync()~1000+ cycles
Multi-GPUNCCL barrier / cudaDeviceSynchronize~µs

Barrier Performance on Multi-Socket Servers

Algorithm2 threads64 threads256 threads
Centralized50 ns2 µs15 µs
Tree (degree-2)50 ns400 ns800 ns
Butterfly50 ns300 ns600 ns
MCS (scalable)50 ns350 ns650 ns

Sense-Reversing Barrier

Barrier synchronization is the heartbeat of bulk-synchronous parallel computing — every iterative solver, every GPU kernel with shared memory cooperation, and every MPI collective operation depends on efficient barriers to enforce ordering between computation phases, making barrier algorithm choice a critical performance factor for any parallel application that synchronizes more than a few dozen threads.

barrier synchronizationbarrier algorithmtree barriersense reversing barriergpu barrier

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.