Skip to main content

CUDA in Rust · Episode 8

CUDA Kernel Fusion in Rust: Episode 8

Published 6 min 19 sMeasured on NVIDIA RTX 3090

What this video shows

About this walkthrough

Three kernels (scale, bias and ReLU) each read and write a full array. The episode fuses them into one kernel in a custom Rust Workbench project and removes the temporary arrays. Across 105 pipeline repetitions, launches fall from 315 to 105 and summed GPU kernel time from 49.98 ms to 16.65 ms, with peak tracked allocations going from 256 to 128 MiB. The full captured session barely moves: 356.19 ms to 354.92 ms.

Read along

Transcript

Select a time to play the video from that chapter.

0:00CUDA Kernel Fusion in Rust

For our final Rust CUDA episode, we will turn three GPU kernels into one. We will follow the temporary arrays, change the launch sequence, and measure the resulting profile.

0:13One calculation, three stages

Imagine a small data-processing pipeline. First, multiply each input by a scale. Then add a bias. Finally, replace negative values with zero. That last operation is called ReLU. Here it is simply a clamp at zero. The complete expression is the maximum of zero and A times X plus B. With A equal to two and B equal to one half, an input of three produces six and a half. An input of minus one produces zero. Separate stages are useful when building a program from reusable operations. But materializing every intermediate result can add memory work. We will use a custom Rust project in GPUFlight Workbench. The input has about sixteen point eight million floats, and both versions check every output.

1:07Follow the temporary data

In the baseline, the scale kernel reads X and writes a temporary array. The bias kernel reads that temporary array and writes a second temporary array. The ReLU kernel reads the second temporary array and writes Y. All three launches use the same stream, which preserves their order. Reading the code gives us three loads and three stores per element across the pipeline. That is a source-level count, not a hardware-counter measurement. The two temporary arrays each hold sixty-four mebibytes. A mebibyte is about one million bytes. These arrays exist only to pass values between stages. Fusion lets one thread keep those intermediate values locally, then write only the final answer.

1:57Keep the experiment consistent

Here is the baseline launch sequence. The three calls use the same input length and 256 threads per block. We allocate the buffers before the repeated work. Both versions do five warm-up repetitions followed by one hundred timed repetitions. We synchronize after the warm-up, and again when the timed loop finishes. Without waiting for completion, a host timer could measure submission instead of completed work. The trace contains the warm-ups too. That means one hundred five pipeline repetitions, or three hundred fifteen baseline kernel launches. We will compare the trace kernel totals over that same scope. Allocation, transfers, and validation are separate work.

2:45Fuse work owned by one thread

Here is the fused kernel. Each thread still owns one input and one output index. It reads X once, calculates A times X plus B, clamps the value, and stores Y. There is no need to share an intermediate array because no other thread needs this thread's intermediate value. We can remove both scratch buffers and replace the three launch calls with one call to fused. Keep the bounds guard and the unique-output rule. Fusion changes the work between the load and store, not who owns each output. In a more complicated pipeline, fusion can increase register use or change scheduling. It still needs a measurement, and floating-point rounding may need a tolerance check.

3:33Change the actual Workbench program

In the actual app, these are the three baseline calls. The project already contains the fused kernel so we can compare the launch paths consistently. Replace the three calls with module dot fused, and remove the two temporary buffer allocations. Keep the input, block size, warm-up count, and repetition count unchanged. The chosen values make the expected result exactly representable in this example. Run it and inspect the output check. Both versions correctly produce all 16,777,216 outputs. Open the saved profiles. First compare the kernel lists and timelines, then the Memory tab.

4:20Compare the same profiling scope

The baseline trace has three hundred fifteen kernel launches. The fused trace has one hundred five. The timeline now repeats one kernel instead of three. Summed GPU kernel time falls from forty-nine point nine eight milliseconds to sixteen point six five milliseconds across those one hundred five repetitions. This is about one third of the baseline GPU kernel time in this pair. It is not a claim that the entire application became three times faster. The Memory tab shows peak in-flight tracked allocations falling from 256 to one hundred twenty-eight mebibytes. The two scratch arrays are gone. CPU-to-GPU and GPU-to-CPU transfers are unchanged: sixty-four mebibytes in each direction. We removed intermediate device-memory work, not the input or output transfer. The full captured session is still about three hundred fifty-five milliseconds on both sides. Startup, host work, and profiling overhead have a different scope from the kernel totals.

5:32Choose the next experiment

If a later stage needs every element's result before it can proceed, can we always replace the kernel boundary with an ordinary block barrier? No. A block barrier only coordinates threads in that block. A dependency across blocks needs a suitable global coordination strategy. Start with the dependency, preserve the answer, change one thing, and compare measurements over the same scope. Across this series, we have followed thread indices, memory requests, shared tiles, banks, reductions, shuffles, and now fusion. Try one of these ideas in GPUFlight Performance Lab, or build your own Rust experiment in Workbench. 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 fusion warm-up lab