Optimize Global Variable Usage - MATLAB & Simulink Example (original) (raw)

Main Content

To tune your application and choose tradeoffs for execution speed and memory usage, you can choose a global variable reference optimization for the generated code.

On the Configuration Parameters dialog box, in the Optimize global data access drop-down list, three parameter options control global variable usage optimizations.

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.

Example Model

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

UseGlobalsForTemporaryResultsModel.png

model = 'UseGlobalsForTemporaryResults'; load_system('UseGlobalsForTemporaryResults')

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 data access parameter, select None or enter the following command in the MATLABĀ® Command Window:

set_param('UseGlobalsForTemporaryResults','GlobalVariableUsage','None');

Build the model.

Searching for referenced models in model 'UseGlobalsForTemporaryResults'.

Total of 1 models to build.

Starting build procedure for: UseGlobalsForTemporaryResults

Successful completion of build procedure for: UseGlobalsForTemporaryResults

Build Summary

Top model targets:

Model Build Reason Status Build Duration

UseGlobalsForTemporaryResults Information cache folder or artifacts were missing. Code generated and compiled. 0h 0m 10.294s

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

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

cfile = fullfile(pwd,'UseGlobalsForTemporaryResults_ert_rtw',... 'UseGlobalsForTemporaryResults.c'); coder.example.extractLines(cfile,'/* Model step','/* Model initialize',1, 0);

/* Model step function */ void UseGlobalsForTemporaryResults_step(void) { real_T rtb_Assignment[5]; int32_T i;

/* SignalConversion generated from: '/Assignment' incorporates:

/* End of SignalConversion generated from: '/Assignment' */

/* Assignment: '/Assignment' incorporates:

/* Outport: '/Out1' incorporates:

/* End of Outport: '/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 data access parameter, select Use global to hold temporary results, or enter the following command in the MATLAB Command Window:

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

Build the model.

Searching for referenced models in model 'UseGlobalsForTemporaryResults'.

Total of 1 models to build.

Starting build procedure for: UseGlobalsForTemporaryResults

Successful completion of build procedure for: UseGlobalsForTemporaryResults

Build Summary

Top model targets:

Model Build Reason Status Build Duration

UseGlobalsForTemporaryResults Generated code was out of date. Code generated and compiled. 0h 0m 8.9326s

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

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

cfile = fullfile(pwd,'UseGlobalsForTemporaryResults_ert_rtw',... 'UseGlobalsForTemporaryResults.c'); coder.example.extractLines(cfile,'/* Model step','/* Model initialize',1, 0);

/* Model step function */ void UseGlobalsForTemporaryResults_step(void) { int32_T i;

/* SignalConversion generated from: '/Assignment' incorporates:

/* End of SignalConversion generated from: '/Assignment' */

/* Assignment: '/Assignment' incorporates:

/* Outport: '/Out1' incorporates:

/* End of Outport: '/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.

Minimize Global Data Access

Generate optimized code that reads from and writes to global variables less frequently.

Example Model

In the model MinimizeGlobalDataAccess, five signals feed into a Multiport Switch block.

model = 'MinimizeGlobalDataAccess'; load_system('MinimizeGlobalDataAccess.slx')

MinimizeGlobalDataAccessModel.png

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 data access parameter, select None or enter the following command in the MATLABĀ® Command Window:

set_param('MinimizeGlobalDataAccess','GlobalVariableUsage','None');

Build the model.

Searching for referenced models in model 'MinimizeGlobalDataAccess'.

Total of 1 models to build.

Starting build procedure for: MinimizeGlobalDataAccess

Successful completion of build procedure for: MinimizeGlobalDataAccess

Build Summary

Top model targets:

Model Build Reason Status Build Duration

MinimizeGlobalDataAccess Information cache folder or artifacts were missing. Code generated and compiled. 0h 0m 12.416s

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

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

cfile = fullfile(pwd,'MinimizeGlobalDataAccess_ert_rtw',... 'MinimizeGlobalDataAccess.c'); coder.example.extractLines(cfile,'/* Model step','/* Model initialize',1, 0);

/* Model step function / void MinimizeGlobalDataAccess_step(void) { / MultiPortSwitch: '/Multiport Switch' incorporates:

case 2: /* Outport: '/Out1' incorporates: * Constant: '/Constant1' */ rtY.Out1 = 2.0; break;

case 3: /* Outport: '/Out1' incorporates: * Constant: '/Constant2' */ rtY.Out1 = 3.0; break;

default: /* Outport: '/Out1' incorporates: * Constant: '/Constant3' */ rtY.Out1 = 4.0; break; }

/* End of MultiPortSwitch: '/Multiport Switch' */ }

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 5.

Enable Optimization and Generate Code

In the Configuration Parameters dialog box, for the Optimize global data access parameter, select Minimize global data access or enter the following command in the MATLAB Command Window:

set_param('MinimizeGlobalDataAccess',... 'GlobalVariableUsage','Minimize global data access');

Build the model.

Searching for referenced models in model 'MinimizeGlobalDataAccess'.

Total of 1 models to build.

Starting build procedure for: MinimizeGlobalDataAccess

Successful completion of build procedure for: MinimizeGlobalDataAccess

Build Summary

Top model targets:

Model Build Reason Status Build Duration

MinimizeGlobalDataAccess Generated code was out of date. Code generated and compiled. 0h 0m 9.0875s

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

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

cfile = fullfile(pwd,'MinimizeGlobalDataAccess_ert_rtw',... 'MinimizeGlobalDataAccess.c'); coder.example.extractLines(cfile,'/* Model step','/* Model initialize',1, 0);

/* Model step function */ void MinimizeGlobalDataAccess_step(void) { real_T tmp_Out1;

/* MultiPortSwitch: '/Multiport Switch' incorporates:

case 2: /* Outport: '/Out1' incorporates: * Constant: '/Constant1' */ tmp_Out1 = 2.0; break;

case 3: /* Outport: '/Out1' incorporates: * Constant: '/Constant2' */ tmp_Out1 = 3.0; break;

default: /* Outport: '/Out1' incorporates: * Constant: '/Constant3' */ tmp_Out1 = 4.0; break; }

/* End of MultiPortSwitch: '/Multiport Switch' */

/* Outport: '/Out1' */ rtY.Out1 = tmp_Out1; }

In MinimizeGlobalDataAccess.c, the code assigns a constant value to the local variable tmp_Out1 in each case statement. The last statement in the code copies the value of tmp_Out1 to the global variable rtY.Out1. Fewer global variable references result in fewer instructions and improved execution speed.

In the Static Code Metrics Report, examine the Global Variables section. As a result of minimizing global data accesses, the total number of reads and writes for global variables has decreased from 5 to 2.

Close the code generation report.

See Also

Optimize global data access

Topics