What this video shows
About this walkthrough
This episode builds a halo tile for a three-point stencil in Rust and follows the shared loads and the block barrier. Issued sectors fall from about 943.7 million to 432.5 million, while the reference kernel goes from 163.93 to 167.71 microseconds. The pair shows lower issued traffic, not faster kernel execution. Repeat the timing before calling it a regression.
Read along
Transcript
Select a time to play the video from that chapter.
0:00CUDA Shared Memory in Rust
This time, we will build a shared-memory tile in Rust, explain its halo, and check whether less memory work also means less time.
0:09Why neighbors read the same values
Imagine smoothing a line of measurements. Each interior output combines the value on its left, its own value, and the value on its right. Our weights are one quarter, one half, and one quarter. The first and last outputs simply keep their original values. This is a three-point stencil. Similar neighbor calculations appear in filters and numerical simulations. Adjacent outputs overlap. Output three needs inputs two, three, and four. Output four needs inputs three, four, and five. We will keep the calculation correct and make that reuse explicit.
0:50A tile with a halo
Global memory holds the input array. Shared memory is a small storage area that threads in the same block can use together. Think of a tile as the part of the input that one block brings into that shared area. A block with 256 output threads needs its own 256 input values. It also needs one extra input on each side. Those two neighboring values are the halo. The tile therefore holds two hundred fifty-eight floats. At four bytes each, that is 1,032 bytes of shared memory. The halo is input data for the edge calculations. It is not two extra outputs.
1:34The original Rust kernel
In the starter, each valid thread reads its left, center, and right values directly from the input slice. The source contains three global input reads for most outputs. Nearby threads ask for many of the same values. Hardware caches may already help with that reuse. We should not assume every source-level read reaches physical GPU memory. The lab measures issued global-memory sectors across repeated launches. That gives us evidence about the requests the code issues. Run the starter first. It passes correctness, but reports about nine hundred forty-four million sectors, above the lab target.
2:18Load, synchronize, then reuse
In the tiled version, each thread loads its center value into slot thread number plus one. The first thread also loads the left halo. The last thread loads the right halo. Unused center slots in the final partial block are initialized to zero. The actual boundary outputs still follow the original rule. Now every thread reaches the block barrier. This waits for the tile loads before any thread consumes its neighbors. Only after the barrier do valid output threads calculate their result from the shared tile. The unsafe pointer operations need enough allocated shared memory, valid tile indices, and this ordering between writers and readers.
3:04Apply the shared-memory version
In the actual editor, the new code gets a pointer to dynamic shared memory, stages the center and halo values, and calls sync threads. The weighted sum now reads three positions in the tile. The output mapping remains one valid thread per output. Keep the two hundred fifty-six-thread launch and reserve 1,032 shared bytes. Run the new version and check the boundary tests as well as the large input. The result passes correctness. Open Compare to put the code change beside the sector count and kernel time.
3:42Less traffic is not a promised speedup
Issued global-memory sectors fall from nine hundred forty-three point seven million to four hundred thirty-two point five million. That is about fifty-four percent fewer sectors in the same profiling scope. The shared-memory version meets the lab target. But look at kernel time. This pair shows about one hundred sixty-four microseconds before and one hundred sixty-eight after. This small timing difference is not evidence of a speedup. We would need repeated measurements before calling it a meaningful regression, too. The original loads can benefit from caches. The tiled version adds shared-memory operations and a barrier. This is why we measure both the work and the time. A better traffic metric does not automatically make a faster kernel.
4:35What changes with a wider stencil?
Suppose each output needs two neighbors on either side. How many floats does a two hundred fifty-six-thread tile need? Two hundred sixty. The centers, plus two halo values at each end. Try extending the drawing before changing the code. Then check boundaries, synchronization, and the profile again. Next, we will see why shared memory itself can become a bottleneck, and why one extra column can help. 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 1D stencil lab in Rust