Skip to main content

CUDA in Rust · Episode 1

Your First GPU Kernel in Rust: CUDA Series Episode 1

Published 4 min 17 sMeasured on NVIDIA L4

What this video shows

About this walkthrough

If every GPU thread runs the same function, how does each one know which answer to calculate? This first episode writes a vector-add kernel in Rust with cuda-oxide and follows two threads to see where each index comes from. The starter launch with one thread per block reaches 29.5% achieved occupancy. With 256 threads per block it reaches 83.6%, and the output is correct in both runs.

Read along

Transcript

Select a time to play the video from that chapter.

0:00The question

This Rust function runs on the GPU. But if every thread runs the same function, how does each one know which answer to calculate? Let's write our first CUDA kernel and follow the data.

0:15Define the job

Our job is vector addition. We have two input arrays, A and B, and an output array, C. At each position, add the two inputs and store the answer in the same position in C. Two plus three gives five. Four plus seven gives eleven. We'll use 1,024 elements.

0:37CPU launches, GPU computes

The CPU prepares the data and launches the kernel. The GPU executes the kernel across many threads. For this example, we assign one thread to each output element. GPUFlight's lab harness handles the setup, transfers, and result checks, so we can focus on the Rust kernel.

0:59Read the Rust kernel

The kernel attribute marks this function as a GPU entry point. A and B are read-only slices of 32-bit floating-point numbers. C uses a disjoint slice for the output. And n is the number of elements. Inside the function, index_1d gives this thread its global index for our one-dimensional launch. Calling get gives us the numeric index, i. We use that same i to select the inputs and output.

1:31Follow threads zero and one

Let's zoom into the first two output elements. Thread zero gets index zero. It reads two from A and three from B. Thread one gets index one. It reads four and seven. Both execute the same addition, but with different inputs. Their results are five and eleven. Each thread writes to its own position in C. Same instructions. Different indices. Different answers.

2:03Cross a block boundary

Where does that index come from? Our launch uses four blocks, with 256 threads in each block. The global index is the block number times the block size, plus the thread's local index. The last thread in block zero gets index 255. The first thread in block one gets 256. Together, the blocks cover all 1,024 outputs.

2:31Keep the accesses valid

Before reading the inputs, the kernel checks that i is less than n. The input arrays must also contain at least n elements. Next, the output lookup returns the element for this thread's index, if it exists. We write the sum there. In this mapping, each valid thread has a different index, so two threads do not write the same output. We'll explore boundary cases in the next episode.

3:00Run it in Performance Lab

Now open Vector Add in GPUFlight's Performance Lab and select Rust. Here is the kernel we just read. One difference: the lab's starter launch uses one thread per block, on purpose. Run it as it is. The harness compares the output with its reference calculation. Look at the correctness result first: the output is correct. This lab also has an occupancy challenge, which is a separate performance requirement, and the starter misses it. Now use the launch we just walked through, 256 threads per block, and run it again. The kernel did not change. It is still correct, and now occupancy passes too. A correct answer and a completed performance challenge are different results. Our first goal is to understand and verify the kernel.

3:56A quick prediction

Before you go, predict the outputs if we replace addition with subtraction. The indices stay the same. What changes? Next time, we'll see what happens when the input length doesn't fill a whole number of blocks.

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 Rust