WES237B: Software for Embedded Systems
Pat Pannuto
Department of Computer Science and Engineering
University of California, San Diego
Summer Session 2023
Today
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
Logistics
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
Recap
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
The guts of most interesting stuff is often ~this
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
void multiDimMath(float* const A, float* const B, float* const C, const int M, const int N, const int K)
{
for (int m = 0; m < M; m++) {
for (int n = 0; n < N; n++) {
for (int k = 0; k < K; k++) {
C[m * M + n] += A[m * M + k] * B[k * K + n];
}
}
}
}
Measuring Performance
The bottom line: Performance
Ferrari
Bus
Speed
160 mph
65 mph
Time to Bay Area
3.1 hours
7.7 hours
Passengers
2
60
Throughput (pmph)
320
3900
Measures of “Performance”
Why Study Computer Performance?
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
9
Performance is Affected by,…
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
10
Performance is Affected by,…
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
11
Performance is Affected by,…
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
12
Performance is Affected by,…
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
13
Principles of Measuring Performance
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
14
Performance Metrics
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
15
There are many ways to measure program execution time
$ time make # cargo build
Compiling hail v0.1.0 (/tock/boards/hail)
Finished release [optimized + debuginfo] target(s) in 19.96s
real 0m21.146s
user 0m30.388s
sys 0m2.032s
Execution Time
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
17
How architects define Performance
PerformanceX =
1
Execution TimeX
, for program X
Relative Performance
A runs in 12 seconds
B runs in 20 seconds
Relative Performance (Speedup), the Definition
PerformanceX
Execution TimeX
PerformanceY
Speedup
Execution TimeY
=
=
=
n
(X/Y)
Example
What is Time?
CPU Execution Time = CPU clock cycles * Clock cycle time
Cycle Time
MHz = millions of cycles/second, GHz = billions of cycles/second
X MHz = 1000/X nanoseconds cycle time
Y GHz = 1/Y nanoseconds cycle time
CPU time
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
23
How many clock cycles?
Number of CPU clock cycles =
[Instruction count] * [Average Clock Cycles per Instruction (CPI)]
Exercise:
Computer A runs program C in 3.6 billion cycles.
Program C requires 2 billion dynamic instructions.
What is the CPI?
Clock Cycles Per Instruction (CPI)
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
25
The CPI of ith class
The number of instructions of ith class
Putting it all together
CPU Execution Time = [CPU clock cycles] * [Clock cycle time]
CPU clock cycles = [Instruction count] * [Average Clock Cycles per Instruction (CPI)]
CPU Execution Time
Instruction Count
CPI
Clock Cycle Time
=
X
X
instructions
cycles/instruction
seconds/cycle
seconds
CPU Execution Time
= NI*CPI*Clock Period
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
27
Cycle Time/Clock Rate is no longer fixed
Who Affects Performance? How?
CPU Execution Time
Instruction Count
CPI
Clock Cycle Time
=
X
X
Which programs are best, are “most fair”, to run when measuring performance?
Amdahl’s Law
Execution time
after improvement
=
Execution Time Affected
Amount of Improvement
Execution Time Unaffected
+
Amdahl’s Law and Massive Parallelism
.9
.1
Amdahl’s Law and Massive Parallelism
.9
.1
.1
.45
Speedup
1.0
1/.55 = 1.82
Amdahl’s Law and Massive Parallelism
.9
.1
.1
.1
.45
.225
Speedup
1.0
1/.55 = 1.82
1/.325 = 3.07
Amdahl’s Law and Massive Parallelism
.9
.1
.1
.1
.1
.45
.225
Speedup
1.0
1/.55 = 1.82
1/.325 = 3.07
< 10
Optimizations
What to optimize on a CPU?
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
| Number of Instructions (NI) | CPI | Clock Frequency |
Program | 🗹 | | |
Compiler | 🗹 | 🗹 | |
ISA | 🗹 | 🗹 | |
Organization | | 🗹 | 🗹 |
Technology | | | 🗹 |
= NI*CPI*Clock Period
Optimization is
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
hard
Optimization is hard (and getting harder…)
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
Software Application
Compiler
Hardware (A GPU)
Optimization is hard (and getting harder…)
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
Software Application
Compiler
Hardware (A GPU)
Hardware (A GPU)
Hardware (A GPU)
[ or TPU or …]
Optimizations in Different Levels
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
Software
Hardware
ISA
Pipelining, out-of-order execution,
Branch prediction, superscalar,
Cache organization, multi-core,…
Multithreading, Inline Assembly,
Blocking, Loop interchange,..etc.
Compiler level optimizations such
as dead code elimination, ..
Optimization: Must optimize at multiple levels:
Optimizations
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
Micro Architecture
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
43
Instruction Level Parallelism
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective; Third Edition
Superscalar processor
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective; Third Edition
Pipelined Functional Units
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
Stage 1
Stage 2
Stage 3
long mult_eg(long a, long b, long c) {
long p1 = a*b;� long p2 = a*c;� long p3 = p1 * p2;
return p3;�}
| Time | ||||||
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
Stage 1 | a*b | a*c | | | p1*p2 | | |
Stage 2 | | a*b | a*c | | | p1*p2 | |
Stage 3 | | | a*b | a*c | | | p1*p2 |
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective; Third Edition
Haswell CPU
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective; Third Edition
SIMD (Single Instruction Multiple Data)
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
%zmm0
%zmm1
vaddsd %zmm0, %zmm1, %zmm1
+
+
+
+
+
+
+
+
+
+
+
+
%zmm0
%zmm1
vaddds %zmm0, %zmm1, %zmm1
Operation | Syntax |
* | vmulps %zmm0,%zmm1,%zmm2 |
+ | vaddps %zmm0,%zmm1,%zmm2 |
| …. |
Float + | vfmadd132ps %zmm0,%zmm1,%zmm2 |
load | vmovups 256(%rax),%zmm0 |
Store | vmovups %zmm0,256(%rax) |
ARM Matrix Multiplication with SIMD
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
void matrix_multiply_4x4_neon(float32_t *A, float32_t *B, float32_t *C) {
…
// Multiply accumulate in 4x1 blocks,
// i.e. each column in C
B0 = vld1q_f32(B);
C0 = vfmaq_laneq_f32(C0, A0, B0, 0);
C0 = vfmaq_laneq_f32(C0, A1, B0, 1);
C0 = vfmaq_laneq_f32(C0, A2, B0, 2);
C0 = vfmaq_laneq_f32(C0, A3, B0, 3);
vst1q_f32(C, C0);
…
}
The Memory Hierarchy
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
50
Random-Access Memory (RAM)
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
| Trans per bit | Access Time | Needs Refresh | Cost | Application |
SRAM | 4/6 | 1X | No | 100X | Cache memories/Scratch pad |
DRAM | 1 | 10X | Yes | 1X | Main memories, frame buffers |
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective; Third Edition
Processor and Memory Gap
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
HP, Computer Architecture A Quantitate Approach, 2011
Processor and Memory Gap
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective; Third Edition
Processor and Memory Power Consumption
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
The energy consumption for various arithmetic operations and memory accesses in a 45 nm
process.
Sze, Vivienne, et al. "Efficient Processing of Deep Neural Networks." Synthesis Lectures on Computer Architecture 15.2 (2020): 1-341.
Slow memory, faster processor: What can we do?
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
55
Locality
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective; Third Edition
Locality Example
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
sum = 0;
for (i = 0; i < n; i++)
sum += a[i];
return sum;
C Memory
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
Locality Example
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
int sum_array_rows(int a[M][N])
{
int i, j, sum = 0;
for (i = 0; i < M; i++)
for (j = 0; j < N; j++)
sum += a[i][j];
return sum;
}
int sum_array_cols(int a[M][N])
{
int i, j, sum = 0;
for (j = 0; j < N; j++)
for (i = 0; i < M; i++)
sum += a[i][j];
return sum;
}
Does these functions have good locality with respect to array a[]?
Memory Hierarchies
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
Trade-off
Memory Hierarchies
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
Regs
L1 cache
(SRAM)
Main memory
(DRAM)
Local secondary storage
(local disks)
Larger,
slower,
and
cheaper
(per byte)
storage
devices
Remote secondary storage
(e.g., Web servers)
Local disks hold files retrieved from disks
on remote servers
L2 cache
(SRAM)
L1 cache holds cache lines retrieved from the L2 cache.
CPU registers hold words retrieved from the L1 cache.
L2 cache holds cache lines
retrieved from L3 cache
L0:
L1:
L2:
L3:
L4:
L5:
Smaller,
faster,
and
costlier
(per byte)
storage
devices
L3 cache
(SRAM)
L3 cache holds cache lines
retrieved from main memory.
L6:
Main memory holds disk blocks retrieved from local disks.
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective; Third Edition
A Modern Memory Hierarchy
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
Register File, 32 words, sub-nsec
L1 Cache, ~32 KB, ~nsecs
L2 cache, 512KB – 1 MB, ~nsecs
L3 Cache, ~ 10 nsecs
Main memory (DRAM), GB, ~100 ns
Disk, 100 GB, ~10 msec
The guts of most interesting stuff is often ~this
CC BY-NC-ND Pat Pannuto – Many slides adapted from Janarbek Matai
void Foo(float* const A, float* const B, float* const C, const int M, const int N, const int K)
{
for (int m = 0; m < M; m++) {
for (int n = 0; n < N; n++) {
for (int k = 0; k < K; k++) {
C[m * M + n] += A[m * M + k] * B[k * K + n];
}
}
}
}
And these are huge
Lab2: image.tif
480x640 pixels * 4 bytes/float ~= 1.3 MB (greyscale!)
Cache Fundamentals
(if time)
Cache
65
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective; Third Edition
Cache hierarchy vocabulary
66
Locality and cacheing
A typical memory hierarchy
CPU
memory
memory
memory
memory
on-chip caches
off-chip cache
main memory
disk
small
expensive $/bit
cheap $/bit
big
so then where is my program and data??
fast
slow
memory
Cache Fundamentals
cpu
lowest-level
cache
next-level
memory/cache
Cache Fundamentals
cpu
lowest-level
cache
next-level
memory/cache
Cache Fundamentals
cpu
lowest-level
cache
next-level
memory/cache
Cache Fundamentals
cpu
lowest-level
cache
next-level
memory/cache
Cache Fundamentals
cpu
lowest-level
cache
next-level
memory/cache
Cache Fundamentals
cpu
lowest-level
cache
next-level
memory/cache
Cache Fundamentals
cpu
lowest-level
cache
next-level
memory/cache
Cache Fundamentals, cont.
cpu
lowest-level
cache
next-level
memory/cache
Cache Fundamentals, cont.
cpu
lowest-level
cache
next-level
memory/cache
Cache Fundamentals, cont.
cpu
lowest-level
cache
next-level
memory/cache
Cache Fundamentals, cont.
cpu
lowest-level
cache
next-level
memory/cache
Cacheing Issues
cpu
lowest-level
cache
next-level
memory/cache
access
miss
Hardware implications on cache design
Examples of simple caches
82
A simple cache
tag
data
the tag identifies
the address of
the cached data
4 entries, each block holds one word, any block
can hold any word.
address string:
4 00000100
8 00001000
12 00001100
4 00000100
8 00001000
20 00010100
4 00000100
8 00001000
20 00010100
24 00011000
12 00001100
8 00001000
4 00000100
A simple cache
tag
data
the tag identifies
the address of
the cached data
4 entries, each block holds one word, any block
can hold any word.
address string:
4 00000100
8 00001000
12 00001100
4 00000100
8 00001000
20 00010100
4 00000100
8 00001000
20 00010100
24 00011000
12 00001100
8 00001000
4 00000100
A simpler cache
an index is used
to determine
which line an address
might be found in
4 entries, each block holds one word, each word
in memory maps to exactly one cache location.
00000100
tag
data
address string:
4 00000100
8 00001000
12 00001100
4 00000100
8 00001000
20 00010100
4 00000100
8 00001000
20 00010100
24 00011000
12 00001100
8 00001000
4 00000100
A simpler cache
an index is used
to determine
which line an address
might be found in
4 entries, each block holds one word, each word
in memory maps to exactly one cache location.
00000100
tag
data
address string:
4 00000100
8 00001000
12 00001100
4 00000100
8 00001000
20 00010100
4 00000100
8 00001000
20 00010100
24 00011000
12 00001100
8 00001000
4 00000100
A set-associative cache
tag
data
4 entries, each block holds one word, each word
in memory maps to one of a set of n cache lines
00000100
tag
data
address string:
4 00000100
8 00001000
12 00001100
4 00000100
8 00001000
20 00010100
4 00000100
8 00001000
20 00010100
24 00011000
12 00001100
8 00001000
4 00000100
A set-associative cache
tag
data
4 entries, each block holds one word, each word
in memory maps to one of a set of n cache lines
00000100
tag
data
address string:
4 00000100
8 00001000
12 00001100
4 00000100
8 00001000
20 00010100
4 00000100
8 00001000
20 00010100
24 00011000
12 00001100
8 00001000
4 00000100
Longer Cache Blocks
tag
data
4 entries, each block holds two words, each word
in memory maps to exactly one cache location
(this cache is twice the total size of the prior caches).
address string:
4 00000100
8 00001000
12 00001100
4 00000100
8 00001000
20 00010100
4 00000100
8 00001000
20 00010100
24 00011000
12 00001100
8 00001000
4 00000100
00000100
Longer Cache Blocks
tag
data (now 64 bits)
4 entries, each block holds two words, each word
in memory maps to exactly one cache location
(this cache is twice the total size of the prior caches).
address string:
4 00000100
8 00001000
12 00001100
4 00000100
8 00001000
20 00010100
4 00000100
8 00001000
20 00010100
24 00011000
12 00001100
8 00001000
4 00000100
00000100