Saturday, April 20, 2013

Update: Progress on OpenMP and MEX on OS X (EDITED)

I've made some progress on getting things working.  By making the following changes to the mexpots.sh in my home home directory, I got things to compile and link directory.  First, change
CC='xcrun  -sdk macosx10.7  clang'

to
CC='llvm-gcc'

and make a likewise changes for CXX, using llvm-g++.  The other neccessary change is to ensure the lines

CFLAGS="$CFLAGS  -fopenmp"
CXXFLAGS="$CXXFLAGS -fopenmp"
LDFLAGS="$LDFLAGS -fopenmp"

are present.

Once that's taken care of, you should be able to compile a mex file that include OpenMP directives without any additional work; for straightforward C code that doesn't use any external libraries

mex your_omp_code.c -I${MATLABPATH}/extern/include

should be sufficient.  As an example, consider the following code, that also makes use of GSL Library.

This code can then be compiled as:

mex omp_reduction_mex.c -I${GSLPATH}/include -I${MATLABPATH}/extern/include -L${GSLPATH}/lib -lgsl

The only subtlety I've found in this is that to set the number of threads, you need to open a shell, run

export OMP_NUM_THREADS=2 (or 4 or 8, etc.)

and then from within the same terminal, launch MATLAB. If you just launch MATLAB, it seems to grab the maximum number of possible threads. On my machine, which is dual core with hyperthreading, it gets 4.

NOTE: I was having some weird lock ups with my code. I found that switching from the llvm compliers with gcc frontend to just the MacPorts installed gcc (I have 4.7.3 on my machine) seemed to clear things up.

No comments:

Post a Comment