Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Analyse code for spatial and temporal locality

Hi have some question regarding spatial and temporal locality. I have read in the course theory that

spatial locality

If one item is referenced, the likelihood of other address close by will be referenced soon

temporal locality

One item that is referenced at one point in time it tend to be referenced soon again.

Ok, but how do I see that in the code? I think I understood the concept for temporal locality but I don't understand spatial locality yet. For instance in this loop

for(i = 0; i < 20; i++)
    for(j = 0; j < 10; j++)
        a[i] = a[i]*j;

The inner loop will call same memory address when accessing a[i] ten times so that's an example for temporal locality I guess. But is there spatial locality also in the above loop?

like image 438
starcorn Avatar asked Nov 03 '10 21:11

starcorn


People also ask

What is spatial locality and temporal locality?

There are two basic types of reference locality – temporal and spatial locality. Temporal locality refers to the reuse of specific data and/or resources within a relatively small time duration. Spatial locality (also termed data locality) refers to the use of data elements within relatively close storage locations.

What is an example of spatial locality?

The outer loop is an example of spacial locality. It sequentially increments the address the inner for-loop calls. The inside loop demonstrates temporal locality. The exact same memory address is accessed ten times in a row, and multiplied by j each time.

How are temporal and spatial locality exploited in computer?

Temporal locality is exploited by having a cache between the proces- sor and the memory that is big enough so that a word gets accessed multiple times before getting evicted. Spatial lo- cality is exploited by using blocks spanning multiple words to move data between the different layers of the hierarchy.

How do locality and instruction locality relate to spatial locality and temporal locality?

In Spatial Locality, nearby instructions to recently executed instruction are likely to be executed soon. In Temporal Locality, a recently executed instruction is likely to be executed again very soon. 2. It refers to the tendency of execution which involve a number of memory locations .


1 Answers

Of course. For instance, after referencing a[5] you are about to reference a[6].

like image 167
ssegvic Avatar answered Sep 28 '22 08:09

ssegvic