Reuse Global Block Outputs in the Generated Code
Reduce ROM and RAM consumption and data copies and increase execution speed of generated code. Configure the code generator to reuse global variables by selecting the model configuration parameter Reuse global block outputs.
Example Model
In the Command Window, open the GlobalReuse
model.
model='GlobalReuse'; open_system('GlobalReuse')
Generate Code Without Optimization
On the Configuration Parameters dialog box, verify that Signal storage reuse is selected.
On the Code Generation > Report pane, verify that Generate static code metrics is selected.
Clear Reuse global block outputs and click Apply. Alternatively, you can programmatically set this parameter:
set_param(model,'GlobalBufferReuse', 'off');
Press Ctrl+B to generate code.
slbuild(model);
### Starting build procedure for: GlobalReuse ### Successful completion of build procedure for: GlobalReuse Build Summary Top model targets: Model Build Reason Status Build Duration ============================================================================================================== GlobalReuse Information cache folder or artifacts were missing. Code generated and compiled. 0h 0m 12.557s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 13.376s
View the generated code without the optimization. Here is a portion of GlobalReuse.c
.
cfile = fullfile('GlobalReuse_ert_rtw','GlobalReuse.c'); coder.example.extractLines(cfile,'/* Model step','/* Model initialize',1, 0);
/* Model step function */ void GlobalReuse_step(void) { /* Sum: '<Root>/Sum' incorporates: * Delay: '<Root>/Delay' * Inport: '<Root>/In1' */ rtDW.Delay_DSTATE += rtU.In1; /* Outport: '<Root>/Out1' incorporates: * Delay: '<Root>/Delay' */ rtY.Out1 = rtDW.Delay_DSTATE; }
The generated code contains a data copy to the global variable rtDW.Delay_DSTATE
. Open the Static Code Metrics Report. The total number of reads and writes for global variables is 8. The total size is 32 bytes.
Generate Code With Optimization
On the Configuration Parameters dialog box, select Reuse global block outputs and click Apply. Alternatively, you can programmatically set this parameter:
set_param(model,'GlobalBufferReuse', 'on');
Generate code.
slbuild(model);
### Starting build procedure for: GlobalReuse ### Successful completion of build procedure for: GlobalReuse Build Summary Top model targets: Model Build Reason Status Build Duration ========================================================================================== GlobalReuse Generated code was out of date. Code generated and compiled. 0h 0m 8.3567s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 9.1359s
View the generated code with the optimization. Here is a portion of GlobalReuse.c
.
cfile = fullfile('GlobalReuse_ert_rtw','GlobalReuse.c'); coder.example.extractLines(cfile,'/* Model step','/* Model initialize',1, 0);
/* Model step function */ void GlobalReuse_step(void) { /* Sum: '<Root>/Sum' incorporates: * Delay: '<Root>/Delay' * Inport: '<Root>/In1' */ rtY.Out1 += rtU.In1; }
The code generator eliminates a data copy, reduces two statements to one statement and three global variables to two global variables.
Open the Static Code Metrics Report. For global variables, this optimization reduces the total number of reads and writes for global variables from 8 to 5 and the total size from 32 bytes to 24 bytes.
bdclose(model)