Learn more about Solver Profiler, zero-crossing detection, and solver resets.
Modifying and Simplifying Your Model
Most of the techniques described so far require few, if any, changes to the model itself. You can achieve additional performance improvements by applying techniques that involve modifications to the model.
Accelerating the Initialization Phase
When you update or open a model, Simulink runs the mask initialization code. If you have complicated mask initialization commands that contain many calls to set_param, consider consolidating consecutive calls to set_param() into a single call with multiple argument pairs. This can reduce the overhead associated with these calls.
Learn more about mask code execution.
If you use MATLAB scripts to load and initialize data, you can often improve performance by loading MAT-files instead. The drawback is that the data in a MAT-file is not in a human-readable form and can therefore be more difficult to work with than a script. However, loading typically initializes data much more quickly than the equivalent script.
Learn more about loading data from a MAT-file into your workspace.
Reducing Model Complexity
Simplifying your model without sacrificing fidelity is an effective way to improve simulation performance. Here are three ways to reduce model complexity.
Replace a subsystem with a lower-fidelity alternative. In many cases, you can simplify your model by replacing a complex subsystem model with one of the following:
- A linear or nonlinear dynamic model created from measured input-output data using System Identification Toolbox™
- A high-fidelity, nonlinear statistical model created using Model-Based Calibration Toolbox™
- A linear model created using Simulink Control Design™
- A lookup table
You can maintain both representations of the subsystem in a library and use variant subsystems to manage them.
Learn more about using configurable subsystem blocks.
Learn more about working with lookup tables, optimizing breakpoint spacing in lookup tables, and optimizing generated code for lookup table blocks.
Learn more about reduced order modeling.
Reduce the number of blocks. When you reduce the number of blocks in your model, fewer blocks will need to be updated during simulations, leading to faster simulation runs. Vectorization is one way to reduce your block count. For example, if you have several parallel signals that undergo a similar set of computations, try combining them into a vector and performing a single computation. Another way is to enable the Block Reduction optimization in the Optimization > General section of the configuration parameters.
Use frame-based processing. In frame-based processing, samples are processed in batches instead of one at a time. If your model includes an analog-to-digital converter, for example, you can collect the output samples in a buffer and process the buffer with a single operation, such as a fast Fourier transform. Processing data in chunks in this way reduces the number of times that blocks in your model must be invoked. In general, scheduling overhead decreases as frame size increases. However, larger frames consume more memory, and memory limitations can adversely affect the performance of complex models. Experiment with different frame sizes to find one that maximizes the performance benefit of frame-based processing without causing memory issues.
Reducing Interactivity
In general, the more interactive the model, the longer it will take to simulate. The tips in this section illustrate ways to improve performance by giving up some interactivity.
Disable debugging diagnostics. Some enabled diagnostic features noticeably slow simulations. You can disable them in the Diagnostics pane of the Configuration Parameters dialog box.
Note: Running the array bounds exceeded and solver data inconsistency diagnostics can cause a noticeable slowdown in model run-time performance.
Disable simulation animations. By default, Stateflow charts highlight the current active states and animate the state transitions that take place as the model runs. This feature is useful for debugging, but it slows the simulation. To accelerate simulations, either close all Stateflow charts or disable the animation. Similarly, if you’re using Simulink 3D Animation™, SimMechanics™ visualization, FlightGear, or another 3D animation package, consider disabling the animation or reducing scene fidelity to improve performance.
Learn more about speeding up simulation in Stateflow.
Adjust viewer-specific parameters and manage viewers through enabled subsystems. If your model contains a scope viewer that displays a large number of data points and you can’t eliminate the scope, try adjusting the viewer parameters to trade off fidelity for rendering speed. Be aware, however, that by using decimation to reduce the number of plotted data points, you risk missing short transients and other phenomena that would be obvious with more data points. You can place viewers in enabled subsystems to control more precisely which visualizations are enabled and when.
Learn more about how scope signal viewer parameter settings can affect performance.
Using MATLAB Functions Instead of Interpreted MATLAB Function Blocks
To call a MATLAB function within your Simulink model, use a MATLAB Function block instead of an Interpreted MATLAB Function block or a MATLAB S-function. The MATLAB Function is the faster alternative. It supports the generation of embeddable C code. While the MATLAB Function block does not support all MATLAB functions, the subset of the MATLAB language that it does support is extensive.
To quickly find all the Interpreted MATLAB Function blocks in your model, use the Performance Advisor.
Learn more about working with the MATLAB Function block.
You log large data sets. When logging large amounts of data (e.g., in models that include To Workspace, To File, or Scope blocks), use decimation or limit the logged output to the last part of the simulation. Avoid logging redundant data (e.g., log the time only once) and extraneous data (e.g., log integer values instead of doubles when feasible). Logging override can also be used to control which signals are being logged without rebuilding the simulation target for accelerator mode.
Learn more about exporting simulation data.
Additional Useful Techniques
The following techniques apply to some specific models; if your model falls into the patterns described below, you probably can use these to increase simulation speed.
Optimizing Hardware Acceleration
Single instruction multiple data (SIMD) is a data-level parallel processing technique that performs the same operation on multiple data points simultaneously. Many modern CPUs have SIMD instructions that, for example, perform several additions or multiplications at once. For computationally intensive operations on supported blocks, SIMD intrinsics could improve the simulation performance.
Simulink supports SIMD for hardware acceleration in all simulation modes which use code generation technologies (e.g., accelerator or rapid accelerator mode).
Simulink provides a configset parameter to control the type of SIMD in the Simulation Target panel. For hardware acceleration options, the default choice (“leverage generic hardware”) is available on all X86 CPUs and no rebuild is needed. The other choice (“leverage native hardware”) is CPU dependent. If your CPU supports newer SIMD instruction sets such as SSE2/AVX2/AVX512 then this choice could provide even faster simulation speed (Figure 17).