The Cache Hierarchy

Nowadays, processors are determined by the speed of their cores. However, in practice, their performance is often limited by the speed of data access. The problem is that for computation, data must be present in the processor’s registers, but they are initially located in some much lower layer of the memory hierarchy. Computer memory, which is used in modern processors, is organized in layers. Processors have registers, then the levels of caches (L1, L2, and L3), main memory, and finally, disks. Basically, the higher the level, the more significant storage capacity it has, but the slower it is. The same trend is observed for prices – the higher the level is, the more expensive the storage per gigabyte is. However, caches solve the problem of proximity of the last level, since they allow storing the last accessed data close to the processor. 

The ability to store data in such places is due to the fact that programs have spatial and temporal data locality. Temporal locality means that recently accessed data is more likely to be accessed in the near future. Spatial locality implies that if a program accesses one memory location, then it is more probable that it will access the nearby memory locations in the near future. These two effects allow the processor to temporarily store the data being processed in a fast cache. When a processor reads data from main memory, it stores not only the requested data but also the adjacent memory blocks in the cache. Therefore, while the program operates with these data, it can get them from much faster cache memory. 

When the data is present in the proper cache, the processor suffers a cache hit and proceeds to process the data with little delay. If the data is missing, a cache miss occurs. The processor discards the request and moves on to the lower level searching for the appropriate cache line. It is possible that the data is not in the L1 cache but might be in the L2 or L3 if it is available. Otherwise, the request is routed towards the main memory. Each step adds to the penalty incurred during a cache miss. The time a program takes to execute depends on how many operations it performs and how much it misses and what the miss penalties are.

The cache misses are generally categorized into three types. A compulsory miss occurs when there is no data in the cache since it is being accessed for the first time. The capacity miss occurs when the program requires more data than the number of sets that can be accommodated in the cache. Lastly, there are conflict misses which happen when multiple different data blocks compete for the same cache sets. Capacity and conflict misses are reduced by having a bigger cache or better cache organization while reducing the penalty associated with compulsory misses is done by using prefetching. 

The memory organization also plays a significant role in the locality of references. In arrays, the elements are stored in consecutive memory locations allowing the cache to hold more elements within a line. The CPU is also able to recognize this pattern and prefetch the next elements before they are explicitly needed in the program. Other data structures such as linked lists are usually implemented by having pointers to memory addresses that are scattered throughout the system. Accessing one node does not ensure that the next nodes are near the current memory address since every pointer has its own unique address. This might lead to several levels of cache misses and extra memory traffic. Linked lists are suitable for cases where there is a frequent need to insert or delete elements from the middle of the data structure since arrays require much more effort to perform these operations. Generally, all data structures should be selected based on the access patterns that a program would be using. Arrays would be much easier to implement in a case where the program always accesses contiguous elements. 

The same observation can be made in the order of the indices of loops. Row-major ordering stores the elements of a 2D array in consecutive storage by rows while the column-major ordering stores the elements by columns. An algorithm that processes the array elements in one order will experience high cache misses when executed from another order. The memory addresses of the elements accessed from the same row or column are contiguous. This property is not present at all when the elements of a row-major order are accessed in the column order or vice versa. In all numerical algorithms that access arrays, images, matrices, and simulations, it becomes vital to ensure that the data accessed fits within the same cache line and is reused as much as possible. One can decide to change the order of loops so that the next elements accessed by the loop are also in the same cache line. It is usually not enough to just perform more arithmetic operations since the program may still have to read the same data multiple times from the main memory. Changing the loop ordering to improve locality can cause a significant improvement in the performance of a program. This change would not be reflected in the abstract algorithm and might seem redundant but is very important in practice. For large data sets, blocking or tiling is usually used to increase data locality. 

The cache hierarchy is a big reminder that making a computer faster is not as simple as increasing the clock speed of the processor. If the CPU needs data as fast or faster than it can be fetched from the main memory, the extra computational power is wasted. CPU registers, L1, L2, and L3 caches reduce this penalty by keeping more data closer to the CPU while still allowing the main memory to provide a larger pool of data for processing. Disks provide even more storage than the main memory while also being persistent, but the price to pay is extremely long access times. It is not usually possible to store the data sets in the CPU registers, caches, or main memory when executing most applications. A good application would, therefore, try to keep as much data in the CPU as possible. This is done

when the application makes frequent accesses to the same set of data and ensures that the data is arranged contiguously and reused when possible. These optimizations are usually not seen in the final application but are vital in making the program fast. The locality of data access determines how much an application will spend accessing memory instead of doing useful computations.


Bibliography 

  • Princeton University, “Memory Hierarchy” 

  • James Demmel, UC Berkeley, “Memory Hierarchies” 

  • David Patterson, UC Berkeley, “Cache Memory” 

  • MIT OpenCourseWare, “Introduction and Matrix Multiplication” 

  • Cornell University, “Systems Architecture” 

  • Alan J. Smith, “Cache Memories” 

  • Lam, Rothberg, and Wolf, “The Cache Performance and Optimizations of Blocked Algorithms” 

  • Goto and van de Geijn, “Anatomy of High-Performance Matrix Multiplication” Frigo et al., “Cache-Oblivious Algorithms” 

  • Norman Jouppi, “Improving Direct-Mapped Cache Performance” 

  • Mark D. Hill, “Aspects of Cache Memory and Instruction Buffer Performance” Cornell University, “Code Optimisation and Blocking”


Next
Next

NLP Stock Exchange