Clear Filters
Clear Filters

MATLAB Coder incomplete conversion

13 views (last 30 days)
Hi,
I'm trying to convert a little algorithm (about 35 files / 5000 rows of code).
I made some adjustments to the code:
- The unsupported functions have been wrapped in an hollow body function that return the same type of results as the original function. When the code will be ready, I will replace them with the ones found in an external library
- I fixed the code to clear any error that may appear in the "Check for run-time issues"
- The "Generated file partitioning method" has been set to "Generate one file for each MATLAB file"
- I placed the coder.inline('never') instruction in every function to avoid any optimization and understand better the generated code
I can correctly generate the C++ code without any error but when i look at the code I can see that not everything has been converted, some call in the middle are missing, event if i used the coder.inline('never') instruction! Also the wrapped unsupported functions are missing....
Am I missing something?
Thanks in advance
Paul

Accepted Answer

Andy
Andy on 24 Jul 2019
Hi Paolo,
The coder.inline('never') command specifies to turns off the function inlining optimization for a given function. If you want to disable inlining for all functions you can pass the following to the codegen command:
-O disable:inline
There are other optimizations and code transformatiosn that MATLAB Coder performs, not all of them can be turned off, so your code may change in other ways despite using coder.inline('never'). In your case I think MATLAB coder's constant folding algorithm removes functions that do nothing.
What I'd suggest doing is "trick" this optimization into thinking the function may have a side-effect by inserting a call to coder.ceval('//') into it. In other words do what we do in bar here:
function out = test()
bar();
out = 42;
end
function bar()
coder.inline('never');
coder.ceval('//');
end
For more suggestions on how to determine how the generated code relates to your original MALTAB code you may want to look here:
https://www.mathworks.com/help/coder/ug/tracing-between-generated-cc-code-and-matlab-code.html
Hope that helps,
-Andy
  2 Comments
Boris Axelrod
Boris Axelrod on 17 Sep 2019
Hello Andy,
Your suggestions didn't help to prevent code elimination as result of optimizations and code transformatiosn that MATLAB Coder performs. Actuall values will be avalible in run time but Coder eliminates "dead" code and does constant folding based on initial values (which are required to define types of variables). Essentially this "optimizations" make intended conversion of Matlab program into C/C++ imposible defeating purpose of Matlab Coder. Please help me and many other users with similar issues.
Thank you,
Boris
Denis Gurchenkov
Denis Gurchenkov on 17 Sep 2019
Hi Boris,
MATLAB Coder is intended to generate C or C++ code that produces same answers as the soruce MATLAB, and has same entry point functions. You are right that it does not perserve the contents of those functios -- it can eliminate parts of it, inline, and do many other transformations. There are many reasons why these tranformations are performed, some have to do with performance of the generated code, some have to do with the need to do parital evaluation of the source as to be able to convert from dynamically typed MATLAB to statically typed C/C++.
As Andy pointed out, there are "surgical fixes" avaialble that one can use as to stop some transformations in the code. If you have a speicfic example of a transform that you'd like to stop (you mentioned initial values of variables being used), if you can provide an example, someone may be able to offer another surgical solution that you can use. I want to streess that you would need to post a specific example as to get a solution.
That being said, if your goal is to do a line-to-line translation from MATLAB to C or C++, then MATLAB Coder is not the right tool. Its paradigm is biased towards "give me the right numeric answer".
hth,
Denis.

Sign in to comment.

More Answers (1)

Boris Axelrod
Boris Axelrod on 17 Sep 2019
Edited: Boris Axelrod on 17 Sep 2019
Hi Denis,
Thank you for quick response. Please note that based on https://www.mathworks.com/products/matlab-coder.htmlproduct description "MATLAB Coder™ generates C and C++ code from MATLAB® code". That's how it's marketed and sold and that is how most of users need it - to deploy their Matlab programs. While any program gives a numeric answer most of programs are written to work with variety of data not available at the time when program is written or compiled.
Existing paradigm of Coder to provide one-time execution based on the original data is a critical bug and Mathworks users are looking forward to fix. Such fix is crucial for users to be able to use Matlab products for development that can be eventually deployed.
Thank you,
Boris
  1 Comment
Denis Gurchenkov
Denis Gurchenkov on 17 Sep 2019
> provide one-time execution based on the original data
Boris, can you please elaborate? I don't understand you. Can you please provide an example? Thanks!

Sign in to comment.

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!