Video length is 6:11

Simulation Modes | Five Practical Tips to Speed Up Your Simulink Simulations

From the series: Five Practical Tips to Speed Up Your Simulink Simulations

In this video, we will introduce the three common simulation modes of desktop simulation: normal, accelerator, and rapid accelerator.  We will use examples to demonstrate the benefits and tradeoffs of each simulation mode, and when is the right time to apply them to your simulations.

Published: 16 Jan 2020

Hi. Do you have Simulink simulations that you would like to run faster, but don’t know where to start?  Then you are in the right place, as in this video series, I will show you five practical tips that can help you considerably speed up your simulation.

The first tip that I would recommend is to check your simulation mode. For desktop simulations, you can choose between the normal, Accelerator, or Rapid Accelerator modes.

Let’s open an existing model to explain what these three simulation modes are and illustrate how your choice of the simulation mode will affect the simulation speed. 

Once the model is open, you can select the simulation mode directly from the Simulation tab.

However, in this video, I will use the MATLAB command line to toggle between these three simulation modes and measure times, so that we can look a little deeper at the time cost.

To start off, let’s first set the simulation mode to normal and run the model. 

We can see that the initialization stage including the model update takes about 0.8 seconds and the overall simulation execution took about 54 seconds. Let’s now consider this to be the baseline and now let’s use the accelerator mode to see how it performs against the normal mode.

Well, it took a little longer in the initialization phase, about 2.5 seconds, however the simulation execution time just took 6 seconds. That’s about 9 times faster than the normal mode.

How can we explain this behavior?

First, let’s see how the simulation is generally carried out in Simulink. Simulink has an execution engine including solvers that invoke model methods to update the states and outputs of the model at each time step.   

In normal mode, Simulink executes the individual blocks one by one in an interpreted fashion.  This way, normal mode enables a complete feature set for the model including debugging and diagnostics. But the performance is not necessarily optimized.

However, with the Accelerator mode, a just-in-time (JIT) acceleration technique is used to generate code portions of the model methods in memory. Running this compiled code has less runtime overhead than the normal mode’s block-by-block execution, resulting in a shorter simulation execution.

But compiling this acceleration target usually takes extra time and that is why you see an increase in the initialization time.  

Fear not, for once the acceleration target is compiled and in memory, it is available for reuse.

So, if we run the model again in Accelerator mode, you will see a significantly smaller initialization time. 

Finally, let’s run the model with the final simulation mode, the Rapid Accelerator. 

We can see that it’s not too bad, not as good as Accelerator mode, but still better than normal mode. It took a total of 22 seconds of which 20 were used for the initial stage. The simulation execution took just 2 seconds. So how do we explain this behavior?

First, the Rapid Accelerator mode compiles the whole model into a Rapid Accelerator standalone executable which resides outside of MATLAB and Simulink, and runs in its own process. It then communicates with Simulink when the model runs. So while building the executable itself takes time, the runtime overhead is much lower with the whole model compiled. 

Furthermore, if you don’t change the model structure, recompilation is skipped when you rerun the model, thus saving you time for multiple simulations. Let’s see this by running the model again. Now, you see that it takes about 4 seconds which is the fastest among the three modes. 

Now that we have seen how these modes work, a couple quick takeaways about a rough performance comparison between the three modes will be that the Accelerator mode is usually faster than the normal mode unless your simulations are short.

Rapid Accelerator on the other hand has the least per-step simulation execution overhead but the most initialization overhead.

With this there are also trade-offs you need to keep in mind. Let’s conclude this video by summarizing this with the benefits of the Accelerator and Rapid Accelerator modes.

In Accelerator mode, you have this just-in-time (JIT) compiler that generates code for portions of the model, this results in a lesser time overhead when the model runs. 

However, this process of generating code has overhead. Additionally, run-time diagnostics like inf and nan checking are disabled in this mode. 

So a pro tip for you would be that since there is no limitation of model types that can run in this mode it’s always a good check to run your model in Accelerator mode for improved simulation speeds.

Now, Rapid Accelerator mode improves simulation speed by running as a standalone executable which, if possible, runs on a core different from where the MATLAB process runs.

However, to use this simulation mode you need to make sure that your whole model supports code generation. Also, analogous to the Accelerator mode, generating the standalone executable takes more time. So, if your simulation is short and you need to always recompile your model and henceforth a recompile of the Rapid Accelerator executable, then Rapid Accelerator may not be a good fit for you. Furthermore, debugging capabilities are disabled in this mode except for the scopes and viewers.

If you are running long and or many simulations, such as Monte Carlo simulations, then Rapid Accelerator can give you the best performance. 

Thanks for watching and if you are interested in exploring other methods of speed up, please watch our next video of this series about the Performance Advisor.

Related Products

Learn More