When lanes take different branches
Change the signs in a warp’s inputs and track which lanes execute each branch.
Before you run
Each lane reads one value. Positive values are doubled and negative values are negated. Use one full warp and compare inputs with different sign patterns.
Definitions
- Divergence
- Threads of the same warp need different control-flow paths.
- Active mask
- The set of lanes participating in the current modelled instruction.
Read the starter excerpts
Read-only CUDA C++ · transform · starter kernel
__global__ void transform(const float* input, float* output) {
int lane = threadIdx.x;
float value = input[lane];
if (value < 0.0f) output[lane] = -value;
else output[lane] = value * 2.0f;
}transform tests the sign of each lane’s value. Negative values are negated. The other branch doubles its values. All 32 threads belong to one warp. main() uses alternateSigns to prepare the input signs. Both runs use the same branch condition.
CUDA C++ · launch inside main()
transform<<<1, 32>>>(input, output);The settings inside <<< >>> give the block count and threads per block. Parentheses pass the arguments to the kernel. These read-only excerpts show the starter. The CUDA source editor contains the storage and launch settings.
Edit and run CUDA
The CUDA source editor opens with the complete starter or your saved draft. To run the starter again, use Editor tools → Restore starter. Choose Undo restore there to recover the previous draft.
Click Compile and run with the starter. In Console, compare Lane 0 with lane 1. Their input signs send them down different branches.
Change the source
In the CUDA source editor, above transform, change alternateSigns from true to false. Predict the value printed for lane 1, then run again and check Lane 0 and lane 1 in Console. Which branch now handles every input?
Read the starter’s execution diagram
- Test signsEach lane evaluates value < 0.0f.
- Execute pathsOnly participating lanes update their outputs.
- ContinueThe warp proceeds after the branch.
Conceptual dependencies in this example; placement and timing are not specified.
Hardware context
A warp instruction has participating lanes. Different branch paths can require different active lane sets. These lanes are logical positions in a warp, not a drawing of separate dedicated processors.
Read the hardware key
- Resident warp
- A warp has 32 lane positions; a small block can leave some unused. A resident warp has its state and required resources allocated on an SM. Its next instruction may still be waiting for data.
- Instruction
- One operation run by GPU hardware, such as a load, multiply or store.
- Warp scheduler
- Selects ready warp instructions for available execution hardware.
- Registers
- Hold working values associated with individual threads.
- Execution units
- Perform arithmetic and other instructions for threads; they are shared hardware.
- Load/store units
- Execute memory instructions, forming requests from active lanes’ addresses.
- L1 / L2 cache
- Keep recently used data on the GPU chip. L1 is local to an SM; L2 is shared across SMs in this simplified view.
CPU side · CUDA host
Host ↔ device · copies move data; launches request execution
GPU device
GPU compute chip
SM · streaming multiprocessor
Logical groups using this SM’s resources
Issue to the appropriate units
Registers ↔ execution units · operands and results
Have their own execution hardware and registers.
Blocks may be assigned to different SMs.
Global-memory requests ↔ on-chip interconnect
Chip ↔ device memory · data transfers
Simplified NVIDIA-style architecture. Boundaries show containment; arrows name relationships. Unit counts, block placement and timing vary by GPU. A thread or warp is not a dedicated physical processor.
Explore the model: active lanes and branch paths
Predict and run
Try it
Select All positive under Signs in the input and click Run model. Then select Only lane 0 negative, predict Paths taken and run again. Repeat with Alternating signs. Compare Participation in each path: does changing the number of active lanes create another path?
These controls change only this model and its displayed excerpt; they do not edit the CUDA program above. Enter a prediction to compare with the model result.
CUDA C++ excerpt
// Host supplies 32 values. Input rule:
// Every lane reads 2.0f.
// Device body for one warp:
int i = threadIdx.x;
float x = input[i];
if (x < 0.0f) output[i] = -x;
else output[i] = x * 2.0f;The model shows the two arithmetic paths and their active masks. A compiler can implement this short conditional using predication or other instructions.
Apply what you saw
Which path has more active lanes with one negative value? How does alternating signs change the answer?
Compare your explanation
The multiply path has 31 lanes with one negative value. Both paths have 16 lanes with alternating signs.
Follow participating lanes through two paths 1 min 52 sec · Video walkthrough, captions and transcript
Use fullscreen to inspect the code and diagram labels.
Read the transcript
One warp contains both kinds of input
The starter alternates positive two point zero and negative two point zero across one warp. The positive path doubles its value. The negative path negates it. The question is which lanes participate in each operation.
A lane is a position within the warp
A lane identifies a thread’s position in its warp. These positions do not represent separate physical processors owned by those threads. A warp instruction can have only a subset of its lanes participating.
Positive lanes take the multiply
Sixteen lanes have positive inputs. Those lanes participate in the doubling operation; the negative lanes do not update their results through this path. The diagram shows participation, not a claim that the compiler must emit this exact branch order.
Negative lanes take the negation
The other sixteen lanes participate in negation. Their negative two point zero becomes positive two point zero. The same warp still contains all thirty-two threads. A lane inactive for one operation has not disappeared.
The threads retain distinct results
After the paths, lane zero has four point zero and lane one has two point zero. Each thread writes its own output position. Both lane sets belong to the original warp.
Make all inputs positive
Now consider thirty-two positive inputs. Pause and predict which lane set needs the negative-path operation. Does changing the signs change the warp’s size?
The unused path has no participating lanes
No lane needs negation in that case. All thirty-two can participate in doubling, and the warp is still thirty-two threads wide. Input patterns change instruction participation. They do not tell us an exact runtime without compiler and device evidence.
References SIMT architecture · thread hierarchy
The browser exercises need no installed CUDA documentation version. For local execution, use the documentation matching your toolkit. External references need internet access.
- CUDA · SIMT architecture (new tab)
A warp instruction uses its participating lanes. Different branch paths can execute with different active lane sets.
When lanes follow different paths, does each lane acquire its own physical processor for its path?
Compare your answer
No. Threads remain logical executions using shared SM resources. Different paths use participating lane sets; details of instruction issue and reconvergence depend on the architecture.
- CUDA · thread hierarchy (new tab)
For a one-dimensional launch, i = blockIdx.x * blockDim.x + threadIdx.x.
Block 2 contains four threads. Which global index belongs to its local thread 1?
Compare your answer
The one-dimensional expression gives 2 × 4 + 1 = 9. Whether index 9 accesses data depends on the element count and bounds guard.
Further GPU investigation Run this CUDA example on an NVIDIA GPU
In the CUDA source editor, open Editor tools and choose Export .cu to save the complete program. Use an NVIDIA GPU and a matching CUDA toolkit to run it outside the browser.
Browser simulation checks program behaviour; physical GPU timing requires a hardware measurement.
Enable JavaScript to save completion.