Main Content

Combine Code Generated for Multiple Models

To combine code, which the code generator produces for multiple models or multiple instances of a model, into one executable program, use these techniques:


The S-function system target (rtwsfcn.tlc) does not support combining code generated for multiple models.

To combine models for simulation and code generation, consider using model referencing. Model referencing helps with:

  • Symbol naming consistency

  • Required scheduling of the overall algorithm

  • Model configuration consistency

Considerations for Combining Code for Different Models

If you combine code generated for different models that is, without using referenced models, consider the following:

  • Data is global. Symbol (name) clashes can result.

  • Configuration parameter settings for the models must match, including settings such as hardware word sizes.

  • Reuse and sharing of code can be suboptimal (for example, duplicate code for shared utility functions, scheduling, and solvers).

  • Scheduling can be more complex (for example, models can have periodic sample times that are not multiples of each other, making scheduling from a common timer interrupt more complicated)

  • For plant models that use continuous time and state, the continuous time signals connecting models are not handled by a single solver like continuous time signals within a model, which can lead to subtle, numeric differences.

Share Data Across Models

Use unidirectional signal connections between models. These connections affect the order in which models are called. For example, if you use an output signal from modelA as input to modelB, the modelA output computation is called first.

Timing Issues

When combining code generated for multiple models or multiple instances of a model:

  • Configure the models by using the same solver mode (single-tasking or multitasking).

  • If the models use continuous states, configure the models by using the same solver.

If the base rates for the models differ, the main program (such as rt_malloc_main.c) must set up the timer interrupt to occur at the greatest common divisor rate of the models. The main program calls each model at a time interval.

Data Logging and External Mode Support

A multiple-model program can log data to separate MAT-files for each model.

Only one of the models in a multiple-model program can use external mode.

Control Ownership of Data

If you have Embedded Coder software, you can specify an owner for individual data items such as signals, parameters, and states. The owner of a data item generates the definition, the memory allocation and initialization, for the data item. For example, if you apply a storage class to a Simulink.Signal object so that it appears as a global variable in the generated code, specify one of the combined models as the owner of the object. The code generated for that model defines the variable.

If you use model referencing, you can modularize the generated code and establish clear ownership of data when you work in a team.

If you do not use model referencing, you can prevent generation of duplicate definitions for a data item. For example, suppose you store a Simulink.Parameter object in the base workspace and apply the storage class ExportedGlobal. If you generate code from two separate models that use the object, each model generates a definition for the corresponding global variable. Instead, you can specify an owner for the object so that only the owner generates a definition.

To specify an owner for a data item:

  1. Apply a storage class to the data item. See Organize Parameter Data into a Structure by Using Struct Storage Class (Embedded Coder).

  2. Configure the owner of the data item by specifying a value for the Owner property.

  3. Select the model configuration parameter Use owner from data object for data definition placement.

For more information about controlling ownership and file placement of data definitions and declarations, see Control Placement of Global Data Definitions and Declarations in Generated Files (Embedded Coder).

Combine Code Generated for Multiple Models or Multiple Instances of a Model

For each model that you are combining code for, generate the code.

  1. Set the system target file to a GRT- or ERT-based system target file. The system target file for the models that you combine must be the same.

  2. If you intend to have multiple instances of that model in the application, set the model configuration parameter Code interface packaging to Reusable function. If you specified an ERT-based system target file, optionally, you can set the model configuration parameter Use dynamic memory allocation for model initialization, depending on whether you want to statically or dynamically allocate the memory for each instance of the model.

  3. Generate source code. The code generator includes an allocation function in the generated file model.c. The allocation function dynamically allocates model data for each instance of the model.

After generating source code for each model:

  1. Compile the code for each model that you are combining.

  2. Combine the makefiles generated for the models into one makefile.

  3. Create a combined simulation engine by modifying a main program, such as rt_malloc_main.c. The main program initializes and calls the code generated for each model.

  4. Run the makefile. The makefile links the object files and the main program to an executable program.

Related Topics