Manual MEX vs MEX by codegen
이전 댓글 표시
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
Denis Gurchenkov
2022년 10월 6일
- 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
2022년 10월 10일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 MATLAB Coder에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!