L2 Intermediate · Module 6: GEMM and memory optimization
6.4Shared-memory bank conflicts
Shared memory is split into 32 banks, and a warp's access finishes in one pass only if its 32 lanes hit 32 different banks. Reading down a column of a 32-wide tile puts every lane in the same bank and takes 32 passes. This chapter shows how the banks work, measures the cost of each conflict, and fixes the classic case with one column of padding.
By the end you will be able to
- ✓Compute the bank of a shared-memory address, and the number of passes a warp's access needs
- ✓Recognize the stride patterns that conflict and the ones that don't
- ✓Remove conflicts with padding, and know when swizzling is used instead
- ✓Explain why a 2-way conflict can cost nothing in practice
Shared memory can deliver 32 four-byte words per clock, one to each lane of a warp, because it is built from 32 banks that work in parallel. Consecutive 4-byte words go to consecutive banks:
When a warp issues a shared-memory load, each bank can serve one word per pass. If several lanes want different words from the same bank, they are served one after another. That is a bank conflict. If several lanes want the same word, it is read once and broadcast, which costs nothing.
So the cost of a warp’s access is the largest number of distinct words requested from any one bank: an n-way conflict takes n passes.
- passes (n-way conflict)
- 32
- banks used
- 1 / 32
- shared-memory bandwidth
- 3%
Reading down a column of a 32-wide float tile: every word is in the same bank. 32 passes, the worst case.
For lane reading word , the number of passes is : stride 2 is a 2-way conflict, stride 8 is 8-way, and any multiple of 32 puts every lane in one bank.
Measured
The lab has every lane read s[lane * stride] 4,096 times:
Each lane reads s[lane * stride], 4096 times; 80 blocks of 256 threads: stride predicted time vs stride 1 0 1-way 0.182 ms 1.0x all lanes read one word (broadcast) 1 1-way 0.182 ms 1.0x consecutive words 2 2-way 0.183 ms 1.0x every other word 3 1-way 0.183 ms 1.0x odd stride 4 4-way 0.356 ms 2.0x 8 8-way 0.708 ms 3.9x 16 16-way 1.412 ms 7.7x 32 32-way 2.818 ms 15.5x a column of float[32][32] 33 1-way 0.183 ms 1.0x a column of float[32][33] (padded)From 4-way on, time grows exactly with the number of passes: double the conflict, double the time. A column of an unpadded 32-wide tile is 15.5× slower than a row. The padded tile is as fast as the best case.
The 2-way conflict costs nothing here. The likely reason: even at stride 1, this loop doesn’t keep the shared-memory pipeline full, because issuing each load and add takes time too, which leaves room for one extra pass. In a kernel that saturates shared memory, like the inner loop of a register-tiled matmul, a 2-way conflict does cost 2×. Treat small conflicts as cheap only after measuring.
Where conflicts come from
Columns of 2D tiles. Any __shared__ float tile[R][32] read down a column is a 32-way conflict. This appears in transposes
(4.7), in matmul when one operand is read along , and in attention when is read transposed.
The fix: padding. Declare tile[R][33]. Row now starts at word , which is bank , so a column visits all 32 banks.
It costs one wasted column of shared memory.
The other fix: swizzling. Instead of wasting memory, permute where each element is stored, typically XOR-ing the column index with
bits of the row index: tile[r][c ^ (r % 32)]. Every row is still a permutation of the same banks, and every column lands in different
banks. CUTLASS, FlashAttention and Triton all use swizzled layouts, because tensor-core loads (ldmatrix) need tiles with no padding gaps.
Lab: banks 🔴
cd labs/m06/6.4 && nvcc -O2 -arch=native bank_conflicts.cu -o bank_conflicts && ./bank_conflictsSources
Check your understanding
0 / 4 answered1.Lane ℓ reads s[4 * ℓ] (floats). How many passes does the warp need?
2.All 32 lanes read s[7]. What happens?
3.Why does __shared__ float tile[32][33] remove the conflict when reading a column?
4.Why do tensor-core kernels usually swizzle instead of pad?