Matlab is installed on the HPC clusters for Leeds researchers who have access to either their own or a departmental license.

Please note that the University does not have a site licence so Matlab can only be used by staff in certain faculties. Contact your local IT support for further information.

Contents

## Setting up the license

*Please note that the
$ symbol represents the command prompt. You do not type this in.*

As Matlab is a licensed product, you will first need to contact your Faculty IT support to obtain their licence server details.

The license is validated by Matlab through setting the MLM_LICENSE_FILE or LM_LICENSE_FILE environment variable. To set it in the Bash shell, enter:

1 2 3 |
$ export MLM_LICENSE_FILE=port@machine |

or

1 2 3 |
$ export LM_LICENSE_FILE=port@machine:${LM_LICENSE_FILE} |

To obtain the values of LM_LICENSE_FILE specific to your group/department please contact your local IT support. We are not able to supply this information.

## Setting up the environment

All required environment variables can be set by loading the Matlab module, to do this type the command:

1 2 3 |
$ module add matlab |

## Running on the login nodes

Matlab can be launched by entering its name at the command prompt; i.e.:

1 2 3 |
$ matlab |

Please note that this method should not be used for long computational use.

## Running through the batch scheduler

The batch scheduler allows both interactive and batch jobs to be submitted during which users will have exclusive access to the resources they request.

### Running through an interactive shell

The following will launch Matlab interactively, displaying the full GUI:

1 2 3 |
$ qrsh -cwd -V -l h_rt=hh:mm:ss matlab -desktop -singleCompThread |

In the above command:

hh:mm:ss is the length of real-time the shell will exist for,

-cwd indicates the job will run from the current working directory

-V exports the the current environment.

The paramemter -singleCompThread ensures that Matlab will run only on a single thread, since the default multiple threaded behaviour can cause problems.

e.g. to run Matlab for 6 hours:

1 2 3 |
$ qrsh -cwd -V -l h_rt=6:00:00 matlab -desktop -singleCompThread |

This will run Matlab within the terminal from which it was launched.

### Running the command line version of Matlab

The following will launch an interactive session where the interaction is via a command prompt interface only:

1 2 3 |
$ qrsh -pty y -cwd -V -l h_rt=6:00:00 matlab -nodesktop -singleCompThread |

Where, hh:mm:ss is the length of real-time the shell will exist for.

### Batch Execution

To run matlab in batch-mode you must first generate a list of commands for matlab to process in a file; e.g. file cosplot.m:

*The
% symbol denotes a comment in Matlab scripts*

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
% MATLAB M-file example to approximate a sawtooth % with a truncated Fourier expansion. nterms=5; fourbypi=4.0/pi; np=100; y(1:np)=pi/2.0; x(1:np)=linspace(-2.0*pi,2*pi,np); for k=1:nterms twokm=2*k-1; y=y-fourbypi*cos(twokm*x)/twokm^2; end; plot(x,y); print -deps matlab_test_plot.ps; quit; |

Please note that the last line must be ” quit; “!

A script must then be created that will request resources from the queuing system and launch the matlab executable; script runmatlab.sh:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#!/bin/bash # Run in current working directory #$ -cwd # Export current environment #$ -V # Set a 10 min limit #$ -l h_rt=0:10:00 # Load matlab module module add matlab # Set license export MLM_LICENSE_FILE=port@machine # run matlab using command file # -nodisplay flag should be given to suppress graphics matlab -nodisplay &lt; cosplot.m |

This can be submitted to the batch scheduler system using:

1 2 3 |
$ qsub runmatlab.sh |

The output file matlab_test_plot.ps can be viewed with Ghostscript using the command gs matlab_test_plot.ps.

## Parallel Matlab jobs

On version 2012a of Matlab, parallel pools of threads are initiated using the matlabpool command. In versions 2013a onwards, this was replaced with the parpool command. You may need to adjust code to work properly on the clusters.

Parallel Matlab jobs will work on up to 12 cores (1 thread per cores). This should be requested in the job submission script, so to request 8 cores:

1 |
#$ -l nodes=1,ppn=8 |

The scheduler sets an environment variable $NSLOTS to be equal to the number of cores (so 8 in this case) which the Matlab script will need to read in order to set up a worker pool of the correct size. Lines similar to these should be included in the Matlab script:

1 2 3 4 |
thrds=str2num(getenv('NSLOTS')) matlabpool open thrds |

## Compiling Matlab

To run a large number of matlab jobs simultaneously it may be necessary to use the matlab compiler to create a stand-alone application. This will also avoid using too many license tokens. For a full description of the compiler please look at the in-program help or the Matlab compiler user’s guide.

### Code preperation

* Script *m-files cannot be compiled directly, first they have to be converted to

*m-files. In general, this is a case of wrapping the main section of code within a function. For more details look at converting script m-files section in the user guide.*

**function**### Compiling

The Matlab compiler makes use of the GNU C compilers and as the Intel compiler is loaded by default you shouldswitch to the correct version of GNU compiler via the command:

1 2 3 |
module switch intel gnu |

The compiler can be invoked from within Matlab or directly from the command line via the tool mcc . However, to get the code to compile in single threaded mode the compiler should be invoked directly from the command line. In general this would take the form:

1 2 3 |
mcc -R -singleCompThread -m your_program.m |

It is very important that you include the -R singleCompThread, otherwise matlab will run in multi-threaded mode and cause problems when running through the batch queues.

This compilation will yield two files of interest

- your_program , which is the compiled code
- runyourprogram.sh , which is the wrapper to run your compiled code.

### Running the application

Once the matlab module is loaded, you can run your stand-alone application via the wrapper and specifing the correct path to various Matlab components. This can easily be done via the environment variable $MATLAB_HOME. An example job script for submission to the batch queue would look like:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#!/bin/bash # Run in current working directory #$ -cwd # Export current environment #$ -V # Set a 6 hour limit #$ -l h_rt=6:00:00 # Load matlab module module add matlab # Set license export MLM_LICENSE_FILE=port@machine #set the path to Matlab runtime, via $MATLAB_HOME, run program via wrapper ./run_your_program.sh $MATLAB_HOME |