KatInterview

GPU Matrix Transpose: From Strided to Coalesced Memory Access

System DesignModel answer
MediumModel answerSystem DesignGPUmemory-coalescingparallel-computing

Last updated

Explain why a naive GPU matrix transpose causes uncoalesced memory traffic, then design a tiled shared-memory kernel that achieves coalesced reads and writes.

Background

A GPU executes threads in groups (warps or wavefronts, typically 32 or 64 lanes wide). Hardware coalesces memory requests from adjacent lanes into a single transaction only when those lanes access a contiguous, aligned block of addresses. When accesses are strided instead, each lane needs its own transaction, multiplying the effective memory bandwidth demand by the warp width.

A matrix transpose is a classic case where naively satisfying one side of the copy forces the other side to be strided.

Setup

You have a matrix A with R rows and C columns stored in row-major order in global memory. You want to produce matrix B with C rows and R columns, also in row-major, where:

B[col, row] = A[row, col]

A and B are separate allocations (no in-place requirement).

What You Are Expected To Do

  1. Analyze the naive kernel. Assign one thread to each output element: thread (tx, ty) writes B[tx, ty] = A[ty, tx]. Walk through which global-memory addresses adjacent lanes touch on the read side and the write side. Identify which side is coalesced and which is strided, and explain why that matters for throughput.

  2. Design a tiled kernel. Describe a kernel that uses a shared-memory staging tile to make both the global read and the global write coalesced. Cover:

    • How threads cooperatively load a tile from A into scratchpad
    • The synchronization needed before the transpose step
    • How threads cooperatively write the transposed tile to B
    • Why the write is now also coalesced
  3. Handle edge cases. R and C are not guaranteed to be multiples of the tile dimension. Explain how to handle partial tiles without out-of-bounds access.

  4. Address bank conflicts. Shared memory is divided into banks (typically 32 banks of 4-byte words on modern hardware). Describe when a conflict arises during the scratchpad read step and how a one-column padding trick resolves it.

  5. Sketch performance expectations. What roofline metrics would you check? What tile size is a reasonable starting point, and why?

No specific hardware ISA or SDK is required. Use pseudocode or CUDA-style syntax for any code snippets.

Hints

Hint 1

Lay out the naive thread-to-element mapping first, then write out the exact flat index each of the 32 adjacent lanes in a warp computes for the read and for the write. The pattern (contiguous vs. stride-R) becomes obvious from the arithmetic alone.

Hint 2

For the tiled design, think of the operation in two phases: a coalesced load phase where threads fill the shared tile row by row, and a coalesced store phase where they drain it column by row — the shared tile absorbs the transpose so neither global access is strided.

Hint 3

Bank conflicts happen when multiple threads in the same warp read from addresses that map to the same bank (address % 32 == same value). Padding the shared tile's leading dimension by 1 element shifts each row's starting bank, breaking the conflict pattern without changing correctness.