Skip to main content

Memory Layouts

Memory layout is critical for GPU performance. This page explains how CUTLASS represents and optimizes data layouts for different memory hierarchies and access patterns.

Why Layouts Matter

GPU performance depends heavily on memory access patterns:
  • Coalesced Access: Threads in a warp accessing consecutive memory locations
  • Bank Conflicts: Multiple threads accessing the same shared memory bank
  • Cache Utilization: Data reuse within L1/L2 caches
  • Vectorization: Loading/storing multiple elements per instruction
Performance Impact: Optimal layouts can provide 10-100× speedup compared to naive implementations!

Layout Fundamentals

A layout is a mathematical function mapping logical coordinates to linear memory offsets.

Definition

In CUTLASS/CuTe, a layout consists of:
  • Shape: Logical dimensions (e.g., (M, N) for a matrix)
  • Stride: Memory offset between elements in each dimension
Reference: include/cutlass/layout/matrix.h:58

Common Matrix Layouts

Row-Major Layout

Elements in the same row are contiguous in memory:
Use Case: Default for C/C++ multi-dimensional arrays

Column-Major Layout

Elements in the same column are contiguous in memory:
Use Case: Fortran, cuBLAS, optimal for column-wise operations Reference: include/cutlass/layout/matrix.h:150
Neither layout is universally better - it depends on access patterns:Row-Major Wins:
  • Iterating over rows
  • Matrix-vector multiply: y = A * x (A row-major)
Column-Major Wins:
  • Iterating over columns
  • Matrix-vector multiply: y = A^T * x (A column-major)
  • BLAS library compatibility
CUTLASS: Supports both and converts between them efficiently

Tensor Layouts in CuTe

CuTe provides a more general layout representation:
Reference: include/cute/layout.hpp:98

Hierarchical Layouts

CuTe supports nested layouts for expressing complex patterns:

Swizzled Layouts

Swizzling reorders data to avoid shared memory bank conflicts:

Bank Conflict Problem

Shared memory is organized into banks (32 on modern GPUs):

Swizzle Functions

CUTLASS automatically applies appropriate swizzling for shared memory layouts. You rarely need to implement swizzling manually!

Tensor Core Layouts

Tensor Cores require specific layouts for input fragments:

Fragment Layouts

Data is distributed across threads in a warp:
CUTLASS handles this automatically:

TiledMMA Layouts

Combine multiple MMA atoms into larger tile layouts:

Layout Transformations

CuTe provides powerful layout manipulation operations:

Logical Divide

Split a layout into hierarchical tiles:

Composition

Compose two layouts:

Complement

Find the “unused” dimensions:

Coalesce

Simplify nested layouts:

Pitched Linear Layout

For non-matrix data (e.g., images):

Alignment Requirements

GPU memory operations have alignment requirements:

Vectorized Access

Global Memory

Shared Memory

CUTLASS templates automatically handle alignment. Use cutlass::AlignedBuffer for manual allocations:

Layout Examples

Example 1: Shared Memory Layout

Example 2: Thread Partitioning

Example 3: Tensor Core Layout

Performance Best Practices

Layout Optimization Checklist:
  1. Global Memory:
    • ✓ Use coalesced access patterns (adjacent threads → adjacent memory)
    • ✓ Align pointers to 128 bits (16 bytes)
    • ✓ Vectorize loads/stores (128-bit preferred)
  2. Shared Memory:
    • ✓ Apply swizzling to avoid bank conflicts
    • ✓ Pad dimensions to avoid strided access
    • ✓ Reuse data across thread iterations
  3. Registers:
    • ✓ Minimize register pressure (allow more occupancy)
    • ✓ Use correct fragment layouts for Tensor Cores
    • ✓ Prefer compile-time known indices
  4. CuTe Layouts:
    • ✓ Use static shapes when possible (better optimization)
    • ✓ Let CuTe handle index computation (fewer bugs)
    • ✓ Print layouts during development for debugging

Debugging Layouts

Advanced: TMA Layouts (SM90+)

Tensor Memory Accelerator requires special descriptor layouts:

Next Steps

CuTe Library

Deep dive into CuTe layout algebra

Tensor Cores

Understand Tensor Core fragment layouts

GEMM Operations

Apply layouts to GEMM kernels

Examples

See layouts in real code