What this video shows
About this walkthrough
A correct CUDA sum is slow because millions of threads update the same output address atomically. The walkthrough replaces the per-element atomic additions with CUB BlockReduce, so each block adds its total once. On the large test, kernel time falls from 7.63 ms to 32.54 microseconds, and SM throughput rises from 0.4% to 54.3%. The unprofiled whole-harness runtime changes much less, from 351 ms to 310 ms.
Read along
Transcript
Select a time to play the video from that chapter.
0:00Learn CUDA Reductions
Learn CUDA reductions with GPUFlight. We'll find why a simple sum is slow, change the code with NVIDIA's CUB library, and inspect the before and after profiles.
0:14The task: many values, one sum
Open the CUB BlockReduce lab in GPUFlight Performance Lab. The task is to add all the numbers in an array and return one sum. Combining many values into one is called a reduction. It appears in statistics, loss calculations, and many other GPU workloads. The largest test contains just over four million values. We must keep the result correct, including the smaller tests whose sizes do not fill every block.
0:45Why the correct starter is slow
Each thread calculates its index, loads one value, and calls atomic add on the same output address. An atomic update prevents simultaneous additions from overwriting each other. It protects the result, but many threads competing for that one address create contention. This is a deliberately simple baseline. The problem is not that atomic operations are always bad. It is how often we update this shared destination.
1:17Run the unchanged baseline
Run the starter on the RTX 3090. Correctness passes, but SM throughput is only 0.4%, below the lab's target. SMs are the GPU's processing units. This throughput metric is a hardware performance indicator, not the percentage of threads doing useful work. To understand the timing, open the baseline's session details and inspect the kernel launches.
1:47Find the matching kernel launch
Expand reduce sum. There are four launches because the harness checks four input sizes. Averaging them would mix different amounts of work. The large test has 16,384 blocks, with 256 threads per block. That launch takes 7.63 ms in this trace. We will compare the candidate's matching launch. The 100% time share means this is the only kernel name here. It does not mean the GPU is fully utilized.
2:21Combine locally, then update globally
Instead of sending every value straight to the global sum, let each block combine its own values first. Then one thread adds the block total to the output. For this large input, the code changes 4,194,304 global atomic updates into 16,384. That is 256 times fewer global updates, calculated from the code and launch size. It is not a measured speedup.
2:54Add CUB and its shared storage
Back in the editor, define BlockReduce for floats and a block size of 256. This number must match the launch configuration below. Next, reserve CUB's temporary storage in shared memory. Each block gets its own scratch space for the cooperative calculation. The lab already supplies the CUB header. In your own CUDA project, include the block reduce header shown here.
3:23Every thread joins the reduction
Now load the input value when the index is valid. Otherwise, contribute zero. Then every thread calls Sum. Zero leaves an addition unchanged. More importantly, an out-of-range thread still reaches the collective operation, which coordinates threads inside the block. Do not put this call inside the old index check or return early. The last, partly filled block must participate correctly too.
3:53One global update per block
CUB returns the block total to thread zero. Only that thread performs the global atomic add. The grid and block dimensions stay the same. We changed how the threads cooperate, rather than asking for more threads. Dynamic shared memory remains zero because the temporary storage we declared is static shared memory. Now run this version and verify the result.
4:21Correctness and the lab target pass
The candidate passes correctness and the lab's throughput target. SM throughput rises to 54.3% on the same GPU. Open its session and expand the launches. The matching large test still uses 16,384 blocks of 256 threads. Its traced duration is now 32.54 µs. The workload and launch dimensions match; the organization of the reduction changed.
4:55Compare the evidence in GPUFlight
Return to the lab and open Compare. GPUFlight puts the measured results and the exact submitted code next to each other. For this pair, the large kernel falls from 7.63 ms to 32.54 µs. That is about 234 times faster for this profiled kernel launch. The code explains the mechanism: combine within the block, then send one update to the contended address. We did not measure a hand-written tree, so we cannot claim equal performance with one.
5:33Kernel speed is not whole-program speed
Notice the separate unprofiled runtime row. The whole harness takes about 351 ms before, and 310 ms after, in this pair of runs. That scope includes much more than the large reduction kernel. The kernel's speedup does not apply to the whole program. These are observed results from one baseline and candidate pair. Repeat timings on your own workload, and keep the measurement scope consistent.
6:04Use the profiler to guide the change
The useful pattern is simple: reduce locally before updating a shared global result. CUB gives us the block primitive, while profiling tells us whether the change helps. Try the lab in GPUFlight, inspect your own kernel, and compare the code with the measured result. The Performance Lab link is in the description. Thanks for watching!
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 CUB BlockReduce lab