Unit content
Barrier synchronization and phased parallel algorithms
A barrier is a synchronization point that no participating task may pass until all participants have reached it.
Barriers are useful when a parallel algorithm proceeds in phases. Each task completes its portion of phase $k$, waits at the barrier, and only then begins phase $k+1$ knowing that every other task has also completed the previous phase.
For example, suppose a simulation computes a new grid from an unchanged previous grid. Each worker writes its own region of the new grid:
compute my region of new_grid from old_grid
barrier
swap old_grid and new_grid
begin the next step
Without the barrier, one worker could begin the next time step while another region of new_grid is still incomplete.
A barrier provides ordering, not mutual exclusion. Workers may execute freely before and after it; they merely synchronize at the phase boundary.
Barriers can waste time when workloads are imbalanced because fast workers wait for the slowest participant. They can also deadlock if the program expects a participant that never reaches the barrier.
Use a barrier when there is a genuine global phase dependency. Adding barriers merely for reassurance destroys overlap and can serialize an otherwise scalable computation.