Generate Code for Model Reference Hierarchy
To achieve incremental loading of models, incremental code generation, modular development of referenced models, and code reuse, you can generate code for the model reference hierarchy.
You can include one model inside another model by using a Model block. Each instance of a Model block is a model reference. For simulation and code generation, blocks within a referenced model execute together as a unit. The model that contains a referenced model is a parent model. A collection of parent and referenced models constitutes a model hierarchy.
Model Reference Technology
When you generate code for a referenced model hierarchy, the code generator produces a standalone executable for the top model and a library module called a model reference target for each referenced model. When the code executes, the top model executable invokes the model reference targets to compute the referenced model output. Model reference targets are sometimes called Simulink® Coder™ targets.
Do not confuse a model reference target (Simulink Coder target) with these other types of targets:
Target hardware — A platform for which the Simulink Coder software generates code.
System target file — A file that instructs the Simulink Coder software how to generate code for particular purpose.
Rapid Simulation target (RSim) — A system target file that is part of the Simulink Coder product.
Simulation target — A MEX file that implements a referenced model, which is executed by using Simulink Accelerator™ software.
The code generator places the code for the top model in a hierarchy in the code
generation folder and places the code for referenced models in an
slprj folder. Subfolders in
separate places for different types of files. For folder information, see Manage Build Process Folders.
By default, the Simulink Coder uses incremental code generation. When generating code, the software compares structural checksums of referenced model files to the generated code files to determine whether to regenerate model reference targets. To control when rebuilds occur, use the configuration parameter Model Referencing > Rebuild. For details, see Rebuild.
The Simulink Coder software also uses incremental loading. The code for a referenced model is not loaded into memory until the code for its parent model executes and requires the outputs of the referenced model. Simulink Coder then loads the referenced model target and executes the code. Once loaded, the target remains in memory until it is no longer used.
If you have Embedded Coder®, custom targets must declare themselves as compliant with the model reference if they support Model blocks. For more information, see Support Model Referencing.
Generate Code for Model Reference Hierarchy
This example shows how to reference an existing model and generate code for the model reference hierarchy.
Create Model Reference Hierarchy
1. Open an existing model, for example the
ex_model_arg_ref model. This model represents a reusable algorithm. This is a GRT model. If you have Embedded Coder, change the model configuration parameter System target file to
2. A referenced model must specify that it is available to be referenced and whether it can be referenced just once or multiple times. Specify the model configuration parameter Total number of instances allowed per top model as
Multiple. By default, Total number of instances allowed per top model is set to
3. Create a new model that you want as a top model in the model reference hierarchy. The top model refers to other models as referenced models or multiple instances of a referenced model.
4. Add Inports and Outports to the top model as required. To add a referenced model, use the Model block.
5. The new block is initially unresolved because it does not specify a referenced model. To open the block parameters dialog box, double-click the Model block or use the context menu of the Model block.
6. Enter the name of the referenced model in the Model name field. This name must contain fewer than 60 characters, exclusive of the
7. Click OK. If the referenced model contains root-level inputs or outputs, the Model block displays corresponding input and output ports.
8. Create another instance of the Model block so that the top model looks like model:
9. Name the model
ex_model_arg and save the model.
You can also convert a subsystem to a referenced model. For more details, see Convert Subsystems to Referenced Models.
Generate Code for Model Reference Hierarchy
1. Verify that the model configuration parameter Solver selection Type is set to
Fixed-step for the top model and the referenced model. To open the respective Configuration Parameters dialog boxes, on the Modeling tab, select Model Settings for the respective models.
For a model reference hierarchy that has multiple referenced models, you can access the Model Settings of the referenced model that is open and the top model through this menu. To access the Model Settings of another referenced model in the model hierarchy, open the referenced model to access this menu.
2. For the top model, open the C Code tab by selecting either Simulink Coder app or Embedded Coder app. To generate code for the model reference hierarchy, click the Build button.
The Simulink Coder build process generates and compiles code. The current folder now contains new files and a new folder.
ex_model_arg_grt_rtw— The build folder containing the generated code for the top model.
slprj > grt > ex_model_arg_ref— The folder containing the generated code for the referenced model.
ex_model_arg.exe— The executable created by the build process.
ex_model_arg.slxc— The Simulink cache file for the top model.
ex_model_arg_ref.slxc— The Simulink cache file for the referenced model.
For more information about the configuration parameter requirements, see Set Configuration Parameters for Code Generation of Model Hierarchies.
View Generated Code in Code Generation Report
To view the generated code, in the C Code tab, select Open Report. The code generation report contains hyperlinks to all the files generated for the model reference hierarchy.
To open the code generation report for a referenced model, at the top of the report, click
ex_model_arg. In the drop-down, click
ex_model_arg_ref. The source files for the referenced model are displayed along with the generated code for the referenced model.
To navigate back to the top model, click the Go Backward button on the top-left corner of the code generation report window.
Working with Code Generation Folders
Model reference code is generated in your code generation folder. Simulation target code is generated in your simulation cache folder. Due to the two different folders, there are constraints on:
When and where you build model reference targets.
How you access the model reference targets.
You can store the models referenced by Model blocks anywhere on the MATLAB® path. A top model can include models stored on different file systems or in different folders. The simulation targets and generated code derived from these models cannot be stored on different file systems or in different folders. To allow code reuse, you must set up models referenced by a top model to simulate and generate model reference target code in a single code generation folder.
If you reference the same model from several top models, each stored in a different folder, you must choose one of these approaches:
Work with the same code generation folder and be sure that the models are on your path.
Allow separate code generation folders, simulation targets, and Simulink Coder targets to be generated in each folder in which you work.
The second approach requires maintenance of several instances of the model reference code. It is possible for generated code to become redundant, for example, when you make changes to the referenced model. To minimize code regeneration of referenced models, choose a specific code generation folder for all sessions.
Build Model Reference Targets
The code generator produces a model reference target directly from the Simulink model. The code generator generates or regenerates model reference targets, for example, when they require an update.
When you execute the
slbuild command by using arguments
in the MATLAB Command Window, Simulink and Simulink
Coder can generate a simulation target for an accelerator mode referenced
model and a model reference target for a referenced model.
The code generator produces only one model reference target for multiple instances of a referenced model. For more information, see Generate Reentrant Code from Subsystems.
Reduce Change Checking Time
You can reduce the time that Simulink and Simulink Coder spend checking whether model reference targets need to be rebuilt by setting configuration parameter values:
In the top model, consider setting the model configuration parameter Model Referencing > Rebuild to
If any changes in known dependencies detected.
In the referenced models throughout the hierarchy, set the configuration parameter Diagnostics > Data Validity > Signal resolution to
These parameter values exist in a referenced model configuration set, not in the individual Model block. Setting either value for an instance of a referenced model sets it for the instances of that model.