Implementation Tricks and Tools

Home

Calling C/C++ from MATLAB
MATLAB's ability to interactively examine data structures, save and load data to disk, and built-in plotting are very useful. But in order to get code in MATLAB to run fast, you generally have to use vectorization tricks that sometimes result in very ugly and un-readable code. A solution I've been using for the past year has been the C/C++ api for Matlab, called mex. Then you can write C/C++ code using easily-readable for loops. You can also use OpenMP inside your C/C++ code for easily parallelized computations to parallelize over multiple CPU cores. I first read about using OpenMP with MATLAB here. This author says you can't call any mex api routines from a parallel section of code because they are not threadsafe, but I suspect that the get-pointer routines mxGetPr, mxGetData, etc... are fine.


Last updated September 24, 2011.