Unit 2 of 10 · about 4 hours · implement
Shared-memory tiling and boundary predication
Shared memory turns repeated global loads into block-local reuse. Boundary handling is part of the algorithm, not a cleanup after tuning.
You'll be able to
- Implement a shared-memory GEMM with arbitrary rectangular dimensions and zero-filled reduction tails.
- Prove tile ownership and the barriers needed before consumption and before overwrite.
- Derive per-tile input reuse and distinguish useful work from padded work.
Covers
Read
In this order, only the parts listed
0 of 3 done
- Shared-memory cache blockingHow to Optimize a CUDA Matmul Kernel for cuBLAS-like Performance: a Worklog
Kernel 3: Shared Memory Cache-Blocking, through its two-barrier tile loop
Stop before Occupancy Calculation for Kernel 3. The example omits arbitrary-size checks; add masks rather than copying that restriction into your kernel.
- Predicated tile loadsAI Systems Performance Engineering companion code - Chapters 9 and 10
matmul_tiled, at commitb57e4c6This is a shared-memory tiled kernel with one output per thread, despite the file's register-tiled description. Extend its square-only interface to rectangular shapes.
- Coordinates for edge predicatesCuTe GEMM Tutorial
Predication: What to do when tiling isn't perfect; identity-tensor procedure, GEMM epilogue example and input-load predicates, documentation dated September 21, 2026
The final input-copy example handles the output dimensions only. Add reduction-tail handling and initialize masked shared-memory slots before using this pattern for arbitrary GEMM.
Focus on
- For an interior output tile and reduction tile , load input elements for useful FLOPs. Output traffic is separate.
- Every participating thread reaches
__syncthreads(), even if its output lies outside the matrix. Predicate loads and stores, not the block's synchronization. - A masked load must write zero into its shared-memory slot. Leaving a previous tile's value there adds a stale contribution to the reduction.
Common mistakes · 3
- Returning an edge thread before a block barrier. It may still own an input needed by an interior output.
- Using just an output mask. Input loads need separate bounds on their row, column and reduction coordinates.
- Removing the second barrier because the output accumulator is private. The shared input buffers are not private.
Try it yourself
Closed-book, before checking anything
Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.How often is one interior tile element of reused?Answer
Across output columns in that block. One tile element of is reused across output rows.
2.Why is a barrier needed after computation as well as after loading?Answer
It prevents fast threads from overwriting shared inputs for the next tile while slower threads still consume the current tile.
3.What should an overwrite-only product do when the reduction dimension is zero?Answer
Write zero to every valid output. Empty output dimensions require no launch. General GEMM must instead honor its epilogue contract.
You're done when
Your rectangular kernel passes interior, reduction-tail and output-tail tests, and you can explain both barriers from buffer ownership.
Only when you can do this without notes.
Leads to
Notes
Resources · 3 for this unit, 9 more in the section
- AI Systems Performance Engineering companion code - Chapters 9 and 10Chris Fregly · required
Inspect and run compatible baseline and optimized paths from
code/ch09andcode/ch10. Verify whether each path is a custom kernel, compiler selection or library dispatch, then add an independent correctness and timing wrapper. - How to Optimize a CUDA Matmul Kernel for cuBLAS-like Performance: a WorklogSimon Boehm · support
Read kernels 1 to 6, 9 and 10 with the companion repository. Its numbers come from an RTX A6000 (Ampere); remeasure on your GPU before comparing your kernels with them.
- CuTe GEMM TutorialNVIDIA CUTLASS · required
Work through tiling, layouts, copy atoms and MMA atoms. Draw the logical and physical ownership for one tile, then modify a shape or layout and verify both generated mapping and numerical output.
Also in this section
- AI Systems Performance Engineering - Chapters 9 and 10Chris Fregly · primary
Read Chapter 9 on arithmetic intensity, tiling and fusion, and Chapter 10 on intra-kernel pipelining, warp specialization and thread-block clusters. Separate what applies to every NVIDIA GPU from what needs Hopper or Blackwell.
- MIT 6.S894 Accelerated Computing (Fall 2025) - Lectures 6 and 8 and Labs 4 to 6MIT · required
Use Lecture 6 for tensor cores and arithmetic intensity and Lecture 8 (Evolving Ampere to Hopper) for the production GEMM schedule. Do Lab 4 (tiling and reuse), Lab 5 (scheduling and problem sizes) and Lab 6 (tensor cores through inline PTX) built for your own GPU; each harness checks results against bundled test data. Lab 6 needs sm_80 or newer.
- OCP 8-bit Floating Point SpecificationOpen Compute Project · required
Study E4M3 and E5M2 encodings, exceptional values and conversion behavior. Build value-boundary tests and separate format correctness from the scaling policy chosen by a framework or kernel.
- LeetGPU - GEMM and Quantized Matmul ChallengesAlphaGPU · required
Solve 2 Matrix Multiplication and 22 GEMM, then choose 30 Batched Matrix Multiplication or 57 FP16 Batched Matmul. Attempt 32 INT8 Quantized Matmul or 81 INT4 Matmul only after scale and accumulator tests pass. Preserve local baselines and shape matrices.
- GPU MODE - CUTLASS, Tensor Cores and CuTeGPU MODE · support
Use Lectures 15, 23, 36, 57, 86, 103 and 114 selectively. Pair each chosen lecture with one implemented layout, instruction or pipeline change and compare it against a vendor-library baseline.
- DeepGEMMDeepSeek · depth
After the portable GEMM passes, reconstruct one Hopper FP8 path from source, identify its hardware, layout and scaling assumptions, and test a bounded modification only on supported hardware. Do not generalize repository benchmarks to other shapes or GPUs.
- Modern GPU Programming for MLSysMLC · depth
Read the performance, data-layout, tensor-core, TMA, tensor-memory, barrier and GEMM chapters for a Blackwell-depth route. Execute only with sm_100a and the pinned TIRx toolchain; otherwise use it for derivation and code reading.
- CUDA 13.0 matrix programming and PTX referencesNVIDIA · reference
Coverage and correctness gap: the pool lacks a precise WMMA contract, and Lab 6 relies on TF32 conversion and register behavior the PTX ISA leaves unspecified. Use Programming Guide 10.24 and PTX ISA 9.0
mma.m16n8k8fragments,mmaandcvt, archived with CUDA 13.0.0; also consultcp.asynccompletion semantics. - TensorRT quantization schemes and workflowsNVIDIA · reference
Coverage gap: the existing pool does not derive scale granularity and reconstruction conventions precisely. Read INT8/FP8 equations, per-tensor/per-channel/block quantization and dynamic quantization in documentation dated October 5, 2026. Skip engine building and replace the block pseudocode's floor-divided scale count with ceiling division.