Friday, December 14, 2007

Mex files with TBB (Multicore Matlab)


I finally make my Matlab run a mex file with TBB. 
Since I dind't see this in google, and involves a little hack to Matlab script, I start my blog with this entry.
The instructions are for leopard, but I think should be straight forward for linux (I don't know how to do it in Windows) .
First, you need to install intel TBB (or any other library you want) and set the environment variables. This most be done in any shell you start, so the easiest way to do it is add a few lines to your .profile
 
MacBook:Soka~$ cat .profile
PS1='MacBook:Soka\w\$ ';
source /Library/Frameworks/TBB.framework/Versions/2.0/bin/tbbvars.sh 
source /opt/intel/cc/10.1.006/bin/iccvars.sh 
source /Library/Frameworks/Intel_MKL.framework/Versions/10.0.1.014/tools/environment/mklvars32.sh 

The source command runs the scripts to set the environment variables so the program you compile can find the libraries it needs.
So, the idea its to tell Matlab to set this variables before start. You can do it by open a X11 terminal, set the variables and then run the Matlab script. As I am a lazy person, I want Matlab do this for me.
I grep for DYLD in Matlab directory and found that the only file that appears was bin/matlab. I opened this file with my favorite fast text editor, joe, and add the mentioned source commands at the beginning of the script. 
Incredibly, I started Matlab from Spotlight, and all was just fine. So I compile my helloworld.cpp (that has included intel TBB for doing a stupid for) just like this in Matlab prompt:

>> mex helloworld.cpp -ltbb

and the I am able to run my parallel-multicore mexfile.
Ok, it's no so nice, because if there is some kind of error in your mexfile, Matlab will die silently without any warning or error message.
More the next. 
  
 

No comments: