Main Content

Generate Reusable Code from Library Subsystems Shared Across Models

What Is a Reusable Library Subsystem?

A reusable library subsystem is a subsystem included in a library that is configured for reuse. You must define a subsystem in a library and configure it for reuse to reuse the subsystem across models.

To reuse common functionality, you can include multiple instances of a subsystem:

  • Within a single model, which is a top model or part of model reference hierarchy

  • Across multiple referenced models in a model reference hierarchy

  • Across multiple top models that contain Model blocks

  • Across multiple top models that do not include Model blocks

The code generator uses checksums to determine reusability. There are cases when the code generator cannot reuse subsystem code.

For incremental code generation, if the reusable library subsystem changes, a rebuild of itself and its parents occurs. During the build, if a matching function is not found, a new instance of the reusable function is generated into the shared utilities folder. If a different matching function is found from previous builds, that function is used, and a new reusable function is not emitted.

For subsequent builds, unused files are not replaced or deleted from your folder. During development of a model, when many obsolete shared functions exist in the shared utilities folder, you can delete the folder and regenerate the code. If all instances of a reusable library subsystem are removed from a model reference hierarchy and you regenerate the code, the obsolete shared functions remain in the shared utilities folder until you delete them.

If a model changes such that the change might cause different generated code for the subsystem, a new reusable function is generated. For example, model configuration parameters that modify code comments might cause different generated code for the subsystem even if the reusable library subsystem did not change.

Embedded Coder® users can generate code from a library that contains subsystems are configured for reuse. For more information, see Library-Based Code Generation for Reusable Library Subsystems.

Reusable Library Subsystem Code Placement and Naming

The generated code of a reusable library subsystem is independent of the generated code of the model. Code for the reusable library subsystem is generated to the shared utility folder, slprj/target/_sharedutils, instead of the model reference hierarchy folders. The generated code for the supporting types, which are generated to the .h file, are also in the shared utilities folder.

For unique naming, reusable function names have a checksum appended to the reusable library subsystem name. For example, the code and files for a subsystem, SS1, which links to a reusable library subsystem, RLS, might be:

  • Function name: RLS_mgdjlngd

  • File name: RLS_mgdjlnd.c and RLS_mgdjlnd.h

To configure a reusable library subsystem, set the Subsystem parameters as listed here:

  • Select Treat as an atomic unit.

  • On the Code Generation tab:

    • Set Function packaging to Reusable function.

    • Set the Function name options and File name options parameters based on this table. This table summarizes how the code generator produces code and highlights the File name options in bold that enable you to generate code from reusable library subsystems. For the purpose of this table, assume that the Subsystem block is named mySubsystem, the File name value for the User specified option is myFunction, and the Subsystem methods naming rule model configuration parameter has the default value $R$N$M$F.

      Function Name OptionsFile Name OptionsGenerated Folder LocationGenerated Function NameGenerated File Names
      AutoAuto/slprj/target/_sharedutilsmySubsystem_checksummySubsystem_checksum.c, mySubsystem_checksum.h
      Use subsystem name/slprj/target/_sharedutilsmySubsystem_checksummySubsystem_checksum.c, mySubsystem_checksum.h
      Use function name/slprj/target/_sharedutilsmySubsystem_checksummySubsystem_checksum.c, mySubsystem_checksum.h
      User specifiedmodel_target_rtwmodel_SubsystemmyFile.h
      Use subsystem nameAutomodel_target_rtwmodel_SubsystemGenerated code included in model.h
      Use subsystem name/slprj/target/_sharedutilsmySubsystem_checksummySubsystem_checksum.c, mySubsystem_checksum.h
      Use function name/slprj/target/_sharedutilsmySubsystem_checksummySubsystem_checksum.c, mySubsystem_checksum.h
      User specifiedmodel_target_rtwmodel_SubsystemmyFile.h
      User specifiedAuto (This specification is supported for one unique instance of the subsystem in the model.)/slprj/target/_sharedutilsmyFunctionmyFunction.c, myFunction.h
      Use subsystem namemodel_target_rtwmyFunctionmySubsystem.c, mySubsystem.h
      Use function name (This specification is supported for one unique instance of the subsystem in the model.)/slprj/target/_sharedutilsmyFunctionmyFunction.c, myFunction.h
      User specified (This specification is supported if the user-specified function name and file name are same and only for one unique instance of the subsystem in the model.)/slprj/target/_sharedutilsmyFunctionmyFunction.c, myFunction.h

For more information about the Subsystem block parameters, see Subsystem.

Configure Models That Include Reusable Library Subsystems

For a model to use a reusable library subsystem, you must configure the model differently depending on whether the model is a reference model or top model. If the subsystem is in a referenced model hierarchy, set the configuration parameter, Shared code placement to Auto. Otherwise, for each model that uses the subsystem, set the model configuration parameter Shared code placement to Shared location.

If a reusable library subsystem uses a shared local data store and you configure default mapping for model data elements, leave the default storage class mapping for category Shared local data stores set to Default.

In a model reference hierarchy, if an instance of the reusable library subsystem is in the top model, then on the Model Referencing pane of the Configuration Parameters dialog box, you must select the Pass fixed-size scalar root input by value for code generation parameter. If you do not select the parameter, a separate shared function is generated for the reusable library subsystem instance in the top model, and a reusable function is generated for instances in the referenced models.

If a reusable library subsystem is connected to the root Outport, reuse does not happen with identical subsystems that are not connected to the root Outport. However, you can set Pass reusable subsystem outputs as to Individual arguments on the Optimizations pane to make sure that reuse occurs between these subsystems. This parameter requires an Embedded Coder license.

For more information on creating a library, see Custom Libraries. For an example of creating a reusable library subsystem, see Generate Reusable Code for Subsystems Shared Across Models.

Generate Reusable Code for Subsystems Shared Across Models

This example shows how to configure a reusable library subsystem and generate a reusable function for a subsystem shared across referenced models. The result is reusable code for the subsystem, which is generated to the shared utility folder (slprj/target/_sharedutils).

Create a reusable library subsystem.

  1. In the Simulink Editor, on the Simulation tab, select New > Library. Select Blank Library from the Simulink Start Page window. Open GeneratedCodeFunctionReuse.

    openExample('GeneratedCodeFunctionReuse')

  2. Copy and paste subsystem SS1 into the Library Editor. This action loads the variables for SS1 into the base workspace. Rename the subsystem block to RLS.

  3. Click the Subsystem block and press Ctrl+U to view the contents of subsystem RLS.

  4. To configure the subsystem, in the Library editor, right-click RLS. In the context menu, select Block Parameters (Subsystem). In the Subsystem Parameters dialog box, choose the following options:

    • Select Treat as an atomic unit.

    • On the Code Generation tab:

      • Set Function packaging to Reusable function.

      • Set Function name options to User specified and verify that the Function name is set to myfun.

      • Set File name options to Auto.

  5. Click Apply and OK.

  6. Save the reusable library as ssreuselib, which creates a file, ssreuselib.slx.

Create the example model.

  1. Create a model which includes one instance of RLS from ssreuselib. Name this subsystem SS1. Add another subsystem and name it SS2. Name the model ex_model1.

  2. Create another model which includes one instance of RLS from ssreuselib. Name this subsystem SS1. Add another subsystem and name it SS3. Name the model ex_model2.

  3. Create a top model with two model blocks that reference ex_model1 and ex_model2. Save the top model as ex_mdlref_ssreuse.

Set configuration parameters of the top model.

  1. With model ex_mdlref_ssreuse open in the Simulink® Editor, on the C Code tab, click Settings to open the Configuration Parameters dialog box.

  2. On the Solver pane, specify the Type as Fixed-step.

  3. On the Model Referencing pane, click the ellipsis to view the advanced parameters. Then, select Pass fixed-size scalar root inputs by value for code generation.

  4. On the Code Generation > Report pane, select Create code generation report and Open report automatically.

  5. On the Code Generation > Interface pane, set the Shared code placement to Shared location.

  6. On the Code Generation > Identifiers pane, set the Maximum identifier length to 256. This step is optional.

  7. Click Apply and OK.

Create and propagate a configuration reference.

  1. In the Simulink Editor, on the Modeling tab, click Model Explorer to open the Model Explorer. In the left navigation column of the Model Explorer, expand the ex_mdlref_ssreuse node.

  2. Select the Configurations node below the ex_mdlref_ssreuse node. In the Contents pane, right-click Configuration and select Convert to Configuration Reference.

  3. In the Convert Active Configuration to Reference dialog box, click OK. This action converts the model configuration set to a configuration reference, Simulink.ConfigSetRef, and creates the configuration reference object, configSetObj, in the base workspace.

  4. In the left navigation column, right-click Reference (Active) and select Propagate to Referenced Models.

  5. In the Configuration Reference Propagation to Referenced Models dialog box, select the referenced models in the list. Click Propagate.

Now, the top model and referenced models use the same configuration reference, Reference (Active), which points to a model configuration reference object, configSetObj, in the base workspace. When you save your model, you also need to export the configSetObj to a MAT-file.

Generate and view the code.

  1. To generate code, in the Simulink Editor, press Ctrl+B. After the code is generated, the code generation report opens.

  2. To view the code generation report for a referenced model, in the left navigation pane, in the Referenced Models section, select ex_model1. The code generation report displays the generated files for ex_model1.

  3. In the left navigation pane, expand the Shared files. The code generator uses the reusable library subsystem name. The code for subsystem SS1 is in myfun.c and myfun.h.

  4. Click Back and navigate to the ex_model2 code generation report. ex_model2 uses the same source code, myfun.c and myfun.h, as the code for ex_model1. Your subsystem function and file names will be different.

Related Topics