Why is there a performance slow down when I am running a model in SimEvents 4.2 (R2012b)?

12 views (last 30 days)
I am trying to run a model with 1000s of replications and it takes a long time to run the model (around 1 hour). After incorporating the functionality of parallel computing toolbox on a 8 core machine, the simulations still took a long time. I am looking for ways to improve the speed of the model simulation in SimEvents.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 5 Nov 2020
Edited: MathWorks Support Team on 5 Nov 2020
The performance bottleneck could be caused by the presence of the MATLAB Function blocks. If there are several of them used in a model, there could be a severe performance slow down.
As a limit test, one could comment out all the MATLAB Function blocks in the model and check if the compile time reduces.
If MATLAB Function blocks are replaced with built-in Simulink blocks implementing the same algorithm, the compile time would reduce quite a lot. Ofcourse, some of the algorithms implemented in MATLAB Function block are not easy to replace.
Secondly, in MATLAB function blocks, if one uses functions that call MATLAB interpreter, it leads to a severe slow down of model simulation.
For example, consider this code:
function y = fcn(u)
numEls = find(u>0);
y = length(numEls);
This function finds the number of elements of 'u' that are greater than zero and return this number (always a scalar value). The local variable numEls however, is not a scalar value. In fact, it is a variable-sized value because each time that 'u' changes, the answer of find(u>0) will have a different dimensionality. This means that each time we reallocate the memory for numEls. This results in a lot of overhead.
This code can be re-written without any variable-sized values as:\n
function y = fcn(u)
greaterThanZero = u>0;
y = sum(greaterThanZero(:));
If one replaces all occurrences of such functions with their equivalent fixed-size functions, then the MATLAB Function block can run as compiled C code, without making any expensive calls to the MATLAB Interpreter at runtime.
A list of the supported functions for MATLAB Function block is available here:
 https://www.mathworks.com/help/simulink/slref/matlabfunction.html
Finally, to increase performance, turn off all debugging-related options from the Simulation Target pane of the model's Configuration Parameters dialog. These options are useful when one is developing the algorithms. But once in production, they can be turned off for better performance.
More information about these options:
Additional useful performance-related pages:
How can I speed up simulation of my Simulink model?

More Answers (0)

Categories

Find more on Discrete-Event Simulation in Help Center and File Exchange

Tags

No tags entered yet.

Products


Release

R2012b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!