core24 min studyLesson 455 in Engineering

Synchronization scopes: order the right memory for the right participants

Separate warp and block barriers, memory fences, atomic publication, and host-visible completion.

On this page

What you’ll understand

  • Choose synchronization scope based on the actual communicating participants.
  • Distinguish memory ordering from collective execution completion.
  • Explain why a fence alone does not publish a safe ready flag.

Synchronization is not one universal pause button. A warp primitive coordinates a named subset, a block barrier coordinates a block, a memory fence orders particular accesses, and a host synchronization call waits for submitted device work within its documented scope. Correct communication requires both the appropriate participants and the right visibility relationship. This lesson follows CUDA12.6 and treats cross-device or host participation as a stronger scope that requires supported mechanisms, not an automatic consequence of using an atomic instruction.

Execution rendezvous and memory order differ

A block barrier such as __syncthreads coordinates required non-exited threads in that block and provides the documented visibility for their relevant earlier accesses. It is not a grid-wide barrier between arbitrary blocks. Calling it in a conditional reached by only part of a still-participating block can violate the contract. Warp synchronization similarly requires a valid mask and matching participation. A warp shuffle or vote with a sync suffix is not automatically the same memory-ordering primitive as a shared-memory barrier.

A memory fence orders a calling thread’s specified memory operations at its documented scope; it does not make all other threads arrive or wait for a producer to finish. Publishing data through a flag therefore needs a correct atomic communication protocol, not a plain store plus the word fence in a comment. Volatile accesses do not generally replace atomics or establish the required happens-before relationship. Choose a supported memory scope that includes both producer and consumer.

Wpayload  →  release⁡(F)  →  acquire⁡(F)  →  RpayloadW_{\rm payload}\;\to\;\operatorname{release}(F)\;\to\;\operatorname{acquire}(F)\;\to\;R_{\rm payload}
This sketches a publication happens-before chain when an acquire reads from the corresponding release on an appropriate atomic flag F. Payload writes and reads must be race-free under the actual CUDA/C++ memory model, and atomic scope must include the communicating threads.↔ Scroll wide equations horizontally.

Match the scope to the actual boundary

Threads within one block may use shared memory and a block barrier. Threads in different blocks require a supported device-wide communication protocol or a separate correctly ordered kernel phase; one block’s barrier cannot synchronize the other. Device-scope atomics do not automatically synchronize a CPU reader or a different device. System scope requires supported hardware, memory, and atomic semantics for all participating agents. Do not use a broad phrase such as globally visible without naming which observers and which operation establish the relation.

Host completion is another boundary. cudaStreamSynchronize waits for preceding work in one stream; cudaDeviceSynchronize waits according to its device/context scope and may report asynchronous errors. Neither repairs a data race that already occurred inside a kernel. A host thread also must not consume an asynchronous copy’s destination or reuse its source before the relevant completion. A synchronous-looking CPU call and a device memory fence are different mechanisms with different participants and costs.

Tbarrier≥max⁡iaiT_{\rm barrier}\geq\max_i a_i
a_i is the arrival time of participating thread i at one idealized collective, measured from a common origin. T_barrier is its release time in the same units. This lower bound assumes valid participation and excludes release overhead; it does not specify CUDA barrier latency.↔ Scroll wide equations horizontally.

Prove the communication before tuning it

For each shared value, identify its producer, consumers, storage location, and lifetime. Draw the ordering edge that makes the write visible before the read. Check every branch around barriers and every mask under partial warps. For inter-block cooperation, avoid a busy-wait scheme that assumes all blocks are simultaneously resident; waiting blocks can occupy the resources needed by the producer. Test with varied block sizes and inputs, then use diagnostic tools within their documented coverage. Fewer barriers are only an optimization if the required ordering remains true.

The network lab shows a mathematical dependency, not concurrent visibility. Reset, set x=1 and w=1, predict output sigmoid(0.5)≈0.622, then Step. The displayed output depends on its input computation, but the diagram has no parallel memory agents, barriers, atomics, or fences. Its limited role is to encourage an explicit dependency chain. The actual GPU proof must add the memory-order and scope edges that a simple function graph does not contain.

The slowest required participant sets a lower bound

Four participating threads reach a valid toy barrier at times 2,5,3, and 4 microseconds from a common start. Assume an additional one-microsecond release cost after all arrive.

  1. The maximum arrival is five microseconds, so release cannot occur before that time. The thread arriving at two waits at least three microseconds for the last arrival.

  2. Adding the declared one-microsecond release cost gives release at six microseconds in this toy schedule. The earliest thread waits four microseconds total, while the last waits one.

  3. If a required non-exited thread never reaches the barrier, this finite calculation no longer applies; the program violates the assumed participation. A memory fence executed by one thread would not cause the missing thread to arrive or make the collective complete.

The result

Valid participation and arrival imbalance determine this schedule. The numbers are a teaching scenario, not a measured CUDA barrier cost.

Explore the model

Interactive lab

Interactive lab · network

A dependency graph omits memory visibility

Reset, set x=1 and w=1, predict σ(0.5)≈0.622, then Step. This single sigmoid graph illustrates input dependence only; it has no GPU threads, memory-order edges, synchronization scopes, atomics, or barriers.

A dependency graph omits memory visibilityOne inspectable sigmoid unit, σ(wx − 0.5). Playback traces the computation graph. There is no training, attention mechanism or hidden deep network. Current readings: Product · wx: 1.2; Logit · wx − 0.5: 0.7; Sigmoid output: 0.6682. All parameters are adjustable with the labeled controls below.One input. One weight. One sigmoid.x1.20× w1.20+ b0.70σ0.668output0.668w = 1.00b = −0.50σ(z) = 1 / (1 + exp(−z))
Computation graphSignal trace
Product · wx
1.2
Logit · wx − 0.5
0.7
Sigmoid output
0.6682
t = 0 s

One inspectable sigmoid unit, σ(wx − 0.5). Playback traces the computation graph. There is no training, attention mechanism or hidden deep network. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forA concurrent communication proof needs explicit visibility and participant scope beyond mathematical data dependence.

Check your understanding

What does a device memory fence alone fail to provide?

Try it yourself

Three required participants arrive at 1,7, and 4 microseconds. Under an ideal zero-release-cost barrier, when can they leave? Would a device-scope atomic alone establish a CPU consumer’s required system-scope relation?

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Take the latest arrival for the toy schedule, then check the scope boundary separately.

Compare with the solution

They can leave no earlier than seven microseconds, assuming every required participant arrived correctly. A device-scope atomic does not automatically establish the required relation with a CPU observer. The protocol needs supported system-scope memory and atomic semantics, plus correct ordering and completion for that use.

Take it with you

  • Rendezvous, memory ordering, and host completion are distinct operations.
  • Scope must include every actual producer and consumer.
  • A correct publication protocol needs explicit race-free ordering and participation.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.