Unit 9 of 11 · about 5 hours · implement, measure
Pinned memory, streams, events and copy-compute overlap
Copy-compute overlap is the cheapest speedup in GPU computing and the easiest to fake. Without pinned memory and explicit dependencies the timeline stays sequential.
You'll be able to
- Set up pinned host buffers and asynchronous copies with correct stream arguments.
- Build a multi-stream copy and compute pipeline with event dependencies and prove overlap from a timeline.
- Diagnose serialized overlap from trace evidence: pageable memory, default-stream use or dependency errors.
Covers
Read
In this order, only the parts listed
0 of 4 done
- Streams and copy-compute overlapAI Systems Performance Engineering - Chapters 11 and 12
Chapter 11, Overlapping Kernel Execution with CUDA Streams and Using Streams to Overlap Compute with Data Transfers (pinned memory,
cudaMallocHost,cudaMemcpyAsync, two DMA engines), plus Stream-Ordered Memory Allocator - Default streams and event dependenciesAI Systems Performance Engineering - Chapters 11 and 12
Chapter 11, Legacy Default Stream, Modern Per-Thread Default Stream, Fine-Grained Synchronization with Events and Callbacks, and Using CUDA Events for Cross-Stream Synchronization
- Asynchronous execution referenceCUDA C++ Programming Guide (v13.4.2)
Section 2.5: 2.5.2.3 (async copies need pinned host memory), 2.5.6 (blocking streams versus non-blocking streams and the legacy default stream), 2.5.7 (
cudaStreamWaitEvent) and 2.5.8 (implicit synchronization) - Streams intuitionMIT 6.S894 Accelerated Computing (Fall 2025) - Lectures 6, 7 and 9 and Lab 7
Lecture 6, slides 1 to 9: dedicated DMA engines running parallel to kernels, the implicit barrier, command queues, and one buffer per stream
Focus on
cudaMemcpyAsyncon pageable host memory degrades to a synchronous staged copy, so overlap starts withcudaMallocHost.- The legacy default stream synchronizes every blocking stream. Create non-blocking streams with
cudaStreamCreateWithFlagsand order cross-stream work withcudaStreamWaitEvent, notcudaDeviceSynchronize. - Overlap needs comparable copy and compute phases on different engines. Two kernels that each fill the GPU barely overlap, while a chunked copy and compute pipeline shows in
nsys. - Every buffer must outlive its stream work. Free or reuse device and host buffers only after the consuming stream synchronizes.
Common mistakes · 3
- Timing around launches with a host timer measures launch submission, not work. Bracket with events or synchronize the stream first.
- Reusing a host buffer right after enqueueing its copy corrupts the transfer, because the copy is asynchronous.
- One synchronous
cudaMemcpyor default-stream kernel inside the pipeline reimposes global ordering and silently deletes the overlap.
Try it yourself
Closed-book, before checking anything
cudaStreamWaitEvent chaining. Capture with nsys profile --trace=cuda,nvtx --sample=none --cpuctxsw=none and show copy and compute overlapping in the timeline, with event math placing overlapped wall time below the sequential sum.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Why does overlap require pinned host memory?Answer
The DMA engine cannot read pageable memory directly, so the driver inserts a synchronous staged copy through a pinned buffer and the transfer no longer overlaps.
2.A default-stream kernel launches between two blocking-stream kernels. What runs concurrently?Answer
Nothing. The legacy default stream synchronizes all blocking streams, so all three serialize.
3.What does
cudaStreamWaitEventorder, exactly?AnswerWork enqueued into the waiting stream after the call waits for the event. Work already enqueued in that stream is unaffected.
4.Two 5 ms kernels that each fill the GPU run in different streams. How much overlap, and why?Answer
Almost none. Each kernel already occupies every SM, so the second waits for free resources; overlap needs independent engines, such as a copy engine beside the SMs.
Practice
- AI Systems companion code: Chapter 11 stream overlap demos
streams_overlap_demo.cu,event_timing_demo.pyandstream_overlap_demo.py: build and run the overlap demo, reproduce one overlap timeline with event timing, and compare the event timing variantsFrom the
codedirectory,nvcc -O3 -arch=native ch11/streams_overlap_demo.cu -o streams_overlap_demobuilds the CUDA demo, which reports its sequential and overlapped times;event_timing_demo.pyruns with PyTorch alone;stream_overlap_demo.pyimports the shared harness and needs the repository'srequirements.txt. Ignore the B200 expectation files.
You're done when
Your pipeline timeline shows copy and compute overlapping, event math confirms it, and switching back to pageable memory visibly collapses the overlap.
Only when you can do this without notes.
Leads to
Notes
Resources · 3 for this unit, 8 more in the section
- AI Systems Performance Engineering - Chapters 11 and 12Chris Fregly (O'Reilly) · required
Read Chapter 11 on streams, events, inter-kernel pipelining and stream-ordered allocation, and Chapter 12 on CUDA graphs and dynamic scheduling. Skip device-initiated launch beyond the vocabulary.
- MIT 6.S894 Accelerated Computing (Fall 2025) - Lectures 6, 7 and 9 and Lab 7MIT · support
Use Lecture 6 for host-device copies and streams, and Lectures 7 and 9 for map, stencil, reduce, compaction, scan, segmented scan, histogram and merge. Do Lab 7 (Run-Length Compression, with its scan and shuffle warm-ups) built for your own GPU.
- CUDA C++ Programming Guide (v13.4.2)NVIDIA · reference
Use Section 2.3.4 for coalesced access, the transpose example and bank-conflict padding; Section 2.5 for streams, events, synchronization and the stream-capture intro; and Section 4.2 for graph creation, update and capture constraints. This is the API reference beside the book chapters.
Also in this section
- Programming Massively Parallel Processors, 4th edition - Chapters 6 and 9 to 12Wen-mei Hwu, David Kirk and Izzat El Hajj · primary
Read thread coarsening in Chapter 6, then Chapters 9 (histogram), 10 (reduction), 11 (prefix sum) and 12 (merge, optional). Work the exercises that test work, depth, contention and boundary handling.
- Efficient Matrix Transpose in CUDA C/C++NVIDIA · required
Rebuild copy, naive transpose, coalesced transpose and padded shared-memory variants. Update deprecated details for the installed toolkit and compare requested transactions, bank behavior and achieved bandwidth on odd as well as friendly shapes.
- Optimizing Parallel Reduction in CUDANVIDIA · required
Implement the optimization sequence from interleaved addressing through sequential addressing, first-add-during-load, unrolling and warp-level completion. Correct outdated assumptions and retain the stage where each optimization stops helping.
- Single-pass Parallel Prefix Scan with Decoupled Look-backNVIDIA Research · depth
Read after two-pass scans pass. Derive the state and progress argument, then implement or inspect a bounded version only if the available hardware and time support meaningful comparison.
- Online Normalizer Calculation for SoftmaxOnline Softmax Authors · required
Derive the running maximum and normalizer recurrence, test extreme and all-masked rows, and compare materialized, two-pass and online implementations before using the recurrence inside attention.
- LeetGPU - CUDA Primitive ChallengesAlphaGPU · required
Solve challenge 3 Matrix Transpose, 4 Reduction, 5 Softmax, 13 Histogramming and 16 Prefix Sum in CUDA. Add 70 Segmented Prefix Sum or 72 Stream Compaction only after the required set. Keep local source, hidden-shape assumptions, hardware, raw timings and correctness evidence outside the leaderboard.
- GPU MODE - Reductions and ScansGPU MODE · support
Use Lectures 9, 20, 21 and 24 after the PMPP implementations. Reproduce one faster scan or reduction idea and show the exact shape and device regime where it changes the bottleneck.
- GPU Microbenchmark and Kernel Suite - CUDA CoreRoadmap-Final · support
Complete the CUDA vector map, reduction, transpose and stable softmax requirements plus seeded access, synchronization, mask, stream and numerical failures. Preserve library and framework baselines.