Guy on Simulink

Simulink & Model-Based Design

Tips for Simulation Performance

Blogger, Guy RouleauBy Guy Rouleau

One of the questions I hear the most often from Simulink users is What can I do to make my model run faster? Here is a list of tricks that can help.

Displays and Scopes

When you have a display or a scope in your model, Simulink needs to update it. To speed up the execution of the model, display only the necessary. This can mean:

  • Limit the amount of visible displays and open scopes.
  • For open scopes and displays, set the decimation to a reasonable value.
  • 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.

Interpreted blocks

When a model includes a MATLAB Fcn block or MATLAB file S-function, the MATLAB interpreter is called at each time step. Try replacing these blocks by the Fcn block, the Math Function block, the Embedded MATLAB Function block (without extrinsic functions) or a C-MEX S-function.

Algebraic loops

I saw a TV commercial recently. There was a comment about pick-up lines that I think could also apply for algebraic loops:

"There is a time and a place for them. The time is never... you can figure out the place on your own."

Seriously, sometimes algebraic loops are unavoidable, but they always slow down models.

Solver Options

Solver settings influence simulation performance, especially for variable-step solvers. Look at the time steps taken by your model. I usually do it by logging the simulation time (tout) and displaying it using this command:

semilogy(tout(1:end-1),diff(tout))
Based on what you see, it is possible to determine which setting to change. This can include solver choice, max step size, relative and absolute tolerances and zero crossings detection, etc.

How solver settings influence simulation performance is a complex topic too long for this post. I recommend looking at the documentation page titled Choosing a solver for more details. Leave a comment below if you would like to see a future post on improving simulation performance through solver settings.

Accelerator and Rapid Accelerator modes

Accelerator mode converts the block methods into a C-MEX S-function. Since the block methods are compiled instead of interpreted, the model usually runs faster. The Rapid Accelerator mode creates a standalone executable from your model. The executable is launched in a different process that will run in a separate thread on a different processor if you have a multi-core machine.

Simulation mode can be set to Normal, Accelerator or Rapid Accelerator for optimal performance.

Here are a few tips when using the Accelerator and Rapid Accelerator mode:

  • Avoid blocks that do not support code generation. They run in interpreted mode and can slowdown your model.
  • Try switching the Compiler Optimization Level from faster build to faster run.
  • For maximum speed, start your simulation using the sim command when using these modes.

Look at the documentation page titled Comparing Performance. This will give you a good idea of when these modes should be used.

Parallel Computing Toolbox

To run multiple simulations in parallel, calling sim from within parfor is a good idea. This allows you to fully take advantage of all the processors in your machine when performing parameter sweeps and Monte Carlo analysis.

In your MATLAB installation, look for the demo titled Rapid Accelerator Simulations Using PARFOR for an example.

Where to look for more?

The Simulink documentation contains a chapter titled Improving Simulation Performance and Accuracy. I strongly recommend going through this chapter to create simulations running at optimal speed.

Now it's your turn

How do you ensure that your simulation is running at optimal speed? Do you have tricks not mentioned here? Post a comment here.

|
  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.