[IPOL discuss] matlab to C/C++ translation tools
Pascal Getreuer
getreuer at cmla.ens-cachan.fr
Sat Mar 23 20:20:32 CET 2013
Automatic code translation is interesting because it opens new
possibilities. Suppose we can translate authors' MATLAB code to C++,
then the demo server could use the C++ translation to run the online
demo without needing MATLAB. We could also consider to distribute the
C++ translation alongside the MATLAB code as a second reference
implementation; readers who do not have MATLAB can still use the C++
version.
There are, however, serious issues with such automatic translation
that must be considered. We must be careful about the implications to
correctness, readability, and library dependencies.
== Correctness ==
I first want to point out that translation of arbitrary MATLAB code to
C++ is a monster of a problem. There are fundamental differences
between these languages:
* MATLAB is dynamically typed, C++ is not (but it can be emulated,
e.g. boost::any [1]).
* MATLAB is vector-based, C++ is not (but it can be emulated, see
e.g. Blitz++ [2] or OpenCV [3]).
* MATLAB supports anonymous functions and closures, C++98 does not
support them (but C++11 does).
* MATLAB supports nested functions, C++ does not (but it can be
emulated with nested classes).
* MATLAB's symbolic toolbox allows to perform symbolic algebra and
calculus, I am not aware of any C++ analog.
* MATLAB's combination of dynamically typing and polymorphism allows
generically written routines (e.g., function Y = mymean(X), Y =
sum(X(:))/numel(X); works for X an array of any dimension, dense or
sparse, real or complex, single or double). C++ can, with hard work,
approximate this effect (see e.g. the cv::Mat class in OpenCV [3])
* MATLAB has an "eval" function that executes a given string of
arbitrary M-code.
* MATLAB continues to add new features to the language
Since translation is difficult, it would not be surprising if it were
to break correctness of the code. It would be good to require authors
to include a suite of unit tests so that we can adequately test the
translation's correctness.
== Readability ==
Translation probably degrades readability, as Jean-Michel noted:
> -will such a C++ code be readable? I gather that still it will be better to
> review the original Matlab code
I believe that it is definitely the case that the MATLAB code is
better for review. Humans are much better than machines at making
human-readable code. The review process could be to review only the
MATLAB code, then apply the unit tests (as mentioned above) to verify
the C++ translation.
== Libraries ==
> -what about the functions in Matlab whose code is not public? How could they
> possibly be translated?
Fortunately, many MATLAB functions are open-source because they are
implemented in M-code or because MATLAB links to open-source libraries
(LINPACK, UMFPACK, Qhull, etc.). For those functions where the code
is not public, there are usually open-source equivalents in GNU Octave
[4].
The downside is that translated code might depend on any or all of
those libraries that MATLAB uses. We currently allow just a few
essential and very mature libraries (FFTW, libpng, libtiff, and zlib)
to avoid maintenance problems. For example, if a library has a
security bug that involves API changes to fix, then the published code
would need to be updated to the new API---but we certainly want to
minimize any changes to already-published code. Furthermore,
additional dependencies makes code harder to build and complicates
licenses.
[1] http://www.boost.org/doc/libs/1_53_0/doc/html/any.html
[2] http://blitz.sourceforge.net/
[3] http://opencv.willowgarage.com/wiki/
[4] http://www.gnu.org/software/octave/
Best,
Pascal
On Sat, Mar 23, 2013 at 1:55 PM, Nicolas Limare
<nicolas.limare at cmla.ens-cachan.fr> wrote:
> Hi, (one message for two answers)
>
> Jean-Michel Morel wrote:
>> -will such a C++ code be readable? I gather that still it will be
>> better to review the original Matlab code
>
> I do not know if it will be readable, but it is not excluded. Code
> generation tools are improving, and if the C++ code uses the same
> variable names, comments and code structure as the MATLAB version, it
> could be as readable. Be we can can only know more if we try it.
>
>> -what about the functions in Matlab whose code is not public? How
>> could they possibly be translated?
>
> Here again, I do not know. I see a few possibilities. They could use
> the functions from MATLAB binary libraries, without disclosing the
> implementation. They couls call functions in other binary libraries
> designed by the translation tool vendor to reproduce MATLAB
> results. Or they could use functions available in source code, either
> from the translation tool vendor or using existing open-source
> libraries.
>
> Miguel Colom wrote:
>> Mathworks (the Matlab company) sells also a product called "Matlab
>> coder" that creates ANSI C/C++ code [1].
>>
>> If we had to chose one, using the product from the company that
>> sells Matlab is the clearly the best option over third-party
>> solutions, for long-term maintenance reasons.
>
> It depends on how it works. If these translators produce 100% source
> code versions, there is no maintenance problem since after the
> translation we only need to manipulate C++ code. If the translated
> code uses MATLAB libraries, then yes the MATLAB product looks more
> safe.
>
> But I read that in "some cases", the MATLAB products fails to
> translate while the other products can succeed. And the two other
> companies are interested in academic collaborations, so it could be
> more flexible than just buying a Mathworks product.
>
>> As Jean-Michel pointed out, there's some problems with these kind of
>> tools. Users prefer Matlab because they can use black-boxed
>> libraries and Matlab build-in types. The black-boxes will simply be
>> called from the C/C++ code and therefore the requirement of knowing
>> exactly what the code can't be accomplished. And for the build-in
>> types, it's the same problem.
>
> Yes, the MATLAB Coder page[1] states that it produces MEX functions,
> so yes, clearly, they still need to be used with MATLAB and call the
> same black box internal libraries. The only benefit is better speed
> performance.
>
> [1] http://www.mathworks.fr/products/matlab-coder/
>
> But if I understood correctly, the two other tools may produce real
> C++ programs, at least usable without the MATLAB run-time interface
> (and maybe without MATLAB libraries).
>
> If you are interested (or anyone else using MATLAB here), I can give
> you contacts of people at Silkan and Metascale to evaluate their tool
> and compare with MATLAB Coder.
>
> Best,
>
> --
> Nicolas LIMARE - CMLA - ENS Cachan http://limare.perso.math.cnrs.fr/
> IPOL - image processing on line http://www.ipol.im/
>
> _______________________________________________
> discuss mailing list
> discuss at list.ipol.im
> https://tools.ipol.im/mailman/listinfo/discuss
--
Pascal Getreuer
CMLA, ENS Cachan
getreuer at cmla.ens-cachan.fr
www.getreuer.info
More information about the discuss
mailing list