Git Product home page Git Product logo

mex's Introduction

Using Fortran and C++ mex files with Matlab

Build Status

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.

Installation

Fortran

  • Edit fortran.makefile and make sure that MDIR points to your Matlab installation
  • The default compiler is gfortran. If you want to use Intel's ifort, uncomment the appropriate lines in fortran.makefile
  • Compile the mex file using make -f fortran.makefile

C++

  • Edit cpp.makefile and make sure that MDIR points to your Matlab installation
  • The default compiler is g++ (gcc or clang depending on your system). If you want to use Intel's icpc, uncomment the appropriate lines in cpp.makefile
  • Compile the mex file using make -f cpp.makefile

Example

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;
}

Computational Performance

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

Files in this Repository

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 Matlab
  • cpp.makefile C++ makefile for Linux and Mac OS X
  • fibonacci.m Matlab implementation of the Fibonacci function
  • fortran.makefile Fortran makefile for Linux and Mac OS X
  • main.m Matlab script that calls both the Matlab and Fortran implementation

mex's People

Contributors

jtilly avatar klimentyev avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.