8/10/2019 5-ProgramDesign
1/78
8/10/2019 5-ProgramDesign
2/78
8/10/2019 5-ProgramDesign
3/78
8/10/2019 5-ProgramDesign
4/78
8/10/2019 5-ProgramDesign
5/78
8/10/2019 5-ProgramDesign
6/78
8/10/2019 5-ProgramDesign
7/78
8/10/2019 5-ProgramDesign
8/78
8/10/2019 5-ProgramDesign
9/78
8/10/2019 5-ProgramDesign
10/78
8/10/2019 5-ProgramDesign
11/78
8/10/2019 5-ProgramDesign
12/78
8/10/2019 5-ProgramDesign
13/78
8/10/2019 5-ProgramDesign
14/78
8/10/2019 5-ProgramDesign
15/78
8/10/2019 5-ProgramDesign
16/78
8/10/2019 5-ProgramDesign
17/78
8/10/2019 5-ProgramDesign
18/78
8/10/2019 5-ProgramDesign
19/78
8/10/2019 5-ProgramDesign
20/78
8/10/2019 5-ProgramDesign
21/78
8/10/2019 5-ProgramDesign
22/78
8/10/2019 5-ProgramDesign
23/78
8/10/2019 5-ProgramDesign
24/78
8/10/2019 5-ProgramDesign
25/78
8/10/2019 5-ProgramDesign
26/78
8/10/2019 5-ProgramDesign
27/78
8/10/2019 5-ProgramDesign
28/78
8/10/2019 5-ProgramDesign
29/78
8/10/2019 5-ProgramDesign
30/78
8/10/2019 5-ProgramDesign
31/78
8/10/2019 5-ProgramDesign
32/78
8/10/2019 5-ProgramDesign
33/78
8/10/2019 5-ProgramDesign
34/78
8/10/2019 5-ProgramDesign
35/78
8/10/2019 5-ProgramDesign
36/78
8/10/2019 5-ProgramDesign
37/78
8/10/2019 5-ProgramDesign
38/78
8/10/2019 5-ProgramDesign
39/78
8/10/2019 5-ProgramDesign
40/78
8/10/2019 5-ProgramDesign
41/78
8/10/2019 5-ProgramDesign
42/78
8/10/2019 5-ProgramDesign
43/78
8/10/2019 5-ProgramDesign
44/78
8/10/2019 5-ProgramDesign
45/78
8/10/2019 5-ProgramDesign
46/78
8/10/2019 5-ProgramDesign
47/78
8/10/2019 5-ProgramDesign
48/78
8/10/2019 5-ProgramDesign
49/78
8/10/2019 5-ProgramDesign
50/78
8/10/2019 5-ProgramDesign
51/78
8/10/2019 5-ProgramDesign
52/78
8/10/2019 5-ProgramDesign
53/78
8/10/2019 5-ProgramDesign
54/78
8/10/2019 5-ProgramDesign
55/78
8/10/2019 5-ProgramDesign
56/78
8/10/2019 5-ProgramDesign
57/78
8/10/2019 5-ProgramDesign
58/78
8/10/2019 5-ProgramDesign
59/78
8/10/2019 5-ProgramDesign
60/78
8/10/2019 5-ProgramDesign
61/78
8/10/2019 5-ProgramDesign
62/78
8/10/2019 5-ProgramDesign
63/78
8/10/2019 5-ProgramDesign
64/78
8/10/2019 5-ProgramDesign
65/78
8/10/2019 5-ProgramDesign
66/78
8/10/2019 5-ProgramDesign
67/78
8/10/2019 5-ProgramDesign
68/78
stemas Embebidos 2013/14
ogram Design and Analysis 68
d cc ][
Performance profiling
Count the nr of times procedures or basicblocks are called (not time)Use a counter on that block/procedureAnalyse PC during executionHLL may hide long sequence ofinstructions
SE 2013/14 - Program design and analysis - pbrandao (see at beginning)
135
d cc ][
Performance optimization hints
Use registers efficiently.Use page mode memory accesses.
Re-arrange variables to reference themcontiguously
Analyse cache behaviour:instruction conflicts can be handled byrewriting code, rescheduling;
conflicting scalar data can easily be moved;
conflicting array data can be moved,padded.
136
SE 2013/14 - Program design and analysis - pbrandao (see at beginning)
8/10/2019 5-ProgramDesign
69/78
stemas Embebidos 2013/14
ogram Design and Analysis 69
d cc ][
HLL Performance optimization
Use a better algorithm or data structureEnable compiler optimizations
The ARM Infocenter as a coding practice
Don't optimize what doesn't matter
SE 2013/14 - Program design and analysis - pbrandao (see at beginning)
137
From The Practice ofProgramming Kernighanand Pike
d cc ][
HLL Performance optimization:tuning the code
Collect common sub expressions
Replace expensive operations by cheap ones
Unroll or eliminate loops
Cache frequently-used values.
Write a special-purpose allocator
Buffer input and output
Handle special cases separately
Precompute results
Use approximate values
SE 2013/14 - Program design and analysis - pbrandao (see at beginning)
138
From The Practice ofProgramming Kernighanand Pike
http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0205j/CJAFJCFG.htmlhttp://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0205j/CJAFJCFG.html8/10/2019 5-ProgramDesign
70/78
stemas Embebidos 2013/14
ogram Design and Analysis 70
Program level Energy andPower Analysis andOptimization
SE 2013/14 - Program design and analysis - pbrandao (see at beginning)
139
d cc ][
Energy/power optimization
Energy : ability to do work.Most important in battery-powered systems.
Power : energy per unit time.Important even in wall-plug systems---powerbecomes heat.
140
SE 2013/14 - Program design and analysis - pbrandao (see at beginning)
8/10/2019 5-ProgramDesign
71/78
stemas Embebidos 2013/14
ogram Design and Analysis 71
d cc ][
Measuring energy consumption
Execute a small loop, measure current:
SE 2013/14 - Program design and analysis - pbrandao (see at beginning)
141
while (TRUE){testCode();
}
IA
CPU
d cc ][
Sources of energy consumption
Relative energy per operation (Catthoor et al):
memory transfer: 33
external I/O: 10
SRAM write: 9
SRAM read: 4.4multiply: 3.6
add: 1
SE 2013/14 - Program design and analysis - pbrandao (see at beginning)
142
8/10/2019 5-ProgramDesign
72/78
stemas Embebidos 2013/14
ogram Design and Analysis 72
d cc ][
Cache behaviour is important
Energy consumption has a sweet spot ascache size changes:
cache too small : program thrashes, burningenergy on external memory accesses;
cache too large : cache itself burns too muchpower.
143
SE 2013/14 - Program design and analysis - pbrandao (see at beginning)
d cc ][
Cache sweet spot
[Li98] 1998 IEEE
144
SE 2013/14 - Program design and analysis - pbrandao (see at beginning)
8/10/2019 5-ProgramDesign
73/78
stemas Embebidos 2013/14
ogram Design and Analysis 73
d cc ][
Optimizing for energy
First-order optimization:high performance = low energy .
Not many instructions trade speed forenergy.
145
SE 2013/14 - Program design and analysis - pbrandao (see at beginning)
d cc ][
Optimizing for energy, cont d.
Use registers efficiently.Identify and eliminate cache conflicts.Moderate loop unrolling eliminates someloop overhead instructions.Eliminate pipeline stalls.Inlining procedures may help: reduceslinkage, but may increase cachethrashing.
146
SE 2013/14 - Program design and analysis - pbrandao (see at beginning)
8/10/2019 5-ProgramDesign
74/78
stemas Embebidos 2013/14
ogram Design and Analysis 74
d cc ][
Efficient loops
General rules:Don t use function calls.
Keep loop body small to enable local repeat(only forward branches).
Use unsigned integer for loop counter.
Use
8/10/2019 5-ProgramDesign
75/78
stemas Embebidos 2013/14
ogram Design and Analysis 75
Analysis and Optimizationof Program Size
SE 2013/14 - Program design and analysis - pbrandao (see at beginning)
149
d cc ][
Optimizing for program size
Goal:reduce hardware cost of memory;
reduce power consumption of memory units.
Two opportunities:data;
instructions.
150
SE 2013/14 - Program design and analysis - pbrandao (see at beginning)
This big
8/10/2019 5-ProgramDesign
76/78
stemas Embebidos 2013/14
ogram Design and Analysis 76
d cc ][
Data size minimization
Reuse constants, variables, data buffers indifferent parts of code.
Requires careful verification of correctness.
Generate data using instructions.
151
SE 2013/14 - Program design and analysis - pbrandao (see at beginning)
d cc ][
Reducing code size
Avoid function inlining.Choose CPU with compact instructions.
ARM has Thumb and ARM instruction sets
Thumb are 16 bit instructions (see diff )
Use specialized instructions where possible.
152
SE 2013/14 - Program design and analysis - pbrandao (see at beginning)
http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0068b/ch02s02s09.htmlhttp://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0068b/ch02s02s09.html8/10/2019 5-ProgramDesign
77/78
stemas Embebidos 2013/14
ogram Design and Analysis 77
d cc ][
Size Hints
Use the smallest possible data typeDon't store what you can easilyrecomputed
This may have a cost in run-time
SE 2013/14 - Program design and analysis - pbrandao (see at beginning)
153
From The Practice ofProgramming Kernighanand Pike
Program design andanalysisThe End
154
SE 2013/14 - Program design and analysis - pbrandao (see at beginning)
8/10/2019 5-ProgramDesign
78/78
stemas Embebidos 2013/14
d cc ][
Summary
Components for Embedded ProgramsModels of ProgramsAssembly, Linking, and LoadingBasic Compilation TechniquesProgram OptimizationProgram-level Performance Analysis
Software Performance Optimization
155
SE 2013/14 - Program design and analysis - pbrandao (see at beginning)