Optimize shared memory tiling

From wenyi-li/awesome-agent-kernel-skills · View interactive version

Optimize shared memory tiling is an AI agent skill from the GitHub repository wenyi-li/awesome-agent-kernel-skills. Guide the agent through designing and tuning shared memory tiling strategies for CUDA kernels, covering bank conflict analysis and elimination, tile shape selection, double buffering with async copy,

How to use this skill

Skill file

Skill: Optimize Shared Memory Tiling

Purpose

Guide the agent through designing and tuning shared memory tiling strategies for CUDA kernels, covering bank conflict analysis and elimination, tile shape selection, double buffering with async copy, occupancy tradeoffs from shared memory allocation, and the decision of when smem tiling is worth the complexity.

Use this when

Do not use this when

Inputs the agent should gather first

Required reasoning process

  1. Map each smem access to a bank. Bank index for a 4-byte access is (address_in_smem_bytes / 4) % 32. For an smem array T smem[rows][cols], element smem[r][c] maps to bank (r * cols + c) * sizeof(T) / 4 % 32 (assuming sizeof(T) is a multiple of 4; for fp16, adjust accordingly).
  1. Identify conflicting access patterns. A bank conflict occurs when multiple threads in the same warp access different addresses that map to the same bank in the same memory transaction. No conflict: all threads access the same bank (broadcast). No conflict: all threads access different banks. Conflict: k threads access k different addresses in the same bank (k-way conflict, requiring k serial transactions).
  1. Compute the conflict multiplier for the specific access pattern.

Work through the specific access pattern systematically rather than guessing.

  1. Apply smem padding to eliminate conflicts. The standard technique: add pad extra columns to the smem array, changing the stride from BK to BK + pad. This shifts the bank mapping for each row, breaking the periodic conflict pattern. For fp32 with 32-bank smem, adding 1 column of padding (4 bytes) shifts consecutive rows by 1 bank, typically eliminating 32-way column-access conflicts. For fp16, adding 2 columns of padding shifts by 1 bank (since 2 fp16 = 4 bytes = 1 bank). Choose the minimum pad that eliminates the conflict.
  1. Verify padding after application. Re-derive the bank mapping for the padded layout and verify that no two threads in a warp access the same bank. Walk through at least: warp accessing a row (consecutive K or N access) and warp accessing a column (strided K access). Confirm that the conflict multiplier is 1 after padding.
  1. Choose tile dimensions relative to warp size. The innermost loop access should map 32 consecutive threads to 32 different smem banks. This requires the innermost stride of the smem array to result in 32 distinct bank indices across 32 threads. For row-major smem with row stride of BK (or BN): if BK (or BN) >= 32 and the access is along the row direction, and dtype is fp32, consecutive threads access consecutive elements → consecutive banks → no conflict.
  1. Design double buffering with cp.async (SM80+). Allocate two smem buffers: smem_A[2][BM][BK+pad] and smem_B[2][BK][BN+pad]. Use buffer index ping = 0, pong = 1 alternating. While computing on smem_*[ping], issue async copies into smem_*[pong] using cp.async:

```

cp.async.cg.global.shared::cta [smem_ptr], [global_ptr], 16;

```

After issuing all copies for the next tile: cp.async.commit_group().

Before using smem_*[pong] for computation: cp.async.wait_group(1) (leaves at most 1 uncommitted group in flight, i.e., allows the previous group to still be pending but waits for all earlier groups). Then __syncthreads() to ensure all threads see the completed stores to smem.

  1. Estimate occupancy impact. Look up the smem capacity per SM for the target architecture:

Active blocks per SM = floor(smem_per_SM / smem_per_block). If this limits active blocks below the warp-level occupancy target, consider reducing tile size or using a different smem configuration. Request maximum smem with cudaFuncSetAttribute(kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, bytes).

  1. Consider L1 cache as an alternative to explicit smem. If the access pattern has low spatial reuse but high temporal reuse (the same addresses are re-read by different thread blocks), L2 cache may serve those reads without smem tiling. For access patterns where data is reused only within a single thread block execution, explicit smem tiling is essential. For access patterns with cross-block reuse, L2 is the relevant level.

Kernel design rules

Correctness requirements

Performance requirements

Output format

The final response must include:

  1. Bank conflict analysis: derive the bank mapping for each smem access in the inner loop. Show the calculation explicitly.
  2. Conflict resolution: specify the padding required (which dimension, how many elements). Verify the bank mapping after padding.
  3. Smem layout specification: exact array declarations with dimensions and padding. ASCII diagram of the layout if helpful.
  4. Tile size recommendation: BM, BN, BK (or equivalent), with justification based on occupancy and register budget.
  5. Double buffering implementation (if SM80+): smem buffer declarations, cp.async call pattern, fence placement, sync placement, buffer index management.
  6. Occupancy estimate: smem per block, active blocks per SM, theoretical occupancy percentage.
  7. Before/after comparison: show the smem access code before and after the optimization, with the specific change highlighted.

Common failure modes

Review checklist

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.