A few days ago I happened to speak with a colleague about Simulink run-time performance, and we discussed various ideas for simulation speed-up. This topic is very important to MathWorks, as evidenced by a dedicated documentation section, newsletter articles (1, 2), webinars (1, 2, 3, 4) and multiple blog posts on improving simulation performance using the Simulink product.
This blog covers mainly the Matlab core product and closely-related toolboxes. However, since the various suggestions for improving performance are spread across so many resources, I thought that it would be worthwhile to create a post listing all the suggestions in a single place. When faced with a slow simulation, it’s nice to know that there are so many available speed-up options, so I hope readers will find it useful. Note that these suggestions are in the large part fully documented and supported. The ideas are listed based on semantic relationship, and not by order of importance:
- Select Accelerator or Rapid Accelerator simulation mode (also see here)
- Enable the options for Compiler optimization, automatic Block Reduction and others in the Optimization pane of the Configuration Parameters window.
- Switch the Compiler optimization from faster build to faster run.
- Disable the options for debugging/animation support, overflow detection and echoing expressions without semicolons in the Simulation Target pane of the Configuration Parameters window. Note that overflow detection is important for fixed-point data so if your model includes FP data, keep this option enabled.
- Disable similar configuration options in Stateflow’s Configuration Parameters window (which is similar but separate from the Simulink window).
- Keep Stateflow charts and Simulink blocks/models closed during simulation to prevent run-time updates (this relates to the animation feature, if enabled)
- Keep output scopes closed during simulation run-time; open the scopes only after the simulation ends. If a scope must remain open in run-time, reduce the number of data points (via decimation, reduced time range and reduced number of plotted signals), increase the plotting refresh period, disable scope scrolling, remove data markers and legends, limit the history size, and use reduced fidelity in the viewer parameters. A similar recommendation applies to regular Matlab plots, by the way.
- Vectorize processing by combining multiple signals into a vector and applying the processing on the vector rather than on the separate signals.
- Sample data in batches (rather than one-by-one) using frame-based processing (R2011b and newer). This can speed up simulations by 10x or more, at the expense of just a little extra memory.
- Aggregate small blocks into larger ones.
- Load the model in memory using load_system instead of open_system and simulate it using the sim command, then post process/display the outputs.
- Avoid blocks that do not support code generation, since they would have to be run in slower interpreted mode.
- If your simulation does not use linear algebra (matrix arithmetic), disable BLAS library support in the Simulation Target pane. But if it does use it, then ensure that BLAS is enabled for optimal performance.
- Avoid algebraic loops where possible.
Interpreted Matlab Functionblocks. Instead, use
Note: IMHO, the naming of these blocks is quite confusing:
Matlab Functionrefers to a subset of the Matlab language previously called
Embedded Matlabthat can be directly converted into C-code without requiring the Matlab run-time (MCR); this is basically the language subset used by the Matlab Coder Toolbox.
Interpreted Matlab Functionrefers to the full Matlab functionality and requires the Matlab run-time (MCR) – it was previously called
- Reduce, simplify or eliminate initialization and termination phases.
- Use the Mask Editor to reduce block images resolution and size.
- Store configurations in loadable MAT files rather than programmatically.
- Consolidate multiple set_param calls into a single call having several name / value pairs.
- Use a stronger platform (esp. increase the amount of memory).
- Limit or disable log output, and disk I/O in general.
- Use the sldiagnostics and slprofreport functions and the Simulink Profiler, to identify and eliminate simulation bottlenecks.
- Use the performanceadvisor function and the Simulink Model Performance Advisor to automatically adjust model configurations for best performance (R2012b and newer; also see this webinar).
- Manually adjust model solver parameters for optimal performance, based on a-priori knowledge about the model behavior.
- Use a faster solver that can still process the model. Different models may require different solvers to run optimally (also see here).
- Tune solver parameters (decrease solver order, increase step size and error tolerance) to improve solution convergence speed at the expense of some accuracy. One user reported a 20x speedup by simply increasing the step-size.
- Prevent excessive zero-crossing or disable zero-crossing detection (also see here and here).
- Use simpler models or models with reduced fidelity.
- Store the simulation state at specific points, then load these states for repeated later simulations, thus saving the simulation time of the loaded portion.
- Run simulations in parallel using Parallel Computing Toolbox (also see here, here and here). An easy way to do this is to run the sim command within a parfor loop.
- Move some calculations onto FPGA or GPU.
- Use a real-time system and set simulation speed to real-time (high priority).
Some additional ideas can be found in the blog posts and webinars mentioned in the hyperlinks at the top of this post. One of the newsletter articles concludes with an example of how applying some of these techniques to a specific model resulted in reduced simulation time, from 453 down to 5 seconds.
I would like to take this opportunity to congratulate MathWorks on a job well done with Simulink’s Performance Advisor. I expect a variant of this tool to be available for regular Matlab m-code in some future Matlab release; it would blend in nicely with the existing mlint (Code Analyzer) tool.
Advanced Matlab Programming course – London 10-11 March, 2014
If this topic has piqued your interest, consider joining my Advanced Matlab Programming course in London on 10-11 March, 2014. In this course/seminar I will explore numerous other ways by which we can improve Matlab’s performance and create professional code. This is a unique opportunity to take your Matlab skills to a higher level within a couple of days.
- Improving save performance There are many different ways of improving Matlab's standard save function performance. ...
- Improving fwrite performance Standard file writing performance can be improved in Matlab in surprising ways. ...
- Simulink Data Dictionary Simulink contains undocumented public API for access to its data dictionary functionality. ...
- Plot performance Undocumented inner plot mechanisms can be used to significantly improved plotting performance...
- Performance: scatter vs. line In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...
- New book: Accelerating MATLAB Performance Accelerating MATLAB Performance (ISBN 9781482211290) is a book dedicated to improving Matlab performance (speed). ...