Spatial Locality: The Core Driver of Loop Interchange Gains
Loop interchange's effectiveness stems from enhancing spatial locality, ensuring that sequentially accessed data elements are stored contiguously in memory, thus efficiently filling cache lines and drastically reducing cache misses.
The top 3
- Cache Lines: The Key to Spatial Locality Gains: Modern CPUs fetch data in fixed-size blocks called cache lines, typically 64 bytes. When a program requests a single byte, the entire cache line containing it is loaded, exploiting spatial locality by bringing nearby data into the fast cache for 'free' subsequent access.
- Cache Miss Penalty: Up to 100x Slower Without Spatial Locality: The performance difference between accessing data from cache (a 'hit') and fetching it from main memory (a 'miss') can be 10-100 times, with main memory access taking 60-100 nanoseconds compared to less than a nanosecond for CPU instructions. Optimizing spatial locality drastically reduces these costly cache misses.
- 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.
Sources
Open the full topic