<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
locality	B-General_Concept
of	I-General_Concept
reference	I-General_Concept
,	O
also	O
known	O
as	O
the	O
principle	O
of	O
locality	O
,	O
is	O
the	O
tendency	O
of	O
a	O
processor	O
to	O
access	O
the	O
same	O
set	O
of	O
memory	B-General_Concept
locations	O
repetitively	O
over	O
a	O
short	O
period	O
of	O
time	O
.	O
</s>
<s>
Temporal	B-General_Concept
locality	I-General_Concept
refers	O
to	O
the	O
reuse	O
of	O
specific	O
data	O
and/or	O
resources	O
within	O
a	O
relatively	O
small	O
time	O
duration	O
.	O
</s>
<s>
Spatial	O
locality	O
(	O
also	O
termed	O
data	B-General_Concept
locality	I-General_Concept
)	O
refers	O
to	O
the	O
use	O
of	O
data	O
elements	O
within	O
relatively	O
close	O
storage	O
locations	O
.	O
</s>
<s>
Sequential	B-General_Concept
locality	I-General_Concept
,	O
a	O
special	O
case	O
of	O
spatial	O
locality	O
,	O
occurs	O
when	O
data	O
elements	O
are	O
arranged	O
and	O
accessed	O
linearly	O
,	O
such	O
as	O
traversing	O
the	O
elements	O
in	O
a	O
one-dimensional	O
array	B-Data_Structure
.	O
</s>
<s>
Systems	O
that	O
exhibit	O
strong	O
locality	B-General_Concept
of	I-General_Concept
reference	I-General_Concept
are	O
great	O
candidates	O
for	O
performance	O
optimization	O
through	O
the	O
use	O
of	O
techniques	O
such	O
as	O
the	O
caching	B-General_Concept
,	O
prefetching	O
for	O
memory	B-General_Concept
and	O
advanced	O
branch	B-General_Concept
predictors	I-General_Concept
at	O
the	O
pipelining	B-General_Concept
stage	O
of	O
a	O
processor	O
core	O
.	O
</s>
<s>
There	O
are	O
several	O
different	O
types	O
of	O
locality	B-General_Concept
of	I-General_Concept
reference	I-General_Concept
:	O
</s>
<s>
Temporal	B-General_Concept
locality	I-General_Concept
:	O
If	O
at	O
one	O
point	O
a	O
particular	O
memory	B-General_Concept
location	O
is	O
referenced	O
,	O
then	O
it	O
is	O
likely	O
that	O
the	O
same	O
location	O
will	O
be	O
referenced	O
again	O
in	O
the	O
near	O
future	O
.	O
</s>
<s>
There	O
is	O
temporal	O
proximity	O
between	O
adjacent	O
references	O
to	O
the	O
same	O
memory	B-General_Concept
location	O
.	O
</s>
<s>
In	O
this	O
case	O
it	O
is	O
common	O
to	O
make	O
efforts	O
to	O
store	O
a	O
copy	O
of	O
the	O
referenced	O
data	O
in	O
faster	O
memory	B-General_Concept
storage	O
,	O
to	O
reduce	O
the	O
latency	O
of	O
subsequent	O
references	O
.	O
</s>
<s>
Temporal	B-General_Concept
locality	I-General_Concept
is	O
a	O
special	O
case	O
of	O
spatial	O
locality	O
(	O
see	O
below	O
)	O
,	O
namely	O
when	O
the	O
prospective	O
location	O
is	O
identical	O
to	O
the	O
present	O
location	O
.	O
</s>
<s>
Spatial	O
locality	O
:	O
If	O
a	O
particular	O
storage	O
location	O
is	O
referenced	O
at	O
a	O
particular	O
time	O
,	O
then	O
it	O
is	O
likely	O
that	O
nearby	O
memory	B-General_Concept
locations	O
will	O
be	O
referenced	O
in	O
the	O
near	O
future	O
.	O
</s>
<s>
Memory	B-General_Concept
locality	I-General_Concept
(	O
or	O
data	B-General_Concept
locality	I-General_Concept
)	O
:	O
Spatial	O
locality	O
explicitly	O
relating	O
to	O
memory	B-General_Concept
.	O
</s>
<s>
Branch	B-General_Concept
locality	O
:	O
If	O
there	O
are	O
only	O
a	O
few	O
possible	O
alternatives	O
for	O
the	O
prospective	O
part	O
of	O
the	O
path	O
in	O
the	O
spatial-temporal	O
coordinate	O
space	O
.	O
</s>
<s>
Branch	B-General_Concept
locality	O
is	O
typically	O
not	O
spatial	O
locality	O
since	O
the	O
few	O
possibilities	O
can	O
be	O
located	O
far	O
away	O
from	O
each	O
other	O
.	O
</s>
<s>
Equidistant	O
locality	O
:	O
Halfway	O
between	O
spatial	O
locality	O
and	O
branch	B-General_Concept
locality	O
.	O
</s>
<s>
For	O
branch	B-General_Concept
locality	O
,	O
the	O
contemporary	O
processors	O
have	O
sophisticated	O
branch	B-General_Concept
predictors	I-General_Concept
,	O
and	O
on	O
the	O
basis	O
of	O
this	O
prediction	O
the	O
memory	B-General_Concept
manager	O
of	O
the	O
processor	O
tries	O
to	O
collect	O
and	O
preprocess	O
the	O
data	O
of	O
plausible	O
alternatives	O
.	O
</s>
<s>
The	O
reasons	O
below	O
are	O
not	O
disjoint	B-Algorithm
;	O
in	O
fact	O
,	O
the	O
list	O
below	O
goes	O
from	O
the	O
most	O
general	O
case	O
to	O
special	O
cases	O
:	O
</s>
<s>
This	O
means	O
that	O
if	O
a	O
lot	O
of	O
processing	O
is	O
done	O
,	O
the	O
single	O
item	O
will	O
be	O
accessed	O
more	O
than	O
once	O
,	O
thus	O
leading	O
to	O
temporal	B-General_Concept
locality	I-General_Concept
of	O
reference	O
.	O
</s>
<s>
Furthermore	O
,	O
moving	O
to	O
the	O
next	O
item	O
implies	O
that	O
the	O
next	O
item	O
will	O
be	O
read	O
,	O
hence	O
spatial	O
locality	B-General_Concept
of	I-General_Concept
reference	I-General_Concept
,	O
since	O
memory	B-General_Concept
locations	O
are	O
typically	O
read	O
in	O
batches	O
.	O
</s>
<s>
Linear	O
data	B-General_Concept
structures	I-General_Concept
:	O
Locality	O
often	O
occurs	O
because	O
code	O
contains	O
loops	O
that	O
tend	O
to	O
reference	O
arrays	O
or	O
other	O
data	B-General_Concept
structures	I-General_Concept
by	O
indices	O
.	O
</s>
<s>
Sequential	B-General_Concept
locality	I-General_Concept
,	O
a	O
special	O
case	O
of	O
spatial	O
locality	O
,	O
occurs	O
when	O
relevant	O
data	O
elements	O
are	O
arranged	O
and	O
accessed	O
linearly	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
simple	O
traversal	O
of	O
elements	O
in	O
a	O
one-dimensional	O
array	B-Data_Structure
,	O
from	O
the	O
base	O
address	O
to	O
the	O
highest	O
element	O
would	O
exploit	O
the	O
sequential	B-General_Concept
locality	I-General_Concept
of	O
the	O
array	B-Data_Structure
in	O
memory	B-General_Concept
.	O
</s>
<s>
Equidistant	O
locality	O
occurs	O
when	O
the	O
linear	O
traversal	O
is	O
over	O
a	O
longer	O
area	O
of	O
adjacent	O
data	B-General_Concept
structures	I-General_Concept
with	O
identical	O
structure	O
and	O
size	O
,	O
accessing	O
mutually	O
corresponding	O
elements	O
of	O
each	O
structure	O
rather	O
than	O
each	O
entire	O
structure	O
.	O
</s>
<s>
This	O
is	O
the	O
case	O
when	O
a	O
matrix	B-Architecture
is	O
represented	O
as	O
a	O
sequential	O
matrix	B-Architecture
of	O
rows	O
and	O
the	O
requirement	O
is	O
to	O
access	O
a	O
single	O
column	O
of	O
the	O
matrix	B-Architecture
.	O
</s>
<s>
Efficiency	O
of	O
memory	B-General_Concept
hierarchy	O
use	O
:	O
Although	O
random-access	B-Architecture
memory	I-Architecture
presents	O
the	O
programmer	O
with	O
the	O
ability	O
to	O
read	O
or	O
write	O
anywhere	O
at	O
any	O
time	O
,	O
in	O
practice	O
latency	O
and	O
throughput	O
are	O
affected	O
by	O
the	O
efficiency	O
of	O
the	O
cache	B-General_Concept
,	O
which	O
is	O
improved	O
by	O
increasing	O
the	O
locality	B-General_Concept
of	I-General_Concept
reference	I-General_Concept
.	O
</s>
<s>
Poor	O
locality	B-General_Concept
of	I-General_Concept
reference	I-General_Concept
results	O
in	O
cache	B-General_Concept
thrashing	B-General_Concept
and	O
cache	B-General_Concept
pollution	I-General_Concept
and	O
to	O
avoid	O
it	O
,	O
data	O
elements	O
with	O
poor	O
locality	O
can	O
be	O
bypassed	O
from	O
cache	B-General_Concept
.	O
</s>
<s>
The	O
branch	B-General_Concept
locality	O
is	O
a	O
more	O
elaborate	O
possibility	O
,	O
hence	O
more	O
developing	O
effort	O
is	O
needed	O
,	O
but	O
there	O
is	O
much	O
larger	O
reserve	O
for	O
future	O
exploration	O
in	O
this	O
kind	O
of	O
locality	O
than	O
in	O
all	O
the	O
remaining	O
ones	O
.	O
</s>
<s>
Hierarchical	O
memory	B-General_Concept
is	O
a	O
hardware	O
optimization	O
that	O
takes	O
the	O
benefits	O
of	O
spatial	O
and	O
temporal	B-General_Concept
locality	I-General_Concept
and	O
can	O
be	O
used	O
on	O
several	O
levels	O
of	O
the	O
memory	B-General_Concept
hierarchy	O
.	O
</s>
<s>
Paging	B-Architecture
obviously	O
benefits	O
from	O
temporal	O
and	O
spatial	O
locality	O
.	O
</s>
<s>
A	O
cache	B-General_Concept
is	O
a	O
simple	O
example	O
of	O
exploiting	O
temporal	B-General_Concept
locality	I-General_Concept
,	O
because	O
it	O
is	O
a	O
specially	O
designed	O
,	O
faster	O
but	O
smaller	O
memory	B-General_Concept
area	O
,	O
generally	O
used	O
to	O
keep	O
recently	O
referenced	O
data	O
and	O
data	O
near	O
recently	O
referenced	O
data	O
,	O
which	O
can	O
lead	O
to	O
potential	O
performance	O
increases	O
.	O
</s>
<s>
Data	O
elements	O
in	O
a	O
cache	B-General_Concept
do	O
not	O
necessarily	O
correspond	O
to	O
data	O
elements	O
that	O
are	O
spatially	O
close	O
in	O
the	O
main	O
memory	B-General_Concept
;	O
however	O
,	O
data	O
elements	O
are	O
brought	O
into	O
cache	B-General_Concept
one	O
cache	B-General_Concept
line	I-General_Concept
at	O
a	O
time	O
.	O
</s>
<s>
This	O
means	O
that	O
spatial	O
locality	O
is	O
again	O
important	O
:	O
if	O
one	O
element	O
is	O
referenced	O
,	O
a	O
few	O
neighboring	O
elements	O
will	O
also	O
be	O
brought	O
into	O
cache	B-General_Concept
.	O
</s>
<s>
Finally	O
,	O
temporal	B-General_Concept
locality	I-General_Concept
plays	O
a	O
role	O
on	O
the	O
lowest	O
level	O
,	O
since	O
results	O
that	O
are	O
referenced	O
very	O
closely	O
together	O
can	O
be	O
kept	O
in	O
the	O
machine	B-General_Concept
registers	I-General_Concept
.	O
</s>
<s>
Some	O
programming	O
languages	O
(	O
such	O
as	O
C	B-Language
)	O
allow	O
the	O
programmer	O
to	O
suggest	O
that	O
certain	O
variables	O
be	O
kept	O
in	O
registers	O
.	O
</s>
<s>
Data	B-General_Concept
locality	I-General_Concept
is	O
a	O
typical	O
memory	B-General_Concept
reference	O
feature	O
of	O
regular	O
programs	O
(	O
though	O
many	O
irregular	O
memory	B-General_Concept
access	O
patterns	O
exist	O
)	O
.	O
</s>
<s>
It	O
makes	O
the	O
hierarchical	O
memory	B-General_Concept
layout	O
profitable	O
.	O
</s>
<s>
In	O
computers	O
,	O
memory	B-General_Concept
is	O
divided	O
into	O
a	O
hierarchy	O
in	O
order	O
to	O
speed	O
up	O
data	O
accesses	O
.	O
</s>
<s>
The	O
lower	O
levels	O
of	O
the	O
memory	B-General_Concept
hierarchy	O
tend	O
to	O
be	O
slower	O
,	O
but	O
larger	O
.	O
</s>
<s>
Thus	O
,	O
a	O
program	O
will	O
achieve	O
greater	O
performance	O
if	O
it	O
uses	O
memory	B-General_Concept
while	O
it	O
is	O
cached	O
in	O
the	O
upper	O
levels	O
of	O
the	O
memory	B-General_Concept
hierarchy	O
and	O
avoids	O
bringing	O
other	O
data	O
into	O
the	O
upper	O
levels	O
of	O
the	O
hierarchy	O
that	O
will	O
displace	O
data	O
that	O
will	O
be	O
used	O
shortly	O
in	O
the	O
future	O
.	O
</s>
<s>
Typical	O
memory	B-General_Concept
hierarchy	O
(	O
access	O
times	O
and	O
cache	B-General_Concept
sizes	O
are	O
approximations	O
of	O
typical	O
values	O
used	O
for	O
the	O
purpose	O
of	O
discussion	O
;	O
actual	O
values	O
and	O
actual	O
numbers	O
of	O
levels	O
in	O
the	O
hierarchy	O
vary	O
)	O
:	O
</s>
<s>
Modern	O
machines	O
tend	O
to	O
read	O
blocks	O
of	O
lower	O
memory	B-General_Concept
into	O
the	O
next	O
level	O
of	O
the	O
memory	B-General_Concept
hierarchy	O
.	O
</s>
<s>
If	O
this	O
displaces	O
used	O
memory	B-General_Concept
,	O
the	O
operating	B-General_Concept
system	I-General_Concept
tries	O
to	O
predict	O
which	O
data	O
will	O
be	O
accessed	O
least	O
(	O
or	O
latest	O
)	O
and	O
move	O
it	O
down	O
the	O
memory	B-General_Concept
hierarchy	O
.	O
</s>
<s>
A	O
common	O
example	O
is	O
matrix	B-Architecture
multiplication	O
:	O
</s>
<s>
By	O
switching	O
the	O
looping	O
order	O
for	O
j	O
and	O
k	O
,	O
the	O
speedup	O
in	O
large	O
matrix	B-Architecture
multiplications	O
becomes	O
dramatic	O
,	O
at	O
least	O
for	O
languages	O
that	O
put	O
contiguous	O
array	B-Data_Structure
elements	I-Data_Structure
in	O
the	O
last	O
dimension	O
.	O
</s>
<s>
In	O
this	O
case	O
,	O
"	O
large	O
"	O
means	O
,	O
approximately	O
,	O
more	O
than	O
100,000	O
elements	O
in	O
each	O
matrix	B-Architecture
,	O
or	O
enough	O
addressable	O
memory	B-General_Concept
such	O
that	O
the	O
matrices	O
will	O
not	O
fit	O
in	O
L1	O
and	O
L2	O
caches	B-General_Concept
.	O
</s>
<s>
The	O
reason	O
for	O
this	O
speedup	O
is	O
that	O
in	O
the	O
first	O
case	O
,	O
the	O
reads	O
of	O
A[i][k]	O
are	O
in	O
cache	B-General_Concept
(	O
since	O
the	O
k	O
index	O
is	O
the	O
contiguous	O
,	O
last	O
dimension	O
)	O
,	O
but	O
B[k][j]	O
is	O
not	O
,	O
so	O
there	O
is	O
a	O
cache	B-General_Concept
miss	O
penalty	O
on	O
B[k][j]	O
.	O
</s>
<s>
C[i][j]	O
is	O
irrelevant	O
,	O
because	O
it	O
can	O
be	O
hoisted	O
out	O
of	O
the	O
inner	O
loop	O
--	O
the	O
loop	O
variable	O
there	O
is	O
k	O
.	O
</s>
<s>
In	O
the	O
second	O
case	O
,	O
the	O
reads	O
and	O
writes	O
of	O
C[i][j]	O
are	O
both	O
in	O
cache	B-General_Concept
,	O
the	O
reads	O
of	O
B[k][j]	O
are	O
in	O
cache	B-General_Concept
,	O
and	O
the	O
read	O
of	O
A[i][k]	O
can	O
be	O
hoisted	O
out	O
of	O
the	O
inner	O
loop	O
.	O
</s>
<s>
Thus	O
,	O
the	O
second	O
example	O
has	O
no	O
cache	B-General_Concept
miss	O
penalty	O
in	O
the	O
inner	O
loop	O
while	O
the	O
first	O
example	O
has	O
a	O
cache	B-General_Concept
penalty	O
.	O
</s>
<s>
On	O
a	O
year	O
2014	O
processor	O
,	O
the	O
second	O
case	O
is	O
approximately	O
five	O
times	O
faster	O
than	O
the	O
first	O
case	O
,	O
when	O
written	O
in	O
C	B-Language
and	O
compiled	O
with	O
gcc	B-Application
-O3	O
.	O
</s>
<s>
(	O
A	O
careful	O
examination	O
of	O
the	O
disassembled	O
code	O
shows	O
that	O
in	O
the	O
first	O
case	O
,	O
GCC	B-Application
uses	O
SIMD	B-Device
instructions	O
and	O
in	O
the	O
second	O
case	O
it	O
does	O
not	O
,	O
but	O
the	O
cache	B-General_Concept
penalty	O
is	O
much	O
worse	O
than	O
the	O
SIMD	B-Device
gain	O
.	O
)	O
</s>
<s>
Temporal	B-General_Concept
locality	I-General_Concept
can	O
also	O
be	O
improved	O
in	O
the	O
above	O
example	O
by	O
using	O
a	O
technique	O
called	O
blocking	O
.	O
</s>
<s>
The	O
larger	O
matrix	B-Architecture
can	O
be	O
divided	O
into	O
evenly	O
sized	O
sub-matrices	O
,	O
so	O
that	O
the	O
smaller	O
blocks	O
can	O
be	O
referenced	O
(	O
multiplied	O
)	O
several	O
times	O
while	O
in	O
memory	B-General_Concept
.	O
</s>
<s>
The	O
temporal	B-General_Concept
locality	I-General_Concept
of	O
the	O
above	O
solution	O
is	O
provided	O
because	O
a	O
block	O
can	O
be	O
used	O
several	O
times	O
before	O
moving	O
on	O
,	O
so	O
that	O
it	O
is	O
moved	O
in	O
and	O
out	O
of	O
memory	B-General_Concept
less	O
often	O
.	O
</s>
<s>
Spatial	O
locality	O
is	O
improved	O
because	O
elements	O
with	O
consecutive	O
memory	B-General_Concept
addresses	O
tend	O
to	O
be	O
pulled	O
up	O
the	O
memory	B-General_Concept
hierarchy	O
together	O
.	O
</s>
