Main Content

Optimize Generated Code Using Minimum and Maximum Values

To optimize the generated code for your model, you can choose an option to use input range information, also known as design minimum and maximum, that you specify on signals and parameters. These minimum and maximum values usually represent environmental limits, such as temperature, or mechanical and electrical limits, such as output ranges of sensors.

In the Configuration Parameters dialog box, on the Optimization pane, when you select the Optimize using specified minimum and maximum values check box, the software uses the minimum and maximum values to derive range information for downstream signals in the model. It then uses this derived range information to determine if it is possible to streamline the generated code by:

  • Reducing expressions to constants

  • Removing dead branches of conditional statements

  • Eliminating unnecessary mathematical operations

This optimization results in:

  • Reduced ROM and RAM consumption

  • Improved execution speed

Configure Your Model

To make optimization more likely:

  • Provide as much design minimum and maximum information as possible. Specify minimum and maximum values for signals and parameters in the model for:

    • Inport and Outport blocks

    • Block outputs

    • Block inputs, for example, for the MATLAB Function and Stateflow Chart blocks

    • Simulink.Signal objects

  • Before generating code, test the minimum and maximum values for signals and parameters. Otherwise, optimization might result in numerical mismatch with simulation. You can simulate your model with simulation range checking enabled. If errors or warnings occur, fix these issues before generating code.

     Enable Simulation Range Checking

  • Provide design minimum and maximum information upstream of blocks as close to the inputs of the blocks as possible. If you specify minimum and maximum values for a block output, these values are most likely to affect the outputs of the blocks immediately downstream.

Optimize Generated Code Using Specified Minimum and Maximum Values

This example shows how the minimum and maximum values specified on signals and parameters in a model are used to optimize the generated code.

Overview

The specified minimum and maximum values usually represent environmental limits, such as temperature, or mechanical and electrical limits, such as output ranges of sensors.

This optimization uses these values to streamline the generated code. For example, it reduces expressions to constants or removes dead branches of conditional statements.

NOTE: Make sure the minimum and maximum values that you specify are valid limits. Otherwise, this optimization might result in numerical mismatch with simulation.

The benefits of optimizing the generated code are:

  • Reducing the ROM and RAM consumption.

  • Improving the execution speed.

Review Minimum and Maximum Information

Consider the MinMaxOptimization model. In this model, there are minimum and maximum values specified on Inports and on the gain parameter of the Gain block.

model = 'MinMaxOptimization';
open_system(model);

Generate Code Without This Optimization

First, generate code for this model without considering the min and max values.

slbuild(model)
### Starting build procedure for: MinMaxOptimization
### Successful completion of build procedure for: MinMaxOptimization

Build Summary

Top model targets built:

Model               Action                        Rebuild Reason                                    
====================================================================================================
MinMaxOptimization  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 12.431s

A portion of MinMaxOptimization.c is listed below.

cfile = fullfile('MinMaxOptimization_ert_rtw','MinMaxOptimization.c');
coder.example.extractLines(cfile,'/* Model step', '/* Model initialize', 1, 0);
/* Model step function */
void MinMaxOptimization_step(void)
{
  int32_T tmp;

  /* Sum: '<Root>/Sum' incorporates:
   *  Inport: '<Root>/U1'
   *  Inport: '<Root>/U2'
   *  Sum: '<Root>/Sum2'
   *  Switch: '<Root>/Switch'
   */
  tmp = U1 + U2;

  /* Switch: '<Root>/Switch' incorporates:
   *  Gain: '<Root>/Gain'
   *  Inport: '<Root>/U3'
   *  RelationalOperator: '<Root>/Relational Operator'
   *  Sum: '<Root>/Sum'
   */
  if (tmp <= k * U3) {
    /* Outport: '<Root>/Out1' incorporates:
     *  Sum: '<Root>/Sum2'
     */
    rtY.Out1 = tmp + U3;
  } else {
    /* Outport: '<Root>/Out1' incorporates:
     *  Inport: '<Root>/U1'
     *  Inport: '<Root>/U2'
     *  Product: '<Root>/Product'
     */
    rtY.Out1 = U1 * U2 * U3;
  }
}

Enable This Optimization

  1. Open the Configuration Parameters dialog box.

  2. On the Optimization pane, select Optimize using the specified minimum and maximum values.

Alternatively, you can enable this optimization by setting the command-line parameter.

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

Generate Code with This Optimization

In the model, with the specified minimum and maximum values for U1 and U2, the sum of U1 and U2 has a minimum value of 50. Considering the range of U3 and the specified minimum and maximum values for the Gain block parameter, the maximum value of the Gain block's output is 40.

The output of the Relational Operator block remains false, and the output of the Switch block remains the product of the three inputs.

Build the model.

slbuild(model)
### Starting build procedure for: MinMaxOptimization
### Successful completion of build procedure for: MinMaxOptimization

Build Summary

Top model targets built:

Model               Action                        Rebuild Reason                   
===================================================================================
MinMaxOptimization  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 9.9856s

View the optimized code from MinMaxOptimization.c.

cfile = fullfile('MinMaxOptimization_ert_rtw','MinMaxOptimization.c');
coder.example.extractLines(cfile,'/* Model step', '/* Model initialize', 1, 0);
/* Model step function */
void MinMaxOptimization_step(void)
{
  /* Outport: '<Root>/Out1' incorporates:
   *  Inport: '<Root>/U1'
   *  Inport: '<Root>/U2'
   *  Inport: '<Root>/U3'
   *  Product: '<Root>/Product'
   *  Switch: '<Root>/Switch'
   */
  rtY.Out1 = U1 * U2 * U3;
}

Close the model and cleanup.

bdclose(model)

Limitations

  • This optimization does not take into account minimum and maximum values for:

    • Merge block inputs. To work around this issue, use a Simulink.Signal object on the Merge block output and specify the range on this object.

    • Bus elements.

    • Conditionally-executed subsystem (such as a triggered subsystem) block outputs that are directly connected to an Outport block.

      Outport blocks in conditionally-executed subsystems can have an initial value specified for use only when the system is not triggered. In this case, the optimization cannot use the range of the block output because the range might not cover the initial value of the block.

  • If you use Polyspace® software to verify code generated using this optimization, it might mark code that was previously green as orange. For example, if your model contains a division where the range of the denominator does not include zero, the generated code does not include protection against division by zero. Polyspace might mark this code orange because it does not have information about the minimum and maximum values for the inputs to the division.

    Polyspace Code Prover™ automatically captures some minimum and maximum values specified in the MATLAB® workspace, for example, for Simulink.Signal and Simulink.Parameter objects. In this example, to provide range information to the Polyspace software, use a Simulink.Signal object on the input of the division and specify a range that does not include zero.

    Polyspace Code Prover stores these values in a Data Range Specification (DRS) file. However, they do not capture all minimum and maximum values in your Simulink® model. To provide additional minimum and maximum information to Polyspace, you can manually define a DRS file.

  • If you are using double-precision data types and the non-finite numbers configuration parameter is selected, this optimization does not occur.

  • If your model contains multiple instances of a reusable subsystem and each instance uses input signals with different minimum and maximum values, this optimization might result in different generated code for each subsystem so code reuse does not occur. Without this optimization, code is generated once for the subsystem and shares this code among the multiple instances of the subsystem.

  • The Model Advisor check Check safety-related optimization settings (Simulink Check) generates a warning if this option is selected. For many safety-critical applications, removing dead code automatically is unacceptable because doing so might make code untraceable.

See Also

Related Topics