Technical Articles

MathWorks Insights

Speed Up Your Simulations with Rapid Accelerator Mode


Author:

  • Guy Rouleau, MathWorks

4-minute read

Summary

  • Rapid Accelerator mode improves Simulink simulation performance by generating an executable version of a model, often providing substantially faster execution than Normal mode.
  • Rapid Accelerator delivers maximum simulation performance for batch and Monte Carlo workflows, though it requires code generation compatibility and does not support debugging, or profiling.
  • Batch simulation scripts can run more efficiently by skipping up-to-date checks when the model structure remains unchanged.

If you are doing Monte Carlo simulation or system optimization on your Simulink® model, you’ll need to run multiple simulations while varying model parameters. How do you get each simulation to run as quickly as possible?

In Simulink, there are three desktop simulation modes: Normal, Accelerator, and Rapid Accelerator. For most models, Accelerator is faster than Normal, and Rapid Accelerator is faster still. Rapid Accelerator mode speeds up simulation by generating an executable for your model. The exact speedup varies depending on the model, but Rapid Accelerator can be more than 10 times faster than Normal mode.

Using Rapid Accelerator involves trading off flexibility and performance. Rapid Accelerator does not support the debugger or profiler, and it works only when the entire model is capable of generating C code. But if your goal is efficient batch and Monte Carlo simulations, then Rapid Accelerator is your best option.

This article shows how to set up your batch simulation script to get the maximum performance out of Rapid Accelerator.

Setting Up the Model—A Bouncing Ball Example

Each time you run a model or use the sim command, Simulink checks for changes in the model. Depending on the size of the model, this initialization phase can be time-consuming. In Rapid Accelerator mode, if you know that your model did not change structurally, you can skip this step by simply turning off the RapidAcceleratorUpToDateCheck option. For any model, no matter how large, this reduces initialization time to zero.

For illustration, we’ll use a simple model of a bouncing ball (Figure 1). In this model, a ball drops from a certain height onto a hard surface. The position and velocity of the ball are measured at each step of the simulation. The parameters are gravity, the ball’s initial height and velocity, and the coefficient of restitution (the bounciness of the ball).

A diagram showing the parameters for the bouncing ball model.
Figure 1. Bouncing ball model.

Suppose we want to vary the coefficient of restitution, specified as k in the block dialog.

Define the vector of values to be simulated:

k_values = [-0.9:0.1:-0.1];

Create an array of Simulink.SimulationInput objects that will tell Simulink to:

  • Simulate the model in Rapid Accelerator mode.
  • Skip the Rapid Accelerator up-to-date check.
  • Use the coefficient of restitution values from the above vector.
mdl = 'sldemo_bounce'; 
in(1:length(k_values)) = Simulink.SimulationInput(mdl); 
for i = 1:length(k_values) 
    in(i) = in(i).setModelParameter('SimulationMode','Rapid'); 
    in(i) = in(i).setModelParameter('RapidAcceleratorUpToDateCheck','off'); 
    in(i) = in(i).setVariable('k',k_values(i)); 
end

We are now ready to simulate the model multiple times in Rapid Accelerator mode using the sim command:

out = sim(in);

This will return an array of Simulink.SimulationOutput objects containing the simulation results.

Using this workflow with parallel computing is straightforward. All you do is replace sim with parsim in the above line of code.

If you modify the model and need to rebuild the Rapid Accelerator target, you can use Simulink.BlockDiagram.buildRapidAcceleratorTarget.

Rapid Accelerator mode is just one way to speed up simulations. You can also use the Performance Advisor, which lets you analyze your model for simulation bottlenecks, or Fast Restart, which lets you perform iterative simulations without recompiling the model or terminating the simulation each time.

Published 2023 - 92303v00


Products used: Simulink

About the Author

Guy Rouleau

Guy Rouleau is a consulting advanced support engineer at MathWorks and has been supporting Simulink users since 2007. In his role, Guy works closely with customers on technical challenges, bringing his expertise in engineering systems and applied modeling to support advanced MATLAB and Simulink workflows. Guy is the author of the “Guy on Simulink” blog, where he shares tips and tricks he’s learned while supporting Simulink users. He holds a master’s degree in mechanical engineering from McGill University, in collaboration with the Canadian Space Agency.

Keep Exploring