Spatial vs. Temporal Locality: Loop Interchange's Focus

Programs exhibit two types of locality: temporal (reusing the same data over time) and spatial (accessing nearby data in memory). Loop interchange primarily optimizes spatial locality by reordering nested loops to ensure consecutive memory access patterns, enhancing cache efficiency for data elements stored contiguously.

The top 3

  1. Loop Interchange's Core: Maximizing Spatial Locality: Loop interchange primarily optimizes spatial locality by reordering nested loops to access multi-dimensional array elements contiguously in memory, matching the system's memory layout (e.g., row-major). This ensures that when one data element is fetched into the cache, subsequent elements that are likely to be accessed are already present in the same cache line, minimizing cache misses.
  2. Spatial Locality Gains: Fewer Cache Misses, Faster Access: Exploiting spatial locality significantly reduces cache misses, leading to faster data retrieval and improved CPU utilization. When a CPU fetches a 64-byte cache line, it anticipates future accesses to adjacent memory locations. Efficient spatial locality ensures these anticipated accesses are cache hits, minimizing the hundreds of cycles it would take to fetch data from main memory.
  3. Temporal Locality Boost: Loop Tiling & Data Reuse: In contrast to loop interchange's spatial focus, loop tiling (or blocking) is a key optimization for temporal locality. It partitions loop iteration spaces into smaller 'tiles' that fit entirely within a cache level, maximizing the reuse of data already in cache before it's evicted. This strategy reduces the need to fetch the same data repeatedly from slower main memory.

Sources

Open the full topic