liveBook · Manning

Predication

As explained in Grokking GPU Programming

Predication is a technique for avoiding divergent control flow by conditionally executing instructions or computing alternatives and selecting a result.

When threads in a warp take different branches, the GPU may run each path in turn. For a short, cheap branch, predication can avoid that split: threads compute alternatives and select the result matching their own condition. This does extra arithmetic, but avoids the control-flow work of masking threads and switching paths. Compilers often make this change automatically. section 14.3

// Branchless: compute both, select
int sum = a + b;    #A
int diff = a - b;
result = condition ? sum : diff;    #B

This branchless version computes both arithmetic results, then selects one for each thread. It is suitable when both alternatives are short, cheap, and have no memory-writing side effects. section 14.3

Predication is not always faster: computing both alternatives wastes work, and a branch that touches memory or performs expensive operations may not be predicated. For those cases, the compiler may keep the branch and divergence remains. Consider predication when the branch body is short, cheap, and side-effect-free. section 14.3 section 14.4

How Grokking GPU Programming covers it

How deep it goes
Explains selects, extra computation, and control-flow overhead while leaving compiler decisions dependent on cost and side effects.
Examples
Demonstrates replacing an if-else that adds or subtracts with computing both results and selecting one.
What you can do after
Apply predication by favoring short, cheap, side-effect-free branches and letting the compiler often convert them to selects.
How it connects
Connects predication to warp divergence, select instructions, warp-uniform branching, and sorting.

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.