I\'m trying to compile a very simple MEX file in Matlab using GCC/G++... First I checked that this is already installed in matlab by: !which gcc
output: /usr/bin/gcc
I was unable to get the mexopts.sh approach to work.
Instead I followed this first to make sure that I could get omp to work with xcode: clang-omp in Xcode under El Capitan
Then the mex compile st
mex CC='/usr/local/bin/clang-omp' -I/usr/local/include -I/usr/local/lib /usr/local/lib/libiomp5.dylib test.c
However, I get some weird results when comparing between Matlab and pure terminal based c. The common work, I put in the file "do_work.c":
double do_work(int maxit){
double tmp,x,x2;
int numThreads=0;
numThreads = omp_get_max_threads() ;
// numThreads = 4;
printf("Setting max num threads to %d.\n",numThreads);
omp_set_num_threads(numThreads);
// int Nthreads=omp_get_num_threads();
tmp = 0.0;
x2 = 0.0;
#pragma omp parallel for shared(tmp,x2) private(x)
for (int i=0;i<maxit;i++) {
x = 0.0;
for (int k=0; k<10000; k++) x += pow(.011,1.0/.5); // does some heavy computations
tmp += (double) i ;
if (i%1000==0){
printf("Hello, %d\n",i);
}
x2 += x;
}
printf("x2 = %f\n",x2);
return tmp;
}
I can compile this from command line using this wrapper file:
#include "stdio.h"
#include "math.h"
#include <libiomp/omp.h>
#include "do_work.c"
double do_work(int maxit);
int main(int argc, const char * argv[]) {
do_work(10000);
return 0;
}
I compile it by running
clang-omp -fopenmp test_c_wrapper.c
I can also interface to it from Matlab with this code:
#include "mex.h"
#include <libiomp/omp.h>
#include "math.h"
#include "stdio.h"
#include "do_work.c"
double do_work(int maxit);
void mexFunction(int nlhs, mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
do_work(10000);
return;
}
To compile this from Matlab, save it as test.c and run the first mex statement that I mentioned higher up. However, the results are really strange. It looks as if Matlab is not even using omp. At the same time, Matlab actually runs faster than the terminal based one, even though Matlab appears to run in serial mode? Strange... Also, the omp_get_max_threads() is acting weird, it doesn't return the same number every time when called from Matlab even though it does from terminal.
Finally, I found a proper way to solve it... First, the file mexopts.sh doesn't appear in the folder by default, is necessary to open a terminal and look for it and create it (then Matlab will redirect to it automatically when compiling with MEX):
find ~/ -name 'mexopts.sh'
and will appear:
/Users/FOO//.Trash/MATLAB_R2014a.app/bin/mexopts.sh
Then, copy it as:
cp /Users/FOO//.Trash/MATLAB_R2014a.app/bin/mexopts.sh ~/.matlab/R2014a
then go to the folder cd ~/.matlab/R2014a
and change permissions for your user as:
chmod u+rwx mexopts.sh
then, open it with your default text editor (Sublime text recommended) as:
open mexopts.sh
and edit the following: Change where appears macosx10.7 to your current version, in my case macos10.10 then, modify the following lines as describen in (http://www.mathworks.com/matlabcentral/newsreader/view_thread/334250):
# CC='xcrun -sdk macosx10.7 clang' #OLD
CC='xcrun /usr/local/bin/gcc' #NEW
# CXX='xcrun -sdk macosx10.7 clang++' #OLD
CXX='xcrun /usr/local/bin/g++' #NEW
# CLIBS="$CLIBS -lstdc++" #OLD
CLIBS="$CLIBS -lstdc++ -I/usr/local/lib -lgomp" #directory <-I/usr/local/lib> #NEW
#CXXLIBS="$MLIBS -lstdc++" #OLD
CXXLIBS="$MLIBS -lstdc++ -I/usr/local/lib -lgomp" #NEW
IMPORTANT NOTE:
Make sure that your current G++/G++4.9 is able to compile with OpenMP, trying to include <omp.h>
in a hello world file doing in the command line:
g++-4.9 -o test hello.cpp -fopenmp
or
g++ -o test hello.cpp -fopenmp
is also possible that a file is corrupted and is necessary to do Can not compile fortran because dyld: Library not loaded :
brew rm cloog
brew install cloog
(But check first)...
Is also possible that if you're not able to compile with OMP is necessary to do first a couple of things as described here (Compile OpenMP programs with gcc compiler on OS X Yosemite): 1. Got a new gcc complier from http://hpc.sourceforge.net/ 2. Place a new executable folder by $ sudo tar -xvf gcc-4.9-bin.tar -C / 3. Switched to it by export PATH=/usr/local/bin:$PATH
Finally, try to compile your MEX file with:
mex hello.cpp COMPFLAGS="/openmp $COMPFLAGS"
That's it ... .
MATLAB only supports XCode 5.1+ or 6.0 on Mac.
See this list of supported compilers, from Mathworks: Supported Compilers and Compatible Compilers - Release R2015a
EDIT!!!
Even Windows doesn't support a GCC compiler. However, if you can build your library in Windows there is a 3rd party library called Gnumex designed to provide MinGW or CygWin GCC compiler capabilities for Matlab MEX. Take a look.
An update on their website says that changed to Matlab have broken their utility. However, there seems to be a workaround available via this StackOverflow post.