Matlab

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

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:

or

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:

Launching on the front end

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

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:

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:

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:

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

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:

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:

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:

 

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:

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:

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: