This repository contains example programs for the use of mex files in Matlab on Linux and Mac OS X. I'm computing Fibonacci numbers in Matlab, Fortran, and C++. All of this works with GCC and Intel compilers.
- Edit
fortran.makefile
and make sure thatMDIR
points to your Matlab installation - The default compiler is
gfortran
. If you want to use Intel'sifort
, uncomment the appropriate lines infortran.makefile
- Compile the mex file using
make -f fortran.makefile
- Edit
cpp.makefile
and make sure thatMDIR
points to your Matlab installation - The default compiler is
g++
(gcc or clang depending on your system). If you want to use Intel'sicpc
, uncomment the appropriate lines incpp.makefile
- Compile the mex file using
make -f cpp.makefile
Run main.m
in Matlab:
% use matlab (correct answer is 75025)
tic
fibonacci(25)
toc
% use Fortran
tic
gatewayFortran(25)
toc
% use C++
tic
gatewayCpp(25)
toc
Next, consider the different implementations of the Fibonacci function. Note that this is only meant as an illustration. There are much better ways to compute the Fibonacci numbers than the algorithm I'm using here.
The function fibonacci
calls a Matlab implementation of the Fibonacci function:
function [ fnum ] = fibonacci( n )
if (n<2)
fnum = n;
else
fnum = fibonacci(n-1) + fibonacci(n-2);
end
end
The function gatewayFortran
calls the mex file that is implemented in Fortran. The underlying Fortran function is very simple:
recursive function fib (n) result (fnum)
integer, intent(in) :: n
integer :: fnum
if (n<2) then
fnum = n
else
fnum = fib(n-1) + fib(n-2)
endif
end function fib
The function gatewayCpp
calls the mex file that is implemented in C++. The underlying C++ function is very simple:
int fib(const int n) {
int fnum;
if (n<2) {
fnum = n;
} else {
fnum = fib(n-1) + fib(n-2);
}
return fnum;
}
Average time in seconds to compute the 25th Fibonacci number (=75025) on different platforms using the GCC compilers:
Matlab | Fortran | C++ | |
---|---|---|---|
Desktop Computer running Ubuntu | 1.7893 | 0.0022 | 0.0019 |
Macbook Air running Mac OS X Yosemite | 2.0151 | 0.0024 | 0.0023 |
I'm keeping the Fortran and C++ code self-contained. I'm also providing standalone programs ./standalone.f90.out
and ./standalone.cpp.out
that can be used to debug the Fortran and C++ code independently from Matlab. Travis CI is only building these standalone programs.
/c++
-fibonacci.cpp
Fibonacci function -fibonacci.h
header file for Fibonacci function -gatewayCpp.cpp
gateway script that can be called from Matlab -standalone.cpp
a standalone program that calls the Fibonacci function independently from Matlab/fortran
-fibonacci.f90
module with Fibonacci function -gatewayFortran.f90
gateway script that can be called from Matlab -globaldef.f90
module with global definitions -standalone.f90
a standalone program that calls the Fibonacci function independently from Matlabcpp.makefile
C++ makefile for Linux and Mac OS Xfibonacci.m
Matlab implementation of the Fibonacci functionfortran.makefile
Fortran makefile for Linux and Mac OS Xmain.m
Matlab script that calls both the Matlab and Fortran implementation