Basis Lab

How a Program Runs on a GPU

Nobody Hands a Worker Its Index, So It Works It Out from Where It Sits

Last timeOne Function, Run a Million Times

Workers come in equal blocks, and blocks come in a grid. A worker finds its element from two positions, and one integer division picks a block count that covers any length.

Where the index comes from

The last lesson ended with a function of an index and left one line unexplained. In a loop the index is handed over by the loop. Here there is no loop, and a million workers all start the same function with the same arguments. Each has to find out which element is its own.

The device tells a worker only where it sits. Workers are launched in equal blocks, and the blocks are laid out in a row called the grid. A worker can ask three things: its position inside its block, its block's position in the grid, and the size of a block. From those it builds the index itself.

FIG 1A worker's element, from where it sits
the index of the element this worker handles
the position of the worker's block in the grid, counting from zero
the number of workers in every block
the worker's position inside its own block, counting from zero
In the vendor's dialect these three are blockIdx.x, blockDim.x and threadIdx.x, and this is the second line of the function in the previous lesson. With blocks of 256, the worker at position 17 of block 3 computes 3 times 256 plus 17, which is 785.

The multiplication skips the elements that belong to earlier blocks. Three blocks come before block 3, each covering 256 elements, so block 3 starts at element 768. The worker then counts 17 further along.

A small case makes the pattern visible. Take blocks of only 4 workers and an array of 14 elements.

FIG 2The index each worker computes, blocks of four
position 0position 1position 2position 3
block 00123
block 14567
block 2891011
block 312131415
Each row is one block and each cell is the index that worker computes. Every index from 0 to 13 appears exactly once, which is what makes it safe for each worker to write its own cell. The two marked workers compute 14 and 15, and an array of 14 elements has no such cells.

The positions can also be given in two or three dimensions, which suits images and volumes. The vendor's guide says this is for convenience only, and the arithmetic is the same idea with more terms. This course stays with one.

The lesson stops here

4 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 Sitsyou are here
  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 Oneopening only
  8. 08The Timer Said the Function Took No Time, Because It Had Not Run Yetopening only

Read alongside