Skip to main content

CUDA in Rust · Episode 3

CUDA Memory Coalescing in Rust: Episode 3

Published 5 min 19 sMeasured on NVIDIA RTX 3090

What this video shows

About this walkthrough

One index change turns a SAXPY kernel's strided memory access into coalesced access. The episode follows a warp's addresses, makes the edit in Rust, and reads the issued-sector count beside kernel time. Issued sectors fall from 5,033,177,505 to 629,147,457, and the reference kernel goes from 1.96 ms to 240.92 microseconds. The full harness changes from 1.297 s to 1.073 s, so the roughly 8x kernel difference is not a whole-application speedup.

Read along

Transcript

Select a time to play the video from that chapter.

0:00CUDA Memory Access in Rust

In this episode, we will use Rust to change how GPU threads access memory, then measure what that changes in GPUFlight.

0:10Same calculation, different addresses

Our program multiplies each value in X by a number, then adds it to the matching value in Y. This operation is called SAXPY. The name sounds complicated, but each output is just alpha times X, plus Y. For example, if alpha is two, X is three, and Y is one, the new Y is seven. The starting kernel already gives the correct answer. Our goal is to keep that answer and improve the way it accesses memory. Open the SAXPY lab, select Rust, and choose the RTX 3090. We will use the same input sizes and launch configuration for both versions.

0:52Follow neighboring threads

A CUDA warp is a group of thirty-two threads. Each thread has a position in that group, called its lane. Consider one load instruction. All the active lanes request their values, and the GPU groups the requested addresses into memory regions. In this example, each region is a thirty-two-byte sector. It holds eight four-byte floats. If thirty-two lanes request thirty-two adjacent floats, starting at a sector boundary, four sectors cover the request. That is coalesced access. The important question is which addresses the lanes request together, not just how many values the program eventually reads.

1:35Read the indexing before changing it

Look at the starting index expression. The local thread number is multiplied by the number of blocks. For the large input, there are 65,536 blocks. Neighboring lanes are that many elements apart. The first few lanes in block zero therefore access zero, 65,536, and 131,072. Their addresses land in different sectors. A full warp uses thirty-two sectors for this load, although the same amount of useful data could fit in four. The mapping still covers the output correctly. Correctness alone does not tell us whether the memory requests are efficient.

2:20Make adjacent lanes access adjacent data

Now change the index to the block number times the block size, plus the local thread number. In block zero, neighboring lanes now access zero, one, two, and so on. In the next block, they start at 256. Keep the bounds guard. The final block may contain extra threads, just as we saw in Episode two. We also keep the same arithmetic and the same output buffer. Only the assignment of elements to threads changes. The unsafe output access still requires a valid index and a unique writer. The guard and this one-dimensional mapping are the reasons those requirements hold here.

3:04Change it in the actual app

Here is that edit in GPUFlight. Replace the indexing expression, then run the Rust version again. The lab checks the large input and several small boundary cases. The corrected version passes correctness. Select the baseline and the candidate in Run history, then open Compare. This view keeps the two submitted code versions beside the measured result. We can check that the launch shape stayed the same. The lab reports coalescing efficiency rising from twelve point five percent to one hundred percent. It now meets the performance target.

3:43Check sectors and kernel time

Open the session details and choose Memory. Scroll to Memory access analysis. The measured issued-sector total falls from about five point zero three billion to six hundred twenty-nine million, across the profiling repetitions. These are issued sector requests. They are not a measurement of bytes fetched from physical GPU memory, because caches can satisfy requests. The comparison also reports reference kernel time falling from about one point nine six milliseconds to two hundred forty-one microseconds in this pair. That is about an eightfold difference in the displayed kernel time. It does not mean the whole application is eight times faster. The full runtime includes allocation, transfers, and validation. Keep that timing scope separate.

4:38Try the mapping yourself

Here is a quick check. If lane zero reads element zero and lane one reads element eight, do those two floats occupy the same thirty-two-byte sector? No. Eight floats fill one sector, so those two addresses start different sectors. Before changing arithmetic, inspect the thread-to-address mapping. Small indexing changes can reshape a great deal of memory work. Next, we will reuse values that neighboring outputs need, using shared memory and a halo tile. You can try the Rust example in GPUFlight Performance Lab. 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 SAXPY coalescing lab in Rust