Unit 7 of 10 · about 4 hours · explain, derive
Synchronization scopes, atomics and memory ordering
An atomic flag is not automatically a publication protocol. Correct coordination needs both a matching scope and an ordering relation for the payload.
You'll be able to
- Choose warp, block, cluster or device coordination from the communicating participants.
- Distinguish atomicity, memory ordering and barriers, and derive a release/acquire publication relation.
- Identify a synchronization hazard caused by divergent participation or inter-block scheduling.
Covers
Read
In this order, only the parts listed
0 of 3 done
- CUDA Programming Guide 13.4.2: scope and atomic orderCUDA Programming Guide
Section 3.2.3 scope table and Section 3.2.4.1.1
Read the scoped atomic definitions, not the full producer-consumer sample: its shared flag is not initialized. Cluster and grid cooperative programming are later implementation work.
- CUDA Programming Guide 13.4.2: barriers and participating lanesCUDA Programming Guide
Sections 5.4.4.1 and 5.4.4.2,
__syncthreadsand__syncwarpdefinitionsSkip the Section 5.4.4.1 example labelled valid behavior:
shared_data[128 - threadIdx.x]addresses index 128 for thread 0 in a 128-element array. - CUDA Programming Guide 13.4.2: memory model and publicationCUDA Programming Guide
Sections 5.7.1, 5.7.3 to 5.7.5
Trace the message-passing ordering, but do not use an inter-block spin wait as a general grid barrier: waiting resident blocks can prevent a producer block from being scheduled.
Focus on
- Scope identifies which threads an atomic operation can coordinate. Device scope does not turn the operation into a device-wide barrier; system scope has additional type, alignment and platform prerequisites.
- A release store publishes earlier payload writes to a consumer whose acquire load reads that store, provided both operations have sufficient scope. A relaxed atomic update alone orders no unrelated payload.
- A block barrier requires block-uniform participation and orders participating threads' relevant prior accesses. A warp barrier requires a correct participant mask; implicit lockstep is not a synchronization contract.
- Initialization must happen before flag use. Separate correctness of memory ordering from forward progress: an ordered spin loop can still deadlock under an unsuitable launch.
Common mistakes · 3
- Putting a block barrier inside a lane-dependent bounds condition. Guard memory operations while keeping required barrier participation uniform.
- Using
volatileor a memory fence as a substitute for an atomic flag and a matching acquire operation. - Calling a successful stress test proof of a racy protocol. A missing ordering relation can remain invisible on a particular run.
Try it yourself
Closed-book, before checking anything
cuda::atomic_ref<int, cuda::thread_scope_block> to release-store flag 1. Thread 32 acquire-loads until it sees 1, then writes the payload to an output slot. The host must observe 42 after checked completion. Explain the initialization barrier and release/acquire happens-before path separately; do not move this protocol to arbitrary blocks without a forward-progress argument.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Does a relaxed atomic increment publish an unrelated ordinary write?Answer
No. It makes the increment atomic at its scope, but supplies no release/acquire relation for the unrelated payload.
2.Can a device-scoped flag safely coordinate host reads on every platform?Answer
No. Host coordination needs a supported system-scoped mechanism and compatible memory, or explicit stream completion and transfer.
3.Why can a correctly ordered inter-block spin protocol hang?Answer
Waiting blocks can occupy the available residency slots while the producer remains unscheduled. Memory ordering does not guarantee block scheduling or a global barrier.
You're done when
Your publication test returns 42, and you can trace its initialization, synchronization scope and happens-before relation without relying on observed lockstep.
Only when you can do this without notes.
Leads to
Notes
Resources · 1 for this unit, 10 more in the section
- CUDA Programming GuideNVIDIA · required
Read the Basics and Programming Model chapters plus the sections on memory, synchronization, hardware implementation and compute capabilities used by your kernels. Record the toolkit documentation version alongside every claim.
Also in this section
- Programming Massively Parallel Processors, 4th edition - Chapters 2 to 6Wen-mei Hwu, David Kirk and Izzat El Hajj · primary
Read Chapters 2 to 6: data-parallel kernels, multidimensional grids, compute architecture and scheduling, memory architecture and data locality, and performance considerations. Work the chapter exercises on warps, occupancy, memory and coalescing, and predict three altered launch or address patterns before running them.
- MIT 6.S894 Accelerated Computing (Fall 2025) - Lectures 2 to 5 and Labs 2 and 3MIT · required
Use Lecture 2 (Throughput Processors), Lectures 3 and 4 (Memory, Parts 1 and 2) and Lecture 5 with its continuation (scratchpads, bank conflicts, register shuffles, overlap) for why the hardware looks as it does. Do Lab 2 (Massively-Parallel Mandelbrot, with its FMA-latency and warp-scheduler probes) and Lab 3 (Wave Simulation, with its coalesced-load and memory-latency probes) from the public lab repositories, built with nvcc for your own GPU instead of the course service. The labs were written for an RTX 4000 Ada, so recompute any SM count or bandwidth the questions assume.
- CUDA C++ Best Practices GuideNVIDIA · required
Study Assess, Parallelize, Optimize, Deploy; Getting the Right Answer; memory optimization; execution configuration; instruction optimization; and deployment compatibility. Convert recommendations into hypotheses, not universal rules.
- Parallel Thread Execution ISANVIDIA · reference
Read the PTX purpose, machine model, state spaces, memory consistency and target directives. Compile two tiny kernels, map source to PTX and SASS, and explain what PTX does and does not guarantee across devices.
- GPU MODE Foundations LecturesGPU MODE · support
Use Lectures 2, 3, 4, 5, 8, 37 and 41 for PMPP review, CUDA setup, architecture, optimization checklists, SASS and documentation reading. Reproduce only the examples that test a prediction from this stage.
- TensorTonic Language Modeling from Scratch - GPU analysis problemsTensorTonic · support
Solve GPU Occupancy Calculator, Shared-Memory Bank Conflict Analyzer and Global-Memory Coalescing Counter after the matching units, and check each answer against your own derivation.
- AI Performance Engineering - GPU FundamentalsWafer · reference
A curated index. Use Section 1 only to find a CUDA, PTX or architecture source the other entries lack.
- NVIDIA CUDA Pro Tips: grid-stride loops and vectorized memory accessNVIDIA · required
Read the grid-stride SAXPY loop and its scalability and debugging discussion (April 22, 2013), plus the scalar-copy SASS and vector-type alignment discussion in Increase Performance with Vectorized Memory Access (August 4, 2025). Skip the latter article's full vector-copy kernels: their tail-thread condition can omit remainders. These fill the pool's grid-stride and vector-width coverage gaps.
- CUDA Binary Utilities 13.4NVIDIA · reference
Sections 1.1 to 1.2, 2.1.1 to 2.1.3, 3.1.1 and 3.1.4 for cubin extraction, resource reports and line-correlated SASS. The existing compiler and PTX references lack a worked binary-tool workflow.
- PyTorch custom C++/CUDA operators and CUDA semanticsPyTorch · required
Custom C++ and CUDA Operators: build system, backend registration, Testing an operator and mutable schema; CUDA semantics 2.14: CUDA streams. These fill the native tensor-boundary gap in the CUDA-only pool. Stop before fake-kernel and autograd implementation; harden the tutorial's CUDA example for empty inputs, same-device checks, device guards, indexing width and launch errors.