[MAC] How to compile MATLAB MEX with Intel Composer XE Compiler

With new things, the good old working things may break. This was the case for Matlab [WWW] MEX [WWW] compilation after migration from Snow Leopard [WWW] to Lion [WWW]. For those who don’t know, Matlab is an advanced scientific computation environment with many tools available in different domains (image & signal processing and well beyond). Some tasks are quite slow in Matlab, that’s why it is possible to compile a binary using GNU GCC [WWW] which then runs at full speed and returns the result to the environment.

Some time ago I bought Intel Composer XE compiler [WWW] with TBB [WWW] and IPP [WWW] libraries. It is logical that I wanted to use the compiler in Matlab. Unfortunately, even the most recent version of Matlab (2012a as of this writing) cannot detect the compiler under Mac OS X Lion. Matlab’s support confirmed that it is not supported and Google gave no solution to my best search efforts.

Having abandoned quickly the idea of tweaking the mexopts.sh file responsible of setting compiler and its options on each system, I chose a different approach. After all, Matlab will just call the C/C++ compiler with some bunch of options. You could just compile the MEX file [WWW] from console as any other binary, right? What are those options?

Intel’s C++ compiler is typically named “icpc” and can be run from console. Basically all you need is to specify Matlab’s include and library directories (remember the “mex.h” header in each MEX file) + some special options.

I have Matlab 2012a version installed in “/Applications/MATLAB_R2012a.app“.

I have a simple C++ code file doing some very smart computations “match.cpp“.

After digging in the mexopts.sh file to understand what options are passed, I came up with the following command:

icpc match.cpp -I/Applications/MATLAB_R2012a.app/extern/include/ -lmx -lmex -lmat -L/Applications/MATLAB_R2012a.app/bin/maci64 -fno-common -bundle -Wl,-exported_symbols_list,/Applications/MATLAB_R2012a.app/extern/lib/maci64/mexFunction.map -o match.mexmaci64

The command looks long, weird and unfriendly. But just take a look and you will see some important information there.

Notice -I and -L switches that points to the Matlab’s header and library directions. Notice the three special Matlab libraries. Notice a bunch of special switches that enable the output binary to be a real MEX file. Without them the compiler will complain about the missing main() function and some other weird stuff. Finally notice that I had to specify the -o switch which will basically rename the a.out file to a proper file with a proper extension.

Now let’s do two simple checks to see if the binary we got is not a rubbish but a valid Matlab MEX file. We will use the “file” and “otool” utilities. The latter is installed if you have Xcode and command-line developer tools installed.

1) The system recognizes the output file as a MEX file:

SURFmexv2 xeon$ file match.mexmaci64
match.mexmaci64: Mach-O 64-bit bundle x86_64

All is well here.

2) If we check the used libraries (we see 3 Matlab specific libraries and one Intel’s runtime library):

SURFmexv2 xeon$ otool -L match.mexmaci64
match.mexmaci64:
   @rpath/libmx.dylib (compatibility version 0.0.0, current version 0.0.0)
   @rpath/libmex.dylib (compatibility version 0.0.0, current version 0.0.0)
   @rpath/libmat.dylib (compatibility version 0.0.0, current version 0.0.0)
   mac64/libcilkrts.5.dylib (compatibility version 0.0.0, current version 0.0.0)
   /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 52.0.0)
   /usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1094.0.0)
   /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)

All is ok here too. Matlab’s libraries are listed as well as Intel’s runtime libraries + Standard C++ library. You may have more dependent libraries depending on complexity of your code.

I did not try it with more C++ source files. If there are multiple source code files, all you need is to first compile the object files separately and the add the ***.o files in the final compilation and linking stage. This should be tested.

As I am not strong with Make files, a nice make project can be built to compile the MEX files without using the Matlab environment.

About

Vladd... call me Vladd
This entry was posted in Uncategorized and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *