Cache-oblivious algorithm

Cache-oblivious algorithm

In computing, a cache-oblivious algorithm is an algorithm designed to exploit the CPU cache without having the size of the cache (or the length of the cache lines, etcetera) as an explicit parameter. An optimal cache-oblivious algorithm is a cache-oblivious algorithm that exploits the cache optimally (in an asymptotic sense, ignoring constant factors). Thus, a cache oblivious algorithm is designed to perform well, without modification, on multiple machines with different cache sizes, or for a memory hierarchy with different levels of cache having different sizes. The idea (and name) for cache-oblivious algorithms was conceived by Charles E. Leiserson as early as 1996 and first published by Harald Prokop in his master's thesis at the Massachusetts Institute of Technology in 1999.

Optimal cache-oblivious algorithms are known for the Cooley-Tukey FFT algorithm, matrix multiplication, sorting, matrix transposition, and several other problems. Because these algorithms are only optimal in an asymptotic sense (ignoring constant factors), further machine-specific tuning may be required to obtain nearly optimal performance in an absolute sense. The goal of cache-oblivious algorithms is to reduce the amount of such tuning that is required.

Typically, a cache-oblivious algorithm works by a recursive divide and conquer algorithm, where the problem is divided into smaller and smaller subproblems. Eventually, one reaches a subproblem size that fits into cache, regardless of the cache size. For example, an optimal cache-oblivious matrix multiplication is obtained by recursively dividing each matrix into four sub-matrices to be multiplied, multiplying the submatrices in a depth-first fashion.

Idealized cache model

Cache-oblivious algorithms are typically analyzed using an idealized model of the cache, sometimes called the cache-oblivious model. This model is much easier to analyze than a real cache's characteristics (which have complicated associativity, replacement policies, etcetera), but in many cases is provably within a constant factor of a more realistic cache's performance.

In particular, the cache-oblivious model is an abstract machine (i.e. a theoretical model of computation). It is similar to the RAM machine model which replaces the Turing machine's infinite tape with an infinite array. Each location within the array can be accessed in O(1) time, similar to the Random access memory on a real computer. Unlike the RAM machine model, it also introduces a cache: a second level of storage between the RAM and the CPU. The other differences between the two models are listed below. In the cache-oblivious model:

*Memory is broken into lines of L words each
*A load or a store between main memory and a CPU register may now be serviced from the cache.
*If a load or a store cannot be serviced from the cache, it is called a "cache miss".
*A cache miss results in one line being loaded from main memory into the cache. Namely, if the CPU tries to access word w and b is the line containing w, then b is loaded into the cache. If the cache was previously full, then a line will be evicted as well (see replacement policy below).
*The cache holds Z words, where Z = Omega(L^2). This is also known as the "tall cache assumption".
*The cache is fully associative: each line can be loaded into any location in the cache.
*The replacement policy is optimal. In other words, the cache is assumed to be given the entire sequence of memory accesses during algorithm execution. If it needs to evict a line at time t, it will look into its sequence of future requests and evict the line that is accessed furthest in the future. This can be implemented in practice with the Least Recently Used policy, which is shown to be within a small constant factor of the offline optimal replacement strategy.

To measure the complexity of an algorithm that executes within the cache-oblivious model, we can measure the familiar (running time) "work" complexity W(n). However, we can also measure the "cache complexity", Q(n,L,Z), the number of cache misses that the algorithm will experience.

The goal for creating a good cache-oblivious algorithm is to match the work complexity of some optimal RAM model algorithm while minimizing Q(n,L,Z). Furthermore, unlike the external-memory model, which shares many of the listed features, we would like our algorithm to be independent of cache parameters (L and Z). The benefit of such an algorithm is that what is efficient on a cache-oblivious machine is likely to be efficient across many real machines without fine tuning for particular real machine parameters. Frigo et al. showed that for many problems, an optimal cache-oblivious algorithm will also be optimal for a machine with more than two memory hierarchy levels.


For example, it is possible to design a variant of unrolled linked lists which is cache-oblivious and allows list traversal of n elements in n/L time, where b is the cache size. For a fixed L, this is O(n) time. However, the advantage of the algorithm is that it can scale to take advantage of larger cache line sizes (larger values of L).

The simplest cache-oblivious algorithm presented in Frigo et al. is an out-of-place matrix transpose operation (in-place algorithms have also been devised for transposition, but are much more complicated for non-square matrices). Given m imes n arrays A and B, we would like to store the transpose of A in B. The naive solution traverses one array in row-major order and another in column-major. The result is that when the matrices are large, we get a cache miss on every step of the column-wise traversal. The total number of cache misses is Theta(mn).

The cache-oblivious algorithm has optimal work complexity O(mn) and optimal cache complexity O(1+mn/L). The basic idea is to reduce the transpose of two large matrices into the transpose of small (sub)matrices. We do this by dividing the matrices in half along their larger dimension until we just have to perform the transpose of a matrix that will fit into the cache. Because the cache size is not known to the algorithm, the matrices will continue to be divided recursively even after this point, but these further subdivisions will be in cache. Once the dimensions m and n are small enough so an "input" array of size m imes n and an output array of size n imes m fit into the cache, both row-major and column-major traversals result in O(mn) work and O(mn/L) cache misses. By using this divide and conquer approach we can achieve the same level of complexity for the overall matrix.

(In principle, one could continue dividing the matrices until a base case of size 1×1 is reached, but in practice one uses a larger base case (e.g. 16×16) in order to amortize the overhead of the recursive subroutine calls.)

Most cache oblivious algorithms rely on a divide and conquer approach. They reduce the problem, so that it eventually fits in cache no matter how small the cache is, end the recursion at some small size determined by the function-call overhead and similar cache-unrelated optimizations, and then use some cache-efficient access pattern to merge the results of these small, solved problems.

External links

*Harald Prokop. [ Cache-Oblivious Algorithms] . Masters thesis, MIT. 1999.
*M. Frigo, C.E. Leiserson, H. Prokop, and S. Ramachandran. Cache-oblivious algorithms. In "Proceedings of the 40th IEEE Symposium on Foundations of Computer Science" (FOCS 99), p.285-297. 1999. [ Extended abstract at IEEE] , [ at Citeseer] .
*Erik Demaine. [ Review of the Cache-Oblivious Model] . Notes for MIT Computer Science 6.897: Advanced Data Structures.
*Piyush Kumar. [ Cache-Oblivious Algorithms] . Algorithms for Memory Hierarchies, LNCS 2625, pages 193-212, Springer Verlag.

Wikimedia Foundation. 2010.

См. также в других словарях:

  • Cache algorithms — This article is about general cache algorithms. For detailed algorithms specific to paging, see page replacement algorithm. For detailed algorithms specific to the cache between a CPU and RAM, see CPU cache. In computing, cache algorithms (also… …   Wikipedia

  • Cache — This article is about the computer science optimization concept. For other uses, see Cache (disambiguation). In computer engineering, a cache (  /ˈk …   Wikipedia

  • Cooley–Tukey FFT algorithm — The Cooley–Tukey algorithm, named after J.W. Cooley and John Tukey, is the most common fast Fourier transform (FFT) algorithm. It re expresses the discrete Fourier transform (DFT) of an arbitrary composite size N = N1N2 in terms of smaller DFTs… …   Wikipedia

  • Cooley-Tukey FFT algorithm — The Cooley Tukey algorithm, named after J.W. Cooley and John Tukey, is the most common fast Fourier transform (FFT) algorithm. It re expresses the discrete Fourier transform (DFT) of an arbitrary composite size N = N 1 N 2 in terms of smaller… …   Wikipedia

  • Divide-and-conquer eigenvalue algorithm — Divide and conquer eigenvalue algorithms are a class of eigenvalue algorithms for Hermitian or real symmetric matrices that have recently (circa 1990s) become competitive in terms of stability and efficiency with more traditional algorithms such… …   Wikipedia

  • Minimum degree algorithm — In numerical analysis the minimum degree algorithm is an algorithm used to permute the rows and columns of a symmetric sparse matrix before applying the Cholesky decomposition, to reduce the number of non zeros in the Cholesky factor. This… …   Wikipedia

  • Coppersmith–Winograd algorithm — In the mathematical discipline of linear algebra, the Coppersmith–Winograd algorithm, named after Don Coppersmith and Shmuel Winograd, is the asymptotically fastest known algorithm for square matrix multiplication as of 2008. It can multiply two… …   Wikipedia

  • Eigenvalue algorithm — In linear algebra, one of the most important problems is designing efficient and stable algorithms for finding the eigenvalues of a matrix. These eigenvalue algorithms may also find eigenvectors. Contents 1 Characteristic polynomial 2 Power… …   Wikipedia

  • Bareiss algorithm — In mathematics, the Bareiss algorithm, named after Erwin Bareiss, is an algorithm to calculate the determinant of a matrix with integer entries using only integer arithmetic; any divisions that are performed are guaranteed to be exact (there is… …   Wikipedia

  • Divide and conquer algorithm — In computer science, divide and conquer (D C) is an important algorithm design paradigm based on multi branched recursion. A divide and conquer algorithm works by recursively breaking down a problem into two or more sub problems of the same (or… …   Wikipedia

Поделиться ссылкой на выделенное

Прямая ссылка:
Нажмите правой клавишей мыши и выберите «Копировать ссылку»