University of Calgary

Using MATLAB on Terminus

Submitted by phillips on Sat, 2009-07-04 06:40.
 

MATLAB 3D graphics example

Introduction

MATLAB is a mathematical computing and programmng environment distributed by The MathWorks.  Its strong points include ease of use, matrix manipulation, and variety of visualization functions.  The core commands can be extended through use of "toolboxes" covering computational areas such as image processing, optimization, neural networks, statistics and wavelets. UCIT has recently purchased a license for the Parallel Computing Toolbox.

The MATLAB executable is a link from /usr/apps/bin/matlab .  As of 2011-08-30, this points to the R2011a version of MATLAB, installed as /usr/apps/matlab/r2011a/bin/matlab.

Issues with MATLAB on Terminus

A number of problems that arose with earlier versions of  MATLAB on Terminus seem to have been resolved with the installation of release R2011a.  A few issues that remain are mentioned here.

Errors when using the MATLAB compiler (mcc)

When using the MATLAB compiler (mcc) to create a standalone executable from a number of function m-files, there are a couple of error messages that may be encountered.  For example:

mcc -v -m main.m mrank.m

may give a warning:

sh: ifconfig: command not found

The ifconfig error can be avoided by adding /sbin to your command PATH.

You may also receive a warning error about the gcc compiler version being older than certified for use with MATLAB.  However, in our testing, the default gcc, version 4.1.2, has been used successfully in spite of the warning message.

Do not attempt to download and install another version of gcc.  You can use the module command to load a newer version of gcc that is already on the system (but is not the default version).  For example:

module load gcc/4.4

That makes version 4.4 of gcc available.  This is newer than the compiler that MATLAB recommends, but, it also worked in our testing.  If you encounter any problems you think are related to compiler version, please write to support [at] hpc [dot] ucalgary [dot] ca.

Running MATLAB in batch jobs

Although MATLAB may be run on the Terminus login nodes for development and debugging, production MATLAB work must be submitted through the SLURM batch job handling system.  See the Terminus Running Jobs page for general information about the SLURM sbatch command for submitting jobs.

As an example, suppose a file, test_bench.m, contains a list of MATLAB commands, ending with a quit command:

bench(10)
ver
quit

A batch job script to run the test_bench.m commands is:

#!/bin/bash

BASE_MFILE_NAME=test_bench
MATLAB_MFILE=${BASE_MFILE_NAME}.m

unset DISPLAY

echo "Starting at `date`"
echo "Running $MATLAB_MFILE on `hostname`"
/usr/apps/bin/matlab -nodesktop -nosplash \
-nodisplay -r ${BASE_MFILE_NAME}
echo "Finished at `date`"

Note that for the -r command-line option to work, the name of the MATLAB m-file must be specified without the .m suffix.

If the script file above script is is in a file named test_bench.slurm, it can be submitted for execution with

sbatch test_bench.slurm

An alternative approach creates a MATLAB command file as part of the batch job script itself, using the SLURM_JOBID number as part of the file name.  This allows multiple jobs to be submitted from the same directory and makes it easy to associate the output (which SLURM labels with the SLURM_JOBID, by default) with the corresponding input file.  Here is a job script to illustrate:

#!/bin/bash

BASE_MFILE_NAME=test_bench_${SLURM_JOBID}
MATLAB_MFILE=${BASE_MFILE_NAME}.m

cat > $MATLAB_MFILE <<EOF
bench(10)
ver
quit
EOF

unset DISPLAY

echo "Starting at `date`"
echo "Running $MATLAB_MFILE on `hostname`"
/usr/apps/bin/matlab -nodesktop -nosplash \
-nodisplay -r ${BASE_MFILE_NAME}
echo "Finished at `date`"

Using mcc, the MATLAB compiler

Standalone applications can be created from function m-files with the MATLAB compiler, mcc. The main advantage of using this approach is that it avoids the requirement for MATLAB licenses when the program is run.  This may allow large numbers of simultaneous jobs to be run without depleting the license pool. A MATLAB compiler license is used only during the compilation itself.

For example, given a file, main.m, containing:

function main
r = mrank(5)

where mrank is defined in a file, mrank.m, containing:

function r = mrank(n)
  r = zeros(n,1);
  for k = 1:n
   r(k) = rank(magic(k));
  end

then, a standalone executable can be built using:

mcc -v -m -R -singleCompThread main.m rank.m

Note the use of the -R -singleCompThread argument to mcc. That requests that the compiled application run using only one computational thread.  Without that option, MATLAB uses all the CPUs of the compute node on which it is run, which can interfere with other users' jobs on Terminus.

The resulting program, main, can be run using the shell script run_main.sh that is produced by the MATLAB compiler.  The location of the MATLAB Compiler Runtime (MCR) files must be specified as an argument to the run_main.sh script. For version R2011a of the compiler, the appropriate MCR files are in /usr/apps/matlab/mcr/v715.  A batch job script on Terminus to run a standalone MATLAB application could look like:

#!/bin/bash

echo "Starting at `date`"
echo "Running standalone application main on `hostname`"
run_main.sh /usr/apps/matlab/mcr/v715
echo "Finished at `date`"

Standalone applications and the corresponding run scripts can be copied to compatible WestGrid systems for execution there.  For example, Breezy is a WestGrid system that supports jobs requiring up to 250 GB of RAM.

See the MATLAB page on the WestGrid web site for more information.

Running MATLAB in parallel

University of Calgary IT has licensed the MATLAB Parallel Computing Toolkit for experimentation.  This allows up to 8 MATLAB worker processes to run in parallel to try to speed up an individual computation.  However, since Terminus nodes only have 4 cores, you should not attempt to run more than four workers on a single node.  Another University of Calgary IT machine, Bigbox, which has 16 processors (and 128 GB of RAM) can be used by approved U of C researchers for large memory MATLAB jobs, using all 8 worker processes (assuming that the licenses are available at the time).

If you have questions or comments about MATLAB, the use of Terminus or access to Bigbox, please contact support [at] hpc [dot] ucalgary [dot] ca .


Updated 2011-08-30.

 


Please send corrections or suggestions about the hpc.ucalgary.ca site to support@hpc.ucalgary.ca.