MATLAB Examples

Generate Reentrant, Multi-Instance Code

This example shows you how to configure a model for reentrant, multi-instance code generation. Multiple programs can use reentrant code simultaneously. When you configure a model for reentrancy, the execution (step) entry-point function uses root-level input and output arguments instead of global data structures. After examining the configuration settings, generate and review the generated code.


Open the Model

Open the model rtwdemo_reusable. The model contains two root Inport blocks and a root Outport block.


In your working folder, create a temporary folder for generating and reviewing the code.

[~,cgDir] = rtwdemodir();

Examine Relevant Model Configuration Settings

1. Open the Model Configuration Parameters dialog box.

2. System target file is set to ert.tlc. Although you can generate reentrant code for a model configured with the System target file set to grt.tlc, ERT and ERT-based system target files provide more control over how the code passes root-level I/O.

3. Open the Code Generation > Interface pane and explore relevant parameter settings.

  • Code interface packaging is set to Reusable function. This parameter setting instructs the code generator to produce reusable, multi-instance code.
  • The Reusable function parameter setting also displays the Multi-instance code error diagnostic parameter. That parameter is set to Error, indicating that the code generator abort if the model violates requirements for generating multi-instance code.
  • Pass root-level I/O as is set to Part of model data structure. This setting packages root-level model input and output into the real-time model data structure (rtModel), which is an optimized data structure that replaces SimStruct as the top-level data structure for a model.
  • Remove error status field in real-time model data structure is selected. This parameter setting reduces memory usage by omitting the error status field from the generated real-time model data structure.

Generate and Review Code

### Starting build procedure for model: rtwdemo_reusable
### Successful completion of build procedure for model: rtwdemo_reusable

From the code generation report, review the generated code.

  • ert_main.c is an example main program (execution framework) for the model. This code controls model code execution by calling the entry-point function rtwdemo_reusable_step. Use this file as a starting poitn for coding your execution framework.
  • rtwdemo_reusable.c contains entry points for the code that implements the model algorithm. This file includes the rate scheduling code.
  • rtwdemo_reusable.h declare model data structures and a public interface to the model entry points and data structures.
  • rtwtypes.h defines data types, structures, and macros that the generated code requires.

Open and review the Code Interface Report. Use the information in that report to write the interface code for you execution framework.

1. Include the generated header file by adding directive #include rtwdemo_reusable.h.

2. Write input data to the generated code for model Inport blocks.

3. Call the generated entry-point functions.

4. Read data from the generated code for the model Outport block.

Input ports:

  • <Root>/In1 of data real_T with dimension of 1
  • <Root>/In2 of data real_T with dimension of 1

Entry-point functions:

  • Initialization entry-point function, void rtwdemo_reusable_initialize(RT_MODEL *const rtM). At startup, call this function once.
  • Output and update (step) entry-point function, void rtwdemo_reusable_step(RT_MODEL *const rtM). Call this function periodically at the fastest rate in the model. For this model, call the function every second. To achieve real-time execution, attach this function to a timer.

Output port:

  • <Root>/Out1 of data type real_T with dimension of 1
Examine the |rtwdemo_reusable_step| function code in |rtwdemo_reusable.c|.
cfile = fullfile(cgDir,'rtwdemo_reusable_ert_rtw','rtwdemo_reusable.c');
rtwdemodbtype(cfile,'/* Model step function', '/* Model initialize function ', 1, 0);
/* Model step function */
void rtwdemo_reusable_step(RT_MODEL *const rtM)
  Parameters *rtP = ((Parameters *) rtM->defaultParam);
  D_Work *rtDWork = ((D_Work *) rtM->dwork);
  ExternalInputs *rtU = (ExternalInputs *) rtM->inputs;
  ExternalOutputs *rtY = (ExternalOutputs *) rtM->outputs;

  /* Outport: '<Root>/Out1' incorporates:
   *  UnitDelay: '<Root>/Delay'
  rtY->Out1 = rtDWork->Delay_DSTATE;

  /* Gain: '<Root>/Gain' incorporates:
   *  Inport: '<Root>/In1'
   *  Inport: '<Root>/In2'
   *  Sum: '<Root>/Sum'
   *  UnitDelay: '<Root>/Delay'
  rtDWork->Delay_DSTATE = (rtU->In1 + rtU->In2) * rtP->k1;

The code generator passes model data to the rtwdemo_reusable_step function as part of the real-time model data structure. Try different settings for the Code interface packaging and Pass root-level I/O parameters and regenerate code. Observe how the function signature for the rtwdemo_reusable_step function changes.

Close the model and the code generation report.