CUDA Warp Divergence: Find Performance Losses and Verify Improvements
CUDA warp divergence occurs when threads in the same warp choose different paths at a conditional. The GPU executes the paths taken by that warp while disabling lanes that do not take the path being executed, so longer paths with mixed lane choices can leave more lanes doing no useful work. That does not mean every conditional should be removed: first determine whether the kernel is a real hotspot, what code the compiler generated, and whether data-reordering costs outweigh the change.
CUDA Warp Divergence: Find Performance Losses and Verify Improvements
CUDA warp divergence occurs when threads in the same warp choose different paths at a conditional. The GPU executes the paths taken by that warp while disabling lanes that do not take the path being executed, so longer paths with mixed lane choices can leave more lanes doing no useful work. That does not mean every conditional should be removed: first determine whether the kernel is a real hotspot, what code the compiler generated, and whether data-reordering costs outweigh the change.
This article follows NVIDIA CUDA and Nsight Compute documentation checked on 2026-11-22. A warp contains 32 threads, but details of branch handling and performance metrics can depend on GPU architecture, compiled code, and Nsight Compute version. Cross-vendor execution groups are outside this article’s scope.
Branch cost depends on the condition distribution within a warp
When all threads in a warp choose the same branch target, that warp generally executes only that path. If the condition splits a warp, each path that is taken is executed, while threads that did not select it are inactive for that path’s instructions. The CUDA guide describes divergent paths within a warp as executing the paths taken with off-path threads disabled. Different warps can execute different code independently, so all warps in a block do not have to choose the same path. CUDA C++ Programming Guide — SIMT Architecture
The diagram below simplifies the case where half the 32 lanes choose each path. It is a conceptual view, not a trace of the actual instruction schedule or subwarp scheduling.
One warp: lanes 0–31
if path A: lanes 0–15 active | lanes 16–31 inactive
else path B: lanes 0–15 inactive | lanes 16–31 active
If both condition groups exist: process both paths A and B
If the whole warp has one condition: process only its selected pathThe key point is that when both branch paths are needed within one warp, each path is processed while lanes that chose the other path do not execute its instructions. If a branch condition depends on a per-thread value such as threadIdx.x, lanes in a warp may split. For example, assume a one-dimensional block and a condition that sends half the lanes in each warp to A and the rest to B; if both paths perform substantial computation, some lanes produce no result during each path. By contrast, if the condition depends on a value that is constant per warp, such as threadIdx.x / warpSize, the lanes within a warp see the same condition and do not diverge at that branch. NVIDIA’s Best Practices Guide explains that aligning such conditions with warp boundaries can reduce the number of divergent warps. CUDA C++ Best Practices Guide — Control Flow Instructions
| Condition within a warp | Expected path behavior | What to keep in mind |
|---|---|---|
| All lanes choose the same target | The warp executes its selected path | Another warp choosing a different path does not by itself make this warp divergent |
| Different lane groups choose different targets, and both paths are present | Each selected path is processed with other lanes inactive for it | Both the path instruction count and the number of useful lanes matter |
| The conditional is short and the compiler uses predication | Predicated instructions may be scheduled in place of a branch | The absence of a branch instruction does not mean no instructions were scheduled |
Why removing a branch is not always faster
The compiler may replace a short if or switch with predication instead of an actual branch. In this case, instructions controlled by the condition are scheduled, but threads with a false predicate do not write results, evaluate addresses, or read operands. Rewriting an if as a conditional-selection expression, or making both calculations run, therefore does not automatically reduce cost. If both paths are expensive, scheduling work for the unneeded path can make the change slower. The compiler’s predication decision is not a fixed source-level rule, so do not infer the generated code from the source syntax alone. CUDA C++ Best Practices Guide — Branch Predication
For example, suppose a particle simulation uses different physics calculations for different particle types. If particles of the same type are grouped within warps, lanes in one warp may follow the same path. If types are randomly mixed, divergence may increase, but sorting or partitioning particles before the kernel adds work. If several later kernels reuse the grouped array, that cost may be amortized; if the workload runs one short kernel once, rearrangement may cost more than the branch. This is an illustrative decision scenario, not a measured result for a particular application.
There is no single optimization that fits every case. Consider whether conditions can be aligned to warp membership, whether a short branch can be handled efficiently by predication, whether grouped data will be reused, and whether splitting paths adds kernel launches or memory traffic. To see whether the compiler chose branching or predication, inspect source-level execution and predication information in Nsight Compute and, when needed, the generated device code.
Confirm divergence as a bottleneck with Nsight Compute
Nsight Compute’s SourceCounters section includes branch efficiency and sampled warp-stall information. The current Compute Triage Guide recommends checking thread-inst-executed together with predicated-on ratios; it says that Avg. Predicated-On Threads Executed well below 32 can indicate divergence or predication losses. This value alone does not establish that divergence is the main cause of total execution time. Compare the hot source line with the workload-wide average, the kernel’s throughput, and the actual input distribution; also check whether memory waiting or a saturated resource is a more important cause. Nsight Compute Compute Triage Guide — Divergence or Predication Losses Nsight Compute Profiling Guide — Source Counters
Prerequisites are a CUDA application to profile, a compatible NVIDIA GPU and driver, and the Nsight Compute CLI. In a Linux shell, the basic form for collecting source counters from a kernel is below. Replace the executable and arguments with those for your application, and first check that the section is available in your installed version. Nsight Compute CLI User Guide
ncu --list-sections
ncu --section SourceCounters ./appUse the following sequence to test one change at a time and distinguish a better divergence metric from a faster application.
**Record a real hotspot and a baseline.** Note the GPU, driver, CUDA and Nsight Compute versions, input size, and data distribution. First check correctness and repeated execution time without the profiler.
**Look for branch evidence in source counters.** Check branch efficiency, `Avg. Predicated-On Threads Executed`, and available active-lane information in `SourceCounters`. Which values are available and where they appear can vary by tool version.
**Compare the hot lines with generated code.** Check whether the condition depends on lane-specific data, how much code each path contains, and whether compiler predication was used. A low metric on an unimportant line is not a reason to optimize it first.
**Choose one change that matches the hypothesis.** Try warp-aligned conditions, grouping the data, conditional selection for short work, or separating path-specific work. Include sorting and classification time, extra memory traffic, and kernel-launch costs in the comparison.
**Recheck correctness and total runtime.** Use the same input, verify the result, and repeat timing under the same conditions without the profiler. If there is no improvement or the result is worse, revert the change and evaluate another hypothesis separately. Nsight Compute can add overhead depending on collection settings and replay, so do not compare its report duration directly with ordinary execution time. [Nsight Compute Profiling Guide — Overhead and Replay](https://docs.nvidia.com/nsight-compute/ProfilingGuide/)
Volta and later GPUs use independent thread scheduling with per-thread execution state, so code should not assume that every thread in a warp stays in lockstep at every instruction. If correctness depends on data exchange or a warp collective after divergent control flow, check the participation mask and use __syncwarp() where synchronization is required. This synchronization supports correctness; it does not automatically improve branch performance. CUDA C++ Programming Guide — Independent Thread Scheduling
Warp divergence describes control flow and lane utilization within a warp; it does not mean that every conditional is slow. An optimization pays off only when different long paths frequently overlap in a real hotspot and total time decreases after accounting for data rearrangement and other side costs.
References
- NVIDIA CUDA C++ Programming Guide — SIMT Architecture and Independent Thread Scheduling
- NVIDIA CUDA C++ Best Practices Guide — Control Flow Instructions and Branch Predication
- NVIDIA Nsight Compute Compute Triage Guide — Divergence or Predication Losses
- NVIDIA Nsight Compute Profiling Guide — Source Counters and Profiling Overhead
- NVIDIA Nsight Compute CLI User Guide