| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Real-Time Workshop |
| Contents | Index |
| Learn more about Real-Time Workshop |
| On this page… |
|---|
Getting Advice About Optimizing Models for Code Generation Demos Illustrating Optimizations Minimizing Memory Requirements for Parameters and Data During Code Generation |
Using the Model Advisor, you can quickly analyze a model for code generation and identify aspects of your model that impede production deployment or limit code efficiency. You can select from a set of checks to run on a model's current configuration. The Model Advisor analyzes the model and generates check results providing suggestions for improvements in each area. Most Model Advisor diagnostics do not require the model to be in a compiled state; those that do are noted.
Before running the Model Advisor, select the target you plan to use for code generation. The Model Advisor works most effectively with ERT and ERT-based targets (targets based on the Real-Time Workshop Embedded Coder software).
Use the following Model Advisor demos to investigate optimizing models for code generation using the Model Advisor:
Note Demo models rtwdemo_advisor2 and rtwdemo_advisor3 require Stateflow and Fixed-Point Toolbox™ software. |
For more information on using the Model Advisor, see Consulting the Model Advisor in the Simulink documentation. For more information about the checks in the Model Advisor, see Real-Time Workshop Checks in the Real-Time Workshop documentation.
The rtwdemos demo suite includes a set of demonstration models that illustrate optimization settings and techniques. To access these demos, type
rtwdemos
or click the above command. The MATLAB Help browser opens the Real-Time Workshop demos page. Click Optimizations in the navigation pane. Use the listed demos to learn about the specific effects that optimization parameters and techniques have on models.
When the Real-Time Workshop product generates code, it creates an intermediate representation of your model (called model.rtw), which the Target Language Compiler parses to transform block computations, parameters, signals, and constant data into a high-level language, (for example, C). Parameters and data are normally copied into the model.rtw file, whether they originate in the model itself or come from variables or objects in a workspace.
Models which have large amounts of parameter and constant data (such as lookup tables) can tax memory resources and slow down code generation because of the need to copy their data to model.rtw. You can improve code generation performance by limiting the size of data that is copied by using a set_param command, described below.
Data vectors such as those for parameters, lookup tables, and constant blocks whose sizes exceed a specified value are not copied into the model.rtw file. In place of the data vectors, the Real-Time Workshop code generator places a special reference key in the intermediate file that enables the Target Language Compiler to access the data directly from the Simulink software when it is needed and format it directly into the generated code. This results in maintaining only one copy of large data vectors in memory.
You can specify the maximum number of elements that a parameter or other data source can have for the Real-Time Workshop code generator to represent it literally in the model.rtw file. Whenever this threshold size is exceeded, the product writes a reference to the data to the model.rtw file, rather than its values. The default threshold value is 10 elements, which you can verify with
get_param(0, 'RTWDataReferencesMinSize')
To set the threshold to a different value, type the following set_param function in the MATLAB Command Window:
set_param(0, 'RTWDataReferencesMinSize', <size>)
Provide an integer value for size that specifies the number of data elements above which reference keys are to be used in place of actual data values.
In addition to analyzing models with Model Advisor (see Getting Advice About Optimizing Models for Code Generation), you can use a variety of other tools and techniques that work with any code format. Here are some particularly useful ones:
Run the slupdate command to automatically convert older models (saved by prior versions or by the current one) to use current features. For details about what slupdate does, type
help slupdate
Before building, set optimization flags for the compiler (for example, -O2 for gcc, -Ot for the Microsoft Visual C++ compiler).
Directly inline C/C++ S-functions into the generated code by writing a TLC file for the S-function. See Creating Component Object Libraries and Enhancing Simulation Performance and the Target Language Compiler documentation for more information on inlining S-functions.
Use a Simulink data type other than double when possible. The available data types are Boolean, signed and unsigned 8-, 16-, and 32-bit integers, and 32- and 64-bit floats (a double is a 64-bit float). See Working with Data in the Simulink documentation for more information on data types. For a block-by-block summary, click showblockdatatypetable or type the command in the MATLAB Command Window.
Remove repeated values in lookup table data.
Use the Merge block to merge the output of signals wherever possible. This block is particularly helpful when you need to control the execution of function-call subsystems with a Stateflow chart. The following model shows an example of how to use the Merge block.

When more than one signal connected to a Merge block has a non-Auto storage class, all non-Auto signals connected to that block must be identically labeled and have the same storage class. When Merge blocks connect directly to one another, these rules apply to all signals connected to any of the Merge blocks in the group.
If you are licensed to use the Real-Time Workshop Embedded Coder product, see also Configuring Production Code Optimizations and Tips for Optimizing the Generated Code in the Real-Time Workshop Embedded Coder documentation.
![]() | Optimization Parameters Overview | Minimizing Computations and Storage for Intermediate Results | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |