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.

Tuesday, April 16, 2013

Current Gripes on MATLAB, MEX, OpenMP and OS X

I write a lot of C code around which I then bind to MEX so that I can use MATLAB to manage the pre/post processing phases of my simulations. Some of my recent work has a lot of loops for computing averages that are ripe for OpenMP, so it'd be great if I could get OpenMP and MEX to play together nicely. Currently, I'm facing a couple of obstacles, many of which seem to revolve around OS X 10.8. First, the only automated MEX configuration available for OS X 10.8 uses clang, which does not support OpenMP (yet). My next thought was to write pure C, but use MATLAB's .mat interface to handle the pre/post processing script. Unfortunately, to use the libraries needed for that, they need to be set in the DLYD_LIBRARY_PATH, and OS X 10.8 seems to have dyld issues. I know I could always resort to just reading/writing ASCII files, but this all makes me grumpy about OS X as a development environment for scientific computing. I moved to OS X about 10 years ago specifically thinking that it'd give me well maintained OS with the ability to leverage all sorts of *nix software.