Skip to content
Snippets Groups Projects
matlab.md 11.1 KiB
Newer Older
Lukáš Krupčík's avatar
Lukáš Krupčík committed
Matlab
Lukáš Krupčík's avatar
Lukáš Krupčík committed
======

Introduction
------------
Lukáš Krupčík's avatar
Lukáš Krupčík committed
Matlab is available in versions R2015a and R2015b. There are always two variants of the release:
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Lukáš Krupčík's avatar
Lukáš Krupčík committed
-   Non commercial or so called EDU variant, which can be used for common research and educational purposes.
-   Commercial or so called COM variant, which can used also for commercial activities. The licenses for commercial variant are much more expensive, so usually the commercial variant has only subset of features compared to the EDU available.
Lukáš Krupčík's avatar
Lukáš Krupčík committed

To load the latest version of Matlab load the module

Lukáš Krupčík's avatar
Lukáš Krupčík committed
```bash
Lukáš Krupčík's avatar
Lukáš Krupčík committed
    $ module load MATLAB
Lukáš Krupčík's avatar
Lukáš Krupčík committed
```
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Lukáš Krupčík's avatar
Lukáš Krupčík committed
By default the EDU variant is marked as default. If you need other version or variant, load the particular version. To obtain the list of available versions use
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Lukáš Krupčík's avatar
Lukáš Krupčík committed
```bash
Lukáš Krupčík's avatar
Lukáš Krupčík committed
    $ module avail MATLAB
Lukáš Krupčík's avatar
Lukáš Krupčík committed
```
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Lukáš Krupčík's avatar
Lukáš Krupčík committed
If you need to use the Matlab GUI to prepare your Matlab programs, you can use Matlab directly on the login nodes. But for all computations use Matlab on the compute nodes via PBS Pro scheduler.
Lukáš Krupčík's avatar
Lukáš Krupčík committed

If you require the Matlab GUI, please follow the general information about [running graphical applications](../../../get-started-with-it4innovations/accessing-the-clusters/graphical-user-interface/x-window-system/).
Lukáš Krupčík's avatar
Lukáš Krupčík committed

David Hrbáč's avatar
David Hrbáč committed
Matlab GUI is quite slow using the X forwarding built in the PBS (qsub -X), so using X11 display redirection either via SSH or directly by xauth (please see the "GUI Applications on Compute Nodes over VNC" part [here](../../../get-started-with-it4innovations/accessing-the-clusters/graphical-user-interface/x-window-system/)) is recommended.
Lukáš Krupčík's avatar
Lukáš Krupčík committed

To run Matlab with GUI, use

Lukáš Krupčík's avatar
Lukáš Krupčík committed
```bash
Lukáš Krupčík's avatar
Lukáš Krupčík committed
    $ matlab
Lukáš Krupčík's avatar
Lukáš Krupčík committed
```
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Lukáš Krupčík's avatar
Lukáš Krupčík committed
To run Matlab in text mode, without the Matlab Desktop GUI environment, use
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Lukáš Krupčík's avatar
Lukáš Krupčík committed
```bash
Lukáš Krupčík's avatar
Lukáš Krupčík committed
    $ matlab -nodesktop -nosplash
Lukáš Krupčík's avatar
Lukáš Krupčík committed
```
Lukáš Krupčík's avatar
Lukáš Krupčík committed

plots, images, etc... will be still available.

Running parallel Matlab using Distributed Computing Toolbox / Engine
------------------------------------------------------------------------
Distributed toolbox is available only for the EDU variant

David Hrbáč's avatar
David Hrbáč committed
The MPIEXEC mode available in previous versions is no longer available in MATLAB 2015. Also, the programming interface has changed. Refer to [Release Notes](http://www.mathworks.com/help/distcomp/release-notes.html#buanp9e-1).
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Lukáš Krupčík's avatar
Lukáš Krupčík committed
Delete previously used file mpiLibConf.m, we have observed crashes when using Intel MPI.
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Lukáš Krupčík's avatar
Lukáš Krupčík committed
To use Distributed Computing, you first need to setup a parallel profile. We have provided the profile for you, you can either import it in MATLAB command line:
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Lukáš Krupčík's avatar
Lukáš Krupčík committed
```bash
Lukáš Krupčík's avatar
Lukáš Krupčík committed
    > parallel.importProfile('/apps/all/MATLAB/2015b-EDU/SalomonPBSPro.settings')

Lukáš Krupčík's avatar
Lukáš Krupčík committed
    ans =
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Lukáš Krupčík's avatar
Lukáš Krupčík committed
    SalomonPBSPro
```
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Lukáš Krupčík's avatar
Lukáš Krupčík committed
Or in the GUI, go to tab HOME -> Parallel -> Manage Cluster Profiles..., click Import and navigate to :
Lukáš Krupčík's avatar
Lukáš Krupčík committed

/apps/all/MATLAB/2015b-EDU/SalomonPBSPro.settings

Lukáš Krupčík's avatar
Lukáš Krupčík committed
With the new mode, MATLAB itself launches the workers via PBS, so you can either use interactive mode or a batch mode on one node, but the actual parallel processing will be done in a separate job started by MATLAB itself. Alternatively, you can use "local" mode to run parallel code on just a single node.
Lukáš Krupčík's avatar
Lukáš Krupčík committed

### Parallel Matlab interactive session

Following example shows how to start interactive session with support for Matlab GUI. For more information about GUI based applications on Anselm see [this page](../../../get-started-with-it4innovations/accessing-the-clusters/graphical-user-interface/x-window-system/).
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Lukáš Krupčík's avatar
Lukáš Krupčík committed
```bash
Lukáš Krupčík's avatar
Lukáš Krupčík committed
    $ xhost +
Lukáš Krupčík's avatar
Lukáš Krupčík committed
    $ qsub -I -v DISPLAY=$(uname -n):$(echo $DISPLAY | cut -d ':' -f 2) -A NONE-0-0 -q qexp -l select=1 -l walltime=00:30:00
    -l feature__matlab__MATLAB=1
```
Lukáš Krupčík's avatar
Lukáš Krupčík committed

This qsub command example shows how to run Matlab on a single node.

Lukáš Krupčík's avatar
Lukáš Krupčík committed
The second part of the command shows how to request all necessary licenses. In this case 1 Matlab-EDU license and 48 Distributed Computing Engines licenses.
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Lukáš Krupčík's avatar
Lukáš Krupčík committed
Once the access to compute nodes is granted by PBS, user can load following modules and start Matlab:
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Lukáš Krupčík's avatar
Lukáš Krupčík committed
```bash
Lukáš Krupčík's avatar
Lukáš Krupčík committed
    r1i0n17$ module load MATLAB/2015a-EDU
    r1i0n17$ matlab &
Lukáš Krupčík's avatar
Lukáš Krupčík committed
```
Lukáš Krupčík's avatar
Lukáš Krupčík committed

### Parallel Matlab batch job in Local mode

Lukáš Krupčík's avatar
Lukáš Krupčík committed
To run matlab in batch mode, write an matlab script, then write a bash jobscript and execute via the qsub command. By default, matlab will execute one matlab worker instance per allocated core.
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Lukáš Krupčík's avatar
Lukáš Krupčík committed
```bash
Lukáš Krupčík's avatar
Lukáš Krupčík committed
    #!/bin/bash
    #PBS -A PROJECT ID
    #PBS -q qprod
    #PBS -l select=1:ncpus=24:mpiprocs=24:ompthreads=1

    # change to shared scratch directory
    SCR=/scratch/work/user/$USER/$PBS_JOBID
    mkdir -p $SCR ; cd $SCR || exit

Lukáš Krupčík's avatar
Lukáš Krupčík committed
    # copy input file to scratch
Lukáš Krupčík's avatar
Lukáš Krupčík committed
    cp $PBS_O_WORKDIR/matlabcode.m .

    # load modules
    module load MATLAB/2015a-EDU

    # execute the calculation
    matlab -nodisplay -r matlabcode > output.out

    # copy output file to home
    cp output.out $PBS_O_WORKDIR/.
Lukáš Krupčík's avatar
Lukáš Krupčík committed
```
Lukáš Krupčík's avatar
Lukáš Krupčík committed

David Hrbáč's avatar
David Hrbáč committed
This script may be submitted directly to the PBS workload manager via the qsub command.  The inputs and matlab script are in matlabcode.m file, outputs in output.out file. Note the missing .m extension in the matlab -r matlabcodefile call, **the .m must not be included**.  Note that the **shared /scratch must be used**. Further, it is **important to include quit** statement at the end of the matlabcode.m script.
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Submit the jobscript using qsub

Lukáš Krupčík's avatar
Lukáš Krupčík committed
```bash
Lukáš Krupčík's avatar
Lukáš Krupčík committed
    $ qsub ./jobscript
Lukáš Krupčík's avatar
Lukáš Krupčík committed
```
Lukáš Krupčík's avatar
Lukáš Krupčík committed

### Parallel Matlab Local mode program example

Lukáš Krupčík's avatar
Lukáš Krupčík committed
The last part of the configuration is done directly in the user Matlab script before Distributed Computing Toolbox is started.
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Lukáš Krupčík's avatar
Lukáš Krupčík committed
```bash
Lukáš Krupčík's avatar
Lukáš Krupčík committed
    cluster = parcluster('local')
Lukáš Krupčík's avatar
Lukáš Krupčík committed
```
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Lukáš Krupčík's avatar
Lukáš Krupčík committed
This script creates scheduler object "cluster" of type "local" that starts workers locally.
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Lukáš Krupčík's avatar
Lukáš Krupčík committed
Please note: Every Matlab script that needs to initialize/use matlabpool has to contain these three lines prior to calling parpool(sched, ...) function.
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Lukáš Krupčík's avatar
Lukáš Krupčík committed
The last step is to start matlabpool with "cluster" object and correct number of workers. We have 24 cores per node, so we start 24 workers.
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Lukáš Krupčík's avatar
Lukáš Krupčík committed
```bash
Lukáš Krupčík's avatar
Lukáš Krupčík committed
    parpool(cluster,24);
Lukáš Krupčík's avatar
Lukáš Krupčík committed


Lukáš Krupčík's avatar
Lukáš Krupčík committed
    ... parallel code ...
Lukáš Krupčík's avatar
Lukáš Krupčík committed


Lukáš Krupčík's avatar
Lukáš Krupčík committed
    parpool close
Lukáš Krupčík's avatar
Lukáš Krupčík committed
```
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Lukáš Krupčík's avatar
Lukáš Krupčík committed
The complete example showing how to use Distributed Computing Toolbox in local mode is shown here.
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Lukáš Krupčík's avatar
Lukáš Krupčík committed
```bash
Lukáš Krupčík's avatar
Lukáš Krupčík committed
    cluster = parcluster('local');
    cluster

    parpool(cluster,24);

    n=2000;

    W = rand(n,n);
    W = distributed(W);
    x = (1:n)';
    x = distributed(x);
    spmd
    [~, name] = system('hostname')
Lukáš Krupčík's avatar
Lukáš Krupčík committed

David Hrbáč's avatar
David Hrbáč committed
        T = W*x; % Calculation performed on labs, in parallel.
                 % T and W are both codistributed arrays here.
Lukáš Krupčík's avatar
Lukáš Krupčík committed
    end
    T;
David Hrbáč's avatar
David Hrbáč committed
    whos         % T and W are both distributed arrays here.
Lukáš Krupčík's avatar
Lukáš Krupčík committed

    parpool close
    quit
Lukáš Krupčík's avatar
Lukáš Krupčík committed
```
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Lukáš Krupčík's avatar
Lukáš Krupčík committed
You can copy and paste the example in a .m file and execute. Note that the parpool size should correspond to **total number of cores** available on allocated nodes.
Lukáš Krupčík's avatar
Lukáš Krupčík committed

### Parallel Matlab Batch job using PBS mode (workers spawned in a separate job)

Lukáš Krupčík's avatar
Lukáš Krupčík committed
This mode uses PBS scheduler to launch the parallel pool. It uses the SalomonPBSPro profile that needs to be imported to Cluster Manager, as mentioned before. This methodod uses MATLAB's PBS Scheduler interface - it spawns the workers in a separate job submitted by MATLAB using qsub.
Lukáš Krupčík's avatar
Lukáš Krupčík committed

This is an example of m-script using PBS mode:

Lukáš Krupčík's avatar
Lukáš Krupčík committed
```bash
Lukáš Krupčík's avatar
Lukáš Krupčík committed
    cluster = parcluster('SalomonPBSPro');
    set(cluster, 'SubmitArguments', '-A OPEN-0-0');
    set(cluster, 'ResourceTemplate', '-q qprod -l select=10:ncpus=24');
    set(cluster, 'NumWorkers', 240);

    pool = parpool(cluster,240);

    n=2000;

    W = rand(n,n);
    W = distributed(W);
    x = (1:n)';
    x = distributed(x);
    spmd
    [~, name] = system('hostname')

        T = W*x; % Calculation performed on labs, in parallel.
                 % T and W are both codistributed arrays here.
    end
    whos         % T and W are both distributed arrays here.

    % shut down parallel pool
    delete(pool)
Lukáš Krupčík's avatar
Lukáš Krupčík committed
```
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Lukáš Krupčík's avatar
Lukáš Krupčík committed
Note that we first construct a cluster object using the imported profile, then set some important options, namely : SubmitArguments, where you need to specify accounting id, and ResourceTemplate, where you need to specify number of nodes to run the job.
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Lukáš Krupčík's avatar
Lukáš Krupčík committed
You can start this script using batch mode the same way as in Local mode example.
Lukáš Krupčík's avatar
Lukáš Krupčík committed

### Parallel Matlab Batch with direct launch (workers spawned within the existing job)

Lukáš Krupčík's avatar
Lukáš Krupčík committed
This method is a "hack" invented by us to emulate the mpiexec functionality found in previous MATLAB versions. We leverage the MATLAB Generic Scheduler interface, but instead of submitting the workers to PBS, we launch the workers directly within the running job, thus we avoid the issues with master script and workers running in separate jobs (issues with license not available, waiting for the worker's job to spawn etc.)
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Please note that this method is experimental.

David Hrbáč's avatar
David Hrbáč committed
For this method, you need to use SalomonDirect profile, import it using [the same way as SalomonPBSPro](matlab.md#running-parallel-matlab-using-distributed-computing-toolbox---engine)
Lukáš Krupčík's avatar
Lukáš Krupčík committed

This is an example of m-script using direct mode:

Lukáš Krupčík's avatar
Lukáš Krupčík committed
```bash
Lukáš Krupčík's avatar
Lukáš Krupčík committed
    parallel.importProfile('/apps/all/MATLAB/2015b-EDU/SalomonDirect.settings')
    cluster = parcluster('SalomonDirect');
    set(cluster, 'NumWorkers', 48);

    pool = parpool(cluster, 48);

    n=2000;

    W = rand(n,n);
    W = distributed(W);
    x = (1:n)';
    x = distributed(x);
    spmd
    [~, name] = system('hostname')

        T = W*x; % Calculation performed on labs, in parallel.
                 % T and W are both codistributed arrays here.
    end
    whos         % T and W are both distributed arrays here.

    % shut down parallel pool
    delete(pool)
Lukáš Krupčík's avatar
Lukáš Krupčík committed
```
Lukáš Krupčík's avatar
Lukáš Krupčík committed

### Non-interactive Session and Licenses

If you want to run batch jobs with Matlab, be sure to request appropriate license features with the PBS Pro scheduler, at least the " -l __feature__matlab__MATLAB=1" for EDU variant of Matlab. More information about how to check the license features states and how to request them with PBS Pro, please [look here](../../../anselm-cluster-documentation/software/isv_licenses/).
Lukáš Krupčík's avatar
Lukáš Krupčík committed

The licensing feature of PBS is currently disabled.

In case of non-interactive session please read the [following information](../../../anselm-cluster-documentation/software/isv_licenses/) on how to modify the qsub command to test for available licenses prior getting the resource allocation.
Lukáš Krupčík's avatar
Lukáš Krupčík committed

### Matlab Distributed Computing Engines start up time

Lukáš Krupčík's avatar
Lukáš Krupčík committed
Starting Matlab workers is an expensive process that requires certain amount of time. For your information please see the following table:
Lukáš Krupčík's avatar
Lukáš Krupčík committed

  |compute nodes|number of workers|start-up time[s]|
  |---|---|---|
  |16|384|831|
  |8|192|807|
  |4|96|483|
  |2|48|16|

Lukáš Krupčík's avatar
Lukáš Krupčík committed
MATLAB on UV2000
Lukáš Krupčík's avatar
Lukáš Krupčík committed
-----------------
Lukáš Krupčík's avatar
Lukáš Krupčík committed
UV2000 machine available in queue "qfat" can be used for MATLAB computations. This is a SMP NUMA machine with large amount of RAM, which can be beneficial for certain types of MATLAB jobs. CPU cores are allocated in chunks of 8 for this machine.
Lukáš Krupčík's avatar
Lukáš Krupčík committed

Lukáš Krupčík's avatar
Lukáš Krupčík committed
You can use MATLAB on UV2000 in two parallel modes:
Lukáš Krupčík's avatar
Lukáš Krupčík committed

### Threaded mode

David Hrbáč's avatar
David Hrbáč committed
Since this is a SMP machine, you can completely avoid using Parallel Toolbox and use only MATLAB's threading. MATLAB will automatically detect the number of cores you have allocated and will set  maxNumCompThreads accordingly and certain operations, such as  fft, , eig, svd, etc. will be automatically run in threads. The advantage of this mode is that you don't need to modify your existing sequential codes.
Lukáš Krupčík's avatar
Lukáš Krupčík committed

### Local cluster mode

You can also use Parallel Toolbox on UV2000. Use l[ocal cluster mode](matlab/#parallel-matlab-batch-job-in-local-mode), "SalomonPBSPro" profile will not work.