Main Content

Enable and Reuse Local Block Outputs in Generated Code

This example shows how to specify block output as local variables. The code generator can potentially reuse these local variables in the generated code. Declaring block output as local variables conserves ROM consumption. Reusing local variables conserves RAM consumption, reduces data copies, and increases execution speed.

Example Model

  1. Use Inport, Outport, Gain, and Switch blocks to create the following model. In this example, the model is named local_variable_ex.

  2. For G2, open the Gain Block Parameters dialog box. Enter a value of 2.

  3. For G1, enter a value of 3.

  4. For the Switch block, open the Block Parameters dialog box. For the Criteria for passing first input parameter, select u2>=Threshold.

Generate Code Without Optimization

  1. Open the Model Configuration Parameters dialog box. Select the Solver pane. For the Type parameter, select Fixed-step.

  2. Clear the Configuration Parameters > Signal storage reuse parameter.

  3. Select the Code Generation > Report pane and select Create code generation report.

  4. Select the Code Generation pane. Select Generate code only, and then, in the model window, press Ctrl+B. When code generation is complete, an HTML code generation report appears.

  5. In the code generation report, select the local_variable_ex.c section and view the model step function. The Gain block outputs are the global variables local_variable_ex_B.G2 and local_variable_ex_B.G1.

/* Model step function */
void local_variable_ex_step(void)
{
  /* Switch: '<Root>/Switch' incorporates:
   *  Inport: '<Root>/In1'
   */
  if (local_variable_ex_U.In1 >= 0.0) {
    /* Gain: '<Root>/G2' */
    local_variable_ex_B.G2 = 2.0 * local_variable_ex_U.In1;

    /* Outport: '<Root>/Out1' */
    local_variable_ex_Y.Out1 = local_variable_ex_B.G2;
  } else {
    /* Gain: '<Root>/G1' */
    local_variable_ex_B.G1 = 3.0 * local_variable_ex_U.In1;

    /* Outport: '<Root>/Out1' */
    local_variable_ex_Y.Out1 = local_variable_ex_B.G1;
  }

  /* End of Switch: '<Root>/Switch' */

Enable Local Block Outputs and Generate Code

  1. Select the Configuration Parameters > Signal Storage Reuse parameter. The Signal Storage Reuse enables the following optimization parameters:

    • Enable local block outputs

    • Reuse local block outputs

    • Eliminate superfluous local variables (expression folding)

  2. Clear Reuse local block outputs and Eliminate superfluous local variables (expression folding).

  3. Generate code and view the model step function. There are three local variables in the model step function because you selected the optimization parameter Enable Local Block Outputs. The local variables rtb_G2 and rtb_G1 hold the outputs of the Gain blocks. The local variable rtb_Switch holds the output of the Switch block.

/* Model step function */
void local_variable_ex_step(void)
{
  real_T rtb_Switch;
  real_T rtb_G2;
  real_T rtb_G1;

  /* Switch: '<Root>/Switch' incorporates:
   *  Inport: '<Root>/In1'
   */
  if (local_variable_ex_U.In1 >= 0.0) {
    /* Gain: '<Root>/G2' */
    rtb_G2 = 2.0 * local_variable_ex_U.In1;
    rtb_Switch = rtb_G2;
  } else {
    /* Gain: '<Root>/G1' */
    rtb_G1 = 3.0 * local_variable_ex_U.In1;
    rtb_Switch = rtb_G1;
  }

  /* End of Switch: '<Root>/Switch' */

  /* Outport: '<Root>/Out1' */
  local_variable_ex_Y.Out1 = rtb_Switch;

Reuse Local Block Outputs and Generate Code

  1. Select the Configuration Parameters > Reuse local block outputs parameter.

  2. Generate code. In the local_variable_ex.c section, view the model step function. There is one local variable, rtb_G2, that the code generator uses three times.

/* Model step function */
void local_variable_ex_step(void)
{
  real_T rtb_G2;

  /* Switch: '<Root>/Switch' incorporates:
   *  Inport: '<Root>/In1'
   */
  if (local_variable_ex_U.In1 >= 0.0) {
    /* Gain: '<Root>/G2' */
    rtb_G2 = 2.0 * local_variable_ex_U.In1;
  } else {
    /* Gain: '<Root>/G1' */
    rtb_G2 = 3.0 * local_variable_ex_U.In1;
  }

  /* End of Switch: '<Root>/Switch' */

  /* Outport: '<Root>/Out1' */
  local_variable_ex_Y.Out1 = rtb_G2;

The extra temporary variable rtb_Switch and the associated data copy is not in the generated code.

See Also

Related Topics