What this video shows
About this walkthrough
Two versions of a kernel perform exactly the same table lookups and produce identical outputs. In the first, every block of threads touches all 128 pages of the table, so the cache close to the block cannot hold what it needs. In the second, each block works on one page, and that page stays close: 9.31 microseconds against 39.42, which is 4.2 times faster. A separate test with 1 to 128 pages per block shows the time rising and then flattening at about 39.5 microseconds. No cache counter was read; the evidence is the timing curve.
Read along
Transcript
Select a time to play the video from that chapter.
0:00The puzzle
Here are two versions of a GPU kernel, a function that runs on the GPU. They do exactly the same lookups. They give exactly the same answers. One takes 39 microseconds. The other, only 9 microseconds. The only thing we changed is the order of the work. So why does order matter?
0:22The job: a table and a list of questions
First, the job. There is a big table, like an encyclopedia with 128 volumes. And there is a list of questions. Each question belongs to one volume: look up 64 numbers in it, and add them up. That's one answer. We have about 33,000 questions.
0:43Where the numbers live
Now, where do those numbers come from? The table lives in the GPU's memory, and memory is far away. So the GPU keeps copies of recently used data closer to the threads. Those copies are the GPU's own caches.
1:00A school, bookshelves and one library
Picture a school. The threads are students, sitting in classrooms. A classroom is one block of threads. Every classroom has a small bookshelf by the door. It is tiny, but it is right there. Down the hall there is a library that the whole school shares. It is much bigger, but it is a walk. Our whole encyclopedia fits in the library. It does not fit on a shelf.
1:26The first order
In the first version, the questions are handed out in the order they are stored: volume 0, volume 1, volume 2, round and round. So one classroom needs all 128 volumes. The shelf cannot hold them. Each new volume pushes another one off. The students keep walking to the library. We time it. 39 microseconds.
1:53The idea
Now look at the questions again. No question needs another question's answer. So we are free to choose who does which. Give each classroom all the questions for one volume. Now that volume stays on the shelf, and everyone in the room shares it.
2:10Three lines change
In the code, three lines change. Before, a thread's number picked its question directly. Now the block picks the volume, and the thread picks the question in that volume. Then we work out where the answer belongs, so the output stays in its original order.
2:27Measured: 4.2x faster
Run it again. 9 microseconds. That's 4.2 times faster. Every question still does the same 64 lookups, and all the answers are identical.
2:40Is it really the shelf?
But is it really the shelf? We tested it. Two volumes per classroom: 17 microseconds. Four volumes: 34 microseconds. Eight volumes: 38 microseconds. And from 16 all the way to 128, it stays flat. Once the volumes no longer fit on the shelf, more of them make no difference. You are at library speed.
3:07Remember this
So remember this. A GPU has small caches close to its threads, and they reward work that stays in a small area. When your work items are independent, you choose the order. Group the ones that read the same data. And measure on your own GPU, because shelves come in different sizes. You can try it yourself. The lab is called Read-Only Cache, in the 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 lookup locality lab