What processes are included in the compilation of a simulink model? How can the compile time of a MATLAB function block be reduced?

7 views (last 30 days)
I am currently working on a simulink model which includes a MATLAB function block. This function block is used to evaluate an expression for a Jacobian of a complex function at a certain state. This expression for the Jacobian was derived using the symbolic toolbox and yields a very lengthy expression for each element in the Jacobian. Since the Jacobian has size 3x6 a total of 18 expressions need to be evaluated in the MATLAB function.
When running the model it takes a significant amount of time to compile (15-30min). But once complete the simulation itself is quite fast (+/- 20sec). I am trying to understand what is happening during the compiling of the model, so that I can perhaps alter the code. I already tried to find common terms in the lengthy expressions, as I expect that the compilation process might include this process, but this does not seem to speed up the compilation time. Is there any advice available as to how to reduce the time required for the compilation of a simulink model? specifically for simulink models that include a MATLAB function.
  2 Comments
Fred Smith
Fred Smith on 14 Jan 2016
The compile time you are seeing are excessive for such a small example. In the past we have seen problems with code produced by the symbolic toolbox due to excessive use of specific indices x(1),x(2).... This triggered some bad combinatorics in the code generator. But in the cases I am referring to there were hundreds of thousands of specific indices. 18 should not be a problem.
What version are you using? 15a/15b have some changes that might make a difference here.
When you say compile time, do you mean update-diagram time (Ctrl-D) or build time (Ctrl-B)?
If a newer release doesn't resolve it, would you mind sharing the code? The development team would be interested in seeing this example.
Thanks!
Robbin van Hoek
Robbin van Hoek on 1 Feb 2016
I'm currently using Matlab 15a. would it make a difference if I switch to 15b?.
Regarding the 'compile time' I mentioned. I initially was referring to the time it took before the model started simulating after i made a change, since I normally do not build the model manually. I tested both the (ctrl-D) and (ctrl-B) actions and the update diagram is actually rather fast, whereas the build time is the slow time i'm referring to.
I dont mind sharing my code, but it is hard to produce a minimum working example i'm guessing. What would be the preferred method of sharing?

Sign in to comment.

Answers (1)

Jason Moore
Jason Moore on 23 Jan 2016
I have a few suggestions that you could do.
1.) Use the parallel computing toolbox with a parfor loop in your MATLAB function block. This should speed up your function. 2.) Also you could place this block into a model reference and the rest of your model could be outside of the model reference. Then you can turn on incremental build under the Configuration Parameter section for Model Reference. This way every time you rebuild the model reference will only be built when a change to that section occurs.
  1 Comment
Robbin van Hoek
Robbin van Hoek on 3 Feb 2016
Thank you for your reply!
1) I think that using parfor wont solve anything for my case. Inside the matlab function block there isn't a single loop. Only 18 explicit expressions. The problem is that these expressions are very long (they involve the derivatives of complex functions generated by the symbolic toolbox). From what i understand from http://nl.mathworks.com/help/distcomp/parfor.html the parfor is not suitable in this case.
2) The model reference might be a good idea. However, the problem that i have is that sometimes i do need to make some changes to the start of the matlab function (the long expressions remain the same however). But i assume that during compiling the entire matlab function is compiled from scratch, rather then only the part that has changed. Is there any way around this? I cannot separate the part that i change and the long expressions into separate blocks. But is there some way to inform the compiler that it can reuse certain parts? I'm guessing this is a long-shot since it is a very specific case, but if it is possible that would help tremendously.

Sign in to comment.

Categories

Find more on Manual Performance Optimization in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!