What is Warp divergence?

The splitting of threads within one warp onto different paths at a conditional branch. The paths execute one after another, lowering efficiency.

Field: GPU Programming

Japanese name: ワープダイバージェンス

Why it happens

On NVIDIA GPUs, threads are executed in warps of 32. A warp executes one common instruction at a time. Efficiency is therefore highest when all its threads follow the same path. When the outcome of a conditional branch depends on each thread's data, threads in the same warp go different ways. This is warp divergence.

How a diverged warp executes

The warp executes each path that its threads have taken. While one path is being executed, the threads that are not on it are disabled. The computed results remain correct. Suppose the if side and the else side are both 10 instructions long and the 32 threads split 16 and 16. Counted simply, an undiverged warp needs the time of 10 instructions, while the diverged warp needs 10 + 10 = 20. During that time only 16 threads are doing work at any moment.

Scope and the basic countermeasure

Divergence occurs only within a single warp. Different warps taking different paths do not affect one another. Besides if statements, it also arises from loops whose iteration count differs from thread to thread. The basic countermeasure is to make the threads of one warp follow the same path. For example, data or thread indices are arranged so that the condition switches on warp boundaries.

Try it: questions about Warp divergence

Within one warp, a conditional branch sends half the threads to the if path and half to the else path (warp divergence). How is this generally executed?

  1. ABoth paths run simultaneously, so speed is unaffected
  2. BThe else threads are discarded without executing
  3. CThe if and else paths run one after the other, with non-participating threads idle each time, lowering efficiency
Show answer and explanation

Answer: C. The if and else paths run one after the other, with non-participating threads idle each time, lowering efficiency

A warp's 32 threads execute the same instruction, so on divergence the if-path instructions run first with only the if threads active, and then the else path runs with the rest. Results are correct, but half the threads are idle each time, so the cost is the sum of both paths. Arranging data or threads so that branches agree within each warp improves efficiency.

FAQ

Does divergence change the computed result?

No. Each thread ends up with the same result as if it had executed only the instructions on its own path. What drops is execution efficiency.

Does writing an if statement in a kernel make it slow by itself?

Not necessarily. If every thread in a warp goes the same way, the paths do not split. What costs time is a branch whose outcome varies within one warp.

Books on GPU Programming Ad

As an Amazon Associate I earn from qualifying purchases.

Comparisons that include this term

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary