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
include/cutlass/layout/matrix.h:58
Common Matrix Layouts
Row-Major Layout
Elements in the same row are contiguous in memory:Column-Major Layout
Elements in the same column are contiguous in memory:include/cutlass/layout/matrix.h:150
Row-Major vs Column-Major Performance
Row-Major vs Column-Major Performance
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)
- Iterating over columns
- Matrix-vector multiply:
y = A^T * x(A column-major) - BLAS library compatibility
Tensor Layouts in CuTe
CuTe provides a more general layout representation: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
Tensor Core Layouts
Tensor Cores require specific layouts for input fragments:Fragment Layouts
Data is distributed across threads in a warp: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:Common Layout Transformations
Common Layout Transformations
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
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