Main Content

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.

Explore Example Model

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

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

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

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

set_param('InlineBlockParameters','DefaultParameterBehavior','Tunable')

Generate code from the model.

slbuild('InlineBlockParameters')
### Starting build procedure for: InlineBlockParameters
### Successful completion of build procedure for: InlineBlockParameters

Build Summary

Top model targets:

Model                  Build Reason                                         Status                        Build Duration
========================================================================================================================
InlineBlockParameters  Information cache folder or artifacts were missing.  Code generated and compiled.  0h 0m 10.215s 

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

In the code generation report, view the source file InlineBlockParameters_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(...
    'InlineBlockParameters_grt_rtw','InlineBlockParameters_data.c');
coder.example.extractLines(cfile,'/* Block parameters (default storage) */', '};', 1, 1);
/* Block parameters (default storage) */
P_InlineBlockParameters_T InlineBlockParameters_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 InlineBlockParameters.c.

cfile = fullfile('InlineBlockParameters_grt_rtw','InlineBlockParameters.c');
coder.example.extractLines(...
    cfile,'/* Model step function */','/* Model initialize function */',1,0);
/* Model step function */
void InlineBlockParameters_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'
   */
  InlineBlockParameters_Y.Out1 = InlineBlockParameters_P.G1_Gain *
    InlineBlockParameters_U.In1 + InlineBlockParameters_P.G2_Gain * look2_binlx
    (InlineBlockParameters_P.MAX_LIFT, look1_binlx
     (InlineBlockParameters_P.SLIDER_POS, InlineBlockParameters_P.T1Break,
      InlineBlockParameters_P.T1Data, 10U), InlineBlockParameters_P.T2Break,
     InlineBlockParameters_P.T2Break, InlineBlockParameters_P.T2Data,
     InlineBlockParameters_P.uDLookup_maxIndex, 3U);
}

Generate Code with Optimization

Set Default parameter behavior to Inlined.

set_param('InlineBlockParameters','DefaultParameterBehavior','Inlined')

Generate code from the model.

slbuild('InlineBlockParameters')
### Starting build procedure for: InlineBlockParameters
### Successful completion of build procedure for: InlineBlockParameters

Build Summary

Top model targets:

Model                  Build Reason                       Status                        Build Duration
======================================================================================================
InlineBlockParameters  Global variable MAX_LIFT changed.  Code generated and compiled.  0h 0m 7.7645s 

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

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

coder.example.extractLines(...
    cfile,'/* Model step function */','/* Model initialize function */',1,0);
/* Model step function */
void InlineBlockParameters_step(void)
{
  /* Outport: '<Root>/Out1' incorporates:
   *  Gain: '<Root>/G1'
   *  Inport: '<Root>/In1'
   *  Sum: '<Root>/Sum'
   */
  InlineBlockParameters_Y.Out1 = 2.0 * InlineBlockParameters_U.In1 +
    InlineBlockParameters_ConstB.G2;
}

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('InlineBlockParameters')

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 Create Tunable Calibration Parameter in the Generated Code.

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 Inline Invariant Signals.

See Also

Related Topics