GPU GEMM Speedup Limits and Tiled Memory Strategies
System DesignModel answerLast updated
Apply Amdahl's law to a partially accelerated simulation, then explain GPU tiled matmul and the cost of virtual dispatch.
A C++ simulation library spends 80% of its wall-clock time in a CPU matrix-multiply (GEMM) routine and 20% in serial bookkeeping that resists parallelization. You are asked to move the GEMM onto a GPU.
Address three things:
-
Quantitative ceiling. With the GEMM portion accelerated 50x on the GPU, what overall speedup do you get? What if the kernel were instead 500x faster? What is the absolute limit, and what does that imply about where to invest engineering effort next?
-
Why naive GPU GEMM underperforms, and how tiling fixes it. Explain the memory-access behavior of a straightforward one-thread-per-output kernel. Then describe a tiled implementation: what gets staged where, why shared memory helps, how the load pattern changes, and what synchronization and hazards you must handle. Mention edge cases (dimensions not divisible by the tile size, bank conflicts).
-
Virtual dispatch. Your library exposes an abstract kernel interface so the caller can pick a CPU or GPU backend at runtime. Explain what a
virtualcall costs in terms of branch prediction, pipelining and inlining — and at what call granularity that cost stops mattering.
Hints
Hint 1
Before computing anything, ask which fraction of the runtime can even be touched, and bound the answer by that fraction alone.
Hint 2
A 10x better kernel on a portion that's already small barely moves the total — compute the fraction after the first port before proposing more kernel work.
Hint 3
Arithmetic intensity (FLOP per byte moved) is the number you want: compare the naive kernel's value against the machine's FLOP/byte ratio to see whether it is compute- or memory-bound.
Hint 4
Shared memory buys reuse; the second barrier is what prevents a block from overwriting a tile another warp is still reading.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
1. The ceiling comes from the serial fraction
Use Amdahl's law in the form
total speedup = 1 / ((1 - f) + f / s)
where f is the accelerated fraction (0.8) and s is the local speedup of that fraction.
s = 50:1 / (0.2 + 0.8/50) = 1 / (0.2 + 0.016) = 1/0.216 ≈ 4.63xs = 500:1 / (0.2 + 0.8/500) = 1 / (0.2 + 0.0016) = 1/0.2016 ≈ 4.96xs → ∞:1/0.2 = 5x
So going from a 50x kernel to a 500x kernel — ten times the kernel effort — moves the end-to-end number from 4.63x to 4.96x, roughly a 7% gain. After the first port, GEMM is about 0.016/0.216 ≈ 7% of runtime and the non-parallelizable bookkeeping is about 93%.
The right conclusion is not "tune the kernel harder." It is to re-profile and attack the 20%: overlap orchestration with GPU work via CUDA streams, push part of it onto the device, and — often forgotten — count host↔device copies as serial time, since they are typically synchronous and can dominate the 20% after acceleration.
2. Naive GEMM and the arithmetic-intensity argument
The textbook kernel assigns one thread per output element and accumulates over k:
float acc = 0.f;
for (int k = 0; k < N; ++k)
acc += A[row*N + k] * B[k*N + col]; // two global loads per FMA
C[row*N + col] = acc;
Each fused multiply-add costs 2 FLOPs and 8 bytes of global traffic, so arithmetic intensity is about 0.25 FLOP/byte. A GPU with a few TB/s of HBM bandwidth and tens of FP32 TFLOPS needs on the order of 10 FLOP/byte to become compute-bound. This kernel is firmly memory-bound, and worse, it issues 2N³ loads when only 2N² distinct values exist — every element of A is re-fetched by each thread in its output row, and L2 catches only some of that. Latency makes it worse: a global access costs hundreds of cycles, and without reuse warps simply stall.
3. Tiling
Stage T×T sub-blocks of A and B in shared memory (on-chip, per-SM, tens of cycles vs. hundreds for HBM), and have each block accumulate one T×T tile of C, walking the K dimension in steps of T:
template <int T>
__global__ void sgemm_tiled(const float* A, const float* B, float* C, int N) {
__shared__ float As[T][T];
__shared__ float Bs[T][T + 1]; // padding breaks bank conflicts
int row = blockIdx.y*T + threadIdx.y;
int col = blockIdx.x*T + threadIdx.x;
float acc = 0.f;
for (int t = 0; t < (N + T - 1)/T; ++t) {
int ak = t*T + threadIdx.x, bk = t*T + threadIdx.y;
As[threadIdx.y][threadIdx.x] = (row < N && ak < N) ? A[row*N + ak] : 0.f;
Bs[threadIdx.y][threadIdx.x] = (bk < N && col < N) ? B[bk*N + col] : 0.f;
__syncthreads();
#pragma unroll
for (int k = 0; k < T; ++k) acc += As[threadIdx.y][k] * Bs[k][threadIdx.x];
__syncthreads();
}
if (row < N && col < N) C[row*N + col] = acc;
}
Two properties matter. First, adjacent threads read adjacent addresses, so global loads coalesce into wide transactions. Second, each value loaded into shared memory is reused T times, cutting global traffic from 2N³ to about 2N³/T; with T = 32 that lifts intensity to roughly 8 FLOP/byte. The two barriers are both required — the first prevents reading a half-filled tile, the second prevents a fast warp from overwriting a tile a slow warp is still consuming. Missing the second barrier is a classic race that passes small tests and fails under load.
Production libraries (CUTLASS, cuBLAS) layer on register tiling — each thread computes an 8×8 micro-tile so results stay in registers — plus vectorized float4 loads and asynchronous double buffering (cp.async/TMA) that prefetches the next tile while the current one computes.
Edge cases to name: dimensions not divisible by T (the zero-fill guards above), bank conflicts when a warp's shared-memory accesses land in the same bank (hence the +1 padding), and the occupancy trade-off — a 64 KB tile on an SM with ~228 KB shared memory permits only three resident blocks, yet tuned GEMMs often run at 25–50% occupancy and hide latency with ILP and prefetch.
Finally, the design judgment: use larger tiles for reuse, but tune against the actual FLOP/byte ratio; don't assume more occupancy is always better.
4. Virtual dispatch
A virtual call costs three things. It performs two dependent loads (vptr, then the vtable slot), which can miss. The indirect branch may be mispredicted — cheap at a monomorphic call site, expensive (pipeline flush, ~15–20 cycles) when the target varies. The largest cost is that the compiler loses visibility: no inlining, so no constant propagation of sizes, no hoisting of loop invariants, no auto-vectorization across the boundary, and the call is an alias-analysis barrier.
The decisive factor is granularity. Dispatching once per GEMM call — kernel->gemm(A, B, C) — costs nanoseconds against microseconds or milliseconds of work; that is a clean and correct design. Dispatching per element or per tile in an inner loop is a genuine performance bug. Two refinements: mark implementations final so the compiler can devirtualize when the dynamic type is known, and remember that host-side vtables cannot be used in device code (vtable pointers are host addresses), so device-side polymorphism must be resolved at compile time.