What this video shows
About this walkthrough
This warm-up starts from a correct SAXPY-and-scale kernel that runs two calculations through a temporary array. The walkthrough reads the memory operations in the source, measures their traffic, fuses the two loops, and removes the scratch allocation. Global-memory sectors fall from 10,488,855 to 6,293,313, and peak allocation from 256 MiB to 192 MiB. Kernel time drops 25.5% in this pair of runs, while the unprofiled harness runtime is 2.1% longer.
Read along
Transcript
Select a time to play the video from that chapter.
0:00Remove unnecessary memory traffic
Last time, we made neighboring threads access neighboring memory. This warmup deliberately uses two loops and a temporary array. The fix is simple: combine the loops. We will use the profiler to measure why that helps.
0:15Understand the calculation and goal
The program computes alpha times x, plus y, then multiplies by beta. Our goal is correct output with at most 8 million memory sectors, and no scratch allocation.
0:28Measure the original code
Start with the original code on the RTX 3090. It passes correctness, but uses 10.5 million sectors. Let's inspect where that traffic comes from.
0:41Explain the instructions using the code
Memory access analysis lists five instructions, with 100% coalescing. The table doesn't label the arrays, so let's read the code. The first loop reads x and y, then writes scratch. The second reads scratch and writes the output.
0:59Measure the memory footprint
At the top of Memory, Peak in flight is 256 MiB. This is the maximum tracked allocation alive at once, not the amount of data accessed.
1:10Fuse the loops
Back in the editor, replace the two loops with one. Compute the SAXPY result into a local value, then multiply that value by beta. Each thread uses its own intermediate, so we don't need a global scratch array or shared memory.
1:27Remove the scratch allocation
Now open pipeline.cu. Remove the scratch allocation and its matching free. Pass a null pointer for the unused scratch argument. Keep the error checks and synchronization, then run on the same GPU.
1:43Verify correctness and traffic
The fused version passes correctness and uses about 6.3 million sectors. That clears the 8 million target. Now verify the detailed measurements.
1:55Verify the missing scratch traffic
The memory table now has three instructions, instead of five. Coalescing remains 100%, while measured sectors fall by 40%. We removed work without changing the calculation. Each sector is a 32-byte memory chunk. Fewer sector accesses mean less traffic for the memory system to handle. When memory bandwidth is the bottleneck, that can help the kernel finish sooner.
2:21Compare Peak in flight
Peak in flight is now 192 MiB, down from two hundred fifty-six. That's 64 MiB less: one full-size temporary float array.
2:33Compare measured runtimes
Compare both runs to check the timings too. Kernel time decreased, but full harness runtime was slightly longer. Less memory traffic doesn't guarantee the same reduction in total runtime. These measurements come from one pair of runs.
2:50Measure. Change. Verify.
Fusing the loops reduced memory traffic. Removing the allocation reduced memory footprint. Measure both, and verify correctness. Try this lab in GPUFlight Performance Lab.
Measure it yourself
Run the same experiment on a real GPU
Edit the kernel in your browser, run it on managed hardware, and read the profile the video walks through.
Run the fusion warm-up lab