liveBook · Manning

threadIdx.x

As explained in Grokking GPU Programming

threadIdx.x is a CUDA-provided per-thread coordinate identifying a thread's position along the x dimension within its block.

Every CUDA thread runs the same kernel code, but needs to know which piece of data is its own. threadIdx.x is the thread’s built-in x-coordinate: in a one-dimensional block, threads count from 0 upward, so each can use its index to select an array element. section 2.6 section 2.11

One block, six threads

With a one-dimensional launch such as kernel<<<1, 6>>>(), the threads see indices 0 through 5. Each can use its own index for the matching array element. Because there is only one block, threadIdx.x alone is enough. section 2.11 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 one-dimensional block gives each thread its own index, starting at zero. section 2.6

Keep the block boundary in mind:

  • threadIdx.x starts over at zero in each block, so it is not a grid-wide index. Using it alone with multiple blocks makes them address the same elements; combine it with blockIdx.x and blockDim.x for the global index. section 3.13

How Grokking GPU Programming covers it

How deep it goes
Defines and demonstrates single-block indexing, then defers grid-wide indexing to the combination with blockIdx.x and blockDim.x.
Examples
Uses array-element indexing, a conditional print by thread 2, and a six-thread one-dimensional layout.
What it assumes
Explains the built-in thread index from the beginning and uses it within a CUDA kernel launch.
What you can do after
Apply it to assign per-thread work in a single block and recognize when a multi-block index needs a block offset.
How it connects
Connects threadIdx.x to CUDA kernels, thread blocks, blockIdx.x, and blockDim.x.

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.