liveBook · Manning

Thread layout

As explained in Grokking GPU Programming

A thread layout is the arrangement of threads in a CUDA block along one, two, or three axes.

A thread layout arranges threads in a CUDA block as a line (1D), rectangle (2D), or box (3D). Choose the shape when launching a kernel: a flat array fits a 1D layout, while an image or matrix can fit a 2D layout. Each thread uses its built-in threadIdx components to identify its position. In 1D, threadIdx.x starts at 0 and gives each thread an index within its block. With multiple blocks, that index repeats; combine it with blockIdx.x to distinguish threads across blocks. section 2.6

Figure 2.2 1D thread layout: one index per thread. In a 1D block, each thread gets a unique threadIdx.x starting from 0; that's all it needs to know which element to work on. (These numbers are unique within a block; with multiple blocks, each block counts from 0 again.) The six threads shown here are what a one-dimensional launch like kernel<<<1, 6>>>() creates: the launch is what brings the threads into being, and each one then reads its own threadIdx.x.

See how a six-thread, one-dimensional launch gives each thread its own threadIdx.x value. section 2.6

How Grokking GPU Programming covers it

How deep it goes
Defines 1D, 2D, and 3D arrangements and explains indexing, while deferring multi-block indexing to a later discussion.
Examples
Uses a flat array, an image or matrix, a grading analogy, and a six-thread 1D launch.
What you can do after
Apply it via a CUDA kernel launch by choosing a thread arrangement and using thread indices to assign work.
How it connects
Connects threadIdx to CUDA kernel launches and blockIdx.

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.