liveBook · Manning

Two-dimensional thread layout

As explained in Grokking GPU Programming

A 2D thread layout arranges the threads of a CUDA block along two dimensions, giving each thread two coordinates.

A 2D thread layout gives each thread two coordinates: x for the column and y for the row. That matches data such as images and matrices more naturally than a single index. A CUDA block gets this shape from a dim3 launch configuration. section 2.7 section 9.3

For a row-major array, convert coordinates to a memory position with row * width + column. When launching a grid, calculate its size for both dimensions; threads beyond the data edges must check their coordinates before accessing memory. section 9.3

Blurring an image

A 16×16 block assigns one thread to each pixel in its tile. Each thread can average the valid pixels in a surrounding 3×3 neighborhood, checking boundaries so edge pixels do not read outside the image. section 9.3

Figure 2.3 2D thread layout: two coordinates, one thread. In a 2D block, each thread is identified by a (threadIdx.y, threadIdx.x) pair (row, column), making it a natural fit for working with grids, images, or matrices. A block like this comes from a 2D launch, where you pass the block shape as a dim3 instead of the single number a 1D launch uses: here dim3 block(5, 4); kernel<<<1, block>>>(); for 5 columns (x) and 4 rows (y).

See how a 2D block identifies each thread by its row and column coordinates. section 2.7

How Grokking GPU Programming covers it

How deep it goes
Explains coordinates, dim3 launches, linear indexing, and boundary checks through an image-blur example.
Examples
Uses a 16×16 block to blur an image by averaging each pixel's valid 3×3 neighborhood.
What it assumes
Builds on one-dimensional CUDA launches and introduces the second thread coordinate for 2D data.
What you can do after
Apply CUDA 2D launches and coordinate-to-memory indexing to image-sized data.
How it connects
Connects CUDA blocks and thread indexing with data parallelism, row-major arrays, and stencil computations.

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.