liveBook · Manning

Warp-uniform branch

As explained in Grokking GPU Programming

A warp-uniform branch is a conditional branch for which all threads in a warp take the same path.

A warp-uniform branch sends every thread in a warp down the same path, avoiding branch divergence within that warp. One way to do this is to branch on blockIdx: threads in a block share its block index, and a warp never spans multiple blocks. section 14.3

Choose a path by block

For example, test whether blockIdx.x is even to choose between two workloads. Every thread in that block makes the same choice. By contrast, testing threadIdx.x < 16 splits a warp’s threads across two paths. section 14.3

Use this strategy when the work can be divided at block level:

  • Branch on blockIdx rather than threadIdx so each warp follows one path. section 14.4
  • For short, cheap branches without memory side effects, predication may be preferable; for randomly intermixed data, consider sorting if its cost is worthwhile. section 14.4
// blockIdx.x is the same for all threads in a warp: no divergence
if (blockIdx.x % 2 == 0)
    heavyWork1();    #A
else
    heavyWork2();    #B

The blockIdx condition is shared by threads in a block, so each warp takes the same branch. section 14.3

How Grokking GPU Programming covers it

How deep it goes
Explains block-index branching with a contrasting thread-index example and places it in a strategy-selection procedure.
Examples
Contrasts a threadIdx threshold that diverges with a blockIdx parity test that keeps warp paths uniform.
What you can do after
Apply it by branching on blockIdx when work can be split at block level.
How it connects
Connects branch divergence with predication and sorting as alternative ways to manage it.

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.