Search

A technology, a device or an article

When GPU threads disagree: the cost of a condition

An imaginary ensemble explains warp divergence, predication and Independent Thread Scheduling, while keeping the limits of the analogy visible.

A clay ensemble with some performers playing and others waiting, representing divergent threads.
A clay ensemble with some performers playing and others waiting, representing divergent threads. Conceptual illustration created with AI.ITNWS · Original ITNWS AI-assisted editorial illustration

On our imaginary stage, the musicians have received one score. Some must play a long phrase; others need a short one. The conductor can direct a movement only to the part of the ensemble that needs it at that moment. Everyone else remains silent without surrendering their place on stage. Nothing is broken. Some performers are simply not participating in a particular shared command. This picture is a useful opening for GPU branching, provided we remember to put the conductor’s baton away before treating the picture as hardware.

A GPU thread is not a person. It has no hearing, no intention to wait politely, and no permission to improvise. The scene illustrates a distinction between the participants present and the participants doing useful work when an instruction is issued. It is not a diagram of an actual chip. We need a more exact description of how threads are grouped, where their decisions differ, and why different inputs can make a group perform more work than any one participant appears to require.

CUDA groups threads into warps. In the NVIDIA model discussed here, a warp contains 32 threads. That is a platform-specific property, not a promise about every GPU. Threads retain their own register values and process different data, while instruction execution is organised in groups. The same program can consequently suit the machine differently depending on how consistently its data leads those participants through conditional work. Source: CUDA programming model.

Imagine a hypothetical pixel operation. Bright pixels require one formula and dark pixels another. When all threads in a warp choose the same path, the group can follow it coherently. The presence of an if statement does not itself establish divergence. When some threads choose one path and others choose the other, both sets of results must be produced for different participants. This disagreement within a group is the relevant divergence. Separate warps may choose different paths without creating that same within-warp conflict.

The silence on stage becomes easier to understand.

While work belonging to one path is performed, the threads that do not need it must not make the corresponding changes to their own data. Activity is accounted for among the appropriate participants. The other path then requires its own work. In a simple teaching example, both paths consume execution effort even though each individual thread needs only one. This explains a possible loss of efficiency. It does not provide a universal timing equation for arbitrary compiled code.

The score changes after rehearsal

Source code is not the final machine instruction sequence. A compiler can implement a short condition with predication or another suitable form. Counting every if as a guaranteed separate traversal of two long branches is therefore wrong. Generated instructions, work length and dependencies matter. A manual attempt to remove a condition can force every thread to calculate both expensive answers before selecting one. The source looks more uniform while the amount of computation grows.

Our stage does not automatically justify that exchange. Suppose half the group needs a cheap expression and the rest needs a costly one. Calculating the costly expression for everyone just to achieve a uniform line of code may consume substantially more resources. For short alternatives, a predicated implementation may be sensible. The appearance of the original source cannot settle the choice. The slogan that branches are bad conceals quite different forms of actual machine activity.

For illustration, assume a small program region contains only equally expensive instructions and is not limited by memory. If 16 of its 32 participants are active when those instructions are issued, half the potential participant positions do no useful work for that region. This is an explanatory model, not a benchmark. It does not imply that the whole application becomes exactly twice as slow. Other work, waits, warps and limiting resources exist outside the selected region and may dominate elapsed time.

Applying that fraction to a user-facing GPU utilisation percentage is even less defensible. Device busy time need not equal the fraction of active threads for one instruction. Hardware may be occupied issuing work that uses its available execution positions unevenly. Low reported utilisation can also arise for reasons unrelated to divergence. A monitoring number measures a particular quantity. It neither reads the score of our imaginary play nor identifies the cause of its pauses by itself.

Change the scene without changing the program. Instead of randomly alternating bright and dark pixels, the inputs arrive in groups of similar values. Some warps may become more coherent. Arranging that order takes work and data movement, however. If sorting costs more than it saves, or damages a favourable memory access pattern, total performance may decline. Grouping becomes useful through a particular workload and its costs, not through a universal aesthetic preference for similar neighbours.

Meaning imposes another constraint. Elements cannot always be rearranged freely: their order may matter to a later operation, the image layout or the final output. The program may need to preserve indices, scatter results back, or change the surrounding algorithm. Those operations cost something. A practical explanation includes the entire transformation instead of treating a locally cleaner warp as a free improvement to an application. The useful unit of comparison is the same completed task.

Embroidered paths separate and meet again as a metaphor for reconvergence.
Embroidered paths separate and meet again as a metaphor for reconvergence. Conceptual illustration created with AI. — ITNWS · Original ITNWS AI-assisted editorial illustration

Things happen during the pause

The group under discussion exists among other groups. When one warp waits for data or cannot advance, scheduling can select another ready warp. This can hide some latency. It does not rewrite the useful activity inside the execution that has already diverged. Idle participant positions for one instruction do not become unrestricted seats for arbitrary unrelated threads. The ensemble metaphor is especially limited here: seating any musician in an empty chair does not describe how a real execution resource is used.

Modern architecture also complicates the schoolbook claim that all threads always march in lockstep. NVIDIA describes Independent Thread Scheduling, introduced with Volta, which permits more flexible progress through per-thread control state. SIMT and the value of coherent execution nevertheless remain. Independent does not mean that every thread now owns a separate full execution engine for any instruction, or that divergence has become free. Scheduling flexibility and available computational throughput describe different properties of the machine.

This change matters to correctness as well. Code silently relying on older implicit assumptions about neighbouring threads progressing together may be wrong. Threads exchanging values need suitable synchronisation and a correctly defined set of participating threads. Performance discussion begins after the result is established as correct. A fast variant that occasionally gets the expected values because the schedule happened to cooperate is not an optimisation of a working algorithm. Relevant details belong in the current CUDA guide.

The meeting of paths is often called reconvergence. It does not mean every thread necessarily performed identical work before meeting. It describes the possibility of subsequent coherent progress under the execution model and control implementation. Nested branches and loops complicate matters. One fork drawn with arrows cannot predict every joining point or every decision a modern compiler makes. The illustration remains useful only while we avoid asking it to serve as an exact execution trace.

Different loop lengths are particularly revealing. Each thread might process one object, with some objects needing a few iterations and others many. Participants then finish at different times even when the formula is identical. A shrinking active subset may keep execution going after the rest have done their required work. This is related to divergence: the data determines unequal amounts of work inside a group. Running one program does not guarantee that its branches and loops have identical durations for every input.

Sometimes smaller work units and redistribution reduce that imbalance. Additional queues, atomic operations and control also consume resources. For short jobs the overhead may exceed the saving; for long, highly uneven jobs it may be worthwhile. A serious analysis therefore considers the distribution of work lengths and the price of organising them. Removing one word from the source does not necessarily change the distribution itself. The question concerns actual work, not the moral character of conditions.

Memory deserves a qualification even though it is not our main subject. Coherent control flow does not guarantee favourable access addresses. Every thread can follow the same path while reading scattered locations. Conversely, a condition does not itself prove that neighbouring accesses combine poorly. Control coherence and memory organisation interact through the algorithm but describe different sources of cost. Improving one property should be evaluated with its consequences for the other, using an unchanged comparison task.

Draw a final episode of the imaginary rehearsal. The ensemble no longer divides neatly in half. One performer has a very long part, several have medium parts and most have finished. The average number of notes per musician says little about when the whole scene can end. A similar concern arises when a group contains a long tail of activity. Yet musicians still cannot be equated mechanically with hardware threads: scheduling, other groups and operation types determine the actual outcome. The image supplies a question for measurement rather than a measured answer.

What observation would help? Profiling information with a defined scope: a particular kernel, participant activity, branch behaviour, memory limits, duration and launch count. Those observations must be related to source and machine code, followed by a comparison of changed versions completing the same task. We conducted no such experiment for this article. The pixels and performers are explicitly hypothetical, not camouflage for an editorial benchmark that never happened.

Our stage therefore ends without a rule to remove every branch. A condition may avoid far more work than managing it costs. The inputs may make its choice coherent. The compiler may implement it differently. Independent scheduling may alter valid assumptions about progress while leaving resources finite. The useful question is specific: what work does each participant require, and what does organising their shared execution cost in this particular program?

The musicians open the same score again. Some pause, some play, and then their roles change. The mistake would be assuming that everyone present must contribute useful work to every movement of the baton. A GPU has no clay conductor. The difference between presence and participation is nonetheless real.

Sources & documentation

  1. CUDA programming model ↗
  2. CUDA Programming Guide ↗