MATLAB Examples

Flat Structures for Reusable Subsystem Parameters

This example shows how to increase the efficiency of code generated for reusable subsystems by generating a single flat parameter structure instead of a hierarchy of nested parameter structures.

By default, the code generated for reusable subsystems contains separate structures to define the parameters that each subsystem uses. If you use nested reusable subsystems, the generated code creates a hierarchy of nested parameter structures. Hierarchies of structures can reduce code efficiency due to compiler padding between word boundaries in memory.

This optimization is for only ERT-based targets. You must set the configuration parameter Default parameter behavior to Inlined.

Contents

Explore Example Model

Open the example model rtwdemo_paramstruct.

model = 'rtwdemo_paramstruct';
open_system(model);

The model contains two nested reusable subsystems. Each subsystem uses two of the parameters A, B, C, and D that are defined in the base workspace.

Generate Code with Hierarchical Parameter Structures

Create a temporary folder to contain the model build files. Generate code for the model using the default hierarchical data structure for reusable subsystems.

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

In the code generation report, view the parameter structure definitions in the file rtwdemo_paramstruct.h.

cfile = fullfile(cgDir,'rtwdemo_paramstruct_ert_rtw','rtwdemo_paramstruct.h');
rtwdemodbtype(cfile,'/* Parameters for system: ''<S1>/SubsysZ''',...
    '/* Parameters (auto storage)', 1, 0);

The code defines a parameter structure for each reusable subsystem and nests the structures.

Enable Optimization

Open the Configuration Parameters dialog box. On the Optimization > Signals and Parameters pane, select Nonhierarchical in the Parameter structure drop-down list.

Alternatively, enable the optimization at the command prompt.

set_param(model, 'InlinedParameterPlacement', 'NonHierarchical');

Generate Code with Flat Parameter Structure

Generate code for the model using a flat parameter structure for reusable subsystems.

rtwbuild(model)

In the code generation report, view the parameter structure definition in the file rtwdemo_paramstruct.h.

rtwdemodbtype(cfile,'/* Parameters (auto storage) */',...
    '/* Real-time Model Data Structure */', 1, 0);

The code stores all of the parameters for the reusable subsystems in a single flat structure.

Close the model and delete build files.

bdclose(model)
rtwdemoclean;
cd(currentDir)