Unit 2 of 11 · about 5 hours · implement, measure

Transpose: coalescing, shared-memory tiles and bank conflicts

Transpose is the smallest kernel where layout decides bandwidth. The tile, barrier and padding pattern repeats in GEMM and attention.

You'll be able to

  • Predict requested transactions for strided reads and writes of a transpose.
  • Implement copy, naive and padded shared-memory tiled transpose with correct odd-shape guards.
  • Diagnose a 32-way bank conflict and apply padding, showing measured bandwidth against the copy ceiling.

Covers

Naive matrix transpose and requested memory transactionsShared-memory transpose tiling and coalesced reads and writesBank-conflict diagnosis and padding or swizzle fixes
1

Read

In this order, only the parts listed

0 of 3 done

  1. Copy, naive and coalesced transposeEfficient Matrix Transpose in CUDA C/C++

    Simple Matrix Copy; Naive Matrix Transpose, with the 1024-element (4096-byte) write stride on the 1024x1024 test; Coalesced Transpose Via Shared Memory, with TILE_DIM=32, BLOCK_ROWS=8 and 4 elements per thread

    The M2050 and K20c GB/s numbers are 2013 hardware. Reproduce the ranking on your own GPU, not the values.

  2. Bank conflicts and the padding fixEfficient Matrix Transpose in CUDA C/C++

    Shared Memory Bank Conflicts through the padded tile[TILE_DIM][TILE_DIM+1] variant, including the copySharedMem control experiment

  3. Transpose worked example and paddingCUDA C++ Programming Guide (v13.4.2)

    Sections 2.3.4.1.1 (naive transpose with uncoalesced writes), 2.3.4.2.1 (shared-memory transpose) and 2.3.4.2.2 (32-way column conflict, fixed by declaring 32x33 shared memory)

2

Focus on

  • Naive transpose reads coalesced but writes with a stride of the matrix width. On the blog's 1024x1024 test that is 4096 bytes between consecutive threads.
  • A tile[32][32] of floats read down one column maps every element to the same bank: a 32-way conflict. With tile[32][33], element row of column sits in bank , so a column access is conflict-free.
  • The copy kernel is the ceiling: it moves the same twice-the-matrix traffic with ideal access. A transpose near copy bandwidth is done.
  • Fewer threads than tile elements (a block for a tile) amortizes index math, with 4 elements per thread.
Common mistakes · 3
  • Trusting a bandwidth number above the DRAM peak. It usually means the grid covers only part of the matrix or the byte count is wrong, so check coverage first, then compare against the copy kernel.
  • Reading the blog's M2050 and K20c GB/s values as targets. Reproduce the ranking of copy, tiled and naive, not decade-old values.
  • Swapping block indices without swapping tile offsets writes each tile to the wrong place while staying in bounds. Zeros cannot catch this; an index-pattern input where element equals row times width plus column can.
3

Try it yourself

Closed-book, before checking anything

Rebuild copy, naive and padded-tiled transpose for 4096x4096 fp32 random data and time each with events, median of 11 trials. On an RTX 3050 6 GB with CUDA 13.4 the copy reached 152 GB/s, naive 48 GB/s and tiled 151 GB/s, counting one read and one write per element, and the tiled output matched a CPU transpose exactly, including a 1000x1000 check of the edge guards. Expect the same ranking on your GPU, with tiled near copy; your GB/s values will differ, so compare each kernel to your own measured copy bandwidth.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.On a 1024x1024 fp32 matrix, what stride separates consecutive threads' writes in the naive transpose?Answer

    1024 elements, or 4096 bytes. Each warp's 32 writes land in 32 different segments.

  2. 2.A warp reads one column of a tile[32][32] float array. Which banks, and what changes with tile[32][33]?Answer

    All 32 requests hit one bank: a 32-way conflict. With the +1 padding, element of column sits in bank , so the 32 requests hit 32 distinct banks.

  3. 3.The tiled kernel matches copy bandwidth on square shapes but fails on width 1000. What is the likely cause?Answer

    Missing bounds guards for nonmultiple dimensions. Guard both the load loop and the store loop, since a partial edge tile underfills both.

  4. 4.Why is the copy kernel a better ceiling than the vendor peak bandwidth?Answer

    Copy moves the same read-plus-write traffic with ideal access on your GPU, clocks and power state. It measures what is achievable here, not a datasheet bound.

5

Practice

  • LeetGPU challenge 3: Matrix Transpose

    Challenge 3: solve(input, output, rows, cols) over fp32, with functional cases from 1x1 and 1x4 through 8192x8192 and a performance case of 7000 rows by 6000 columns

    Run it on LeetGPU or locally: build your solve with nvcc -shared -Xcompiler -fPIC, load it with ctypes, and check it against reference_impl on the cases from generate_functional_test and generate_performance_test in the challenge's challenge.py (public in AlphaGPU/leetgpu-challenges). That file imports core.challenge_base, which the repository does not ship; a two-line stub class with device = "cuda" is enough. Keep your own source, and add a nonmultiple shape beyond the public cases.

You're done when

Your tiled transpose matches the reference on square, tall, wide and odd shapes and reaches at least 0.9 times your measured copy bandwidth on 4096x4096.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 9 more in the section
  • Efficient Matrix Transpose in CUDA C/C++NVIDIA · required

    Rebuild copy, naive transpose, coalesced transpose and padded shared-memory variants. Update deprecated details for the installed toolkit and compare requested transactions, bank behavior and achieved bandwidth on odd as well as friendly shapes.

  • CUDA C++ Programming Guide (v13.4.2)NVIDIA · reference

    Use Section 2.3.4 for coalesced access, the transpose example and bank-conflict padding; Section 2.5 for streams, events, synchronization and the stream-capture intro; and Section 4.2 for graph creation, update and capture constraints. This is the API reference beside the book chapters.

Also in this section

  • Programming Massively Parallel Processors, 4th edition - Chapters 6 and 9 to 12Wen-mei Hwu, David Kirk and Izzat El Hajj · primary

    Read thread coarsening in Chapter 6, then Chapters 9 (histogram), 10 (reduction), 11 (prefix sum) and 12 (merge, optional). Work the exercises that test work, depth, contention and boundary handling.

  • AI Systems Performance Engineering - Chapters 11 and 12Chris Fregly (O'Reilly) · required

    Read Chapter 11 on streams, events, inter-kernel pipelining and stream-ordered allocation, and Chapter 12 on CUDA graphs and dynamic scheduling. Skip device-initiated launch beyond the vocabulary.

  • Optimizing Parallel Reduction in CUDANVIDIA · required

    Implement the optimization sequence from interleaved addressing through sequential addressing, first-add-during-load, unrolling and warp-level completion. Correct outdated assumptions and retain the stage where each optimization stops helping.

  • Single-pass Parallel Prefix Scan with Decoupled Look-backNVIDIA Research · depth

    Read after two-pass scans pass. Derive the state and progress argument, then implement or inspect a bounded version only if the available hardware and time support meaningful comparison.

  • Online Normalizer Calculation for SoftmaxOnline Softmax Authors · required

    Derive the running maximum and normalizer recurrence, test extreme and all-masked rows, and compare materialized, two-pass and online implementations before using the recurrence inside attention.

  • LeetGPU - CUDA Primitive ChallengesAlphaGPU · required

    Solve challenge 3 Matrix Transpose, 4 Reduction, 5 Softmax, 13 Histogramming and 16 Prefix Sum in CUDA. Add 70 Segmented Prefix Sum or 72 Stream Compaction only after the required set. Keep local source, hidden-shape assumptions, hardware, raw timings and correctness evidence outside the leaderboard.

  • GPU MODE - Reductions and ScansGPU MODE · support

    Use Lectures 9, 20, 21 and 24 after the PMPP implementations. Reproduce one faster scan or reduction idea and show the exact shape and device regime where it changes the bottleneck.

  • MIT 6.S894 Accelerated Computing (Fall 2025) - Lectures 6, 7 and 9 and Lab 7MIT · support

    Use Lecture 6 for host-device copies and streams, and Lectures 7 and 9 for map, stencil, reduce, compaction, scan, segmented scan, histogram and merge. Do Lab 7 (Run-Length Compression, with its scan and shuffle warm-ups) built for your own GPU.

  • GPU Microbenchmark and Kernel Suite - CUDA CoreRoadmap-Final · support

    Complete the CUDA vector map, reduction, transpose and stable softmax requirements plus seeded access, synchronization, mask, stream and numerical failures. Preserve library and framework baselines.