← Course contents · Part 1

· Visual lesson · Session 6 of 56


How does a GPU organize work?

A kernel is the function you ask the GPU to run. A thread is one execution of that function. Blocks group threads, and a grid contains the blocks in one launch.

See the connection

One launch. Eight blocks. Many threads.

Follow one worker through the example: block 2, thread 5.

GridOne kernel launch · 8 blocks
Block 0128 threads
Block 1128 threads
Block 2128 threadsZoom in below ↓
Block 3128 threads
Block 4128 threads
Block 5128 threads
Block 6128 threads
Block 7128 threads
Inside block 2Local thread numbers 0–127
T0T1T2T3T4T5…T127

Thread 5 → item 261
2 × 128 + 5 = 261

8 × 128 = 1,024 launched threads for 1,000 items. The last 24 skip the addition after the bounds check. Numbering starts at zero.

Next: Session 7 opens the same block to show its four warps.

Give each thread an item

Suppose you want to add 1,000 pairs of numbers. Choose 128 threads per block for this example.

Eight blocks launch 1,024 threads. Each thread calculates its item number, then checks that the item exists:

item = block_number * 128 + thread_number
if item < 1000:
    output[item] = input_a[item] + input_b[item]

This is pseudocode. The last 24 threads skip the addition.

Block 2, thread 5 handles item 261: 2 × 128 + 5. Numbering starts at zero.

Work is not hardware

A thread is not a reserved physical core. The GPU schedules these threads on its available hardware. Blocks should not assume they finish in numerical order.

Try it yourself

How many 128-thread blocks cover 2,050 items?

Check your answer

Seventeen. They launch 2,176 threads, so 126 skip the work after the bounds check. Changing the grouping does not change which additions are required.

Sources and further reading

← All sessions