Skip to content

Matlab

Using Matlab

Note: This Matlab version 2017 only works in a Singularity Centos 6 container. It does not work with Centos 8.

Matlab version 2017b is available. It’s located under /shared/opt/. As this HPC uses a batch job submission system to schedule jobs you cannot use Matlabs graphical user interface – you need to run it as a script under the PBS Pro job scheduling system. See Running a Simple HPC Job. If intensive Matlab sessions on the login node adversly affect other users on the login node those Matlab sessions will be killed.

In your PBS submission script load the matlab-2017b module. See the examples below. The matlab module just adds the Matlab bin directory to your PATH.

Examples

There are some trivial Matlab examples with PBS job submission scripts in /shared/eresearch/pbs_job_examples/matlab/. Copy that directory to your own directory and try them out.

$ cp -r /shared/eresearch/pbs_job_examples/matlab .

Starting Matlab with no GUI

Your Matlab script will be started from a PBS Pro job submission script (see Running Single Core Jobs & Running Multi Core Jobs below) on a remote node so you will need to specify that no GUI is used. To do this you should start Matlab in your PBS scripts using matlab -nodisplay -nosplash.

-nodisplay Do not display any X commands. The MATLAB desktop will not be started.
-nosplash Do not display the splash screen during startup.
-nodesktop Do not start the MATLAB desktop. Use the current window for commands.
-nojvm Do not start the Java Virtual Machine.

Matlab also uses the Java Virtual Machine (JVM) to run the desktop and to display graphics. The -nojvm option enables you to start Matlab without the JVM. Using this option minimizes memory usage and improves initial startup speed. However some functionality requires the JVM. The JVM is needed by the Parallel Computing Toolbox, the plot() function and others, so if you intend to use parfor or spmd then you will still need the JVM, i.e. don’t use -nojvm.

Running Single Core Jobs

If you know that your Matlab script will only be using a single core then in your PBS submission script use #PBS -l ncpus=1 and the Matlab command line option -singleCompThread.

#!/bin/bash

#PBS -N TestMatlab
#PBS -l ncpus=1
#PBS -l mem=5GB
#PBS -l walltime=00:05:00

module load matlab-2017b
cd ${PBS_O_WORKDIR}

# Using standard options and additionally -singleCompThread 
OPTS="-nodisplay -nosplash -nojvm"
matlab $OPTS -singleCompThread < my_script.m

The option “-singleCompThread” will limit MATLAB to a single computational thread.

Running Multi Core Jobs

In your Matlab script use parpool() to specify the Matlab workers and request the same number of cores from PBS using PBS -l ncpus. An example for 4 cores follows:

In your Matlab script we use parpool (4):

% Matlab script
p = parpool (4)
parfor n = 1:1000
    % Your Matlab loop code here. 
end
delete(p)

If parpool(n) is not explicitly executed beforehand, the parfor will automatically launch as many workers as possible; the maximum number of physical cores on the node.

Now in your PBS submission script use #PBS -l ncpus=4:

#!/bin/bash

#PBS -N TestMatlab
#PBS -l ncpus=4
#PBS -l mem=5GB
#PBS -l walltime=00:05:00

module load matlab-2017b
cd ${PBS_O_WORKDIR}

OPTS='-nodisplay -nosplash' 
matlab $OPTS < my_script.m

Note: using maxNumCompThreads(num) in your Matlab script where num is the number of threads, e.g. maxNumCompThreads(8) seems to be ignored by Matlab.