1

I'm going to convert a MATLAB code "full of operations on matrices " to C++ ,I'm not sure if MATLAB coder would generate an efficient C++ code.Is it better to convert it by hand although it's so time consuming?

4
  • 1
    Combine your two approaches. Use MATLAB to generate the C++, then review the C++ to see if you can optimize. You may want to optimize the MATLAB code before conversion. Commented May 2, 2018 at 14:25
  • @ThomasMatthews Thanks,just one more thing ,does MATLAB coder use armadillo library or it handles matrix operations via loops? Commented May 2, 2018 at 14:47
  • I don't work at MATLAB, nor have I seen the source code. You'll have to do the conversion to verify. Commented May 2, 2018 at 14:55
  • I have more experience with Simulink Coder, but in that product the generated C++ code is very similar to the generated C code, and it doesn't depend on any kind of external matrix / linear algebra library. I believe that MathWorks puts substantial effort into making efficient generated code. Commented May 3, 2018 at 0:57

1 Answer 1

1

As others have said, generating the code with MATLAB Coder, compiling it with your compiler's optimizations on, and measuring is the only way to know if your needs will be met.

MATLAB Coder generates the code directly for most algorithms without using external libraries. In certain cases libraries may be used or may be requested by the user. Of particular interest for you may be BLAS and LAPACK integration.

If you are doing linear algebra as part of your matrix operations, then consider configuring MATLAB Coder to call high-performance BLAS and LAPACK libraries of your choosing:

https://www.mathworks.com/help/coder/ug/speed-up-matrix-operations-in-generated-standalone-code-by-using-blas-calls.html

https://www.mathworks.com/help/coder/ug/generate-code-that-calls-lapack-functions.html

That will cause MATLAB Coder to replace its algorithms with calls to the libraries you specify for linear algebra operations like *, \, linsolve, lu, svd, eig, etc.

MATLAB Coder provides suggestions for improving the performance of the generated code:

https://www.mathworks.com/help/coder/optimize-speed-of-generated-code.html

One possible approach in cases like this is to generate the code and profile it with a profiler like prof, VTune, AMD Codeanalyst, the Visual Studio performance tools, etc. If you find a few expensive kernels where the generated code may not meet your performance needs, hand write replacements for those parts with a C-like interface and integrate them with your generated code using coder.ceval.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you,that was very helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.