Csapp: The principle of locality

Source: Internet
Author: User

A well-written computer program often has good locality (locality). Locality usually has two different forms: temporal locality (temporal locality) and spatial locality (spatial locality). In a program with good time locality, the memory location that has been referenced once is likely to be referenced more than once in the near future. In a program with good spatial locality, if a memory location is referenced once, the program is likely to refer to a nearby memory location in the near future.

Some simple principles for evaluating the local nature of the procedure are as follows:

      • A program that repeatedly references the same variable has good time locality.
      • For programs with reference patterns with a step size of M, the smaller the step size, the better the spatial locality. A program with a reference pattern of step n is a good spatial locality. Programs that jump in memory in large increments will be poorly localized.
      • For the value order, the loop has good time locality and spatial locality. The smaller the loop body, the more the loop iteration number, the better the locality.

The specific examples of the above principles are analyzed:

from this, we can see that the variable sum is referenced once in each iteration of the loop, so there is good time locality for sum. On the other hand, because sum is a scalar, there is no spatial locality for sum. However, the elements of vector v are read sequentially, in the order in which they are stored in memory (assuming that the array starts at address 0). Therefore, for vector V, the function has good spatial locality, but the time locality is very poor, because each vector element is only accessed once. Judging from the whole loop body, each variable in the loop body has either good spatial locality or good time locality, so we can conclude that the Sumvec function has good locality.

We call a function like Sumvec that accesses each element of a vector in order, with a reference pattern of step 1. Sometimes we call the reference pattern of step 1 as the sequential reference pattern. A continuous vector, accessed every k element, is referred to as the reference pattern of the step size K. Generally speaking, the spatial locality decreases with the increase of step length.

The step size is also an important issue for programs that reference multidimensional arrays.

The difference between the two is that we exchange the cycles of I and J, but it is such a small change that can have a great effect on its locality.

Example Analysis:

void Clear1 (point *p,int N) {    int i,j;    for (i = 0;i < n;i++)    {for        (j = 0;j < 3;j++)            p[i].vel[j] = 0;        for (j = 0;j < 3;j++)            p[i].acc[j] = 0;    }} void Clear2 (point *p,int N) {    int i,j;    for (i = 0;i < n;i++)    {for        (j = 0;j < 3;j++)        {            p[i].vel[j] = 0;            P[I].ACC[J] = 0;}}    } void Clear3 (point *p,int N) {    int i,j;    for (j = 0;j < 3;j++)    {for        (i = 0;i < n;i++)            p[i].vel[j] = 0;        for (i = 0;i < n;i++)            p[i].acc[j] = 0;    }}

By ordering the three functions from the spatial locality (without considering the complexity), it is easy to get the function clear1 to access the array in the reference mode of step 1, so it obviously has the best spatial locality. The function CLEAR2 scans each of the n structures sequentially, but in each structure, it jumps to the following offset from the beginning of the structure in a pattern of not 1 steps: 0, 12, 4, 16, 8, 20. So the spatial locality of CLEAR2 is worse than that of CLEAR1. The function Clear3 not only jumps in each structure, but also jumps from structure to structure, so the spatial locality of LCEAR3 is worse than Clear2 and CLEAR1.

Csapp: The principle of locality

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.