barrier synchronization parallel
**Barrier Synchronization** is **the parallel programming primitive that blocks all participating threads or processes at a synchronization point until every participant has arrived — ensuring that all preceding computation is complete before any thread proceeds past the barrier, essential for phase-separated algorithms, iterative solvers, and collective communication**.
**Barrier Semantics:**
- **Global Barrier**: all threads in the parallel region must reach the barrier before any proceeds — guarantees all writes before the barrier are visible to all reads after the barrier (memory fence semantics)
- **Named/Group Barriers**: only a subset of threads participates — useful when different team subsets synchronize independently; reduces idle time by not waiting for unrelated threads
- **Split-Phase Barrier**: separate arrive (signal completion) and wait (block until all arrived) operations — enables useful computation between signaling and waiting, reducing idle time
- **Counting Barrier**: tracks how many threads have arrived using an atomic counter — simplest implementation but creates contention on the shared counter with high thread counts
**Implementation Algorithms:**
- **Centralized Barrier**: single shared counter incremented atomically by each arriving thread — last thread resets counter and releases all waiters; O(1) space but O(P) contention on counter creates serialization bottleneck for >32 threads
- **Tree Barrier**: binary (or k-ary) tree of local barriers — leaf threads synchronize with parent, propagation reaches root in O(log P) steps, then release propagates back down; reduces contention to O(log P) sequential atomic operations
- **Tournament Barrier**: processes paired in tournament fashion — winner of each round advances to next round; combines reduction and broadcast in a single tree traversal; O(log P) rounds with each round involving only point-to-point communication
- **Butterfly Barrier**: inspired by butterfly network — at round k, process i communicates with process i XOR 2^k; all processes complete simultaneously in O(log P) rounds with all-to-all information exchange
**Performance Considerations:**
- **Barrier Overhead**: time from first arrival to last departure — minimizing this requires both fast notification mechanism and efficient wakeup; typical overhead 1-10 μs for software barriers on multi-core CPUs
- **Load Imbalance Amplification**: barriers force fast threads to wait for the slowest — even 1% load imbalance across 1000 barriers per iteration accumulates to significant performance loss
- **NUMA Effects**: barrier variables accessed by all threads create cross-node coherence traffic — NUMA-aware implementations use per-node local barriers with global coordination between node representatives
- **GPU __syncthreads()**: hardware-implemented barrier within a thread block — zero overhead, completes in single cycle when all threads arrive simultaneously; but cannot synchronize across blocks (requires kernel completion)
**Barrier synchronization is the fundamental coordination mechanism in parallel computing — while conceptually simple, barriers have profound performance implications because they serialize parallel execution, making barrier count and barrier overhead critical factors in parallel scalability.**