MATLAB Examples

Inline Numeric Values of Block Parameters

This example shows how to optimize the generated code by inlining the numeric values of block parameters. Block parameters include the Gain parameter of a Gain block and the table data and breakpoint sets of an n-D Lookup Table block.

This optimization determines whether numeric block parameters occupy global memory in the generated code. The optimization can:

  • Improve execution speed.
  • Reduce RAM and ROM consumption.

Contents

Explore Example Model

Open the example model rtwdemo_paraminline and configure it to show the generated names of blocks.

load_system('rtwdemo_paraminline')
set_param('rtwdemo_paraminline','HideAutomaticNames','off')
open_system('rtwdemo_paraminline')

The model contains blocks that have these numeric parameters:

  • The Gain parameters of the Gain blocks
  • The Constant value parameters of the Constant blocks
  • The table data and breakpoint sets of the n-D Lookup Table blocks

The output of the block G2, and the outputs of blocks upstream of G2, change only if you tune the values of the block parameters during simulation or during code execution. When you update the model diagram, these blocks and signal lines appear magenta in color.

Several blocks use Simulink.Parameter objects in the base workspace to set the values of their parameters. The parameter objects all use the storage class Auto, which means that you can configure the generated code to inline the parameter values.

Generate Code Without Optimization

Create a temporary folder for the build and inspection process.

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

Disable the optimization by setting Configuration Parameters > Optimization > Signals and Parameters > Default parameter behavior to Tunable.

set_param('rtwdemo_paraminline','DefaultParameterBehavior','Tunable')

Generate code from the model.

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

In the code generation report, view the source file rtwdemo_paraminline_data.c. The code defines a global structure that contains the block parameter values. Each block parameter in the model, such as a lookup table array, breakpoint set, or gain, appears as a field of the structure.

cfile = fullfile(cgDir,'rtwdemo_paraminline_grt_rtw','rtwdemo_paraminline_data.c');
rtwdemodbtype(cfile,'/* Block parameters (auto storage) */', '};', 1, 1);
/* Block parameters (auto storage) */
P_rtwdemo_paraminline_T rtwdemo_paraminline_P = {
  /* Variable: MAX_LIFT
   * Referenced by: '<Root>/Constant'
   */
  10.0,

  /* Variable: SLIDER_POS
   * Referenced by: '<Root>/Constant1'
   */
  0.0,

  /* Variable: T1Break
   * Referenced by: '<Root>/1D Lookup'
   */
  { -5.0, -4.0, -3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 },

  /* Variable: T1Data
   * Referenced by: '<Root>/1D Lookup'
   */
  { -1.0, -0.99, -0.98, -0.96, -0.76, 0.0, 0.76, 0.96, 0.98, 0.99, 1.0 },

  /* Variable: T2Break
   * Referenced by: '<Root>/2D Lookup'
   */
  { 1.0, 2.0, 3.0 },

  /* Variable: T2Data
   * Referenced by: '<Root>/2D Lookup'
   */
  { 4.0, 16.0, 10.0, 5.0, 19.0, 18.0, 6.0, 20.0, 23.0 },

  /* Expression: 2
   * Referenced by: '<Root>/G1'
   */
  2.0,

  /* Expression: -2
   * Referenced by: '<Root>/G2'
   */
  -2.0,

  /* Computed Parameter: uDLookup_maxIndex
   * Referenced by: '<Root>/2D Lookup'
   */
  { 2U, 2U }
};

You can tune the structure fields during code execution because they occupy global memory. However, at each step of the generated algorithm, the code must calculate the output of each block, including the outputs of the block G2 and the upstream blocks. View the algorithm in the model step function in the file rtwdemo_paraminline.c.

cfile = fullfile(cgDir,'rtwdemo_paraminline_grt_rtw','rtwdemo_paraminline.c');
rtwdemodbtype(cfile,'/* Model step function */','/* Model initialize function */',1,0);
/* Model step function */
void rtwdemo_paraminline_step(void)
{
  /* Outport: '<Root>/Out1' incorporates:
   *  Constant: '<Root>/Constant'
   *  Constant: '<Root>/Constant1'
   *  Gain: '<Root>/G1'
   *  Gain: '<Root>/G2'
   *  Inport: '<Root>/In1'
   *  Lookup_n-D: '<Root>/1D Lookup'
   *  Lookup_n-D: '<Root>/2D Lookup'
   *  Sum: '<Root>/Sum'
   */
  rtwdemo_paraminline_Y.Out1 = rtwdemo_paraminline_P.G1_Gain *
    rtwdemo_paraminline_U.In1 + rtwdemo_paraminline_P.G2_Gain * look2_binlx
    (rtwdemo_paraminline_P.MAX_LIFT, look1_binlx
     (rtwdemo_paraminline_P.SLIDER_POS, rtwdemo_paraminline_P.T1Break,
      rtwdemo_paraminline_P.T1Data, 10U), rtwdemo_paraminline_P.T2Break,
     rtwdemo_paraminline_P.T2Break, rtwdemo_paraminline_P.T2Data,
     rtwdemo_paraminline_P.uDLookup_maxIndex, 3U);
}

Generate Code with Optimization

Set Default parameter behavior to Inlined.

set_param('rtwdemo_paraminline','DefaultParameterBehavior','Inlined')

Generate code from the model.

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

In the code generation report, view the algorithm in the file rtwdemo_paraminline.c.

rtwdemodbtype(cfile,'/* Model step function */','/* Model initialize function */',1,0);
/* Model step function */
void rtwdemo_paraminline_step(void)
{
  /* Outport: '<Root>/Out1' incorporates:
   *  Gain: '<Root>/G1'
   *  Inport: '<Root>/In1'
   *  Sum: '<Root>/Sum'
   */
  rtwdemo_paraminline_Y.Out1 = 2.0 * rtwdemo_paraminline_U.In1 + 150.0;
}

The code does not allocate memory for block parameters or for parameter objects that use the storage class Auto. Instead, the code generator uses the parameter values from the model, and from the parameter objects, to calculate and inline the constant output of the block G2, 150.0. The generator also inlines the value of the Gain parameter of the Gain block G1, 2.0.

With the optimization, the generated code leaves out computationally expensive algorithmic code for blocks such as the lookup tables. The optimized code calculates the output of a block only if the output can change during execution. For this model, only the outputs of the Inport block In1, the Gain block G1, and the Sum block can change.

Close the model and the code generation report.

bdclose('rtwdemo_paraminline')
rtwdemoclean;
cd(currentDir)

Preserve Block Parameter Tunability

When you set Default parameter behavior to Inlined, you can preserve block parameter tunability by creating Simulink.Parameter objects for individual parameters. You can configure each object to appear in the code as a tunable field of the global parameter structure or as an individual global variable. You can change parameter values during code execution and interface the generated code with your own handwritten code. For more information, see docid:rtw_ug.f1023655.

Inline Invariant Signals

You can select the Inline invariant signals code generation option (which also places constant values in the generated code) only when you set Default parameter behavior to Inlined. See docid:rtw_ug.f1145247.