Skip to main content

GPU concepts explained

Prefix Sum on a GPU: How Each Thread Finds Its Slot

Published 3 min 50 sMeasured on NVIDIA RTX 3090

What this video shows

About this walkthrough

To keep only the slow events from a list, in their original order, each thread must know how many kept items come before it. Counting that for every thread at once is a prefix sum: mark each event 1 or 0, then add the value 1, 2, then 4 seats to the left. The video shows the barrier bug that makes the scan double-count and reads the real CUDA C++ loop. With 256 events, the one-thread starter kernel takes 21.47 microseconds and the scan 2.40.

Read along

Transcript

Select a time to play the video from that chapter.

0:00The puzzle

Here is a puzzle. Eight students stand in a row. Some of them hold a card that says keep. On my signal, the keepers must form a new row, in the same order, with no gaps. And they all have to move at the same time. How does each one know where to stand?

0:17The real job

This is a real GPU problem. Each student is a GPU thread, and each card is one kernel event: how long a GPU task took. We keep only the slow ones, 4,000 nanoseconds or more, in their original order. Here, four of the eight are slow.

0:37The safe way: one thread

The lab's starter does it the safe way. One thread walks the list and copies each slow event to the next free slot. It's correct. But the real list has 256 events, and the GPU launches 256 threads for it: one thread for each event. In the starter, one of them does all the work. The other 255 do nothing.

1:02Where do I go?

So why not let every thread write its own event? Because it doesn't know where. Look at seat six. Three keepers are ahead of it. They take slots zero, one and two. So seat six takes slot three. Its slot is just a count: how many keepers are ahead of it.

1:21Turn it into counting

So turn the question into counting. Give each event a one if we keep it, and a zero if we drop it. Now my slot is the number of ones to my left. That running count has a name: a prefix sum.

1:35The doubling trick

But counting everyone to your left sounds like walking the line again. Here's the trick. Round one: everyone adds the card of the neighbor one seat to the left. Now each card counts two seats. Round two: add the card two seats to the left. Now each card counts four seats. Round three: four seats to the left. Now each card counts every seat up to its own. Each round doubles the reach. Eight students need three rounds. 256 threads need only eight.

2:10The trap: read before anyone writes

There is one trap. In each round, everyone must read the old number before anyone writes a new one. If one student writes early, a neighbor reads the new number and counts some people twice. So every round is: read, wait, write, wait. In CUDA, that wait is sync threads.

2:30Everyone moves at once

Each card now counts the ones up to its own seat. So the ones to my left are on my left neighbor's card. That number is my slot. The keepers write there, all at once. No locks, no atomics, and the order is exactly the timeline order.

2:48The code

In code, it's one short loop. The distance doubles each time. Read, barrier. Add, barrier.

2:57Measured on an RTX 3090

On an RTX 3090, with 256 events, the one-thread starter took 21.47 microseconds. The scan took 2.4 microseconds. About nine times faster in these captured runs.

3:13Two honest notes

Two honest notes. The scan does more additions in total than the simple loop. It wins because they happen in eight rounds instead of 256 steps. And this version fits one block. Bigger lists are scanned in pieces, and then the pieces are scanned.

3:31Remember this

So remember this. When every thread needs to know how many came before it, think prefix sum. It's behind parallel filtering, sorting, and more. Try this lab in GPUFlight Performance Lab. Class dismissed.

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 ordered compaction lab