liveBook · Manning

Global index

As explained in Grokking GPU Programming

A global index identifies a thread's corresponding element across all blocks in a CUDA grid.

When an array needs more threads than one CUDA block can hold, each thread needs to know which element to handle. A global index gives the thread a unique position across the whole grid, rather than just within its own block. section 3.0 section 3.4

For a one-dimensional grid, compute `idx = blockIdx.x * blockDim.x + threadIdx.x`. Here, `blockIdx.x` identifies the block, `blockDim.x` is the number of threads per block, and `threadIdx.x` is the thread’s position within that block. The multiplication finds the block’s starting position; the addition finds the thread’s position from there. section 3.4

Three blocks of four threads

With three blocks of four threads, block 0 covers indices 0–3, block 1 covers 4–7, and block 2 covers 8–11. Block 1, thread 2 gets `1 × 4 + 2 = 6`; block 2, thread 0 gets `2 × 4 + 0 = 8`. section 3.6

Figure 3.2 Global indexing: no two threads share the same index. By combining blockIdx.x, blockDim.x, and threadIdx.x, each thread gets a unique global index: Block 0 owns 0–3, Block 1 owns 4–7, and Block 2 owns 8–11.

See how the block offset gives each thread a different position across the grid. section 3.5

Using only `threadIdx.x` is a common mistake: it starts again at zero in every block. With three blocks of four, all three target elements 0–3 and leave elements 4–11 untouched. Overlapping writes can also race, so updates may be lost. Include the block offset to give each thread its own element. section 3.3 section 3.13

For an array of `n` elements, choose a block size, use ceiling division `(n + threadsPerBlock - 1) / threadsPerBlock` to count blocks, and compute the global index inside the kernel. Guard the work with `if (idx < n)`: the last block may contain extra threads that must not access the array. section 3.11 section 3.16

This is a one-thread-per-element mapping for 1D arrays. The same group-size-plus-position idea also appears in row-major flattening, where `row × width + col` locates an element in a flat array. section 3.4 section 3.5

How Grokking GPU Programming covers it

How deep it goes
Works through the 1D formula and bounds checks, while deferring multidimensional grids and mappings where one thread handles several elements.
Examples
Traces three blocks of four threads and uses an array-doubling kernel to demonstrate indexing and bounds checks.
What it assumes
Assumes familiarity with launching a kernel and using a thread’s local index, then teaches indexing across multiple blocks.
What you can do after
Apply it via CUDA to assign array elements to threads and guard extra threads against out-of-bounds access.
How it connects
Connects thread indexing with grids, blocks, race conditions, row-major flattening, and the map pattern.

Also explained in

Compare how 2 books explain global index →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking GPU Programming. The section links point to the parts of the book it draws on.