What this video shows
About this walkthrough
A warp can add its 32 values without shared memory by shuffling registers between lanes. The episode follows lane zero through offsets 16, 8, 4, 2 and 1, then joins eight warp totals through shared memory. Against the shared-memory tree adapted from Episode 6, the reference kernel goes from 61.82 to 33.95 microseconds. Issued sectors are about 541.5K on both sides, and both versions pass.
Read along
Transcript
Select a time to play the video from that chapter.
0:00CUDA Warp Shuffles in Rust
This episode builds on our Rust reduction. We will exchange values inside a warp, use a smaller shared array, and compare the result with the shared-memory tree.
0:11Start from the reduction we understand
Previously, each block reduced its values through shared memory and then updated a global sum. For this lab, each block writes its own partial sum instead. The runner combines those partials to check the final answer. That keeps the lesson focused on work inside one block. A block barrier cannot synchronize every block in a launch. Our baseline is the shared-memory tree from the previous episode, adapted to write one partial per block. The lab also offers a serial starter, but we are comparing the tree with shuffles so we can isolate this next change.
0:51What a shuffle actually does
Registers hold values used by individual threads. A warp shuffle lets one lane obtain a register value from another participating lane. In CUDA, a warp contains thirty-two lanes. A shuffle down by sixteen reads from the lane sixteen positions above the caller. It returns a value. It does not move a thread, rearrange the array, or write into the other lane. Each lane can add that returned value to its own partial sum. Our example uses full warps and keeps all lanes participating in each shuffle stage. Threads beyond the input end contribute zero.
1:32Follow lane zero through five stages
Give the lanes the values one through thirty-two. The total should be five hundred twenty-eight. At offset sixteen, lane zero adds lane sixteen. Its value becomes eighteen. At offset eight, it adds a partial that lane eight has already formed. The total in lane zero becomes fifty-two. Offsets four, two, and one finish the fold. Lane zero reaches one hundred twenty, then 256, then five hundred twenty-eight. Every stage reads the values from the preceding stage. The drawing updates the lanes together. Only lane zero is the final warp total we keep. The other lanes are helpers, not additional copies of the full sum.
2:19Connect the warps in a block
A two hundred fifty-six-thread block has eight warps. After those five shuffle steps, it has eight warp totals. Lane zero of each warp writes its total to a shared slot. Then the block synchronizes. The first warp reads the eight totals. Its other lanes use zero, so all thirty-two lanes can participate in another shuffle reduction. Lane zero writes the block result to the partial-output array. This implementation reserves a static array of thirty-two floats, enough for the maximum thirty-two warps in a block. Shared memory and a block barrier still connect the warps. The shuffle operations replace the reduction work inside each warp.
3:05Change the actual Rust code
Here is the new code in GPUFlight. Each thread loads one input into the local variable V. The shuffle-down loop uses offsets sixteen, eight, four, two, and one. The lane and warp identifiers decide who writes a warp total, and who performs the final fold. Keep the block size a multiple of thirty-two. Do not place the shuffle loop behind a bounds check that makes some required lanes skip it. Run the candidate, verify the partial sums, and open Compare against the saved tree-reduction baseline. Both versions already load adjacent input values with neighboring threads. We are not changing that global-memory mapping.
3:50Understand what changed in the profile
The global-memory sector total is about five hundred forty-two thousand on both sides. That is consistent with keeping the same global reads and output writes. Reference kernel time falls from about sixty-two microseconds to thirty-four microseconds in this pair, around forty-five percent lower. The shared-memory tree needs repeated shared loads, stores, and block barriers. The shuffle version does more of that combination through register exchange. The comparison headline says no meaningful change because the lab outcome is unchanged and the full runtime moves by less than five percent. That headline and the lower kernel-time row describe different scopes. Read the individual measurements before drawing a conclusion. We have a faster reference kernel here, not a demonstrated forty-five percent improvement to the whole program.
4:49One last participation check
A final block has only one valid input. Should the other threads leave before the shuffle? No. They contribute zero and keep participating in this full-warp reduction. The important requirements are which lanes participate, which values they exchange, and which lane owns the output. For the final episode, we will look beyond one kernel and fuse a three-stage pipeline into a single Rust kernel. Thanks for watching.
Measure it yourself
Run the same experiment on a real GPU
Edit the kernel in your browser, run it on managed hardware, and read the profile the video walks through.
Run the warp shuffle lab in Rust