MATLAB¶
MATLAB is a numerical computing environment and fourth generation programming language.
Policy¶
MATLAB is available to users at HPC2N, including all official toolboxes.
Note
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.
Citations
See How to cite MATLAB for how to include acknowledgement of the use of this program into scientific papers.
Overview¶
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.
Usage at HPC2N¶
On HPC2N we have MATLAB available as a module.
Loading¶
To use the MATLAB, add it to your environment. You can find versions with
The MATLAB module can be loaded directly (it does not have any prerequisites).
Running¶
Toolboxes
- Currently, all official toolboxes are available. You can always get a list of toolboxes with the command
ver
inside MATLAB.
ThinLinc
We recommended using MATLAB along with the ThinLinc desktop since this enables the graphical user interface (GUI) familiar to many of our users.
This guide will be with that goal in mind. For terminal use, batch jobs from terminal, etc. see the guide MATLAB from the shell and other ways.
Setup guide - GUI¶
First time installing, accessing and configuring of the ThinLinc client and MATLAB application
- Download and install ThinLinc. Login, as described on that page.
- Open a terminal in the ThinLinc desktop (“Applications” → “System Tools” → “MATE Terminal”).
- Follow instructions in Configuration and setup for MATLAB. The command
configCLuster.sh
is only run once per version of MATLAB.
Using MATLAB and the ThinLinc client for submitting jobs to the cluster¶
Note
This assumes that you have connected to the cluster with the ThinLinc client!
- 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:
- 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 in the GUI.
Example
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 in the GUI.
Warning
Any longer, resource-intensive, or parallel jobs must be run through a batch script.
Note
MATLAB is well integrated with SLURM and because of that there are several ways to run these jobs:
- Using the job scheduler (batch command) in MATLAB Desktop/graphical interface (This is the Recommended Use and what we talk about here).
- Starting a parpool with a predefined cluster (This allows for more interactivity).
- Writing a batch script as for any other software and submitting the job with the sbatch command from SLURM (This could be useful if you want to run long jobs and you don’t need to modify the code in the meantime). This is only recommended for more advanced users. You can read something about that here under MATLAB from the shell and other ways.
- 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:
Example, MATLAB script we will use
Example, running the above MATLAB script
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
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:
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:
For full documentation about running parallel jobs in MATLAB please read Mathworks Parallel Computing Toolbox documentation.
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.
Matlab Third-party tools/add-ons/APIs¶
Additional info¶
More information can be found on
- The official MATLAB website
- Parallel Computing Toolbox Overview
- Parallel Computing Toolbox Documentation
- Quick Start Parallel Computing in MATLAB
- Interactively Run Loops in Parallel Using parfor
- Parallel for-Loops (parfor) — Examples
- GPU Computing
- Run MATLAB Functions on a GPU
- GPU Computing in MATLAB — Examples