Basis Lab

How a Program Runs on a GPU

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.

FIG 1Two workers add one to a bin that holds 5
stepstepworker Aworker Bthe binwhat happened
11reads 5idle5Worker A copies the bin into its private storage.
22holds 5reads 55Worker B reads before A has stored anything, so it sees the same 5.
33adds, holds 6adds, holds 65Each adds one to its own private copy. The bin itself has not changed.
44stores 6holds 66Worker A writes its result. The bin is correct for one update.
55donestores 66Worker B writes 6 over 6. Two updates were made and the bin went up by one.
5 steps
Nothing failed and nothing was reported. Had worker B read after step 4 it would have read 6 and stored 7. The answer depends on timing, which is why the count comes out differently from one run to the next.

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 contents

This 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

The rest of this course

  1. 01The Loop Had an Order and the Sum Never Needed It
  2. 02Nobody Hands a Worker Its Index, So It Works It Out from Where It Sitsopening only
  3. 03One Worker Disagrees and Thirty-One Others Stand and Waitopening only
  4. 04A Group That Is Waiting Does Not Need the Arithmetic Units, So Another Takes Themopening only
  5. 05The Function Asked for a Little More Storage and Half Its Groups Had to Leaveopening only
  6. 06Adding 256 Numbers in Eight Stages, with Everyone Waiting at the End of Eachopening only
  7. 07Two Workers Added One to the Same Cell and It Went Up by Oneyou are here
  8. 08The Timer Said the Function Took No Time, Because It Had Not Run Yetopening only

Read alongside