MAh2014
MAh2014

Reputation: 147

Convert MATLAB to C++ using MATLAB coder

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?

Upvotes: 1

Views: 2438

Answers (1)

Ryan Livingston
Ryan Livingston

Reputation: 1928

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.

Upvotes: 1

Related Questions