MATLAB Examples

Optimize Generated Code Using Inline Invariant Signals

This example shows how to use inline invariant signals to optimize the generated code. This optimization transforms symbolic names of invariant signals into constant values.

The InlineInvariantSignals optimization:

  • Reduces ROM and RAM consumption.
  • Improves execution speed.

Contents

Example Model

Consider the model matlab:rtwdemo_inline_invariant_signals.

model = 'rtwdemo_inline_invariant_signals';
open_system(model);

Generate Code

Create a temporary folder (in your system temporary folder) for the build and inspection process.

currentDir = pwd;
[~,cgDir] = rtwdemodir();

Build the model using Simulink Coder.

rtwbuild(model)
### Starting build procedure for model: rtwdemo_inline_invariant_signals
### Successful completion of build procedure for model: rtwdemo_inline_invariant_signals

View the generated code without the optimization. These lines of code are in rtwdemo_inline_invariant_signals.c.

cfile = fullfile(cgDir,'rtwdemo_inline_invariant_signals_grt_rtw',...
    'rtwdemo_inline_invariant_signals.c');
rtwdemodbtype(cfile,'/* Output and update for atomic system',...
    '/* Model output', 1, 0);
/* Output and update for atomic system: '<Root>/InlinedConstFcn' */
void rtwdemo_inline__InlinedConstFcn(int32_T rtu_In1,
  B_InlinedConstFcn_rtwdemo_inl_T *localB, const ConstB_InlinedConstFcn_rtwdem_T
  *localC)
{
  /* Product: '<S1>/Product' */
  localB->Product = rtu_In1 * localC->Sum_p;
}

Enable Optimization

  1. Open the Configuration Parameters dialog box.
  2. On the Optimization->Signals and Parameters pane, select Inline Invariant Signals.

Alternatively, you can use the command-line API to enable the optimization:

set_param(model, 'InlineInvariantSignals', 'on');

Generate Code with Optimization

The generated code uses the numerical values of the folded constants instead of creating an additional structure (rtwdemo_inline_invariant_ConstB).

Build the model using Simulink Coder.

rtwbuild(model)
### Starting build procedure for model: rtwdemo_inline_invariant_signals
### Successful completion of build procedure for model: rtwdemo_inline_invariant_signals

View the generated code with the optimization. These lines of code are in rtwdemo_minmax.c.

rtwdemodbtype(cfile,'/* Output and update for atomic system', '/* Model output', 1, 0);
/* Output and update for atomic system: '<Root>/InlinedConstFcn' */
void rtwdemo_inline__InlinedConstFcn(int32_T rtu_In1,
  B_InlinedConstFcn_rtwdemo_inl_T *localB)
{
  /* Product: '<S1>/Product' */
  localB->Product = rtu_In1 << 5;
}

Close the model and code generation report.

bdclose(model)
rtwdemoclean;
cd(currentDir)