Main Content

Reduce Memory Usage for Models Containing Referenced Models

This example shows how you can generate RAM efficient code for models containing referenced models that have no function prototype control specifications. When you select the configuration parameter Reuse output buffers of Model blocks, the code generator analyzes the referenced model contents and interacts with other optimizations to determine if buffer reuse is possible. If buffer reuse is possible, the code generator tries to reuse existing signal memory or generates reusable temporary buffers to hold referenced model outputs. Reusing buffers can significantly reduce RAM consumption.

Open Example Model

Open the example model RefBufferReuse containing two instances of the referenced model RefModel, which does not have a function prototype control specification.

model = 'RefBufferReuse';
open_system(model)
Rmodel= 'RefModel';
load_system(Rmodel);

Generate Code Without This Buffer Reuse Optimization

Open the configuration parameters for the model. Clear the Reuse output buffers of Model blocks parameter check box. Alternatively, use the command line.

set_param(model, 'ReuseModelBlockBuffer', 'off');

Open the configuration parameters for the referenced model. Select the Reuse output buffers of Model blocks parameter check box and save the changes. Alternatively, use the command line.

set_param(Rmodel, 'ReuseModelBlockBuffer', 'off');
save_system(Rmodel);

Build the model.

slbuild(model);
### Searching for referenced models in model 'RefBufferReuse'.
### Found 1 model reference targets to update.
### Starting serial model reference code generation build.
### Starting build procedure for: RefModel
### Successful completion of build procedure for: RefModel
### Starting build procedure for: RefBufferReuse
### Successful completion of build procedure for: RefBufferReuse

Build Summary

Model reference code generation targets:

Model     Build Reason                        Status                        Build Duration
==========================================================================================
RefModel  Target (RefModel.c) did not exist.  Code generated and compiled.  0h 0m 10.217s 

Top model targets:

Model           Build Reason                                         Status                        Build Duration
=================================================================================================================
RefBufferReuse  Information cache folder or artifacts were missing.  Code generated and compiled.  0h 0m 13.706s 

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

Inspect the generated code.

file = fullfile('RefBufferReuse_ert_rtw','RefBufferReuse.c');
coder.example.extractLines(file,'/* Model step function */','/* Model initialize function',1,1);
/* Model step function */
void RefBufferReuse_step(void)
{
  /* local block i/o variables */
  real_T rtb_RefModel1[100];
  real_T rtb_Add[100];
  real_T rtb_RefModel1_0;
  int32_T i;

  /* ModelReference: '<Root>/RefModel1' incorporates:
   *  Inport: '<Root>/In1'
   */
  RefModel(&rtU.In1[0], &rtb_RefModel1[0]);

  /* Sum: '<Root>/Add' */
  for (i = 0; i < 100; i++) {
    rtb_RefModel1_0 = rtb_RefModel1[i];
    rtb_Add[i] = rtb_RefModel1_0 + rtb_RefModel1_0;
  }

  /* End of Sum: '<Root>/Add' */

  /* ModelReference: '<Root>/RefModel2' incorporates:
   *  Outport: '<Root>/Out1'
   */
  RefModel(&rtb_Add[0], &rtY.Out1[0]);
}

The generated code generates the unique rtb_Model1 buffer to hold the output of RefModel1.

Generate Code With This Buffer Reuse Optimization

Open the configuration parameters for the model. Select the Reuse output buffers of Model blocks parameter check box. Alternatively, use the command line.

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

Open the configuration parameters for the referenced model. Select the Reuse output buffers of Model blocks parameter check box and save the changes. Alternatively, use the command line.

set_param(Rmodel, 'ReuseModelBlockBuffer', 'on');
save_system(Rmodel);

Build the model.

slbuild(model);
### Searching for referenced models in model 'RefBufferReuse'.
### Found 1 model reference targets to update.
### Starting serial model reference code generation build.
### Starting build procedure for: RefModel
### Successful completion of build procedure for: RefModel
### Starting build procedure for: RefBufferReuse
### Successful completion of build procedure for: RefBufferReuse

Build Summary

Model reference code generation targets:

Model     Build Reason                        Status                        Build Duration
==========================================================================================
RefModel  Model or library RefModel changed.  Code generated and compiled.  0h 0m 8.8162s 

Top model targets:

Model           Build Reason                     Status                        Build Duration
=============================================================================================
RefBufferReuse  Referenced models were updated.  Code generated and compiled.  0h 0m 9.6531s 

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

Inspect the generated code.

file = fullfile('RefBufferReuse_ert_rtw','RefBufferReuse.c');
coder.example.extractLines(file,'/* Model step function */','/* Model initialize function',1,1);
/* Model step function */
void RefBufferReuse_step(void)
{
  real_T rtb_Add[100];
  real_T rtb_Add_0;
  int32_T i;

  /* ModelReference: '<Root>/RefModel1' incorporates:
   *  Inport: '<Root>/In1'
   */
  RefModel(&rtU.In1[0], &rtb_Add[0]);

  /* Sum: '<Root>/Add' */
  for (i = 0; i < 100; i++) {
    rtb_Add_0 = rtb_Add[i];
    rtb_Add[i] = rtb_Add_0 + rtb_Add_0;
  }

  /* End of Sum: '<Root>/Add' */

  /* ModelReference: '<Root>/RefModel2' incorporates:
   *  Outport: '<Root>/Out1'
   */
  RefModel(&rtb_Add[0], &rtY.Out1[0]);
}

The generated code reuses the rtb_Add buffer to hold the output of RefModel1.

Clean Up Example Folders and Files

Close the model.

bdclose(model);

See Also

Related Topics