Troubleshoot and Speed Up Simulation Performance
Issue
Running accurate simulations of large, complex systems can take considerable time. Because simulation plays a crucial role at every step of the Model-Based Design process, speeding up simulations can improve the efficiency of the overall design process. However, many choices in model design and simulation configuration can introduce inefficiencies in simulation.
Possible Solutions
Simulink® provides several tools that can help analyze simulation performance so you can identify and configure optimal settings, features, and design choices that create inefficient simulation conditions.
Analyze Recent Changes
If simulations started running slowly after you modified the model, use the Comparison Tool to compare the current version of the model to a prior version. The Comparison Tool highlights all the changes between revisions, which can help you spot changes that might affect simulation performance. For examples of the types of changes that could affect simulation performance, see Speed Up Simulation and Modeling Techniques That Improve Performance.
For more information about the Comparison Tool, see Review Changes in Simulink Models.
Run the Performance Advisor
The Performance Advisor analyzes the model for configuration settings and modeling patterns that can slow down simulation. After analyzing the model, the Performance Advisor suggests changes that can improve performance. You can configure the performance advisor to automatically apply the changes to the model, or you can apply the changes manually. Before making changes to the model, the Performance Advisor simulates the unmodified model to capture a baseline of the simulation performance. After applying changes, the Performance Advisor runs a final validation to confirm that the changes improved simulation performance.
When you analyze models using the Performance Advisor, generate and save a report of the results. The report summarizes the checks the Performance Advisor ran and the changes recommended based on the results of each check. For more information, see Improve Simulation Performance Using Performance Advisor.
Analyze Simulation Timing Information
The approach to improving simulation performance can depend on which phase of simulation affects your workflow the most. To assess the time spent in each phase of simulation, inspect the timing information in the simulation metadata. Simulation metadata is returned along with simulation results when the Single simulation output parameter is enabled for the model.
If you do not have a Simulink.SimulationOutput
object that
contains results from a simulation of the model that exhibited the performance issue you
want to investigate, simulate the model. To ensure the simulation returns the results as a
single SimulationOutput
object, enable the
ReturnWorkspaceOutputs
parameter in the call to the
sim
function.
out = sim(mdl,ReturnWorkspaceOutputs="on");
To view the timing information for the simulation, use this command. If your
SimulationOutput
object is stored in a variable with a different name,
replace out
with the name of that variable. The simulation metadata is
stored in the SimulationMetadata
property of the
SimulationOutput
object as a Simulink.SimulationMetadata
object. The timing information is stored in the
TimingInfo
property of the SimulationMetadata
object.
out.SimulationMetadata.TimingInfo
ans = struct with fields: WallClockTimestampStart: '2024-08-02 14:09:17' WallClockTimestampStop: '2024-08-02 14:09:36' InitializationElapsedWallTime: 16.7115 ExecutionElapsedWallTime: 1.1011 TerminationElapsedWallTime: 0.8088 TotalElapsedWallTime: 18.6213 ProfilerData: 'Profiler is not enabled'
The table provides guidance on how to investigate the performance of each phase.
Simulation Phase | TimingInfo Field Name | Troubleshooting Tools and Considerations |
---|---|---|
Compilation and initialization | InitializationElapsedWallTime | The software determines much of what happens during compilation and initialization. However, callbacks that execute during compilation or initialization and modeling choices can affect performance. |
Execution | ExecutionElapsedWallTime | Many aspects of a model and simulation configuration can affect the performance of the execution phase.
|
Termination | TerminationElapsedWallTime | The software determines much of what happens during termination. However, callbacks that execute in the termination phase can affect performance. |
Identify Use of Callbacks in Model
Callbacks are functions that execute in response to events. For
example, the PreLoadFcn
callback of a model executes in response to a
request to load the model. You can define many types of callbacks in your model using model,
block, and port parameters. For more information about callbacks, see Customize Model Behavior with Callbacks.
Some callbacks execute during simulation, most commonly during the initialization or termination phases. Extraneous or inefficient callback execution can slow down simulations. To identify the callbacks in your model that execute during simulation, use the Callback Tracer. Using the Callback Tracer, you can log each call to each callback function in your model. You can view and filter the results to determine if:
Your model defines unexpected callbacks.
Callbacks execute unexpectedly or with unexpected frequency.
Callback execution takes longer than expected or accounts for a larger portion of initialization time than expected.
For more information, see View and Trace Callbacks in Models.
Profile Execution of MATLAB Code Using Profiler
You can profile the execution of MATLAB code associated with your model by using the Profiler app or the
profile
function. For example, profiling can
identify which lines of code in a callback take the most time. The MATLAB
Profiler analyzes only the execution of MATLAB code associated with your model and does not profile or analyze simulation
execution. To profile simulation execution, use the Simulink Profiler. For more
information, see Profile Simulation Execution.
To programmatically profile the execution of MATLAB code that runs during simulation:
Clear statistics from the profiler. Then, enable the profiler.
profile clear profile on
Run the simulation. If you want to investigate the performance of only the initialization or termination phases, set the stop time of the simulation equal to the start time, typically
0
, to minimize the time required by the execution phase.out = sim(mdl,StopTime="0");
Get the profiling results.
p = profile('info');
Save the profiling results in a MAT file.
save myprofilingdata p
View and analyze the profiling results and statistics by using the Profiler app or by exploring the profiling results in the MATLAB Command Window. To open the Profiler app, use this command.
profile viewer
Profile Simulation Execution
The Simulink Profiler helps you identify bottlenecks for simulation performance by analyzing the distribution of simulation execution time among model components. By analyzing the profiling report, you can:
Determine whether closely investigating one or more model components might improve simulation performance.
Decide which component to investigate first.
For more information, see How Simulink Profiler Captures Performance Data.
Analyze Step Size and Solver Events in Variable-Step Simulations
When you use a variable-step solver, the solver determines the size of the next step for each time step in the simulation. Solver events, such as zero crossings, solver rests, and Jacobian computations, are necessary for accurate simulation but can slow down simulations by:
Increasing the number of calculations required for a time step
Causing the solver to take excessively small time steps
To log the solver events and step size throughout a variable-step simulation, use the Solver Profiler. After running a profiling simulation, use the Solver Profiler to inspect and analyze the results.
Review the model, step, and event information in the Statistics pane to determine whether solver configuration parameter values, an excessive number of small time steps, or an excessive number of solver events could be the cause of slow simulation performance.
Use the Step Size plot to analyze how the step size varies throughout the simulation.
Add markers to the Step Size plot for each occurrence of a solver event to evaluate the relationship between the solver events and step size.
Consider applying suggestions in the Suggestions pane.
Check the pane for each type of solver event you logged in the profiling simulation. The table in each solver event pane identifies the source of each solver event in the model. When a single block is the source of all or most of the solver events, you can start investigating the model around that block.
For more information, see Examine Model Dynamics Using Solver Profiler.
Inspect Information from sldiagnostics
The sldiagnostics
function provides basic
information that can be helpful to assess simulation performance. For example, you can
evaluate the size and complexity of your model by using the
sldiagnostics
function to determine the number of blocks and states
in the model.
The sldiagnostics
function also provides detailed information about
the compilation process, including the execution time and memory required for each stage of
compilation. If the initialization phase is slow in your simulation, analyzing the
compilation statistics the sldiagnostics
function returns can help
identify the source of the slow down.
Check for New Features that Improve Performance
If you recently upgraded to a new version of the software, run the Upgrade Advisor. The Upgrade Advisor analyzes the model to identify new features and model modifications that leverage all the improvements in the version of the software that you have. Some of the new features and model modifications that the Upgrade Advisor suggests improve simulation performance.
For more information, see Upgrade Models Using Upgrade Advisor.
See Also
Tools
- Performance Advisor | Simulink Profiler | Solver Profiler | Comparison Tool | Upgrade Advisor | Profiler