High-Performance Computing at the NIH
GitHub YouTube @nih_hpc RSS Feed
MATLAB on NIH HPC Systems

Description

MATLAB integrates mathematical computing and data visualization in a powerful language to provide a flexible environment for technical computing. The open architecture makes it easy to use MATLAB and its companion products to explore data and create custom tools. MATLAB users benefit from a growing community, pre-written toolboxes containing popular algorithms and analyses, and an integrated development environment (IDE) for writing and debugging code.

Web sites

IMPORTANT: (May 2017) third-party toolboxes not automatically loaded by default
Starting with our installation of R2017a, third party MATLAB toolboxes are no longer loaded by default. You must either load their individual modules before starting MATLAB, or use the nih_matmod command from within the MATLAB environment. A matlab.all module is provided to load all toolboxes for backward compatibility with existing workflows that depend on all installed toolboxes being available.

IMPORTANT: (May 2016) Only compiled Matlab code or interactive Matlab jobs allowed on cluster
MATLAB licenses are now only allocated to interactive programming sessions. MATLAB code must be compiled before it can be submitted to the batch system. It is no longer necessary to allocate licenses with the --licenses or -L options. For details, see this announcement.

MATLAB on Helix
back to top

This documentation for Helix also largely applies to interactive sessions on Biowulf.

Licenses are checked out automatically when you use a MATLAB or its toolboxes. Licenses are returned when you exit your session, or when it times out after 4 hours of inactivity.

Before starting MATLAB, you may wish to check if licenses are available. To do so, check the current MATLAB License status or enter licenses in the shell like so:

[user@helix ~]$ licenses

                              -max available- 

                                Helix,Felix
License                 Total    & Sciware   
--------------------  ---------  --------- 
              MATLAB         24          6 
            SIMULINK          1          1 
      Bioinformatics          3          2 
       Curve Fitting          3          1 
 Global Optimization          1          1 
    Image Processing          6          4 
    Builder for Java          1          1 
            Compiler          2          2 
      Neural Network          2          2 
        Optimization          4          3 
     Dist. Computing          1          1 
     Part. Diff. Eq.          2          2 
   Signal Processing          6          4 
          Simbiology          1          1 
           Statistic         10          6 
       Symbolic Math          2          2 
       System Ident.          2          2 
             Wavelet          2          1 
                 IDL         20          0 

More info at https://hpc.nih.gov/docs/licenses

When idle for 4 hours, you will see the following message displayed in the Command Window:

"Your MATLAB session has timed out. All license keys have been returned."

To resume your MATLAB session, just start using MATLAB again. If a license is available, MATLAB checks it out automatically. Likewise, as you use the toolboxes and other products that you had checked out, MATLAB obtains license keys for these products as well. If a license key is not available, MATLAB periodically attempts to check out a license, issuing warning messages. After 10 warnings, if a license key is still not available, MATLAB will save the workspace and exit.

As with other applications on the HPC systems, MATLAB is managed using environment modules. To see which versions of MATLAB and third-party toolboxes are available, type:

[user@helix ~]$ module avail matlab

------------------------------/usr/local/lmod/modulefiles ------------------------------
   matlab/2012b    matlab/2015b             matlab-Fiji/1.51n      (D)    matlab-caffe/1.0.0-rc3     matlab-pronto/2.0
   matlab/2013a    matlab/2016a             matlab-MBEToolbox/3.0         matlab-conn/17a            matlab-spm/12
   matlab/2013b    matlab/2016b             matlab-MIToolbox/3.0.2        matlab-eeglab/13.4.4b      matlab-wtsimf/1.4
   matlab/2014b    matlab/2017a      (D)    matlab-PGEToolbox/3.1         matlab-kilosort/8738ef7    matlab-xjview/8.14
   matlab/2015a    matlab-Fiji/1.50a        matlab-alexnet/1.0            matlab-npy/524bd14         matlab.all/2017a


  Where:
   (D):  Default Module

Use "module spider" to find all possible modules.
Use "module keyword key1 key2 ..." to search for all possible modules matching any
of the "keys".

To select a module, type:

[user@helix ~]$ module load matlab/[ver]

where [ver] is an optional version specification. Without [ver] the default MATLAB version is loaded.

Please note that, for 2017a and later, the complete set of third-party toolboxes will not be automatically loaded unless you choose the matlab.all module or load them individually.

To load a third-party toolbox, like SPM, for use with MATLAB >= 2017a, do:
[user@helix ~]$ module load matlab-spm

If you have already begun a MATLAB session, there is no need to restart. The loading can be accomplished using the nih_matmod function:

>> nih_matmod avail

   Fiji/1.50a         alexnet/1.0        npy/524bdl4
   Fiji/1.51n     (D) caffe/1.0.0-rc3    pronto/2.0
   MBEToolbox/3.0     conn/17a           spm/12
   MIToolbox/3.0.2    eeglab/13.4.4b     wtsimf/1.4
   PGEToolbox/3.1     kilosort/8738ef7   xjview/8.14
   

Use "module spider" to find all possible modules.
Use "module keyword key1 key2 ..." to search for all possible modules matching
any of the "keys".

>> nih_matmod load spm

Use help nih_matmod from within MATLAB for complete usage information.


Interactive sessions

To run the MATLAB integrated development environment (IDE also known as the Java virtual machine), an X Windows connection is required. NoMachine's NX is the X-Windows client currently recommended by staff for Windows and Mac. When used to start a GNOME desktop session, NX is optimized to provide a fast responsive MATLAB environment.

HINT: Debugging X-Windows clients
If you have trouble starting the MATLAB IDE, ensure that your X-Windows client is working properly by typing 'xclock' in the shell. This shouldn't be an issue with a GUI based client like NX.

After connecting to Helix, load the MATLAB module. (When using an interactive desktop through NX click the Applications menu and select System Tools, Terminal.)

[user@helix ~]$ module load matlab
[user@helix ~]$ matlab&

Including & allows you to continue using the terminal while the MATLAB application is running. You should now see a MATLAB splash screen followed by the IDE:

MATLAB splash image

MATLAB desktop image

HINT: Failure to Launch
If you receive an error message that contains "Failure to Launch Desktop Class", cd to $HOME/.matlab, remove the directory R2015b (or appropriate version) and try again.

Interactive shell sessions

It is also possible to run MATLAB interactively on the command-line, without the IDE. This would be useful if you do not wish to use X-Windows.

[user@helix ~]$ module load matlab [user@helix ~]$ matlab -nodisplay < M A T L A B > Copyright 1984-2012 The MathWorks, Inc. R2012b (8.0.0.783) 64-bit (glnxa64) August 22, 2012 To get started, type one of these: helpwin, helpdesk, or demo. For product information, visit www.mathworks.com. >> quit [user@helix ~]$

Interactive sessions on Biowulf
back to top

Because MATLAB is not permitted on the Biowulf login node, users must request resources on a compute node.

First, establish an ssh or X-Windows connection to the Biowulf login node either directly from your computer or indirectly through Helix. When running an X-Windows session on Biowulf through Helix, remember to use the -X or -Y option with your ssh command.

Then use the sinteractive command to request resources on a compute node:

[user@biowulf ~]$ sinteractive salloc.exe: Pending job allocation 15323416 salloc.exe: job 15323416 queued and waiting for resources salloc.exe: job 15323416 has been allocated resources salloc.exe: Granted job allocation 15323416 salloc.exe: Waiting for resource configuration salloc.exe: Nodes cn1640 are ready for job [user@cn1640 ~]$

By default, the sinteractive command only allocates a few cpus and a small amount of memory. You can request more cpus and memory with the --cpus-per-task=ncpus and mem=GBg options respectively. See sinteractive -h for a complete list of options.

After completing these steps interactive MATLAB sessions on Biowulf are initiated in the same way as interactive sessions and interactive shell sessions on Helix.

MATLAB in shell scripts
back to top

It is not possible to submit jobs containing un-compiled MATLAB code to the batch system. However, it is still possible to write and execute shell scripts containing un-compiled MATLAB code from within an interactive session. This method might be useful for running a few instances of MATLAB on several cpus of a single node, and it will only require one MATLAB license. But for long-running jobs or jobs requiring more than a few instances of MATLAB, you should compile your code instead.

Here, a basic calculation (the pythagorean theorem) is run using multiple approaches.


MATLAB commands and variables hardcoded into shell script

The simplest (but perhaps least useful) way to run a MATLAB job in the background would be to hardcode variables and MATLAB commands directly into a script. Even though this method is not very practical it serves as a useful example to MATLAB users unfamiliar with bash scripting:

#!/bin/bash
# this file is called hyp1.sh
 
module load matlab
matlab -nojvm<<-EOF
    a = 3;                                    
    b = 4;
    H = sqrt(a^2 + b^2)
    exit
EOF

You could run this job in the background of an interactive session like so:

[user@cn0001 ~]$ ./hyp1.sh > hyp1_output 2>&1 &

HINT: Permission denied error
If you receive an error such as this:
bash: ./hyp1.sh: Permission denied
Use the chmod command to make the file executable, like so:
[user@cn0001]$ chmod 755 hyp1.sh

In this case, the output will be redirected to hyp1_output. The computation will proceed in the background allowing the you to run another session of MATLAB interactively or run other MATLAB programs in the background using the same license. If you use this strategy to run a few instances of MATLAB in parallel you must be careful not to overload your allocated CPUs.

HINT:`EOF' warning
If a message similar to the following is found in your output file, make sure to remove any trailing whitespace from both of the EOF lines.
/var/spool/slurm/slurmd/job48226/slurm_script: line 10: warning: here-document at line 5 delimited by end-of-file (wanted `EOF')

Passing MATLAB variables to a shell script

You can set up a batch script that contains MATLAB commands and pass variables to the job at the time of submission. This might be useful for very simple MATLAB analyses. For example:

#!/bin/bash
# this file is called hyp2.sh
 
a=$1
b=$2
echo "a is $a, b is $b";

module load matlab
matlab -nojvm<<-EOF
    H = sqrt($a^2 + $b^2)
    exit
EOF

In this script the values of a and b are expected to be passed from the command line. This is accomplished like so:

[user@cn0001 ~]$ ./hyp2.sh 22 5 > hyp2_output 2>&1 &

Similar to the previous example, the output is directed to hyp2_output.

MATLAB functions in shell scripts

Of course it is possible to write your own functions and call them in a shell script using the same syntax as above. Let's assume you write a function and save it in hyp.m

function H = hyp(a,b)
% return the hypotenuse (H)
% from the legs (a and b)

% convert chars to doubles
% (this is necessary when
% code is compiled)
if ischar(a), a = str2double(a); end
if ischar(b), b = str2double(b); end

H = sqrt(a^2 + b^2)

To run the code in the background, you could write a script like so:

#!/bin/bash
# this file is called hyp3.sh
 
a=$1
b=$2

module load matlab
matlab -nojvm<<-EOF
    cd /full/path/to/hyp.m
    hyp($a,$b);
    exit
EOF

Similar to the last example you would run this code in the backround with:

[user@cn0001 ~]$ ./hyp3.sh 8 4 > hyp3_output 2>&1 &

This method allows you to develop analyses of any complexity in MATLAB (rather than hardcoding commands into shell scripts) and run them in the background of an interactive session.

One common approach is to write a MATLAB function that accepts a file name as input, loads the file (containing all variables and data), performs some analysis, and then saves the analyzed data to a new .mat file. This minimizes the complexity of the input that must be supplied through a shell script. In this case, the _output file will only be used for diagnosing a debugging problems, because the output will be saved to a new .mat file specified by the user in the function.

Submitting jobs to Slurm
back to top

Long running jobs, or jobs that are "embarrassingly parallel" should be compiled and submitted to the batch system. Compiling your code removes the need for MATLAB and toolbox licenses.

The MATLAB compiler generates a shell script called run_X.sh (where the file containing the MATLAB source code is named X.m). This script is used to run the compiled code. It requires at least one argument specifying the location of the MATLAB Component Runtime (MCR) and subsequent arguments are treated as input to the compiled MATLAB function. See the MATLAB compiler for details.

Compile hyp.m from the example above on Helix or a Biowulf compute node like so:

[user@helix ~]$ module load matlab/2015b
[user@helix ~]$ mcc2 -m hyp.m

You can now execute hyp.m by calling run_hyp.sh from the shell without using any MATLAB licenses like so:

[user@helix ~]$ ./run_hyp.sh /usr/local/matlab-compiler/v90 3 4
------------------------------------------
Setting up environment variables
---
LD_LIBRARY_PATH is .:/usr/local/matlab-compiler/v90//runtime/glnxa64:/usr/local/matlab-compiler/v90//bin/glnxa64:/usr/local/matlab-compiler/v90/sys/os/glnxa64:/usr/local/matlab-compiler/v90/sys/opengl/lib/glnxa64

H =

     5

The first argument gives the location of the MCR appropriate for code compiled in MATLAB version 2015b. The next two arguments are the inputs to hyp.m.

To submit the compiled code as a batch job, you could write another script:

#!/bin/bash
# this file is called hyp4.sh
#
#SBATCH --job-name=hypotenuse4
#SBATCH --mail-type=BEGIN,END
 
/full/pathname/run_hyp.sh /usr/local/matlab-compiler/v90 $a $b

Note the optional inclusion of #SBATCH options that set the job name and email alerts. You would then submit the job like so:

[user@biowulf ~]$ sbatch --export=a=3,b=4 /full/pathname/hyp4.sh

Note that the values of a and b are passed at the time of job submission using the --export option with sbatch. In this example, the standard output and standard error from the job are automatically directed to a file called slurm-#.out where # is the assigned jobid.

It might be cumbersome to write a shell script to call another shell script that will invoke the compiled executable. In this instance, the sbatch --wrap option comes in handy. It allows you to pass sbatch a command instead of a filename. In this way, you could submit run_hyp.sh as a batch job directly without the need for an intermediary script.

[user@biowulf ~]$ sbatch --wrap=\"/full/pathname/run_hyp.sh /usr/local/matlab-compiler/v90 3 4\"

Note the double quotes escaped with backslashes that enclose the command. This is necessary because the command contains white space.

For more information on using the sbatch command for SLURM, visit the Job Submission section of the Biowulf User Guide.

Swarm of jobs on Biowulf
back to top

The swarm program is a convenient way to submit large numbers of jobs. With swarm you can run over 1,000 instances of your compiled MATLAB code distributed across the cluster. You create a swarm command file containing a single line for each independent job. The swarm program will then package up the commands into batch jobs and submit them to Slurm for you.

Only compiled MATLAB jobs can be run using swarm. To run a swarm based on the example above, create a swarm command file named hyp.swarm with each line containing a single command:

/full/pathname/run_hyp.sh /usr/local/matlab-compiler/v90 3 4
/full/pathname/run_hyp.sh /usr/local/matlab-compiler/v90 5 6
/full/pathname/run_hyp.sh /usr/local/matlab-compiler/v90 7 8
/full/pathname/run_hyp.sh /usr/local/matlab-compiler/v90 9 10
/full/pathname/run_hyp.sh /usr/local/matlab-compiler/v90 11 12

Submit this file to the batch system with the command:

[user@biowulf ~]$ swarm -f hyp.swarm

For more detailed info about running swarms of MATLAB jobs on Biowulf, including common pitfalls to avoid and strategies for automation, see this recorded training session.

Parallel Computing Toolbox
back to top

The MATLAB Parallel Computing Toolbox enables you to develop distributed and parallel MATLAB applications and execute them using multiple cores in a single node or to utilize the graphical processing units (GPUs) of a properly equipped machine.

Please NOTE:
1. In it's current configuration, the Parallel Computing Toolbox does not scale beyond a single node. This will allow your job to run up to 16 times faster on the norm partition. This may be sufficient for many jobs. But this toolbox will not allow you to run jobs on multiple nodes. To run jobs of larger scale, use sbatch or swarm.

2. Only one Parallel Computing Toolbox has been purchased for users to share. As a workaround you could compile your parallel code as described above and submit it as a batch job or even in a swarm.

3. Within MATLAB and in online documentation this toolbox is referred to as the Parallel Computing Toolbox. Within the licensing software it is referred to as the Distributed Processing Toolbox. There is a related Mathworks product that is not currently installed on our systems that extends the functionality of the Parallel Computing Toolbox called the Distributed Computing Server. This has been the source of confusion.

See MATLAB Parallel Computing Toolbox for examples on using the toolbox on biowulf.

Current versions
back to top

----------------------------------------------------------------------------------------------------
MATLAB Version: 9.1.0.441655 (R2016b)
MATLAB License Number: 35
Operating System: Linux 2.6.32-642.11.1.el6.x86_64 #1 SMP Fri Nov 18 19:25:05 UTC 2016 x86_64
Java Version: Java 1.7.0_60-b19 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
----------------------------------------------------------------------------------------------------
MATLAB                                                Version 9.1         (R2016b)
Simulink                                              Version 8.8         (R2016b)
Bioinformatics Toolbox                                Version 4.7         (R2016b)
Curve Fitting Toolbox                                 Version 3.5.4       (R2016b)
Global Optimization Toolbox                           Version 3.4.1       (R2016b)
Image Processing Toolbox                              Version 9.5         (R2016b)
MATLAB Compiler                                       Version 6.3         (R2016b)
MATLAB Compiler SDK                                   Version 6.3         (R2016b)
Neural Network Toolbox                                Version 9.1         (R2016b)
Optimization Toolbox                                  Version 7.5         (R2016b)
Parallel Computing Toolbox                            Version 6.9         (R2016b)
Signal Processing Toolbox                             Version 7.3         (R2016b)
Statistical Parametric Mapping                        Version 6906        (SPM12) 
Statistics and Machine Learning Toolbox               Version 11.0        (R2016b)
Symbolic Math Toolbox                                 Version 7.1         (R2016b)
System Identification Toolbox                         Version 9.5         (R2016b)
Wavelet Toolbox                                       Version 4.17        (R2016b)

In addition, these 3rd party toolboxes are installed in some versions of MATLAB:

  • MBE/PGE
  • EEGLAB
  • Psychtoolbox
  • SPM
  • MIToolbox

  • The SPM12 Toolbox also has the following toolboxes installed:

  • PRoNTo
  • GIFT/EEGIFT
  • MarsBar
  • VBM8
  • TOM8
  • xjView