Main Content

Inline Invariant Signals

You can optimize the generated code by selecting Inline invariant signals on the Optimization pane. The generated code uses the numerical values of the invariant signals instead of their symbolic names.

An invariant signal is a block output signal that does not change during Simulink® simulation. For example, the signal S3 is an invariant signal. An invariant signal is not the same as an invariant constant. The two constants (1 and 2) and the gain value of 3 are invariant constants. To inline invariant constants, set Default parameter behavior to Inlined.

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.

Example Model

Consider the model 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: rtwdemo_inline_invariant_signals
### Successful completion of build procedure for: rtwdemo_inline_invariant_signals

Build Summary

Top model targets built:

Model                             Action                       Rebuild Reason                                    
=================================================================================================================
rtwdemo_inline_invariant_signals  Code generated and compiled  Code generation information file does not exist.  

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 13.899s

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 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: rtwdemo_inline_invariant_signals
### Successful completion of build procedure for: rtwdemo_inline_invariant_signals

Build Summary

Top model targets built:

Model                             Action                       Rebuild Reason                   
================================================================================================
rtwdemo_inline_invariant_signals  Code generated and compiled  Generated code was out of date.  

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 11.073s

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)

See Also

Related Topics