Technical Articles and Newsletters

Speed Up Your Simulations with Rapid Accelerator Mode

By Guy Rouleau, MathWorks

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 only works 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).

Figure 1. Bouncing ball model.

Suppose we want to vary the coefficient of restitution, specified as k in the block dialog. To tune the value of k, we need to define it as a Simulink.Parameter object1:

k = Simulink.Parameter
k.CoderInfo.StorageClass = 'SimulinkGlobal';
k.Value = -0.9;

Once this is done, we can explicitly build the rapid accelerator executable:

mdl = 'sldemo_bounce';
rtp = Simulink.BlockDiagram.buildRapidAcceleratorTarget(mdl);

The buildRapidAcceleratorTarget function returns a structure containing all the information relevant to tunable parameters in the model. Using this structure, we can create an array of runtime parameter structures for all the values we want to run:

k_values = [-0.9:0.1:-0.1];
for i = 1:length(k_values)
    paramSet(i) = 
        Simulink.BlockDiagram.modifyTunableParameters(rtp, ...
        'k', k_values(i));

We are now ready to simulate the model multiple times in Rapid Accelerator mode using the sim command with the RapidAcceleratorUpToDateCheck and RapidAcceleratorParameterSets parameters:

for i = 1:length(k_values)
    simout(i) = sim(mdl,'SimulationMode','rapid',...
                      'RapidAcceleratorUpToDateCheck','off', ...

Using this workflow with parallel computing is straightforward. All you do is replace for with parfor in the above code.

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.

1 If you are using Simulink R2015a or earlier, you also need to turn on the “Inline Parameters” option in the Configuration Parameters.

Published 2015 - 92303v00

View Articles for Related Capabilities