Matlab In Batch

From CAC Documentation wiki
Jump to navigation Jump to search

The full MATLAB application is installed on the login nodes, but as you know, that's not where we do production computing at CAC. Instead, you will want to run your MATLAB script on one or more batch nodes, using only the MATLAB runtimes that are installed there. The outline of how you go about this is as follows.

1) Create a clean directory containing only a copy of the .m file you want to run in batch. Say the directory is "mydir" and your file is "mypi.m". Important: make sure the last command in your .m file is "exit".

2) In either the MATLAB user interface or in an OS shell, cd to this new directory (e.g., "cd mydir") and issue the command

mcc -m mypi

This will translate your .m file into the C language and compile it using a C compiler, so that you end up with an executable named mypi in Linux or mypi.exe in Windows. During the process it should produce scads of other files. In Linux, these will include an archive called mypi.ctf and a shell script called You will also find a readme.txt with lots and lots of helpful information—really too much, more than you want to know—which you can use as a reference.

3) Let's say your .m file contains merely a simple test program that runs in a second or two. In that case, try running the new executable now; in Windows, do this by typing the name of the file in a cmd shell, e.g., "mypi". In Linux, use the following command from any shell:

./ /opt/matlab-2010a/MATLAB_Compiler_Runtime/v713

The strange-looking argument tells the script where to find the required "runtimes" directory. Arguments to your program, if any, can follow it. If you're using MATLAB R2007b rather than R2010a, the correct command on CAC's Linux systems is

./ /opt/matlab-7.5/MATLAB_Component_Runtime/v77

Again, the first argument tells the script where to find certain necessary files. (This is explained in depth in readme.txt. You can also examine the script itself to see what it does.) Take a moment to verify that the test output is correct. Note, the output that normally goes to the MATLAB console is printed instead to stdout.

4) Finally, submit a batch job to the scheduler, in which the batch script contains exactly the command above. Submit the job from the same "mydir" directory where the mypi executable and the rest of the auxiliary files are located.

Tips - Here a few tips to know when creating a MATLAB code that is suitable for mcc:

  • If your .m file is a function that takes one or more arguments, put these arguments on the command line after the path to the runtimes. But remember that the arguments will be input as strings, just like any arguments (argv) to a C program. Therefore your .m file should use str2num or str2double to convert any input arguments to the expected type.
  • Dump figures into files using "saveas" or "print"; or, write a separate postprocessing script that reads data files written by multiple tasks and produces the final plots.
  • Don't nest curly braces within comment blocks, e.g., %{ comment1 %{ comment2 %} %} - the braces may be misinterpreted as clause delimiters in C, due to an mcc bug.
  • Avoid commands from the Symbolic Math Toolbox, e.g., heaviside(); they may not be recognized.

It is possible to get much more elaborate with MATLAB's mcc system by, for example, using "mcc -l" to create a function library, or perhaps by implementing parallelism (not exactly sure how?), but the above should be enough to get you started. For more information, see the MATLAB help as well as "mcc -?".