Two Workers Added One to the Same Cell and It Went Up by One
Last timeSharing Within a Block
Adding to a cell is three steps, and two workers can interleave them and lose an update. An update that cannot be interrupted repairs it, and makes the workers queue.
One line, three steps
The last loop on the list of failures counts values into bins. Each element holds a small number, say 0 to 15, and the loop adds one to the bin with that number. Give each element a worker and two workers whose elements hold the same number will add one to the same bin. No rearrangement avoids it, because which bin a worker needs is known only when it reads its element.
The trouble is that adding one to a cell is not one action. A worker reads the cell into its private storage, adds one there, and stores the result back. Those are three steps, and other workers are running between them.
| step | step | worker A | worker B | the bin | what happened |
|---|---|---|---|---|---|
| 1 | 1 | reads 5 | idle | 5 | Worker A copies the bin into its private storage. |
| 2 | 2 | holds 5 | reads 5 | 5 | Worker B reads before A has stored anything, so it sees the same 5. |
| 3 | 3 | adds, holds 6 | adds, holds 6 | 5 | Each adds one to its own private copy. The bin itself has not changed. |
| 4 | 4 | stores 6 | holds 6 | 6 | Worker A writes its result. The bin is correct for one update. |
| 5 | 5 | done | stores 6 | 6 | Worker B writes 6 over 6. Two updates were made and the bin went up by one. |
This is the second question of the first lesson, failed. NVIDIA's programming guide, in its 2026 edition, describes exactly this outcome for a sum made with an ordinary add: the final value is less than it should be, and it may change from run to run. For ordinary writes by several workers to one location the guide promises only that one of them will be the last.
The update that cannot be interrupted
The repair is to make the three steps one. The device provides an update that reads the old value, adds to it and stores the result as a single transaction on that cell. The vendor's word is atomic, and the guide names the call atomicAdd. While one worker's update is in progress, no other worker can get at the cell.
The lesson stops here
5 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