supress simulink re-compile on every call

62 views (last 30 days)
Amardeep on 9 Nov 2011
Commented: Andrew Hopkins on 14 Jun 2018
Hi everybody;
I have a somewhat odd problem so bear with me.
I was presented with a rather large simulation suite written in Matlab which calculates sensory returns in an environment. Part of what I had to do was to re-make part of it in Simulink.
The simulink model I have made is called every so often in the model (at a regular number of time steps) and executed once on the current system parameters and sensor returns.
The output of my model will affect the system model for the next four steps and thus affect my inputs for my next execution. It is for this reason I cant simply make a big time based array of inputs and squirt them in in one go simulating a whole run. (Think of it like a feedback element whose tunining parameters can be changed on the through path as well as internal to itself).
This means that I have to call the model every time it is needed rather than pre-generating. My problem is that every time I call the sim command it re-compiles the simulink. This slows down my one simulation run from 30 mins to 11 hours (due to the sim call every n time steps). Is there a way to prevent it from having to recompile? All my inputs are presented as constants as I cant ammend the data structures to include time steps as each execution is only a single time step.
Amardeep on 9 Nov 2011
Yes this appears to be the case. On every call the simulink window displays the compiling messages at the base of the window with the green progress bar. I was able to cut down the execution time a little by fixing all dimensions so it wouldnt have to waste time propagating all of them but this hasnt made a significant impact (although every little helps).

Sign in to comment.

Accepted Answer

Amardeep on 22 Nov 2011
As a solution I was able to find the block which was the problem but execution time using sim was very slow. Thus the best speed solution was to compile using real-time worksop and interface with the .exe using .mat files. This took the per execution time from ~2.5s down to ~0.2s.
  1 Comment
Mikkel Fristrup Schou
Mikkel Fristrup Schou on 11 Apr 2017
Can you elaborate how it is done with .exe and .mat ?

Sign in to comment.

More Answers (3)

Titus Edelhofer
Titus Edelhofer on 9 Nov 2011
Hi Amardeep,
Additionally take a look at the doc for
which can be used to find out, which blocks have changed in a way the accelerator does not like.

Giovanni on 2 Aug 2017
I have just received this technical support that solved this problem. It is a particular situation using simscape (topic is 'run-time parametres Vs compile-time parameters'). So if you do not need to change any simscape parameters but only need to change simulink parameters the 'set_param' lines are not needed, and the , 'FastRestart', option is enough! I love this team!
Support starts here (see also model option about automatic data save):
I understand that you want to avoid compilation time when running multiple simulations of the same Simscape model in a loop, while changing the value of a parameter.
Please note that Simscape run-time parameters are different from Simulink tunable parameters. You can visit the documentation for more information on how Simscape run-time parameters differs from Simulink.
A possible solution is to enable fast restart while specifying run-time parameters as follows :
% First we load the example model 'ssc_bipolar_nonlinear'
model = 'ssc_bipolar_nonlinear';
% In this model, the R3 resistor is varied within a for-loop.
% The result is then plotted for each value of R3.
set_param([model '/R3'], 'R', 'R3_par' )
set_param([model '/R3'], 'R_conf', 'runtime')
hold all
for R = [1000 2000 3000 4000 5000 6000 7000 8000]
R3_par = R;
% The simulations are launched using the FastRestart mode to avoid compilation time
simout = sim(model, 'FastRestart', 'on');
plot(simout.simlog_ssc_bipolar_nonlinear.R3.i.series.time, simout.simlog_ssc_bipolar_nonlinear.R3.i.series.values);
You can visit the following link for additional information on run-time parameters :

Charles Harrison
Charles Harrison on 9 Oct 2013
I feel your pain and have experienced similar situations before. As such there are two solutions that I can think of that can solve your problem:
1st Solution: Instead of using the 'sim' command, I suggest using the model command. Specifically the model command is advantageous if you are just trying to treat your model as a funciton and get outputs for a given set of inputs. Additionally make sure that you 'compile' and 'term' the model as few times as possible to prevent initialization overhead.
Example: model_name([],[],[],'compile');
for i=1:number_cases
output_vector = model_name(0,state_vector,input_vector);
2nd solution:
Tell the reference models not to rebuild. This is a two step process. First Go to Simulation->Configuration Parameters -> Model Referenceing -> Rebuild -> Never (Alternatively this can be also done progratically from the command line if needed. Just reply to this post if you need to know how). After that, make sure the secondary setting next to the rebuild parameter (regarding the error message) is set to None. I think it is the stupidest thing, but even if you tell Simulink not to rebuild, and dont set the error checking message also not to check, then it will still rebuild! Once this step is complete if you need to make changes to the reference models (like make a data or structural change) you need to temporarily set the Rebuild parameter to 'If out of Date' and then run the sim to recompile. Another alternative is if you need changes to be recognize, you can use the slbuild command.
Andrew Hopkins
Andrew Hopkins on 14 Jun 2018
It's sad how much I searched, and then a coworker found this link in 5 seconds.
It was not clear from the other posts (or even the model documentation), that you need to call each function successively for each time step for some total amount of simulation time.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!