What this video shows
About this walkthrough
A three-point stencil reads each input value from global memory more than once. The walkthrough loads a block's inputs and two halo values into a shared-memory tile and reuses them. Global-memory sectors drop from 943.7 million to 432.5 million, but kernel time changes from 159.0 to 166.9 microseconds in this pair of runs. Fewer requests do not automatically mean a faster kernel.
Read along
Transcript
Select a time to play the video from that chapter.
0:00Reuse the neighbors
This smoothing kernel repeatedly loads values that neighboring threads also need. Let's investigate that overlap in GPUFlight Performance Lab.
0:11Understand the stencil and goal
A stencil computes each output from a small neighborhood of inputs. Here, we take one quarter of the left value, half the center, and one quarter of the right. The first and last elements are copied unchanged. Our goal is to reduce global-memory requests while preserving these results.
0:31Measure the baseline
Run the original kernel on the RTX 3090. The output passes, but the profile reports 943.7 million sectors across the repeated launches. The lab's target is 600 million or less.
0:48What does a sector measure?
Open the session, then Memory, and scroll to Memory access analysis. A sector is a 32-byte memory unit. This table counts sectors requested by global-memory instructions. Those requests may hit a cache, so this number is not the amount fetched from physical device memory.
1:08Shared memory and the halo tile
Shared memory is fast on-chip storage that threads in one block can share. A tile holds the block's chunk of input. The first and last threads also need a neighbor outside that chunk. Those extra edge values are the halo. For 256 threads, we need 258 floats.
1:29Load each center once
Declare the shared tile and give each thread a local index, t. Each valid thread loads its center into tile t plus one. That offset leaves the first slot available for the left halo.
1:43Load the halo and synchronize
The first thread loads the left halo, and the last thread loads the right. Bounds checks protect the ends of the array. Then synchronize the block: every thread must finish loading before any thread uses a neighbor's value. Keep this barrier outside the valid-output condition so the whole block reaches it.
2:03Reuse the shared values
Now replace the three global reads with reads from the tile. The arithmetic stays the same, and boundary outputs still copy their center value. We have changed where the inputs come from, not the smoothing operation.
2:18Size the tile
Express the shared-memory reservation as the block size plus two, times the size of a float. That's 1,032 bytes, the same reservation the starter already supplied.
2:32Check correctness
Run the tiled version on the same GPU. The correctness tests pass, including small arrays and partial final blocks. Sectors fall to 432.5 million, meeting the target.
2:46Compare memory requests
The new Memory analysis confirms about 54% fewer sectors. Most input values are now loaded once per block and reused from shared memory. The two halo loads still add some global traffic. We haven't eliminated every repeated request.
3:05Compare kernel time
Now compare kernel time: 159 microseconds before, and 166.9 after. That's about 5% slower in this pair, despite fewer sectors. Caches can already capture reuse, while tile loading and synchronization add work. These are possible explanations, not a diagnosis proven by this table. The overall test runtime moved the other way. Repeat timings before drawing a speed conclusion.
3:33Measure the tradeoff
This lab demonstrates explicit data reuse, but shared memory is not automatically faster. Check correctness, inspect the requests, and measure kernel time. Try the stencil lab in GPUFlight Performance Lab.
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