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

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 `port@machine`

specific to your group/department please contact your local IT support.

## Setting up the environment

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

1 2 3 |
$ module add matlab |

## Launching on the front end

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.

## Using Sun Grid Engine

Sun Grid engine 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`

indicated the current working directory and `-V`

exports the the current environment. The switch `-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

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=<hh:mm:ss> 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 denoted 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 6 hour limit #$ -l h_rt=6:00: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 > cosplot.m |

This can be submitted to the queuing system using:

` $ qsub runmatlab.sh`

## Parallel Matlab jobs

On version 2012a of Matlab (which is the latest version we currently have installed), parallel pools of threads are initiated using the `matlabpool`

command. In version 2013a onwards, this was replaced with the `parpool`

command. You may need to adjust code to work properly on ARC2.

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 licenses. 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 function 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.

### Compiling

The matlab compiler makes use of the GNU C compilers, and need to use version 4.2.3. As the Intel compiler is loaded by default you can switch to the correct version of GNU compiler via the command:

1 2 3 |
module switch intel gnu/4.2.3 |

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`run`

, which is the wrapper to run your compiled code.*your*program.sh

### 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 |