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 references 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

Code generation targets built:

Model     Action                        Rebuild Reason              
====================================================================
RefModel  Code generated and compiled.  RefModel.c does not exist.  

Top model targets built:

Model           Action                        Rebuild Reason                                    
================================================================================================
RefBufferReuse  Code generated and compiled.  Code generation information file does not exist.  

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

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 references 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

Code generation targets built:

Model     Action                        Rebuild Reason                      
============================================================================
RefModel  Code generated and compiled.  Model or library RefModel changed.  

Top model targets built:

Model           Action                        Rebuild Reason                   
===============================================================================
RefBufferReuse  Code generated and compiled.  Referenced models were updated.  

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

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