Effect of using untyped variable in matlab function
5 次查看(过去 30 天)
显示 更早的评论
Does anyone know if the use of untyped variables in Matlab Function Block affect the speed of the simulation?
5 个评论
Paul
2022-10-17
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.
回答(1 个)
Walter Roberson
2022-10-14
编辑:Walter Roberson
2022-10-14
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 个评论
Walter Roberson
2022-10-14
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
2022-10-14
编辑:Paul
2022-10-14
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Model Verification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!