Skip to main content

CUDA in Rust · Episode 2

CUDA Grid Size and Bounds Checks in Rust: Episode 2

Published 4 min 48 sMeasured on NVIDIA RTX 3090

What this video shows

About this walkthrough

A launch often has more threads than input elements. This episode follows the last valid thread and the first extra thread in a Rust kernel, using 1,003 elements and 256 threads per block in the animation. It rounds the grid up, guards the accesses, and tests a rounded-down grid against the corrected launch. This is a correctness lesson, not a speed comparison.

Read along

Transcript

Select a time to play the video from that chapter.

0:00GPU launches and bounds in Rust

In this Rust CUDA lesson, we'll size the launch and handle extra threads safely. Then we'll verify the output in GPUFlight.

0:11What do the extra threads do?

Last time, we used one GPU thread for each output. But what if we have 1,003 elements, and each block has 256 threads? Four blocks launch 1,024 threads. What should the extra 21 threads do? In this episode, we will size the launch and follow the boundary through our Rust kernel.

0:35Round up to cover the input

A block is a group of threads. The grid is the collection of blocks in one launch. For vector addition, we still want one output for each input position. If we divide 1,003 by 256 using integer division, we get three. Three blocks launch only 768 threads. That leaves 235 output positions with no thread to calculate them. Rounding up gives four blocks, enough to cover the whole input.

1:08The launch code

Here is the launch configuration from our Rust lab. The block size stays at 256. The problem is this grid expression: n divided by the block size. Replace it with the lab helper, ceiling divide. It rounds the block count up when there is a remainder. For 1,003 elements, the grid has four blocks. The extra threads still need a guard before they access the arrays.

1:38Follow the last valid thread

Zoom into block three, the fourth block because numbering starts at zero. Its first global index is three times 256, which is 768. Thread 234 in that block gets global index 1,002. Our arrays have 1,003 elements, so that is their last valid position. The condition, i less than n, is true. This thread reads the two inputs and writes their sum to the output at that same index.

2:12The first extra thread

The next thread gets index 1,003. Now i is equal to n, so the condition is false. It skips the entire body: no input read and no output write. The remaining threads through index 1,023 do the same. That is 21 extra threads. They were launched, but they do not calculate an output. Notice the strict less-than sign. The length is not a valid array index.

2:42What the Rust types guarantee

There are two different checks in this code. The outer guard checks the logical problem size, n. The output lookup uses the thread index to ask for its element in the disjoint slice. It returns Some when that element exists, and None otherwise. We only write inside the Some branch. With this one-dimensional mapping, different threads get different output positions. The caller must also provide input and output buffers large enough for n elements. A missing write can still be a wrong answer. Memory safety alone does not prove the calculation is complete.

3:23Check it in the actual lab

Now let us test the launch in GPUFlight. The animation used 1,003 elements to explain the boundary. The actual lab has its own test sizes, including 1,000, 1,024, and 4,097. We first run the rounded-down grid on the RTX 3090. The correctness check catches the missing outputs. Now change only the grid expression back to ceiling divide and run again. The corrected launch covers the inputs and passes correctness. This episode is about complete, valid writes. We are not using these two runs as a speed comparison.

4:07Try one more boundary

Here is a prediction before you go. With 257 elements and 256 threads per block, how many blocks do we need? And how many threads skip the body? Two blocks launch 512 threads. 257 threads write outputs. The remaining 255 skip the body. Round up the launch. Guard the accesses. Verify the whole output. Next time, we will see why the order of memory accesses can change performance. 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 vector-add lab in Rust