Memory coalescing and bandwidth-bound kernels: copy, stride and transpose
How a warp's 32 loads become 32-byte memory sectors, why strided access can waste most of your bandwidth, how vectorised loads help, and how to write a matrix transpose that reads and writes efficiently using shared memory without bank conflicts.
By Sandip Maity, Applied AI Engineer
Most kernels in an inference engine are bandwidth-bound: norms, activations, residual adds, rotary embeddings, sampling, and the decode matmuls at small batch. For these the only question is how close you get to the memory bandwidth limit. The single biggest factor is the order in which threads touch memory.
#How a warp reads memory
When a warp executes a load, the 32 threads each provide an address. The memory system groups those addresses and fetches whole 32-byte sectors (4 sectors make a 128-byte cache line). It can't fetch less than a sector, so any byte in a fetched sector that no thread asked for is wasted bandwidth.
- Consecutive floats (thread reads element ): 32 threads times 4 bytes is 128 bytes, which is 4 sectors, all bytes used. This is a coalesced access.
- Stride 2 (thread reads element ): the warp spans 256 bytes, so 8 sectors, half of each wasted.
- Stride 8 or more: every thread lands in its own sector. 32 sectors are fetched (1,024 bytes) for 128 useful bytes: 12.5% efficiency.
Drag the stride and watch the sectors fill up.
Stride between neighbouring threads
Threads 0 to 31
The 4 sectors spanned by the warp's addresses (lit: fetched)
sectors fetched
4
bytes fetched
128 B
useful
100.0%
Thread k loads the 4-byte float at index k × stride. Memory is fetched in 32-byte sectors (8 floats). Lit squares are sectors the warp fetches; the bar shows how much of what was fetched is used.
The rule: neighbouring threads should touch neighbouring addresses. It's about what the 32 threads of a warp do at the same instruction, not about what one thread does over time.
#Lab: measure it
This program copies an array with a configurable stride and reports useful bandwidth (bytes actually needed divided by time).
// stride.cu
#include <cstdio>
__global__ void copy_strided(const float* in, float* out, int n, int stride) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
long j = (long)i * stride % n; // wrap so every stride touches n elements
if (i < n) out[i] = in[j];
}
int main() {
const int n = 1 << 25; // 32M floats, 128 MB
float *in, *out; cudaMalloc(&in, n * 4); cudaMalloc(&out, n * 4);
cudaEvent_t s, e; cudaEventCreate(&s); cudaEventCreate(&e);
for (int stride : {1, 2, 4, 8, 16, 32}) {
copy_strided<<<n / 256, 256>>>(in, out, n, stride);
cudaEventRecord(s);
for (int r = 0; r < 10; r++) copy_strided<<<n / 256, 256>>>(in, out, n, stride);
cudaEventRecord(e); cudaEventSynchronize(e);
float ms; cudaEventElapsedTime(&ms, s, e); ms /= 10;
printf("stride %2d: %.3f ms, useful %.0f GB/s\n", stride, ms, 2.0 * n * 4 / ms / 1e6);
}
}The writes are always coalesced here; only the reads are strided. What the sector model predicts for the reads alone: stride 1 at full efficiency, stride 2 at half, stride 4 at a quarter, stride 8 and above at an eighth. The measured slowdown is usually a bit smaller than that, because the writes stay efficient and the L2 cache sometimes catches a reused sector. Run it and see how close your T4 gets to the prediction.
Pro content
Keep reading with Inference Academy Pro
Unlock the rest of this lesson, including 1 animated diagram, plus every advanced lesson, hands-on lab, the larger playground models and completion certificates.
Still to come in this lesson
- Vectorised loads
- Transpose: the classic problem
- How fast should each version be?
- Where this shows up in inference
- What you can now do
- Common mistakes
- Exercise
- Going deeper
You will be able to answer
- 32 threads in a warp each load one 4-byte float at consecutive addresses. How many 32-byte sectors does that take?
- The same warp reads every second float (stride 2). What fraction of the fetched bytes is used?
- Why is a naive matrix transpose slow even though it reads each element once?
- Why does the shared-memory transpose tile use [32][33] instead of [32][32]?
- What does loading with float4 instead of float change?