Skip to main content

CUDA in Rust

C++ vs. Rust for CUDA: What the PTX Reveals

Published 6 min 43 sMeasured on NVIDIA L4

What this video shows

About this walkthrough

The same vector addition is written in CUDA C++, compiled with nvcc, and in Rust, compiled with cuda-oxide. The walkthrough follows both PTX listings line by line: each kernel does two loads, one addition and one store, and the Rust version adds slice bounds checks that branch to traps. The listings hold 22 and 57 PTX instructions, and kernel time differs by 0.9%. That is one pair of runs, not a language benchmark.

Read along

Transcript

Select a time to play the video from that chapter.

0:00C++ vs. Rust for CUDA

NVIDIA has introduced CUDA Rust, bringing native GPU kernel programming to Rust. So, which would you choose for CUDA: C++ or Rust? Let's compare one calculation in GPUFlight and follow its compiler output.

0:18The same vector addition

We're using vector addition in GPUFlight Performance Lab. At each index, read A and B, add them, and write C. A kernel is a function run by many GPU threads. Here, each thread handles one element. The language selector switches implementations. The calculation stays the same.

0:41How C++ and Rust reach the GPU

CUDA C++ reaches the GPU through NVIDIA's compiler, nvcc. This Rust example uses cuda-oxide, a compiler backend that translates Rust kernels to PTX. PTX means Parallel Thread Execution: a readable, intermediate instruction language for NVIDIA GPUs. Both paths can produce PTX, then machine instructions for the target GPU, which we inspect as SASS. PTX reveals compiler decisions. We still need measurements to judge performance.

1:20Select runs and open Compare

Open Result and scroll to Run History. These runs are already complete. Select the C++ run as the baseline. Select the Rust run as the candidate, check the order, and click Compare. Both passed correctness on the same L4, using 256 threads per block.

1:43Read the saved C++ source

Scroll to the saved source snapshots. These belong to the selected runs, even if the editor changes later. First, the C++ run. We'll keep this same pair throughout. A, B, and C are raw pointers: buffer addresses. N is a separate element count. Each thread computes its index, checks it against N, then performs the addition.

2:09Read the matching Rust source

Now the Rust run. The kernel attribute marks the GPU entry point. A and B are slices, carrying both an address and a length for checked access. The thread index and check against N have the same purpose as in C++. get_mut asks for the output element. If it returns Some, we add the inputs and write the result. Different interfaces; the same calculation.

2:38Open the PTX listings

Scroll to Compiled code. Keep PTX selected, and choose Raw listing. Scroll to the listings: the C++ run on the left, the Rust run on the right. We'll enlarge one side at a time, with its language and relevant line numbers clearly labeled.

2:58C++ loads, addition, and store

In the left C++ panel, scroll to the calculation. Lines 40 and 41, ld.global, load the two inputs from GPU global memory. Line 42, add.f32, adds the floating-point values. Line 47, st.global, writes the result. Together, they implement C at i equals A at i plus B at i.

3:26Find the same work in Rust

Now the right-hand listing: the Rust run. The two global loads are at lines 85 and 88. The addition is at line 90; the store is at 91. Different registers, same work: two loads, one addition, one store.

3:45The shared index check

C++ lines 28 and 29 check the index against N. setp creates a true-or-false condition. The at sign makes bra, the branch, conditional. Rust lines 62 through 64 perform the corresponding check. Both avoid processing threads beyond N.

4:07Rust input bounds checks

Stay in Rust. Lines 67 through 70 check the first input slice. Load its length, compare the index, then branch to BB0_6 if the index is too large. Lines 74 through 77 repeat this for the second input, branching to BB0_7. N and the slice lengths are separate. Being below N doesn't prove the index fits both slices.

4:36Follow the branches to traps

Scroll down within the same Rust listing to those branch targets. BB0_6 and BB0_7 lead to traps on lines 109 and 112. These signal invalid execution. Valid accesses do not execute the traps.

4:56Output and launch checks

The output is different. In the same saved Rust source, get_mut returns an optional result. If no element is returned, this body is skipped and nothing is written. Rust also checks the one-dimensional launch. Not every extra instruction comes from input bounds checks.

5:18Instruction counts and compiler context

The summary counts twenty-two PTX instructions for C++, and fifty-seven for Rust. These are listing counts, including different paths, not instructions every thread executes. The GPU target matches, but compiler toolchains and PTX versions differ.

5:39Compare kernel time

Return to the results and find Kernel time. C++: 853.25 µs. Rust: 861.15 µs. That's 0.9% in this pair. Repeat measurements before judging a small difference. Unprofiled runtime measures the harness, not just the kernel. Moving data can dominate vector addition, but this comparison doesn't prove the bottleneck.

6:10What the interfaces reveal

Here's what this comparison tells us. The C++ pointer interface leaves buffer validity to the programmer. This Rust slice interface supplies information used for checks. Checks can disappear when the compiler proves an access valid. This is not a universal Rust overhead. Choose the guarantees and ecosystem you need. In GPUFlight, compare the saved source, follow its PTX, and measure your workload.

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 vector-add lab in C++ or Rust