MATLAB Examples

Use Global to Hold Temporary Results

The code generator uses global and local variables when you select None versus when you select Use global to hold temporary results.

Contents

Example Model

In the model matlab:rtwdemo_optimize_global_ebf, an Assignment block assigns values coming from the Inport and Constant blocks to an output signal. The output signal feeds into a Gain block.

model = 'rtwdemo_optimize_global_ebf';
load_system('rtwdemo_optimize_global_ebf')

Generate Code without Optimization

  1. In the Configuration Parameters dialog box, verify that the Signal storage reuse parameter is selected.
  2. In the Configuration Parameters dialog box, for the Optimize global access parameter, select None or enter the following command in the MATLAB Command Window:
set_param('rtwdemo_optimize_global_ebf','GlobalVariableUsage','None');

In your system's temporary folder, create a folder for the build and inspection process:

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

Build the model.

rtwbuild(model);
### Starting build procedure for model: rtwdemo_optimize_global_ebf
### Successful completion of build procedure for model: rtwdemo_optimize_global_ebf

View the generated code without the optimization. Here is a portion of rtwdemo_optimize_global_ebf.c.

cfile = fullfile(cgDir,'rtwdemo_optimize_global_ebf_ert_rtw',...
    'rtwdemo_optimize_global_ebf.c');
rtwdemodbtype(cfile,'/* Model step','/* Model initialize',1, 0);
/* Model step function */
void rtwdemo_optimize_global_ebf_step(void)
{
  real_T rtb_Assignment[5];
  int32_T i;

  /* Assignment: '<Root>/Assignment' incorporates:
   *  Constant: '<Root>/Constant'
   *  Inport: '<Root>/In1'
   */
  for (i = 0; i < 5; i++) {
    rtb_Assignment[i] = rtCP_Constant_Value[i];
  }

  rtb_Assignment[1] = rtU.In1;

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

  /* Outport: '<Root>/Out1' incorporates:
   *  Gain: '<Root>/Gain'
   */
  for (i = 0; i < 5; i++) {
    rtY.Out1[i] = 2.0 * rtb_Assignment[i];
  }

  /* End of Outport: '<Root>/Out1' */
}

The code assigns values to the local vector rtb_Assignment. The last statement copies the values in the local vector rtb_Assignment to the global vector rtY.Out1. Fewer global variable references result in improved execution speed. The code uses more instructions for global variable references than for local variable references.

In the Static Code Metrics Report, examine the Global Variables section.

  1. In the Code Generation Report window, select Static Code Metrics Report.
  2. Scroll down to the Global Variables section.
  3. Select the [+] sign before each variable to expand it.

The total number of reads and writes for global variables is 2.

Generate Code with Optimization

In the Configuration Parameters dialog box, for the Optimize global access parameter, select Use global to hold temporary results, or enter the following command in the MATLAB Command Window:

set_param('rtwdemo_optimize_global_ebf',...
    'GlobalVariableUsage','Use global to hold temporary results');

Build the model.

rtwbuild(model);
### Starting build procedure for model: rtwdemo_optimize_global_ebf
### Successful completion of build procedure for model: rtwdemo_optimize_global_ebf

View the generated code with the optimization. Here is a portion of rtwdemo_optimize_global_ebf.c.

cfile = fullfile(cgDir,'rtwdemo_optimize_global_ebf_ert_rtw',...
    'rtwdemo_optimize_global_ebf.c');
rtwdemodbtype(cfile,'/* Model step','/* Model initialize',1, 0);
/* Model step function */
void rtwdemo_optimize_global_ebf_step(void)
{
  int32_T i;

  /* Assignment: '<Root>/Assignment' incorporates:
   *  Constant: '<Root>/Constant'
   *  Inport: '<Root>/In1'
   */
  for (i = 0; i < 5; i++) {
    rtY.Out1[i] = rtCP_Constant_Value[i];
  }

  rtY.Out1[1] = rtU.In1;

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

  /* Outport: '<Root>/Out1' incorporates:
   *  Gain: '<Root>/Gain'
   */
  for (i = 0; i < 5; i++) {
    rtY.Out1[i] *= 2.0;
  }

  /* End of Outport: '<Root>/Out1' */
}

The code assigns values to the global vector rtY.Out1 without using a local variable. This assignment improves ROM and RAM consumption and reduces data copies. The code places the value in the destination variable for each assignment instead of copying the value at the end. In the Static Code Metrics Report, examine the Global Variables section.

As a result of using global variables to hold local results, the total number of reads and writes for global variables has increased from 2 to 5. This optimization reduces data copies by reusing global variables.

Close the code generation report.

rtwdemoclean;
cd(currentDir)