Manual MEX vs MEX by codegen
Mostra commenti meno recenti
Hi,
The instructions how to write a MEX file look easy: https://uk.mathworks.com/help/matlab/matlab_external/standalone-example.html
However, when I look what autogenerated MEX function does, it is very confusing and much more complicated. I see calls to emlrt library, which should just wrap the conversion of parameters between Matlab and C (no actual C source code is available), but there are references to a global object (emlrtContextGlobal)and also some calls specific to particular Matlab releases. So it looks a bit messy and I wonder why.
I see that for example that mxgetpr calls has been recommended not to be used: https://uk.mathworks.com/help/matlab/apiref/mxgetpr.html. (I tripped over the problem mentioned there before finding this link).
So the question are:
- Why the MEX generated code and the code a user is supposed to write manually are so different?
- How that manully written code is compatible with future Matlab releases?
Thanks
2 Commenti
Denis Gurchenkov
il 6 Ott 2022
- Why the MEX generated code and the code a user is supposed to write manually are so different?
Please do not look at the Codegen-generated mex C code - it is not designed to be human-readable and/or copyable and modifiable. Treat it similar to how you treat the output of a C compiler - if you look at the assembly code, by far not everything you see there would be similar to what a human would write.
As to why there are emlrt calls, the global parameter, etc - that's because Codegen-generated mex files support capabilities require those. Runtime error reporting with good stack traces, ability to execute code in multiple threads using OpenMP, ability to call back to MATLAB to support execution of functions that are not compatible with code genreration, etc. Unless you really need to support all these concepts, it is likely that your hand-written mex code is going to be much simpler, and for a good reason.
Dusko Vujadinovic
il 10 Ott 2022
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su MATLAB Coder in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!