Mc2For
Overview
MATLAB is a dynamic numerical scripting language widely used by scientists, engineers and students. While MATLAB’s high-level syntax and dynamic types make it ideal for prototyping, programmers often prefer using high-performance static languages such as FORTRAN for their final distributable code. Rather than rewriting the code by hand, our solution is to provide a tool that automatically translates the original MATLAB program to an equivalent FORTRAN program. There are several important challenges for automatically translating MATLAB to FORTRAN, such as correctly estimating the static type characteristics of all the variables in a MATLAB program, mapping MATLAB built-in functions, and effectively mapping MATLAB constructs to equivalent FORTRAN constructs.
In this paper, we introduce Mc2FOR, a tool which automatically translates MATLAB to FORTRAN. This tool consists of two major parts. The first part is an interprocedural analysis component to estimate the static type characteristics, such as the shape of arrays and the range of scalars, which are used to generate variable declarations and to remove unnecessary array bounds checking in the translated FORTRAN program. The second part is an extensible FORTRAN code generation framework automatically transforming MATLAB constructs to FORTRAN. This work has been implemented within the McLab framework, and we demonstrate the performance of the translated FORTRAN code on a collection of MATLAB
Built-in Functions
In Table 1, 2, 3 and 4, we list some mappings of arithmetic operators, relational operators, logical operators and commonly used mathematical built-ins from MATLAB to FORTRAN. Note that the left division (.) in MATLAB can be supported by swapping the operands of the operator and replacing the operator with the right division operator (./), and the colon operator in MATLAB can be supported by using an implied DO loop in an array constructor in FORTRAN. For example, the expression var = 1 : 10 in MATLAB will be converted to var = (/I, I=1, 10/) in FORTRAN.
TABLE 1: Mapping MATLAB arithmetic operators to FORTRAN
MATLAB arithmetic operators | FORTRAN arithmetic operators |
---|---|
+ (addition or unary plus) | + |
- (subtraction or unary minus) | - |
.* (multiplication) | * |
./ (right division) | / |
.\ (left division) | swap operands, then right division |
: (colon operator) | implied DO loop in array constructors |
.^ (power) | ** |
.’ (transpose) | TRANSPOSE |
* (matrix multiplication) | MATMUL/DOT_PRODUCT |
Table 2: Mapping MATLAB relational operators to FORTRAN
MATLAB relational operators | FORTRAN relational operators |
---|---|
< (less than) | .LT. |
<= (less than or equal to) | .LE. |
> (greater than) | .GT. |
>= (greater than or equal to) | .GE. |
== (equal to) | .EQ. |
~= (not equal to) | .NE. |
Table 3: Mapping MATLAB logical operators to FORTRAN
MATLAB relational operators | FORTRAN relational operators |
---|---|
< (less than) | .LT. |
<= (less than or equal to) | .LE. |
> (greater than) | .GT. |
>= (greater than or equal to) | .GE. |
== (equal to) | .EQ. |
~= (not equal to) | .NE. |
Table 4: Directly mapping MATLAB commonly used mathematical built-ins to FORTRAN
MATLAB built-ins | FORTRAN intrinsic functions |
---|---|
sum | SUM. |
ceil | CEILING |
floor | FLOOR |
mod | MODULO |
rem | MOD |
round | NINT |
sin | SIN |
asin | ASIN |
sinh | SINH |
cos | COS |
acos | ACOS |
cosh | COSH |
tan | TAN |
atan | ATAN |
tanh | TANH |
exp | EXP |
log | LOG |
log10 | LOG10 |
sqrt | SQRT |
abs | ABS |
conj | CONJG |
min | MIN |
max | MAX |
length | SIZE |
numel | SIZE |
size | SHAPE |
Matrix Indexing In Matlab
A brief intro is available here
People
Publications
- Paper:  Mc2For: A tool for automatically translating MATLAB to FORTRAN 95
- Thesis:  Mc2For: A MATLAB to Fortran 95 Compiler
- Technical Report:  Mc2For: A tool for automatically transforming MATLAB to Fortran 95
Software
Beta Release (April 20, 2014)
Hi, all, I am glad to release the beta version of Mc2For, a new MATLAB-to-Fortran compiler. Since the project is implemented in Java, we render the compiler in a jar package. Here is the link:
Mc2For.jar (One thing to remember: currently, we only support function files, not scripts.)
To run the compiler or view the help menu, you can type the command as “java -jar Mc2For.jar -help”. Since the first phase of the compiler is an interprocedual analysis to infer the type information of all the variables in order to declare them in the generated Fortran, if your MATLAB entry point function has any input, please provide the type information of that input to the compiler. For example, your entry point function has a real double scalar variable as input, the type information of this variable should be represented as “dble&11&REAL”, where “&” is delimiter to separate different components of the type information, like class, shape and isComplex. The example command to run the compiler to translate MATLAB program to equivalent Fortran is “java -jar Mc2For.jar -codegen my_entry.m -args “dble&11&REAL””.
There are numerous MATLAB built-in functions. Besides the commonly used operators or built-in functions which have directly mappings in Fortran, we have to write corresponing user-defined Fortran functions/subroutines to map MATLAB built-ins. Here, to support our benchmarks, we have already written several user-defined Fortran functions/subroutines. We put them in a stand-alone Fortran library. Here is link to the source of the library and the instruction to build the library.
The stand-alone Fortran Library
Finally, we highly recommend that you start the compiler with some simple MATLAB programs or our benchmarks, and after you are familiar with the interface, command and features we supported, feel free to try the compiler with your own code. Have fun!
Availability of Mc2For
We are currently working on this new MATLAB-to-Fortran 95 translator, Mc2For. We will distribute the first version of it when it is ready. You may request to be put on a list for notification when Mc2FOR is ready by sending an e-mail to mclab-list@sable.mcgill.ca.
It would be very helpful if you could also send the .m files for a typical application that you would like to compile, along with some typical input data for us to run the program. This will help us focus on the most important features.
Meanwhile, we are still working on improving the implementation of Mc2For, but you can check our source code on the GitHub repository.
P.S. Because our tool works interprocedurally, when you send us the code, please make sure, you send all the files of your project instead of segments of the project. Thanks again for your interest in Mc2For and McLab.