13
1 © 2015 The MathWorks, Inc. Better than Hand – Generating Highly Optimized Code using Simulink and Embedded Coder Lars Krause Application Engineering

Better than Hand – Generating Highly Optimized Code using ... · 7. Reduce logic Enable component reuse with Simulink Clone Detection Launch Simulink Clone Detection §Identify

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Better than Hand – Generating Highly Optimized Code using ... · 7. Reduce logic Enable component reuse with Simulink Clone Detection Launch Simulink Clone Detection §Identify

1© 2015 The MathWorks, Inc.

Better than Hand –Generating Highly Optimized Code using Simulink and Embedded Coder

Lars KrauseApplication Engineering

Page 2: Better than Hand – Generating Highly Optimized Code using ... · 7. Reduce logic Enable component reuse with Simulink Clone Detection Launch Simulink Clone Detection §Identify

2

ChallengesLimited time and resources are common constraints for development projects

§ Fit advanced algorithms into low-cost production hardware– Limited ROM, RAM, stack, and speed

§ Embedded device often not known during design– Need optimal implementation

§ Hand coding is process bottleneck – Adds bugs, delays, iterations

“The advantages of Model-Based Design over hand-coding in C can’t be overestimated.” Kazuhiro Ichikawa, Ono SokkiOno Sokki Reduces Development Time for Precision Automotive Speed Measurement Device

Page 3: Better than Hand – Generating Highly Optimized Code using ... · 7. Reduce logic Enable component reuse with Simulink Clone Detection Launch Simulink Clone Detection §Identify

3

SolutionsTechniques for accelerating the development process

Optimization Techniques1. Use optimal settings2. Optimize data types3. Target vector engines4. Use hardware support packages5. Reuse components6. Reduce variables 7. Reduce logic

Page 4: Better than Hand – Generating Highly Optimized Code using ... · 7. Reduce logic Enable component reuse with Simulink Clone Detection Launch Simulink Clone Detection §Identify

4

Launch Quick Start

1. Use optimal settings Embedded Coder Quick Start

§ Prepare your model for production code generation

§ Optimize generated code, independently of target

§ Find optimal settings with the Quick Start Tool

Select Optimization Objectives

Apply All Optimizations

Page 5: Better than Hand – Generating Highly Optimized Code using ... · 7. Reduce logic Enable component reuse with Simulink Clone Detection Launch Simulink Clone Detection §Identify

5

2. Optimize data typesSingle Precision Converter

Launch Single Precision Converter§ Bring new algorithms from

simulation to production

§ Convert double-precision systems to single precision

§ Save resources- Less memory footprint- Double precision not optimally

supported in many targets

Auto-convert to single precision

Generate code with less footprint

Page 6: Better than Hand – Generating Highly Optimized Code using ... · 7. Reduce logic Enable component reuse with Simulink Clone Detection Launch Simulink Clone Detection §Identify

6

Identify run-time bottlenecks

3. Target vector enginesReplace time-consuming code with vector instructions

§ Optimize code for your target

§ Generate highly optimized code using vector instruction sets

§ Increase real-time execution efficiency

Select code replacement

Generate code for vector engines

Page 7: Better than Hand – Generating Highly Optimized Code using ... · 7. Reduce logic Enable component reuse with Simulink Clone Detection Launch Simulink Clone Detection §Identify

7

4. Target vector enginesExecution times of a FIR filter - PIL benchmark results, ARM Cortex-A

Compiler optimization level

Inst

ruct

ions

opt

imiz

ed

Embedded Coder NEON16,8 µs Embedded Coder NEON

& compiler optimized14,1 µs

Run Format: [ANSI or Ne10], [gcc no opt or gcc -02], ARM 1Ghz Cortex A8

410,7 µs

Embedded Coder ANSI-C

185,5 µs

Embedded Coder ANSI-C & compiler optimized

§ Vector instructions have a significant impact on execution time

§ This impact can exceed the impact of compiler optimization

Page 8: Better than Hand – Generating Highly Optimized Code using ... · 7. Reduce logic Enable component reuse with Simulink Clone Detection Launch Simulink Clone Detection §Identify

8

4. Use hardware support packagesDownload hardware support packages with the Add-On Explorer

§ Fast code adaption to many targets- Model is the golden reference

for code generation- Generated code is optimized for

specific targets

§ MathWorks package support: - ARM, ..., Zynq

§ Additional packages: - NXP, TI, Infineon,

STMicroelectronics,…

The MATLAB Add-On Explorer

Page 9: Better than Hand – Generating Highly Optimized Code using ... · 7. Reduce logic Enable component reuse with Simulink Clone Detection Launch Simulink Clone Detection §Identify

9

Reusable functions

Reusability is not obvious

Reuse with Simulink functions

5. Reuse componentsReuse with Simulink functions

§ Clear reusability structure

§ Generate compact and efficient code

Generate efficient code from Simulink functions

Page 10: Better than Hand – Generating Highly Optimized Code using ... · 7. Reduce logic Enable component reuse with Simulink Clone Detection Launch Simulink Clone Detection §Identify

10

1) Pass scalar output as individual argument

6. Reduce variablesNew options for global RAM optimization

§ Reduced RAM usage- No additional variables needed

for intermediate results in both cases

2) Reuse input signals for output

Page 11: Better than Hand – Generating Highly Optimized Code using ... · 7. Reduce logic Enable component reuse with Simulink Clone Detection Launch Simulink Clone Detection §Identify

11

7. Reduce logicEnable component reuse with Simulink Clone Detection

Launch Simulink Clone Detection

§ Identify modeling clones

§ Improve model componentization

§ Enable Reuse:Replace clones with - Simulink functions or - library blocks

View results

Refactor your model

Page 12: Better than Hand – Generating Highly Optimized Code using ... · 7. Reduce logic Enable component reuse with Simulink Clone Detection Launch Simulink Clone Detection §Identify

12

7. Reduce logicPolyspace Code Prover

Launch Polyspace Code Prover § Remove unnecessary robustness code

§ Analyze generated and hand written C/C++ source code without program execution

§ Prove absence of run-time errors, e.g. overflow

Identify unreachable robustness code

Page 13: Better than Hand – Generating Highly Optimized Code using ... · 7. Reduce logic Enable component reuse with Simulink Clone Detection Launch Simulink Clone Detection §Identify

13

Solution SummaryAccelerating the software development process with automatic code generation

Optimization Techniques1. Use optimal settings2. Optimize data types3. Target vector engines4. Use hardware support packages5. Reuse components 6. Reduce variables 7. Reduce logic The code generated with Embedded Coder required

about 16% less RAM than the handwritten code used on a previous version of the ECU; the code met all project requirements for efficiency and structure. Mario Wünsche, DaimlerDaimler Designs Cruise Controller for Mercedes-Benz Trucks