Abstract
While the cost of computation is an easy-to-understand local property, the cost of data movement on cached architectures depends on global state, does not compose, and is hard to predict. As a result, programmers often fail to consider the cost of data movement. Existing cache models and simulators provide the missing information but are computationally expensive. We present a lightweight cache model for fully associative caches with least recently used (LRU) replacement policy that gives fast and accurate results. We count the cache misses without explicit enumeration of all memory accesses by using symbolic counting techniques twice: 1) to derive the stack distance for each memory access and 2) to count the memory accesses with stack distance larger than the cache size. While this technique seems infeasible in theory, due to non-linearities after the first round of counting, we show that the counting problems are sufficiently linear in practice. Our cache model often computes the results within seconds and contrary to simulation the execution time is mostly problem size independent. Our evaluation measures modeling errors below 0.6% on real hardware. By providing accurate data placement information we enable memory hierarchy aware software development.
Introduction
In modern computing, the performance of an application is frequently dictated not by the speed of the processor, but by the efficiency of the memory hierarchy. The cost of moving data from main memory to the CPU often dwarfs the cost of the actual computation. However, while computation is easy to reason about locally, cache behavior depends on the "global state" of the program—the sequence and frequency of all memory accesses—making it notoriously hard to predict.
Historically, developers have relied on two types of tools to understand cache behavior. Trace-driven simulators, such as Dinero IV, are highly accurate but prohibitively slow for large-scale problems because they must enumerate every single memory access. Their execution time scales linearly with the problem size; if the data grows by a factor of 100, the simulation takes 100 times longer. On the other hand, analytical models are faster but often restricted to simple code structures or lack the precision required for real-world hardware tuning.
This paper introduces HayStack, a novel analytical cache model that bridges this gap. HayStack provides the speed of an analytical approach with the accuracy of a simulator, enabling cache analysis to become a viable part of the daily developer workflow rather than a slow, post-hoc optimization step.
The HayStack Approach: Symbolic Counting
The core innovation of HayStack is the use of symbolic counting to calculate "stack distance" (also known as reuse distance). In a fully associative cache using a Least Recently Used (LRU) replacement policy, the stack distance is defined as the number of unique memory locations accessed between two consecutive accesses to the same location.
As shown in Figure 3, an access results in a "cache hit" if its stack distance is less than or equal to the cache size. Instead of counting these accesses one by one, HayStack employs the Barvinok algorithm to derive mathematical formulas (quasi-polynomials) that represent the number of misses. This allows the model to "jump" straight to the answer without step-by-step simulation.
Overcoming Non-Linearity
A significant challenge in analytical modeling is handling non-linear operations common in real-world code, such as floor and modulo operations used to map array indices to cache lines. While symbolic counting of non-linear sets is theoretically NP-hard, the authors demonstrate that real-world patterns are "linear enough" in practice. They introduce three techniques to maintain tractability:
- Equalization: Simplifies formulas by identifying regions where floor expressions differ only by a constant.
- Rasterization: Replaces complex polynomials with simplified versions for specific cache line offsets.
- Partial Enumeration: A hybrid approach that explicitly counts only the "difficult" non-linear dimensions while counting the rest symbolically.
Evaluation and Results
The authors evaluated HayStack using the PolyBench 4.2.1-beta suite, which includes 30 kernels representing common scientific computing patterns. HayStack's predictions were compared against Dinero IV, PolyCache, and real hardware (Intel Xeon Gold 6150) using hardware performance counters.
Accuracy and Speed
HayStack achieved remarkable accuracy, with a geometric mean error of only 0.6% for L1 caches and 0.2% for L2 caches compared to real hardware measurements. Despite this high accuracy, it is significantly faster than traditional tools. HayStack showed a geometric mean speedup of 370x over Dinero IV for large problem sizes and 21x over PolyCache.
A key advantage of HayStack is its problem size independence. As illustrated in Figure 1, for a standard matrix multiplication (GEMM) kernel, HayStack’s execution time remains nearly flat even as the number of memory accesses grows from $10^6$ to $10^{10}$. In contrast, the simulator’s execution time grows exponentially, quickly becoming impractical for large datasets.
Conclusion and Future Work
HayStack demonstrates that it is possible to model complex cache behavior both accurately and efficiently. By providing fast, reliable data placement information, it enables memory-hierarchy-aware software development.
While currently limited to static control programs (where loop bounds are affine functions), the speed of HayStack opens new possibilities. The authors suggest that such a model could be integrated directly into compilers to perform automatic memory tuning—selecting optimal tile sizes for loops automatically without the need for time-consuming physical execution or slow simulations.
Summary updated