Content deleted Content added
→Matrix multiplication: improved explanation of hoisting |
mNo edit summary |
||
(35 intermediate revisions by 28 users not shown) | |||
Line 1:
{{Short description|Tendency of a processor to access nearby memory locations in space or time}}
{{more citations needed|date=July 2008}}
In [[computer science]], '''locality of reference''', also known as the '''principle of locality''',<ref>Not to be confused with the [[principle of locality]] o=s*v=411##sts in physics.</ref> is the tendency of a processor to access the same set of memory locations repetitively over a short period of time.<ref>{{Cite book|title=Computer organization and architecture : designing for performance|last=William.|first=Stallings|date=2010|publisher=Prentice Hall|isbn=9780136073734|edition= 8th|___location=Upper Saddle River, NJ|oclc=268788976}}</ref> There are two basic types of reference locality {{
Locality is a type of [[predictability|predictable]] behavior that occurs in computer systems. Systems
== Types of locality ==
There are several different types of locality of reference:
* '''Temporal locality''': If at one point a particular memory ___location is referenced, then it is likely that the same ___location will be referenced again in the near future. There is
* '''Spatial locality''': If a particular storage ___location is referenced at a particular time, then it is likely that nearby memory locations will be referenced in the near future. In this case it is common to attempt to guess the size and shape of the area around the current reference for which it is worthwhile to prepare faster access for subsequent reference.
** '''Memory locality''' (or ''data locality''<ref name="NistBig1"/>): Spatial locality explicitly relating to [[computer memory|memory]].
* '''[[Branch (computer science)|Branch]] locality''': If there are only a few possible alternatives for the prospective part of the path in the spatial-temporal coordinate space. This is the case when an instruction loop has a simple structure, or the possible outcome of a small system of conditional branching instructions is restricted to a small set of possibilities. Branch locality is typically not
* '''Equidistant locality''':
In order to benefit from
== Relevance ==
There are several reasons for locality. These reasons are either goals to achieve or circumstances to accept, depending on the aspect. The reasons below are not [[Disjoint sets|disjoint]]; in fact, the list below goes from the most general case to special cases:
Line 21 ⟶ 22:
* '''Structure of the program''': Locality occurs often because of the way in which computer programs are created, for handling decidable problems. Generally, related data is stored in nearby locations in storage. One common pattern in computing involves the processing of several items, one at a time. This means that if a lot of processing is done, the single item will be accessed more than once, thus leading to temporal locality of reference. Furthermore, moving to the next item implies that the next item will be read, hence spatial locality of reference, since memory locations are typically read in batches.
* '''Linear data structures''': Locality often occurs because code contains loops that tend to reference arrays or other data structures by indices. Sequential locality, a special case of spatial locality, occurs when relevant data elements are arranged and accessed linearly. For example, the simple traversal of elements in a one-dimensional array, from the base address to the highest element would exploit the sequential locality of the array in memory.<ref>Aho, Lam, Sethi, and Ullman. "Compilers: Principles, Techniques & Tools" 2nd ed. Pearson Education, Inc. 2007</ref> Equidistant locality occurs when the linear traversal is over a longer area of adjacent [[data structure]]s with identical structure and size, accessing mutually corresponding elements of each structure rather than each entire structure. This is the case when a [[Matrix (mathematics)|matrix]] is represented as a sequential matrix of rows and the requirement is to access a single column of the matrix.
* '''Efficiency of memory hierarchy use''': Although [[random
== General usage ==
Line 31 ⟶ 32:
== Spatial and temporal locality usage ==
===
{{main|Memory hierarchy}}
Line 41 ⟶ 42:
Typical memory hierarchy (access times and cache sizes are approximations of typical values used {{As of|2013|lc=on}} for the purpose of discussion; actual values and actual numbers of levels in the hierarchy vary):
* [[CPU register]]s (
* L1 [[CPU cache]]s (32
* L2 CPU caches (128
* L3 CPU caches (2
* Main [[physical memory]] ([[random-access memory|RAM]]) (256
* Disk ([[virtual memory]], [[file system]]) (1
* Remote memory (other computers or the cloud) (practically unlimited) – speed varies from very slow to extremely slow
Line 95 ⟶ 96:
On a year 2014 processor, the second case is approximately five times faster than the first case, when written in [[C (programming language)|C]] and compiled with <code>gcc -O3</code>. (A careful examination of the disassembled code shows that in the first case, [[GNU Compiler Collection|GCC]] uses [[SIMD]] instructions and in the second case it does not, but the cache penalty is much worse than the SIMD gain.){{Citation needed|date=September 2014}}
Temporal locality can also be improved in the above example by using a technique called [[Loop blocking|blocking]]. The larger matrix can be divided into evenly sized sub-matrices, so that the smaller blocks can be referenced (multiplied) several times while in memory. Note that this example works for square matrices of dimensions SIZE x SIZE, but it can easily be extended for arbitrary matrices by substituting SIZE_I, SIZE_J and SIZE_K where appropriate.
<syntaxhighlight lang="pascal" line="1">
Line 116 ⟶ 117:
* [[Cache-oblivious algorithm]]
* [[Communication-avoiding algorithm]]
* [[File system fragmentation]]
* [[Partitioned global address space]]
Line 122 ⟶ 124:
* [[Scratchpad memory]]
* [[Working set]]
* [[Heuristic]]
* [[Locality-sensitive hashing]]
== References ==
Line 128 ⟶ 132:
== Bibliography ==
* [[Peter J. Denning]], [http://denninginstitute.com/pjd/PUBS/CACMcols/cacmJul05.pdf "The Locality Principle"], ''Communications of the ACM'', Volume 48, Issue 7, (2005), Pages 19–24
* Peter J. Denning, Stuart C. Schwartz, [http://denninginstitute.com/pjd/PUBS/WSProp_1972.pdf "Properties of the Working-Set Model"], ''Communications of the ACM'', Volume 15, Issue 3 (March 1972), Pages
{{DEFAULTSORT:Locality Of Reference}}
|