Adding 256 Numbers in Eight Stages, with Everyone Waiting at the End of Each
Last timeHow Many Fit at Once
The workers of one block share a small fast store and can wait for each other. Together those make a sum in stages, and the number of stages is the number of halvings.
The loop that failed the second question
The first lesson left a loop on the list of failures: adding every element into one total. Every trip writes the same cell. This lesson does that sum on the device anyway, by rearranging it so that no two workers ever write one cell.
Two tools are needed, and both belong to the block.
The first is storage. Each block is given a piece of the fast storage on its core that all of its workers can read and write, and that no other block can see. The vendor calls it shared memory, and it is the shared storage counted in the last lesson. NVIDIA's programming guide, in its 2026 edition, notes that because it sits on the core it answers faster than the main memory of the device.
The second is a way to wait. The 8 groups of a block of 256 take turns in no promised order, so a worker has no idea whether its neighbour has run yet. A wait-for-all point fixes that. The guide describes the call, which the vendor spells syncthreads with two underscores in front: it blocks every worker of the block until all of them have reached it.
Adding in stages
Take 8 workers and 8 values: 3, 1, 4, 1, 5, 9, 2, 6. Each worker first copies its own value into its own cell of the shared store. Then, in each stage, the workers in the lower half each add in one cell from the upper half.
| step | stage | gap | workers that add | cells 0 to 3 | what happened |
|---|---|---|---|---|---|
| 1 | 0 | none | none | 3, 1, 4, 1 | Every worker has copied its value in. Cells 4 to 7 hold 5, 9, 2, 6. All eight wait here until the last copy is done. |
| 2 | 1 | 4 | 0, 1, 2, 3 | 8, 10, 6, 7 | Worker 0 adds cell 4 into cell 0, worker 1 adds cell 5 into cell 1, and so on. Four additions at once, each writing a different cell. |
| 3 | 2 | 2 | 0, 1 | 14, 17, 6, 7 | Worker 0 adds cell 2 into cell 0 and worker 1 adds cell 3 into cell 1. Cells 2 and 3 are now spent. |
| 4 | 3 | 1 | 0 | 31, 17, 6, 7 | Worker 0 adds cell 1 into cell 0. The total of all eight values, 31, is in cell 0. |
Check the stage against the two questions of the first lesson. Does any worker read a cell that another writes in the same stage? No: the readers reach across the gap into cells nobody is writing. Do two workers write one cell? No: each writes its own. So within a stage the order is free.
Between stages it is not. Stage 2 reads cell 2, which stage 1 wrote. Worker 0 must not begin stage 2 until worker 2 has finished stage 1, and that boundary is exactly where the wait-for-all point goes.
The lesson stops here
3 more paragraphs to go
You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.
The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.
See the planThe contentsThis is the reading half
Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.
The contents