L2 Intermediate · Module 5: Parallel patterns

5.2Warp shuffles

The 32 lanes of a warp can read each other's registers directly with shuffle instructions, without shared memory or barriers. A warp sums 32 values in five instructions. This chapter shows the shuffle variants, builds a block reduction from them, and measures when that speed is actually visible.

GPU required (Colab or a rented GPU)~20 min

By the end you will be able to

  • ✓Use __shfl_down_sync, __shfl_xor_sync, __shfl_up_sync and __shfl_sync, and say what the mask means
  • ✓Write a warp reduction and a block reduction built from warp reductions
  • ✓Explain why shuffles barely change a large sum but halve the cost of a short one
  • ✓Explain why a one-pass sum with atomics is not bit-reproducible

In 5.1, threads passed partial sums to each other through shared memory, with a __syncthreads() after every step. Within a warp, there is a shortcut: the 32 lanes execute in lockstep (2.4), and the hardware lets each lane read a register directly from another lane.

Four shuffles

T __shfl_sync (unsigned mask, T v, int src_lane); // read v from lane src_lane (broadcast)
T __shfl_up_sync (unsigned mask, T v, int delta); // read v from lane - delta
T __shfl_down_sync(unsigned mask, T v, int delta); // read v from lane + delta
T __shfl_xor_sync (unsigned mask, T v, int lane_mask); // read v from lane ^ lane_mask

Every lane names a source lane, and every lane gets that lane’s value of v, all in one instruction. If the source lane is out of range (for example lane 30 reading lane + 4), the lane gets its own value back.

The first argument, the mask, lists the lanes taking part. 0xffffffff means all 32. Since Volta (2017), lanes in a warp can diverge and run separately, so the _sync versions require you to say which lanes must meet before the exchange. Every lane named in the mask must execute the same shuffle. Passing 0xffffffff from inside a branch that only some lanes take is undefined behaviour, the same kind of bug as a __syncthreads() in a branch.

A warp sum in five steps

__device__ float warp_sum(float v) {
for (int offset = 16; offset > 0; offset /= 2)
v += __shfl_down_sync(0xffffffff, v, offset);
return v; // lane 0 holds the sum of all 32
}

It’s the sequential tree from 5.1 (fold the top half onto the bottom), but the data moves between registers. There is no shared memory and no barrier, because the warp is already in lockstep. The lab traces it with lane ii starting at ii (the sum is 496):

output
__shfl_down_sync: the sum (496) ends up in lane 0 (lanes 0-7 ... 31):
start 0 1 2 3 4 5 6 7 ... 31
off 16 16 18 20 22 24 26 28 30 ... 62
off 8 48 52 56 60 64 68 72 76 ... 124
off 4 112 120 128 136 128 136 144 152 ... 248
off 2 240 256 256 272 272 288 288 304 ... 496
off 1 496 512 528 544 560 576 592 608 ... 992
__shfl_xor_sync: every lane ends up with the sum (lanes 0-7 ... 31):
start 0 1 2 3 4 5 6 7 ... 31
off 16 16 18 20 22 24 26 28 30 ... 46
off 8 48 52 56 60 64 68 72 76 ... 76
off 4 112 120 128 136 112 120 128 136 ... 136
off 2 240 256 240 256 240 256 240 256 ... 256
off 1 496 496 496 496 496 496 496 496 ... 496

With shfl_down, only lane 0 ends with the right answer; the other lanes hold partial or double-counted sums. With shfl_xor, lanes swap in pairs (a butterfly), so after five steps every lane has the total. Use xor whenever all lanes need the result, as in softmax, where every thread needs the row max to compute its own exp(x - max).

A block sum from warp sums

A 256-thread block has 8 warps. Each warp reduces its 32 values with shuffles; lane 0 of each warp writes its result into an 8-entry shared array; after one barrier, the first warp reduces those 8 values with shuffles again.

labs/m05/5.2/shuffles.cu
__shared__ float warp_sums[8];
int lane = threadIdx.x % 32, warp = threadIdx.x / 32;
float v = warp_sum(my_partial);
if (lane == 0) warp_sums[warp] = v;
__syncthreads();
if (warp == 0) {
v = warp_sum(lane < 8 ? warp_sums[lane] : 0.0f);
if (lane == 0) out[blockIdx.x] = v;
}

The shared-memory tree from 5.1 needed 8 barriers for 256 values; this needs one.

Measured: when it matters

The lab runs three versions of the grid-stride sum from 5.1: the full shared-memory tree, the tree down to 32 values with one warp finishing by shuffles, and shuffles everywhere. Then it runs the same three over 32,768 rows of 1,024, one block per row. Last, it times the block reduction on its own, with nothing else running:

output (RTX 3050 6GB Laptop)
Sum of 33554432 floats, 160 blocks of 256 threads:
A shared-memory tree (8 barriers) 1.051 ms 127.8 GB/s PASS
B tree to 32, then one warp shuffles 1.050 ms 127.8 GB/s PASS
C shuffles everywhere (1 barrier) 1.050 ms 127.9 GB/s PASS
Row sums: 32768 rows of 1024 floats, one block per row:
A shared-memory tree (8 barriers) 1.055 ms 127.3 GB/s PASS
B tree to 32, then one warp shuffles 1.053 ms 127.4 GB/s PASS
C shuffles everywhere (1 barrier) 1.054 ms 127.3 GB/s PASS
The block reduction alone (one block per SM, 1000 back-to-back sums of 256 values):
shared-memory tree (10 barriers) 413.7 ns per block sum
shuffles (2 barriers) 224.0 ns per block sum

Taken alone, the shuffle reduction is 1.85× faster: 224 ns against 414. Yet in both bandwidth-bound tests the three versions tie, even with one tree per 4 KB row. While one block is in its tree, the other blocks on the same SM keep loading, so the tree’s latency is hidden behind memory traffic.

The saving becomes visible when nothing else can hide it:

  • Small batches. During decode with batch size 1, an RMSNorm over one 4,096-wide hidden vector is a single block. The reduction latency is the kernel’s latency (3.4).
  • Reductions inside a loop. FlashAttention runs a row max and a row sum for every tile of keys (8.5). Those reductions sit on the critical path of each iteration.
  • Fast memory. An H100 reads memory about 20× faster than this laptop GPU, so there is far less memory time to hide the tree behind.

One pass, with an atomic

Since one block-level reduction is now cheap, a large sum can finish in one launch: each block reduces its slice and does a single atomicAdd of its total (4.5). With 160 blocks that is only 160 atomics, so it runs at the same 1.05 ms as the two-pass version and saves a launch. But:

output
One-pass sum, 5 runs (bit patterns): 4c000000 4bfffffe 4bffffff 4bffffff 4bfffffe

Blocks finish in a different order on each run, so the floating-point additions happen in a different order, and the result changes in the last bits. 5.3 shows how to get one launch and a fixed order at the same time.

Lab: shuffles 🔴

Terminal window
cd labs/m05/5.2 && nvcc -O2 -arch=native shuffles.cu -o shuffles && ./shuffles

Sources

Check your understanding

0 / 4 answered
  1. 1.After a shfl_down warp sum, which lanes hold the correct total?

  2. 2.What does the mask 0xffffffff in __shfl_down_sync promise?

  3. 3.Why did the shuffle version not speed up the 128 MB sum, even though its reduction is 1.85× faster on its own?

  4. 4.A block of 256 threads reduces with warp shuffles. How many __syncthreads() does it need?