What this video shows
About this walkthrough
A matrix transpose reads its shared-memory tile by column, and every read in a warp lands in the same bank. The episode adds one padding column, updates the physical stride, and inspects the measured difference. Bank serialization falls from 8.74x to 1.02x, and the reference kernel from 323.10 to 235.74 microseconds, about 27% less. Both versions pass correctness.
Read along
Transcript
Select a time to play the video from that chapter.
0:00CUDA Bank Conflicts in Rust
In this Rust CUDA episode, we will transpose a matrix, explain shared-memory bank conflicts, and measure a two-line padding change.
0:10Turn rows into columns
A matrix transpose swaps rows and columns. The value at row two, column five moves to row five, column two. The mathematical operation is simple. The challenge is arranging memory accesses so many GPU threads can work efficiently. This lab already uses a shared tile. Threads load a row from global memory, then read the tile in the opposite orientation to write the output. The starting program is correct, and its global reads and writes are already coalesced. Our next bottleneck is inside the shared-memory access.
0:49Shared memory has banks
Shared memory is organized into banks that can serve accesses in parallel. For this example on the RTX 3090, consider thirty-two banks and four-byte floats. Consecutive float words map to consecutive banks. After bank thirty-one, the mapping wraps back to bank zero. When lanes request different words in the same bank for one shared-memory operation, that work must be split into additional service steps. That is a bank conflict. It is different from several lanes reading the same word, which can be broadcast. We will track different words down a column of the tile.
1:31Why a column hits one bank
The unpadded tile has thirty-two floats in each row. In column zero, consecutive rows are therefore word zero, word thirty-two, word sixty-four, and so on. Take each word index modulo thirty-two. Every one maps to bank zero. A warp reading that column requests thirty-two different words from the same bank. The problematic source expression is TILE, indexed by local X times STRIDE, plus local Y. Across this warp, local X changes and local Y stays fixed. The row-wise tile stores do not have the same problem. That is why a whole-kernel average need not report exactly thirty-two.
2:16Add one physical column
Now allocate thirty-two rows of thirty-three floats and change STRIDE to thirty-three. The logical tile still contains thirty-two by thirty-two matrix values. The extra column is padding, not another matrix column. Column zero now uses word zero, thirty-three, sixty-six, and ninety-nine. Modulo thirty-two, those map to banks zero, one, two, and three. The full warp spreads across all thirty-two banks. The input and output indices remain unchanged. So do the barrier and the matrix dimensions. The padding costs thirty-two extra floats, or one hundred twenty-eight shared bytes per block.
3:00Make the two-line change
Here is the edit in the Rust lab. Change the SharedArray capacity from thirty-two times thirty-two to thirty-two times thirty-three. Then change the STRIDE constant to thirty-three. Changing only the allocation would not change the access pattern. Keep the thirty-two by thirty-two block. It gives us the lane-to-column mapping we just explained. Run the padded kernel, check correctness, and compare it with the unpadded run. The comparison shows exactly those two lines changing. We are measuring a layout change, not a different transpose algorithm.
3:39Read the measured difference
The measured shared-memory bank serialization falls from eight point seven four times to one point zero two times. Near one means the measured shared operations need very little extra serialization. The average is not thirty-two before padding because the kernel also contains other shared operations, including its efficient row-wise stores. Reference kernel time falls from about three hundred twenty-three microseconds to two hundred thirty-six microseconds in this pair, about twenty-seven percent lower. Correctness passes on both sides. The padded version also meets the lab performance target. This result applies to this kernel, launch shape, and GPU. Padding is a tool for a particular access pattern, not a rule to add everywhere.
4:32Predict another stride
What if the physical row stride were thirty-four floats? Would a column reach all thirty-two banks? No. It advances two banks at a time and repeats after sixteen banks. Write down the addresses, then map them to banks. That small calculation often makes the profile much easier to understand. Next, we will combine thousands of values into one sum without making every thread compete for the same update. 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 matrix transpose lab in Rust