Docsity

Cache Aware Optimization, Slides of Embedded Systems

Toggle sidebar
bg1
1
Embedded Systems
Cache Aware Optimization and Code
Tuning for Performance
Dr. Jeff Jackson Lecture 9-1Electrical & Computer Engineering – Embedded Systems
Tuning
for
Performance
Goals
•The goals of a cache aware optimization technique
should be to:
–
Minimize the stride by which data is accessed from memory
Minimize
the
stride
by
which
data
is
accessed
from
memory
•Stride-1 processing would represent the case where data elements
were accessed from consecutive, sequential memory locations
•Stride-2 processing would represent the case where data elements
were accessed from every other memory location
•Stride minimization is a technique for increasing spatial locality of
reference
–
Access data in a block
-
like fashion
Dr. Jeff Jackson Lecture 9-2Electrical & Computer Engineering – Embedded Systems
Access
data
in
a
block
-
like
fashion
•Blocking (also known as strip mining) is an access technique
whereby data is processed in blocks that fit in the cache
•Accessing data in a blocked fashion is a loose attempt to increase
temporal locality of reference
pf3
pf4
pf5

Partial preview of the text

Download Cache Aware Optimization and more Slides Embedded Systems in PDF only on Docsity!

Embedded Systems

Cache Aware Optimization and Code

Tuning for Performance

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 9-

Tuning for Performance

Goals

• The goals of a cache aware optimization technique

should be to:

– Minimize the stride by which data is accessed from memoryMinimize the stride by which data is accessed from memory

  • Stride-1 processing would represent the case where data elements were accessed from consecutive, sequential memory locations
  • Stride-2 processing would represent the case where data elements were accessed from every other memory location
  • Stride minimization is a technique for increasing spatial locality of reference

– Access data in a block-like fashion

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 9-

Access data in a block-like fashion

  • Blocking (also known as strip mining) is an access technique whereby data is processed in blocks that fit in the cache
  • Accessing data in a blocked fashion is a loose attempt to increase temporal locality of reference

Variables, Arrays, Matrices and Storage

Allocation

  • In general, a compiler will tend to allocate variables

to memory in the order in which they appear in the

user application code

  • Some relocation of variables may be performed by the

compiler for optimization

int i, j, k;

float x, y, z;

i j k x

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 9-

int a[2]; x

y z a[0] a[1]

Variables, Arrays, Matrices and Storage

Allocation (continued)

  • By convention, C compilers allocate storage for

matrices (i.e. two dimensional arrays) in a row

major formmajor form

  • Consecutive elements in a row are stored in sequential

locations in memory

int a[4][4];

a[0][0] a[0][1] a[0][2] a[0][3]

ADDRESS ELEMENT

a a[0][0] a+4 a[0][1] a+8 a[0][2]

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 9-

a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3] a[3][0] a[3][1] a[3][2] a[3][3]

a 8 a[0][2] . . . a+60 a[3][3]

Stride Examples for Basic C Loops

for (i=0;i<n;i++)

a[i] // Stride 1

int a[50][100];

for (i=0;i<x;i++)

a[i+4] // Stride 1

a[2*i] // Stride 2

for (i=0;i<n;i+=3)

a[i] // Stride 3

for (j=0;j<y;j++)

for (k=0;k<z;k++)

a[j][k] // Stride 1

a[k][j] // Stride 100

a[j][i] // No stride

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 9-

for (i=n;i>0;i-=2)

a[2*i] // Stride -

a[j][i] is a scalar (a single,

constant value) with respect to

the inner loop

Cache Memory Characteristics for the NIOS

II Processor

• The NIOS II/f core provides optional instruction and

data caches

• The cache size for each is user-definable, between 512,

bytes and 64 KB

• Instruction cache characteristics

– Direct mapped

– 32 bytes (8 words) per line (block)

• Data cache characteristics

– Direct mapped

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 9-

Direct mapped

– Configurable line size (4, 16, or 32 bytes)

– Write-back policy

– Write-allocate (i.e., on a store instruction, a cache miss

allocates the line for that address)

Case Study

• Assume a NIOS II processor with a 4K instruction cache

• Assume various scenarios with different data cache

configurations

– 0KB data cache

– 4KB data cache, 32 byte line size

• How does the presence of the data cache affect a

simple operation

– Add two arrays together

– Arrays will be a power of 2 in length

• Considerations

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 9-

Considerations

– Implement code to disable interrupts to get as accurate an

execution time as possible using a high resolution timer

– Ensure arrays begin at the beginning of a main memory data

block

Basic C Example

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 9-