Effect of using untyped variable in matlab function

5 views (last 30 days)
Does anyone know if the use of untyped variables in Matlab Function Block affect the speed of the simulation?
  5 Comments
Lorenzo Rossi
Lorenzo Rossi on 17 Oct 2022
No, i don't. I'm asking just to know if there is anything i can do in that direction to speed up my simulation process.
Thank you for your help
Paul
Paul on 17 Oct 2022
Upon further reflection, I'd like to walk back my comment, at least a little bit. I don't work with Matlab Functions that are too large. If you have a function that has many, many source lines of code, then maybe it can be helpful to the code generator to preallocate variables with size and type. Even then, I'm not sure what happens in a situation like this:
z = zero(2,2,'double') % at the top of the code
z = int32(eye(2)); % somewhere downstream
Maybe the doc has a "best practices" page for Matlab Function or code generation in general.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 14 Oct 2022
Edited: Walter Roberson on 14 Oct 2022
When acceleration is turned on, then each MATLAB Function Block has to have a copy of the code generated for each possible set of signal types. A lot of the time this is simple forward signal propagation of sizes and types, often double precision. Sometimes backwards propagation needs to be done to ensure that busses match sizes.
But every once in a while you get variant busses where there are different signal types or sizes on the path, and it can be necessary to do more work to identify all of the possibilities and to generate appropriate code for every combination that it cannot prove does not occur. In such a case if you know through external logic that your function should only ever be called with a particular combination of types, then it can make analysis faster and prevent extra code generation if you use the port manager to configure for particular signal types or sizes. This might end up pushing a type-check backwards in the dynamic path to generate a conflict if it manages to generate a different combination -- but that can be faster than generating code for everything that the analysis cannot disprove.
  5 Comments
Walter Roberson
Walter Roberson on 14 Oct 2022
When you use if/else constructs, or switch constructs, or variant subsystems, then the different routes will not necessarily lead to the same type for the different versions of a signal.
Paul
Paul on 14 Oct 2022
Edited: Paul on 14 Oct 2022
It's my understanding that Simulink determines all signal types and sizes in the compile phase before the simulation actually enters the run loop. I believe a consequence of this is that at compile time the type and size of signals input to a Matlab Function block are all known and code is generated based on those types (assuming the input ports type and size are set to inherited).
AFAIK, Simulink is statically typed with all signal types and sizes determined in the compile phase. Variable size signals are not dynamically allocated duriin a run, as they could be in C++ using 'new' , for example.
As to the code generation question, I put a call to mvnrnd in a Matlab Function block w/o using coder.extrinsic. mvnrnd does not support code generation. Even with the model set to run in Normal mode, clicking Update still generated an error:
Function 'mvnrnd' not supported for code generation.
So it certainly looks like Simulink was trying to generate code, even in Normal mode.

Sign in to comment.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!