Compiling Matlab .m Files
Norman White, November 2006
Matlab allows users to compile their matlab programs into executable images that can be run anywhere without license restrictions. This is particularly useful in a grid computing environment like Stern's, where a user may want to run many jobs at the same time, but be limited by matlab and toolbox license constraints. Until recently, the matlab compiler was difficult to use, but in a recent release, Matlab 73 (2006b), it has been significantly improved. The information below will show you how to convert your matlab .m files into matlab executables.
1) ssh to rnd.
That is where the matlab compiler lives. Since we have many linux nodes and only one Sun Solaris node, it makes sense to generate an intel linux executable. We only have a single user license, but that should be enough, since you only have to compile once. Note, that every time you compile, matlab will lock the compiler license for 30 minutes. You can continue to use it, but a new user will have to wait for 30 minutes after you have finished.
a) Edit your matlab.m file and turn it into a function. This can be done by putting
statement as the first line.
b) Type (You only need to do this once)
to initialize the matlab compile environment.
c) If you have any matlab.m
files that are called by your main matlab file, make sure they are on the path, or in the directory
you are compiling matlab in (For instance, if you have some matlab files in a directory call mylib, execute the following command before compiling).
Note that matlab is going to compile ALL of the matlab code that is needed to run your program, including matlab toolboxes.
d) Compile the program by typing
mcc -m mymatjob.m
This should grind along for a few minutes and hopefully complete with no errors.
When it is done, there will be an executable file named myfun in your directory. It
can now be run on any node that has access to the matlab libraries. To make sure
your job runs on the appropriate nodes, add a
#$ -l matcomp=1
line to your shell script that will run the program (see below)
2) Create a shell script that will setup the libraries needed to run and will invoke your program.
Create a shell script that looks like this.
# Name: myfun.sh
# Run myfun compiled program
# Set up matlab LD_LIBRARY_PATH
# Then run your program
and then save it as myfun.sh. After saving it, make sure it is readable and executable by others by typing
chmod o+r myfun.sh
3) Create the Sun Grid Engine shell script to run the program. Here is an example:
# Name: Runmyfun.sh
#$ -N Runmyfun.sh
#$ -l matcomp=1
#$ -l h_cpu=10:00:00 (Optional, put your cpu requirements here)
#$ -l h_vmem=1000m (This is optional, put your memory requirements here).
# Now run the program
Save the program as Runmyfun.sh, and then
chmod o+rx Runmyfun.sh
4) Submit the job..
qsub -t 1-300 Runmyfun.sh
This will run the job on as many nodes as are available, but not take up ANY matlab licenses (not matlab or toolboxes). The toolbox .m files will already be compiled into your code. For instance, we only have two optimization toolbox license. So you could only run your code on two systems at a time, instead of 10.
This same code can also be run on almost any grid computing environment, just by moving the executable file and a few other files. Again, no need for a matlab license.