Main Content

Prepare Your Model for Scalable Compilation

This example shows how to analyze and restructure a model to prepare it for scalable compilation.

Explore and Restructure the Model

Open the Transmission Line example model:

openExample('simscape/TransmissionLineExample')

This example models a transmission line by concatenating 50 identical T-section segments. A custom Simscape™ block, T-Section Transmission Line, represents a single T-section segment. Each T-section segment is 0.1 m long, therefore the transmission line represents a 5 m length of coaxial cable. The model contains five identical subsystems, named S1 through S5, with each subsystem consisting of ten T-section segment blocks, named T-1 through T-10.

The preliminary analysis shows that the model:

  • Has a repeating pattern of Simscape blocks

  • Contains very few Simulink® blocks, which means that the Simscape part of the model accounts for most of the compilation time

  • Uses a variable-step solver

Based on this analysis, the model is a suitable candidate for scalable compilation. Moreover, the model already consists of a pattern of repeating subsystems and does not need any additional restructuring at this point. The Advisory tool lets you analyze your model as if it already contained reusable components in place of repeating subsystems.

Analyze the Model Using the Advisory Tool

The next step is to analyze the model using the Advisory tool. You can call the sscScalableAdvisor function using the following syntax:

sscScalableAdvisor(modelname,subsyspaths)

where:

  • modelname is the name of the model to analyze, specified as a character vector, string, or a handle to the model. This argument is required.

  • subsyspaths is an optional argument that identifies the repeating subsystems in the model. Specify this argument as a cell array of subsystem names, including the path to each subsystem from the root of the model, to have the compilation artifacts be reused among subsystems with identical contents.

    If the model contains reusable components, the tool recognizes them automatically. Reusable components are individual Simscape blocks or textual components designated as reusable, referenced subsystems, or linked subsystems. If the model contains other types of subsystems, you must provide their names with the subsyspaths argument for the Advisory tool to consider them as repeating components.

In this example, you provide the names of all five subsystems, S1 through S5, in a cell array as the second input argument:

r = sscScalableAdvisor('TransmissionLine', ...
{'TransmissionLine/S1', 'TransmissionLine/S2', 'TransmissionLine/S3', ...
'TransmissionLine/S4', 'TransmissionLine/S5',})
r = 

ScalableReport for model 'TransmissionLine'

          TotalModelCompilationTime: 7.2313
            SimscapeCompilationTime: 6.7703
                         PeakMemory: '27 MB'

    ScalableSimscapeCompilationTime: 3.5232
                 ScalablePeakMemory: '19 MB'

                         Subsystems: [1×4 table]
                         Components: [0×4 table]

The Advisory tool returns the compilation statistics. If the model contains unsupported patterns, workflows, or optimizations, the Advisory tool recommends to not enable scalable compilation and lists the applicable limitations. In this case, however, none of the limitations apply, and the compilation time is significantly reduced. The compilation time for the Simscape part of the model is 3.5232 s, instead of 6.7703 s, which brings the total compilation time down by about 45% (7.2313 - 6.7703 + 3.5232 = 3.9842 s).

Note

  • Model compilation time depends on multiple factors, such as the processor used, other processes running at the same time, memory caching, and so on. The exact numbers may vary slightly between successive runs of the Advisory tool, but the ratio between different parts of the compilation process remains the same. Use this data to estimate the potential gain from scalable compilation.

  • If the compilation time for a model is reduced by more than 5 seconds, the Advisory tool recommends to enable scalable compilation, opens the Configuration Parameters dialog box, and highlights Reuse components during compilation to simplify your workflow. However, 5 seconds is an arbitrary number. Use the Advisory tool to estimate the potential gain from scalable compilation, and if you decide that the results are satisfactory, follow the rest of the steps in this example and enable scalable compilation as described in Turn On Scalable Compilation.

You can also view the data on subsystem reuse:

r.Subsystems
ans =

  1×4 table

                            Total Instances    Compiled Instances    Reuse       Details  
                            _______________    __________________    ______    ___________

    TransmissionLine/S1           5                   1             "100%"    {1×5 table}

In this model, all five subsystems are identical, and therefore the reuse is 100% (the five instances are replaced with one compiled instance). In other models, the reuse statistics might be different. For example, if the subsystems have different configurations or parameterizations, the Advisory tool report indicates how many compiled instances are needed and calculates the reuse statistics accordingly. In that case, the Details table provides more information.

Note

The Components field in the Advisory tool report provides reuse data for individual Simscape blocks or textual components designated as reusable. This model does not contain reusable Simscape blocks or textual components, therefore, the Components table is empty.

Create Reusable Components

Because the compilation results provided by the Advisory tool in the previous step are satisfactory, you do not need to consider other ways to restructure the model. You now need to prepare the model for scalable compilation by actually replacing the repeated subsystems in the Advisory tool analysis with reusable components, in this case, with reference subsystems:

  1. Right-click subsystem S1.

  2. From the context menu, select Subsystem & Model Reference > Convert to > Referenced Subsystem.

  3. Keep Subsystem file name as S1. Use the Browse button if you want to save the subsystem in a different folder.

  4. Click Convert. As a result, the contents of subsystem S1 are saved as a separate block diagram file S1.slx in the folder you specified, and the subsystem S1 in the model is replaced with the referenced subsystem.

  5. Delete subsystems S2, S3, S4, and S5.

  6. Right-click subsystem S1 and drag it in place of subsystem S2.

  7. Repeat for subsystems S3, S4, and S5.

    Notice that your model still contains subsystems named S1, S2, S3, S4, and S5, but they now all reference the same subsystem S1, saved as a separate block diagram file.

Turn On Scalable Compilation

To turn on scalable compilation for a model:

  1. Open the Configuration Parameters dialog box.

  2. On the Simscape pane, select the Reuse components during compilation check box.

You can also use the equivalent command-line interface to set the model configuration parameter:

set_param(bdroot,'SimscapeCompileComponentReuse','on')

Related Topics