L2 Intermediate · Module 4: CUDA programming fundamentals
4.6Kernel launch anatomy, async execution and streams
The CPU doesn't run kernels; it writes them into a queue that the GPU drains. Understanding that queue explains launch overhead, why hidden synchronizations hurt, how streams overlap copies with compute, and why serving engines replay CUDA Graphs.
By the end you will be able to
- ✓Describe what happens between a kernel launch and the kernel running
- ✓Measure launch overhead and explain when it dominates
- ✓Use streams and async copies to overlap transfers with compute
- ✓Explain what CUDA Graphs do and why inference engines rely on them
In 4.1 a launch returned after 4.6 µs while the kernel ran for 5 ms. Here is what happens in between.
Anatomy of a launch
- CPU, your code:
kernel<<<grid, block, smem, stream>>>(args). - CPU, CUDA runtime and driver: check the arguments, pack them, and write a launch command into a queue in memory the GPU can read. This takes a few microseconds of CPU time, and the call returns.
- GPU front end: reads commands from the queue in order and, once the previous work in the same stream is done, hands the blocks to the SMs.
- SMs: run the blocks. When the last one finishes, the next command in the stream can start.
So the CPU and GPU run side by side, connected by a queue. As long as the CPU stays ahead, the GPU never waits.
Launch overhead
The lab launches 20,000 empty kernels:
1) 20000 empty launches: 2.72 us per launch2) same kernels via a CUDA Graph: 0.64 us per kernel2.7 µs sounds small. But a decode step of an 8B model runs several hundred kernels (norms, matmuls, attention, activations, for each of 32 layers), and at small batch many of them finish in a few microseconds. If the CPU needs 2.7 µs to launch a 3 µs kernel, the GPU spends much of the step idle, waiting for the next command. Decode becomes overhead-bound (3.1).
CUDA Graphs
A CUDA Graph records a whole sequence of launches once, then replays it with a single call. The driver has already validated and packed every command, so replay costs far less per kernel: 0.64 µs instead of 2.72 µs in the lab, and the CPU is free during the replay.
Streams
A stream is one of those queues. Work in the same stream runs in order. Work in different streams may run at the same time, and the GPU has separate engines for computing, copying host-to-device and copying device-to-host. So with several streams, one chunk’s kernel can run while the next chunk is copied in and the previous one is copied out.
Three requirements:
- Use
cudaMemcpyAsyncwith a stream argument, notcudaMemcpy. - The host memory must be pinned (4.3), or the copy silently becomes synchronous.
- Split the work into chunks, each with its own stream.
With 4 streams, chunk k's kernel runs while chunk k+1 copies in and chunk k−1 copies out: 1.95× faster. The best possible is the longest single stage, 8 ms. The CPU only enqueues work and is free until it synchronizes.
Assumes separate copy engines for each direction (true of most NVIDIA GPUs) and pinned host memory.
The lab does this for real with 128 MB:
3) copy in + kernel + copy out, 128 MB: one stream : 23.00 ms 4 streams : 14.66 ms (1.57x)Synchronization points
These all make the CPU wait for the GPU:
cudaDeviceSynchronize(),cudaStreamSynchronize(s),cudaEventSynchronize(e)cudaMemcpy(the non-async one), and any device-to-host copy that needs the result- In PyTorch:
.item(),.cpu(),.tolist(),print(tensor), and data-dependent Python control flow on GPU values
Each one drains the queue. The GPU finishes, then waits idle while the CPU decides what to do next.
Lab: overhead, graphs and streams 🔴
cd labs/m04/4.6 && nvcc -O2 -arch=native streams.cu -o streams && ./streamsSources
Check your understanding
0 / 4 answered1.What does the CPU actually do when it launches a kernel?
2.Which condition is required for cudaMemcpyAsync to overlap with a kernel?
3.Why do serving engines capture decode steps as CUDA Graphs?
4.Three stages take 8, 6 and 8 ms. What is the best possible time with perfect overlap over many chunks?