MATLAB

Software name: 
MATLAB
Policy 

MATLAB is available to all users at HPC2N.

General 

MATLAB is a numerical computing environment and fourth generation programming language.

Description 

MATLAB is a numerical computing environment and fourth generation programming language. Developed by The MathWorks, MATLAB allows matrix manipulation, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs in other languages. Although it is numeric only, an optional toolbox uses the MuPAD symbolic engine, allowing access to computer algebra capabilities.

Availability 

MATLAB is available on all our systems.

Licenses

Umeå University have signed a “Third party access rider to the MathWorks, Inc. Software license agreement”. The rider allows Third Parties to use all licensed programs, provided such access and use is solely for the purpose of academic course work and teaching, noncommercial academic research, and personal use which is not for any commercial or other organizational use.

If you work at a non academic organization or need toolboxes not included in the Umeå University license but have your own license, please contact support@hpc2n.umu.se and we will help you find out if you can use MATLAB at HPC2N using that license.

MATLAB Parallel Server is available to all users. 

Available toolboxes

Available toolboxes may shift from time to time. To get a list of all currently available toolboxes use the 'ver' command from within MATLAB. 

Usage at HPC2N 

We recommended using MATLAB along with the ThinLinc desktop since this enables the graphical interface familiar to many of our users.

This first instruction guides to towards that goal, for the other alternatives see the end of this page.

Using the MATLAB Desktop/graphical interface. (Recommended Use)

First time installing, accessing and configuring of the ThinLinc client and MATLAB application

  1. Follow "Download and Install instructions" and "Login to cluster login node" in the Running ThinLinc Guide
  2. Open a terminal in the ThinLinc desktop (“Applications” → “System Tools” → “MATE Terminal”)
  3. Follow instructions in Configuration and setup for MATLAB, "Configure 2019b and later".
    This needs to be done once for each cluster and version of MATLAB.

Using MATLAB and the ThinLinc client for submitting jobs to the cluster

This assumes that you have connected to the cluster with the ThinLinc client, see Running ThinLinc Guide section "Login to cluster login node"

Start MATLAB graphical interface using one of the following methods:

  • (Recommended) Using the menus:  "Application" → "HPC2N Applications" → "Applications" → "Matlab ...." 
    The "Matlab ...." will indicate the version being loaded.

  • Manually loading and starting a specific version of MATLAB from those installed at HPC2N from an open Thinlinc linux terminal:

Check for available versions
module spider matlab
# Select required version
# Since different versions of MATLAB differ in user configuration it is important to specify which version to use
module load MATLAB/2019b
matlab -singleCompThread

Notes when manually starting MATLAB from linux terminal

  • For details about available versions and information about the Module system used at HPC2N see the page about modules

  • As interactive use of MATLAB is usually done on shared login-nodes, excessive use of MATLAB will prevent other users from using the resources. By default MATLAB use as many threads (cores) it possibly can, therefore

    • On the login-nodes MATLAB MUST be started with the option '-singleCompThread', preventing MATLAB from using more than one thread.

    • This will NOT prevent MATLAB from using the MATLAB Parallel Server with which any number of cores can be used for computations

You can either use MATLAB for "Serial batch jobs" or "Parallel batch jobs", see the appropriate section below for details.

Serial batch jobs

The instructions in this chapter is entered in the MATLAB command window.

To run serial MATLAB jobs on the cluster you first needs to define a cluster object and then submit it using the batch command:

% Get a handle to the cluster
% See the page for configuring and setup of MATLAB for details
c=parcluster('kebnekaise')
% myfcn is a command or serial MATLAB program.
% N is the number of output arguments from the evaluated function
% x1, x2, x3,... are the input arguments
j = c.batch(@myfcn, N, {x1,x2,x3,...})

To query the state of the submitted job use:

% Query the state of the job
j.State
% Wait for the job to finish (blocking though so you can not use MATLAB for anything else)
j.wait

After the job has finished you can fetch the output using:

% If the state of the job is finished, fetch the result
j.fetchOutputs{:}
% when you do not need the result anymore, delete the job
j.delete

If you are running a lot of jobs or if you want to quit MATLAB and restart it at a later time you can retrive the list of jobs:

% Create a handle to the cluster
% See the page for configuring and setup of MATLAB for details
c=parcluster('kebnekaise')
% Get the list of jobs
jobs = c.Jobs
% Retrive the output of the second job
j2=jobs(2)
output = j2.fetchOutputs{:}

Note: If calling batch from a script, use load instead of fetchOutputs.

Parallel batch jobs

The instructions in this chapter is entered in the MATLAB command window.

Running parallel batch jobs are quite similiar to running serial jobs, we just need to specify a MATLAB Pool to use and of course MATLAB code that are parallized. This is easiest illustrated with an example:

Notes:

  • Running parallel jobs in MATLAB always requires N+1 CPUs as one worker is required to keep track of the batch job and the pool of workers. I.e if you want to keep your job on one node, you should only use number of cores per node minus one.
  • Increasing the number of workers does not always mean that your job will run faster. Overhead increases and will make the total computation time longer, when using many workers.
function t = parallel_example(iter) 
t0 = tic; 

parfor idx = 1:iter 
    A(idx) = idx; 
    pause(2) 
end 

t = toc(t0);

We will run the example on 4 cores:

% Get a handle to the cluster
% See the page for configuring and setup of MATLAB 2018b for details
c=parcluster('kebnekaise')
% Run the jobs on 4 workers
j = c.batch(@parallel_example, 1, {16}, 'pool', 4)
% Wait till the job has finished. Use j.State if you just want to poll the
% status and be able to do other things while waiting for the job to finish.
j.wait
% Fetch the result after the job has finished
j.fetchOutputs{:}
ans =
   16.9154

 

Another way to get the result from a job at a later time is to keep track of the job ID:

% Get a handle to the cluster
% See the page for configuring and setup of MATLAB 2018b for details
c=parcluster('kebnekaise');
% Run the jobs on 4 workers
j = c.batch(@parallel_example, 1, {16}, 'pool', 4) ;
% get the jobid
id = j.ID

id =
    26
% Clear the job variable. Same as we quit MATLAB
clear j;

Later in another MATLAB session:

% Get a handle to the cluster
% See the page for configuring and setup of MATLAB 2018b for details
c=parcluster('kebnekaise');
% Find the job ID we wrote down
j=c.findJob('ID', 26);
j.State

ans =
finished

j.fetchOutputs{:}
ans =
    4.8630

If you are running MATLAB Desktop, you can use the Job Monitor (Parallel -> Monitor Jobs) to view the current state of your jobs. 

Debugging

Sometimes the jobs produce errors, the errors can be retrived with:

j.Parent.getDebugLog(j)

For full documentation about running parallel jobs in MATLAB please read Mathworks Parallel Computing Toolbox documentation.

Alternative ways to access MATLAB on the cluster

These methods has historical reasons and for some experts or cookbook users they might be needed as a reference, but our recommendation is the first method described at the top of this document.

Using MATLAB from a linux terminal calling it from a sbatch submit script

Login to the login node with your HPC2N username and password

ssh username@kebnekaise.hpc2n.umu.se

Follow the instructions below Using MATLAB in batch-scripts

Using MATLAB Desktop/graphical interface BUT with X11 forwarding enabled instead of the ThinLinc client

ssh -Y username@kebnekaise.hpc2n.umu.se

The method has security implications for your client and should only be used by those familiar and well-versed with the issues and risks.'

You need to load and start MATLAB interface manually, see "Login node load and start MATLAB manually from a linux terminal"

Using MATLAB Desktop/graphical interface connecting from Windows (See Connecting from Windows)

See Connecting from Windows

Login node; load and start MATLAB manually from a linux terminal

You can only use the instructions below you have been referred here from a section earlier in this document.

# Check for available versions
module spider matlab
# Select required version
# Since different versions of MATLAB differ in user configuration it is important to specify which version to use
module load MATLAB/2019b
matlab -singleCompThread

Notes:

  1. For details about available versions and information about the Module system used at HPC2N see the page about modules
  2. As interactive use of MATLAB is usually done on shared login-nodes, excessive use of MATLAB will prevent other users from using the resources. By default MATLAB use as many threads (cores) it possibly can.
    On the login-nodes MATLAB MUST be started with the option '-singleCompThread', preventing MATLAB from using more than one thread..
    This will NOT prevent MATLAB from using the MATLAB Parallel Server with which any number of cores can be used for computations.
  3. If running MATLAB in text-mode, add '-nodesktop' when starting MATLAB.

Using MATLAB in batch scripts

MATLAB can also be used in batch scripts, though this is not anything we recommend anymore.

The submit-file below runs a serial MATLAB job:

#!/bin/bash
# Change to your actual local/SNIC/NAISS id project number
#SBATCH -A hpc2nXXXX-YYY
# The project id can look like SNICXXX-YY-ZZ, NAISSXXXX-YY-ZZ, or hpc2nXXXX-YYY
# Asking for 1 core
#SBATCH -n 1
#SBATCH -t 00:30:00
#SBATCH --error=matlab_%J.err
#SBATCH --output=matlab_%J.out

# Clean the environment from previously loaded modules
module purge > /dev/null 2>&1

# May need to be changed, depending on resource and MATLAB version to be used
# to find out available versions: module spider matlab
module add MATLAB/2019b.Update2

# Executing the matlab program monte_carlo_pi.m for the value n=100000 
# (n is number of steps - see program). 
# The command 'time' is timing the execution 
time matlab -nojvm -nodisplay -r "monte_carlo_pi(100000)"

The submit file and the MATLAB code is available for download: monte_carlo.sbatch, monte_carlo_pi.m

Submit with

sbatch monte_carlo.sbatch
Updated: 2024-03-21, 12:31