L2 Intermediate · Module 4: CUDA programming fundamentals
4.2Thread, block and grid indexing
Every thread needs to know which piece of data is its own. CUDA arranges threads into blocks and blocks into a grid, in up to three dimensions, and one line of arithmetic turns those coordinates into an array index.
By the end you will be able to
- ✓Compute a thread's global index in 1D and 2D launches
- ✓Size a grid with round-up division and guard the tail
- ✓Write a grid-stride loop that handles any problem size
- ✓Map threads to a row-major matrix so that warps read adjacent memory
A launch creates a grid of blocks, each a group of threads (4.1). Inside the kernel, four built-in variables describe where a thread sits:
| Variable | Meaning | Type |
|---|---|---|
threadIdx | this thread’s position inside its block | uint3 (x, y, z) |
blockIdx | this block’s position inside the grid | uint3 |
blockDim | threads per block in each dimension | dim3 |
gridDim | blocks per grid in each dimension | dim3 |
The 1D formula
Block 0 covers indices , block 1 the next stretch, and so on. To cover elements you need
blocks, which is usually written (n + threads - 1) / threads. The last block is usually only partly
needed, so every kernel guards with if (i < n).
grid: 3 blocks × 8 threads = 24 threads (click one)
block 0
block 1
block 2
array x[0 … 21]
This thread handles x[11].
Grid-stride loops
Instead of launching one thread per element, you can launch a fixed grid (a few blocks per SM) and let each thread walk through the array:
__global__ void scale(float* x, float s, int n) { int stride = gridDim.x * blockDim.x; // total threads in the grid for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < n; i += stride) x[i] *= s;}The same launch works for any , and each thread amortizes its setup over several elements. In the lab, 20,480 threads correctly cover 10 million elements.
Two dimensions
For matrices and images, 2D blocks are more natural. With a row-major matrix (element at r * cols + c):
int col = blockIdx.x * blockDim.x + threadIdx.x; // x -> columnsint row = blockIdx.y * blockDim.y + threadIdx.y; // y -> rowsif (row < rows && col < cols) c[row * cols + col] = a[row * cols + col] + b[row * cols + col];Choosing a block size
- Make it a multiple of 32 (the warp size), or the last warp in each block runs partly empty.
- 128–256 threads is a good default. It gives the scheduler several warps per block without hitting resource limits (2.5).
- The hard limits: 1,024 threads per block; grid up to blocks in x and 65,535 in y and z.
In LLM kernels, the grid usually mirrors the data: one block per token row for RMSNorm, a grid of (heads × batch) blocks for decode attention, a 2D grid of output tiles for a matmul.
Lab: indexing in practice 🔴
cd labs/m04/4.2 && nvcc -O2 -arch=native indexing.cu -o indexing && ./indexing1) i = blockIdx.x * blockDim.x + threadIdx.x (3 blocks x 4 threads) i= 0 <- block 0, thread 0 ... i= 4 <- block 1, thread 0 ... i=11 <- block 2, thread 3
2) grid-stride: 20480 threads covered 10000000 elements: PASS
3) 2D: grid (157, 375) of blocks (32, 8) = 15072000 threads for 15000000 elements: PASSSources
Check your understanding
0 / 4 answered1.blockDim.x = 128. What is the global index of thread 5 in block 3?
2.How many 256-thread blocks do you need for n = 1,000 elements?
3.What is the advantage of a grid-stride loop?
4.For a row-major matrix, which dimension should threadIdx.x map to?