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 InvariantSignalsInline
.
model = 'InvariantSignalsInline';
open_system(model);
Generate Code
Build the model using Simulink® Coder™.
slbuild(model)
### Starting build procedure for: InvariantSignalsInline ### Successful completion of build procedure for: InvariantSignalsInline Build Summary Top model targets: Model Build Reason Status Build Duration ========================================================================================================================= InvariantSignalsInline Information cache folder or artifacts were missing. Code generated and compiled. 0h 0m 10.719s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 11.519s
View the generated code without the optimization. These lines of code are in InvariantSignalsInline.c
.
cfile = fullfile('InvariantSignalsInline_grt_rtw',... 'InvariantSignalsInline.c'); coder.example.extractLines(cfile,'/* Output and update for atomic system',... '/* Model output', 1, 0);
/* Output and update for atomic system: '<Root>/InlinedConstFcn' */ void InvariantSignal_InlinedConstFcn(int32_T rtu_In1, B_InlinedConstFcn_InvariantSi_T *localB, const ConstB_InlinedConstFcn_Invari_T *localC) { /* Product: '<S1>/Product' */ localB->Product = rtu_In1 * localC->Sum_p; }
Enable Optimization
Open the Configuration Parameters dialog box.
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.
Build the model using Simulink Coder.
slbuild(model)
### Starting build procedure for: InvariantSignalsInline ### Successful completion of build procedure for: InvariantSignalsInline Build Summary Top model targets: Model Build Reason Status Build Duration ===================================================================================================== InvariantSignalsInline Generated code was out of date. Code generated and compiled. 0h 0m 7.7848s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 8.5958s
View the generated code with the optimization in InvariantSignalsInline.c
.
coder.example.extractLines(cfile,... '/* Output and update for atomic system', '/* Model output', 1, 0);
/* Output and update for atomic system: '<Root>/InlinedConstFcn' */ void InvariantSignal_InlinedConstFcn(int32_T rtu_In1, B_InlinedConstFcn_InvariantSi_T *localB) { /* Product: '<S1>/Product' */ localB->Product = rtu_In1 << 5; }
Close the model and code generation report.
bdclose(model)