Child pages
  • Matlab
Skip to end of metadata
Go to start of metadata

Using Matlab on the MCC



Categories for this softwareMath Libraries
What is MatlabMatlab is a general purpose analysis and graphics plotter from Mathworks. With more than 600 mathematical, statistical, and engineering functions, MATLAB provides immediate access to high-performance numerical computing. This functionality is extended with interactive graphical capabilities for creating plots, images, surfaces, and volumetric representations.

A toolbox algorithms enhance MATLAB's functionality in domains such as signal and image processing, data analysis and statistics, mathematical modeling, and control design. Toolboxes are collections of algorithms, written by experts in their fields, that provide application-specific numerical, analysis, and graphical capabilities.

How to useMonash has a site license for Matlab. In addition, there are no licensing restraints on code compiled using the Matlab compiler

Matlab compiler

The MATLAB Compiler (mcc) can be used to translate M-files into C files. The resultant C files can be used in any of the supported executable types including MEX, executable or library by generating an appropriate wrapper file. A wrapper file contains the required interface between the Compiler-generated code and a supported executable type.  There are three main reasons for compiling M files:

  • To create stand-alone applications.
  • To hide proprietary algorithms.
  • To speed up the code.

Refer to the documentation for full details on using the compiler but the following gives the procedure for a simple example.

Simple example

Please download the example src here matlab-example.tar.gz

To extract, type the following

Extract tar
tar -xf matlab-example.tar.gz

Here is the MCC job script. Note the many SGE options that have been commented out.
#$ -S /bin/sh

#####$ -M
##    notify me about this job
#####$ -m abe
##         (a)bort (b)egin or (e)nd

### This line below is for you to give this calculation a descriptive name
#$ -N taskname

#$ -j y
##    combine output and error to one output file

#$ -cwd
##    use the current working directory to generate files

## how much time in hhh:mm:ss [ upper bound ]
#$ -l h_rt=40:00:00

## how much memory to request [ upper bound memory usage for this calculation ]
#$ -l h_vmem=3G

### You are using version R2011a of MATLAB
### This has to be identical to the version used during compilation (msgln4)
### If you used:
###     module load matlab-compiler/R2011a
### then, you have to use R2011a
###     module load matlab/R2011a

module load matlab/R2013a


### on this next line is where you actually run your program

### echo is used to provide stdin to the program, since this program reads from stdin
### most programs don't need this...

echo 10 | time ./  $MATLABROOT

### Here are some explanations:
### notice that when you compiled you used:
###     mcc -mv name.m
### then this produces as output the file:
### to run this use the line above:
###     ./ $MATLABROOT

To compile on the headnode.

How to compile using Matlab compiler
 mcc -mv cantileverLinear.m
 Compiler version: 4.18.1 (R2013a)
Processing /opt/sw/matlabR2013a/toolbox/matlab/mcc.enc
Processing /opt/sw/matlabR2013a/toolbox/shared/spcuilib/mcc.enc
Processing include files...
2 item(s) added.
Processing directories installed with MCR...
The file mccExcludedFiles.log contains a list of functions excluded from the CTF archive.
0 item(s) added.
Generating MATLAB path for the compiled application...
Created 41 path items.
Parsing file "/nfs/home/hpcmerc/smichnow/MATLAB-Example/cantileverLinear.m"
	(Referenced from: "Compiler Command Line").
Parsing file "/opt/sw/matlabR2013a/toolbox/compiler/deploy/deployprint.m"
	(Referenced from: "Compiler Command Line").
Parsing file "/opt/sw/matlabR2013a/toolbox/compiler/deploy/printdlg.m"
	(Referenced from: "Compiler Command Line").
Deleting 0 temporary MEX authorization files.
Generating file "/nfs/home/hpcmerc/smichnow/MATLAB-Example/readme.txt".
Generating file "".

To run

To run
> qsub
Your job 4062388 ("taskname") has been submitted

The answer is printed to stdout, so will appear in the job file when finished.

Parametric Jobs

It is common for people to run a large number of Matlab programs by using the same executable with a range of input parameters.  This can be achieved by using a number of tools.

Here is an example of running Matlab with a SGE Array Job.

First you must rewrite your Matlab program so that it executes from a single function call. This is equivalent to a 'main' function in C or C++. Consider the following Matlab code in a file called print_input.m:

Simple Matlab Code
function print_input( a  )
fprintf(1,'Input parameter is  %s\n',a)

We then build the executable as described above.

Build Command
module load matlab
mcc -v -m  print_input.m 

We can run the code to test it. Note that most of the output is from the shell script, and not the executable.

Example output
Setting up environment variables
LD_LIBRARY_PATH is .:/usr/local/sw/matlabR2011a/runtime/glnxa64:/usr/local/sw/matlabR2011a/bin/glnxa64:/usr/local/sw/matlabR2011a/sys/os/glnxa64:/usr/local/sw/matlabR2011a/sys/java/jre/glnxa64/jre/lib/amd64/native_threads:/usr/local/sw/matlabR2011a/sys/java/jre/glnxa64/jre/lib/amd64/server:/usr/local/sw/matlabR2011a/sys/java/jre/glnxa64/jre/lib/amd64/client:/usr/local/sw/matlabR2011a/sys/java/jre/glnxa64/jre/lib/amd64
Input parameter is  99

Now we create a shell script for the SGE Array Job.  Note that we use the -t parameter to define a range of values. Here we use steps of five.

Array Job SGE Script ''
#$ -S /bin/sh
#$ -pe smp 1
#$ -j y
#$ -cwd
#$ -t 1-10:5
module load matlab/R2013a
module list
ulimit -s 40000

Submit the job.

$ qsub
Your job-array 4096166.1-10:5 ("") has been submitted

Note that we get two jobs running, with two output files called and  The contents of each are:

Output Of
Currently Loaded Modulefiles:
  1) matlab/R2013a
Setting up environment variables
LD_LIBRARY_PATH is .:/opt/sw/matlabR2013a/runtime/glnxa64:/opt/sw/matlabR2013a/bin/glnxa64:/opt/sw/matlabR2013a/sys/os/glnxa64:/opt/sw/matlabR2013a/sys
Warning: No display specified.  You will not be able to display graphics on the screen.
Input parameter is  1

Output of
Currently Loaded Modulefiles:
  1) matlab/R2013a
Setting up environment variables
LD_LIBRARY_PATH is .:/opt/sw/matlabR2013a/runtime/glnxa64:/opt/sw/matlabR2013a/bin/glnxa64:/opt/sw/matlabR2013a/sys/os/glnxa64:/opt/sw/matlabR2013a/sys
Warning: No display specified.  You will not be able to display graphics on the screen.
Input parameter is  6


Common Problems


Matlab crashes before running

With this problem, the program runs fine on a headnode or compute node, but Matlab crashes when running inside the queue environment. The crash occurs when loading Matlab, and before it starts running your code. You may see error messages in stdout that advise you to contact support.

The problem is that Matlab is running out of stack memory on the compute node. The solution is simple - simply increase it by putting this code in your shell script before the Matlab executable is called.

Increase stack size in the Unix shell
ulimit -s 60000 #increase stack size in the shell
echo 10 | time ./  $MATLABROOT

This increases the stack size to 60,000 kBytes. This figure can be changed (up or down) depending upon your application

Missing Function

You have compiled your Matlab executable but when you run it, you get an error like

Error using myfunct

Undefined function 'newfunction' for input arguments of type 'double'.

The Matlab compiler mcc automatically tracks dependencies, but is not perfect. The solution is to include the 2nd Matlab file (e.g 'newfunction.m') in the mcc command line.

Include another Matlab file when compiling
 mcc -mv cantileverLinear.m newfunction.m
A warning message "which: no ifconfig in <path>" appears when compiling Matlab scripts.

This is a harmless warning for an earlier version of Matlab.  The files should compile and the execute without any other issues.  To remove this warning you can either:

  • Use a more recent version of Matlab
  • Put the path to ifconfig in your PATH variable. i.e. add /sbin to your PATH variable. e.g.


Example (for Bash Shell)
export PATH=$PATH:/sbin
mcc -mv MY_SCRIPT.m



  • No labels