6.2.6 · D3GPU Architecture

Worked examples — Thread blocks and grids

3,561 words16 min readBack to topic

This page is the drill hall for the parent topic. We will hunt down every kind of situation the block/grid machinery can throw at you — clean divisions, ragged remainders, empty inputs, 2D tiling, occupancy walls — and work each one from zero. If any word below feels new, the parent note built it first; here we use it until it is muscle memory.

Before anything else, one reminder of the single formula everything rests on:

Figure — Thread blocks and grids

Look at figure s01. Each row is one block; the row labels on the left ("block 0"…"block 3") name them. The red seat (labelled globalID = 15) is one specific thread. To find its global number we follow the red vertical arrow — jumping over the two full rows above it (that is ) — and then follow the red horizontal arrow stepping across to its column (). That single act of jumping-then-stepping is the entire chapter.


The scenario matrix

Every problem you will ever meet lives in one of these cells. The worked examples afterwards each carry a tag like (Cell A2) so you can see the whole grid gets covered.

# Case class What makes it tricky Covered by
A1 Exact fit divisible by block size No leftover threads; guard never fires Example 1
A2 Ragged fit not divisible Last block has idle threads; need ceiling + guard Example 2
A3 Degenerate: Empty input Example 3
A4 Degenerate: Single element, still one whole block Example 3
A5 one block Grid rounds up to exactly 1 block Example 3
B1 2D tiling — exact Row/col from x,y; flatten row-major Example 4
B2 2D tiling — ragged in ONE dim Guard fires on only one axis (exact x, ragged y) Example 5
B3 2D tiling — ragged in BOTH dims Guard fires on row and col at the corner Example 6
C1 Limiting behaviour — block size max (1024) Fewer, fatter blocks; occupancy risk Example 7
C2 Occupancy wall — shared memory limits blocks/SM Memory, not threads, is the bottleneck Example 8
D1 Word problem — real workload sizing Translate "process 2M pixels" into a launch Example 9
D2 Exam twist — reverse the formula Given globalID, recover block & thread Example 10

Worked Examples


Recall

Recall Why ceiling division and never plain division?

Plain integer division rounds down, dropping a partial last block and leaving tail elements unprocessed ::: ceiling rounds up so every element is covered.

Recall What exactly does the "guard" protect against?

The if (i < N) / if (row < M && col < N) check stops the extra threads in the last (padded) block from reading or writing memory that does not exist ::: they arise because the grid always rounds up to whole blocks.

Recall In a ragged launch, where do the idle threads live?

Always in the last block (1D) or the padded edge/corner blocks (2D); their global indices exceed the data size, so the guard fires ::: none appear in fully-interior blocks.

Recall One-dim vs. two-dim ragged: how many guard sub-conditions fire?

If only one axis is ragged, only that axis's sub-condition (row < M or col < N) fires ::: if both axes are ragged, the corner block trips both sub-conditions at once.

Recall When is occupancy limited by memory instead of threads?

When (a floor) is smaller than ::: the hardware takes the minimum of all floored resource limits.

Recall How do you invert globalID back to (block, thread)?

and ::: because globalID with .

Related deep threads: Warp Execution, Shared Memory, Thread Synchronization, Streaming Multiprocessors, Memory Coalescing, Parallel Algorithm Design, CUDA Programming Model.