L2 Intermediate · Module 4: CUDA programming fundamentals
4.7Coalesced vs strided access
A warp's 32 loads are served together, in 32-byte pieces. When the 32 addresses are next to each other, memory delivers exactly what's needed; when they're spread out, most of every fetched byte is thrown away. Access patterns can cost a factor of four or more in bandwidth.
By the end you will be able to
- ✓Explain how the memory system serves a warp's loads in 32-byte sectors
- ✓Predict a pattern's efficiency from its stride
- ✓Fix strided access with layout changes or a shared-memory transpose
- ✓Recognize coalescing questions in KV-cache layouts and embedding gathers
The 32 threads of a warp execute each load instruction together (2.4). The memory system doesn’t fetch 32 separate 4-byte values. It looks at all 32 addresses, works out which 32-byte sectors of memory they fall in, and fetches each sector once.
Coalescing
If lane reads x[i], the 32 floats are adjacent: 128 bytes in 4 sectors. Every byte fetched is used. This is a coalesced access.
If lane reads x[8*i], each float lands in its own sector. The hardware fetches 32 sectors, 1,024 bytes, to deliver 128 useful bytes.
Seven-eighths of the bandwidth is wasted.
- sectors fetched
- 4
- bytes fetched / used
- 128 / 128
- efficiency
- 100%
32 adjacent floats = 128 bytes = 4 sectors. Every byte fetched is used.
Measured
The lab reads with a stride and writes coalesced:
Strided read, coalesced write (128 MB each way): stride GB/s useful bytes per 32-byte sector 1 157.9 32 of 32 2 106.1 16 of 32 4 64.6 8 of 32 8 35.8 4 of 32 16 35.8 4 of 32 32 35.8 4 of 32 64 35.8 4 of 32Stride 1 reaches 158 GB/s, close to this GPU’s ~168 GB/s peak. From stride 8 on, every lane is in its own sector and the rate settles at 36 GB/s, 4.4× slower for the same work. (It doesn’t fall the full 8× because the writes stay coalesced and the caches help a little.)
The classic case: transpose
Transposing a matrix reads rows and writes columns, or the reverse. One side must be strided. The fix is to go through shared memory: read a
32 × 32 tile with coalesced row reads, __syncthreads(), then write it out as coalesced rows of the transposed tile.
// simplified from labs/m04/4.7/coalescing.cu (which loops over 4 rows per thread)__shared__ float tile[32][33]; // +1 column avoids bank conflicts (6.4)tile[ty][tx] = in[(y0 + ty) * N + x0 + tx]; // coalesced read of a row__syncthreads();out[(x0 + ty) * N + y0 + tx] = tile[tx][ty]; // coalesced write of a row of the transposeTranspose 4096 x 4096 floats: naive 2.073 ms 64.7 GB/s PASS tiled 0.876 ms 153.3 GB/s PASSThe tiled version is 2.4× faster and runs near peak bandwidth. Shared memory acts as a reorder buffer: the strided access happens on chip, where it is cheap.
Other tools
- Vector loads. Reading
float4(16 bytes per lane) issues fewer, wider requests: one instruction moves 512 bytes per warp. - Structure of arrays. An array of structs
{x, y, z, w}makes a warp reading onlyxstride by 16 bytes. Separate arrays for each field keep each read coalesced. - Let the profiler tell you. Nsight Compute reports sectors per request for every load: 4 for coalesced 4-byte loads, up to 32 for the worst case (7.2).
Lab: stride and transpose 🔴
cd labs/m04/4.7 && nvcc -O2 -arch=native coalescing.cu -o coalescing && ./coalescingSources
Check your understanding
0 / 4 answered1.Lane i reads x[i] (4-byte floats, aligned). How many 32-byte sectors does the warp fetch?
2.Lane i reads x[16*i]. What fraction of fetched bytes is used?
3.How does the tiled transpose avoid strided global memory access?
4.Why do paged KV caches use blocks of 16 or more tokens rather than 1?