Improving MATLAB

MATLAB is quite a package. Among the many things it is, MATLAB is essentially a wrapper around BLAS routines and LAPACK. However, MATLAB does not necessarily ship with a BLAS library which is optimized for your particular OS and, more importantly, your particular hardware configuration.

The existence of ATLAS probably proves the point. While MATLAB comes shipped with a BLAS library, usually MKL or ACML, depending on the type of processor it's installed on (Intel or ADM, respectively), those libraries may not be optimized for your particular setup.

If you're feeling courageous, compiling an ATLAS-optimized BLAS and LAPACK library may be one way to go, but I'm going to use Goto BLAS as the example.

Once we have a better BLAS library than what is shipped by MATLAB, we can trick MATLAB in to using this BLAS library.

For the record, these were the steps I followed to obtain a faster BLAS for Mac OSX 10.7, Lion. I believe the same instructions apply to Windows and Linux versions of MATLAB, but with the appropriate grep/sed/awk commands applied to Mac specific paths and library extensions. My MATLAB version is 7.7 (R2008b).

Goto BLAS

Since Goto BLAS is technically not maintained anymore, I have been using SurviveGotoBLAS2, which has updated Makefiles for various architectures (especially for Mac OSX 10.7, Lion).

After I downloaded and unzipped the archive, I ran

make BINARY=32 CC="gcc-4.2 -arch i386" NO_LAPACK=1 NO_CBLAS=1

This will generate a 32-bit binary for MATLAB. Unless you're using one of the newer versions of MATLAB, it will expect 32-bit binaries. Making 64-bit binaries only requires changing the flags BINARY=64 CC=“gcc-4.2 -arch x86_64”. Note that the CC options are for compiling on Mac OSX 10.7.

If you have a Windows machine or a Linux machine, I'm sure the options look different; maybe the default make will just run fine. If that's the case, congratulations, you are one of the lucky few.

Other notes

I found that evoking gcc from the command line in OSX Lion uses the LLVM compiler. This causes problem when trying to compile the dylib.

You may find it useful to also compile LAPACK, though that requires a Fortran compiler, which is a whole other set of problems on the Mac.

I had the gfortran compiler downloaded from GNU. This installation does the silly thing of installing your libraries under /usr/local/gfortran, so the Goto BLAS compilation may throw an error when you compile LAPACK, complaining that it can't find libgfortran. To remedy this, add

-L/usr/local/gfortran/lib

to EXTRALIB in exports/Makefile under the Darwin section.

Souping up MATLAB

Once Goto BLAS has compiled (you'll have a file that looks something like libgoto2_penrynp-r3.14.dylib), copy it to the appropriate MATLAB application folder. For me, it looked like this:

sudo cp lib2goto2_penrynp-r3.14.dylib /Applications/MATLAB_R2008b.app/bin/maci/

Next, use your favorite editor and open up /Applications/MATLAB_R2008b.app/bin/maci/blas.spec. You'll find a line (or several) that look like

GenuineIntel Family  * Model  * mkl.dylib mklcompat.dylib #Intel Mac

These lines tell MATLAB which BLAS library to use. Simply change the filenames for your corresponding architecture to point to your Goto BLAS, and now you're all done!

A cleaner alternative

Alternatively, if you would rather not muck with the MATLAB application folder, you can export the environment variable BLAS_VERSION.

export BLAS_VERSION=/path/to/lib2goto2_penrynp-r3.14.dylib

Note that you must do this before running MATLAB (via command line). Consider putting it in your .bashrc or .profile file.

Results

Your mileage may vary depending on your version of MATLAB. I think later versions have better MKL libraries.

(Coming soon… or when I get around to doing a real analysis….)

Comments and suggestions

I made this page because I spent way too much time trying to compile Goto BLAS on Lion. If you manage to accomplish the same thing on a different OS or a different version of MATLAB or if you have any suggestions about this page (did it work?), feel free to let me know, and I'll report it here.

Thanks to Stephen Becker for catching some errors and for simplifying the process!