Skip to main content

CUDA in Rust · Episode 6

Fixing a Slow CUDA Sum in Rust: Episode 6

Published 5 min 18 sMeasured on NVIDIA RTX 3090

What this video shows

About this walkthrough

A sum is slow when every thread adds to one address atomically. The episode combines input values inside each block with a reduction tree, and explains the tail identity and the barriers it needs. Atomic updates derived from the code fall from 4,194,304 to 16,384, SM throughput rises from 0.4% to 84.5%, and the reference kernel goes from 7.63 ms to 62.34 microseconds. The full harness did not improve: 297.997 ms before, 299.018 ms after.

Read along

Transcript

Select a time to play the video from that chapter.

0:00CUDA Atomic Contention in Rust

In this episode, we will sum an array in Rust, find a contended atomic update, and replace most of those updates with a block-local reduction.

0:10Many inputs, one answer

We want to add all the values in an array and produce one total. A reduction is an operation that combines many inputs into fewer outputs. A sum is one familiar example. Our starter gives each input to a GPU thread. Every valid thread adds its value to the same output address. An atomic addition makes each update indivisible, so overlapping updates do not lose one another. That protects this update, but it does not make unlimited updates to one address fast. Our goal is to preserve the sum while reducing that contention.

0:50The bottleneck is the shared destination

Picture many threads arriving at the same counter. Each needs its addition applied to that one value. The updates compete for the same destination. More threads can create more competition instead of more useful parallel work. For the large case here, more than four million input values each cause a global atomic update. The source says fetch add on output zero. That repeated destination is the clue we want to investigate. We will let a block combine its own values first, then submit one update for the whole block. Atomics are still useful. We are changing how often this contended atomic is needed.

1:33Measure the correct baseline

Open the global-atomic reduction lab and select Rust on the RTX 3090. Run the original version. It passes the correctness check, but the SM throughput metric is only zero point four percent in this capture. An SM is a GPU processing unit that executes groups of threads. This throughput metric describes activity relative to the hardware metric definition. It is not the same as occupancy, which describes resident warps, and neither number is a stopwatch. The reference kernel time is about seven point six three milliseconds. We will compare that separately after the code change.

2:17Combine inside each block

First, each thread writes its input to a distinct shared-memory slot. Out-of-range threads write zero, the identity for addition. The block synchronizes. Then we combine pairs of slots. With eight example values, eight become four partial sums, then two, then one. The same idea takes eight stages for a block of 256 values. Each stage uses a block barrier before the next stage reads the updated slots. All threads keep participating in those barriers. We guard the value and the work, rather than leaving a shared slot uninitialized. Finally, only thread zero adds the block total to the global result.

3:04Implement the reduction in Rust

The Rust version obtains the dynamic shared-memory pointer and stores one value per thread. The loop starts at half the block size. Threads below the current stride add the matching value from the upper half. After sync threads, the stride is halved. We repeat until the first slot holds the block total. The launch reserves 1,024 shared bytes for 256 floats. The unsafe shared accesses rely on those bounds, initialized slots, and the barriers between stages. We also keep the harness resetting the global output before a sum. Atomic addition accumulates into the value already there.

3:47Compare work, kernel time, and full runtime

At the large input size, the atomic updates fall from 4,194,304 to 16,384. Those counts follow from the code: one update per input becomes one update per block. They are not a separate counter displayed by this comparison. The measured SM throughput rises from zero point four percent to eighty-four point five percent. The reference kernel time falls from seven point six three milliseconds to about sixty-two microseconds in this pair. But the full harness runtime stays around three hundred milliseconds. Setup, transfers, and validation are also part of that program. A much faster kernel does not automatically produce the same improvement in whole-program time. Also, a different addition order can change floating-point rounding, so the lab checks a tolerance.

4:47Choose the identity carefully

For a sum, an extra thread contributes zero. What should an extra thread contribute to a product? One. Multiplying by one leaves the result unchanged. Choose the identity for the operation, initialize every slot that will be read, and keep the required synchronization. Next, we will keep more of this reduction in registers and use warp shuffles to exchange values. 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 atomic reduction lab in Rust