What this video shows
About this walkthrough
Counting is slow when millions of events all update the same five counters with atomicAdd. This video explains the contention, counts into private counters in shared memory, and merges each block's totals at the end. The large, evenly distributed kernel goes from about 1.36 ms to 104 microseconds, then to 32.26 microseconds with a larger grid. The full test program stays around 430 ms.
Read along
Transcript
Select a time to play the video from that chapter.
0:00Why is counting slow on a GPU?
Counting is the simplest job there is. So why can a GPU be slow at it? Let's find out with a real GPUFlight lab, and fix it.
0:10What is a histogram?
Here's the job. We have a list of kernel durations: how long each GPU task took. We sort each one into five time ranges and count how many land in each. That table of counts is a histogram. The lab does this for about 4.2 million durations, and every count must match exactly.
0:33How the starter counts
The starter hands the list to thousands of GPU threads. Each one reads a duration, picks its range, and adds one to that range's counter. It uses atomicAdd, so no count is lost when two threads hit the same counter.
0:49Why threads compete for counters
Here's the catch. There are only five counters for all 4.2 million adds, and adds to the same counter happen one at a time. So threads wait in line. When most durations fall into one range, almost everyone waits in the same line.
1:07Baseline measurements
On an RTX 3090, the starter kernel took 1.36 milliseconds with evenly spread durations. With 90 percent in one range, it took 2.55 milliseconds. Same amount of work, nearly twice the time.
1:24Count locally, then merge
Think of an election. If every voter phoned headquarters, the lines would jam. So each polling station counts its own ballots and calls in just the totals. On a GPU, the polling station is a thread block, a team of 256 threads. Its tally sheet is five counters in shared memory: small, fast memory on the chip.
1:49Three code changes
In code, that's three steps. One: zero the five counters, and wait at a barrier until they're ready. Two: the same loop, but add to the block's own counters. Three: wait until the whole block is done, then add its five totals to the global result. The lines still exist, but they're short and close by.
2:11First optimization result
The counts still match, and the kernel drops from 1.36 milliseconds to 104 microseconds, roughly 13 times faster. It barely changes when the data is skewed. But the lab has a second target: SM throughput, how close the GPU's processors get to their peak rate. It needs 10 percent. We're at 5.8 percent.
2:36Give the GPU more blocks
The RTX 3090 has 82 of those processors, called SMs. With only 64 blocks, at least 18 SMs get no work at all. Keep 256 threads per block, but launch 256 blocks, and every SM gets about three.
2:55Final result
Run it again: correct counts, SM throughput 20.1 percent, and the lab is solved. The counting kernel went from 1.36 milliseconds to 32 microseconds, about 42 times faster in these captured runs.
3:12What to remember
So: when many threads add to the same few counters, they wait in line. Count locally in shared memory, merge once per block, and give every SM work. Try this histogram lab in GPUFlight Performance Lab. 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 histogram lab