Optimize global memory access

From tensormux/kernel-skills · View interactive version

Optimize global memory access is an AI agent skill from the GitHub repository tensormux/kernel-skills. Guide the agent through diagnosing and restructuring CUDA global memory access patterns to maximize effective memory bandwidth, covering coalescing requirements, vectorized loads, AoS vs SoA layouts,

How to use this skill

Skill file

Skill: Optimize Global Memory Access

Purpose

Guide the agent through diagnosing and restructuring CUDA global memory access patterns to maximize effective memory bandwidth, covering coalescing requirements, vectorized loads, AoS vs SoA layouts, shared memory staging for non-coalesced patterns, and L2 cache behavior.

Use this when

Do not use this when

Inputs the agent should gather first

Required reasoning process

  1. Classify the access pattern. For each global memory access in the kernel, determine the access pattern for a warp of 32 consecutive threads:
  1. Estimate the access efficiency. For each access:

Use this estimate to prioritize which access to fix first.

  1. Determine if vectorized loads are applicable. Vectorized loads (float4, int4, uint4, half2, half4 via float2) load multiple elements per instruction and reduce the total instruction count:

Vectorized loads are beneficial only when the access is already coalesced (stride-1). Applying vectorized loads to strided accesses does not help and may hurt (the extra loaded elements are wasted).

Check: is ptr + thread_offset guaranteed to be 16-byte aligned for float4? The base pointer alignment and the per-thread offset must both be multiples of 16 bytes for all threads.

  1. For strided or transposed accesses: decide on shared memory staging. The standard pattern: load the tile into shared memory collaboratively with coalesced loads (threads read consecutive global addresses), then access shared memory in any pattern needed for the computation. Shared memory accesses are not subject to the DRAM coalescing requirement. Cost: extra smem capacity and an extra __syncthreads(). Benefit: converts strided global accesses into smem accesses, which have ~100x lower latency and are not bandwidth-limited by the same constraints.
  1. Evaluate layout restructuring (AoS → SoA). Array-of-Structures (AoS) layout stores each element's fields contiguously: [x0, y0, z0, x1, y1, z1, ...]. When a kernel reads only the x fields, consecutive threads access addresses separated by sizeof(struct) — this is strided by the structure size. Structure-of-Arrays (SoA) layout stores all x values contiguously: [x0, x1, x2, ..., y0, y1, y2, ...]. Reading x fields is now stride-1. If most kernels read all fields simultaneously, AoS may be fine (one cache line fetch per element). If most kernels read one or two fields, SoA is strongly preferred.
  1. Evaluate L2 cache reuse. If the same data is read by multiple kernels or multiple passes in the same kernel, the L2 cache may serve subsequent reads. On A100, L2 is 40 MB; on H100, 50 MB. For working sets that fit in L2, the effective bandwidth is much higher than DRAM bandwidth. Use cudaMemAdvise or cudaStreamAttrValue with L2 cache persistence policies (SM80+) to retain frequently reused data.
  1. For read-only data: use __ldg() or const __restrict__ pointers. __ldg() routes the load through the read-only L1 cache (texture cache), which is separate from the regular L1 and provides an additional caching layer for read-only data. On SM35+, this is equivalent to a texture cache load. Declare pointers as const T* __restrict__ to enable the compiler to use this cache automatically in some cases.
  1. For streaming access (no reuse): consider bypassing L1. For data that is read exactly once (e.g., a large input tensor being processed once), loading through L1 wastes L1 capacity on data that will not be reused. Use __ldg() with a streaming hint or load via a non-caching path if the access pattern is known to be streaming. This is a micro-optimization; verify with profiling before applying.

Kernel design rules

Correctness requirements

Performance requirements

Output format

The final response must include:

  1. Access pattern audit: for each global memory access in the kernel, classify it (coalesced/strided/transposed/broadcast/random) and quantify the efficiency loss.
  2. Prioritized fix list: ordered by estimated bandwidth gain. Focus on the highest-impact access patterns first.
  3. Restructured code: for each fix, show the before and after code with explanation.
  4. Alignment verification: for any vectorized load, show the alignment analysis.
  5. Expected improvement: estimated bandwidth gain (not guaranteed speedup — state that profiling is required to confirm).
  6. Layout recommendation (if applicable): AoS vs SoA analysis for the data structure.

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.