# Difference between revisions of "MATLAB Guide"

m (→Using MATLAB on the the cluster: 12 is wrong, most of our nodes have 24 cores now) |
|||

(3 intermediate revisions by 2 users not shown) | |||

Line 71: | Line 71: | ||

===Single Core job === | ===Single Core job === | ||

− | To submit a job which uses a single core | + | To submit a job which uses a single core to run a matlab file '''TestCluster.m'''. Create a script called matlabSP.sh: |

#!/bin/bash | #!/bin/bash | ||

#SBATCH --ntasks=1 | #SBATCH --ntasks=1 | ||

Line 77: | Line 77: | ||

#SBATCH --error=outputfile.txt | #SBATCH --error=outputfile.txt | ||

− | matlab -singleCompThread -r TestCluster\;exit\; | + | module load matlab |

+ | matlab -singleCompThread -r '''TestCluster'''\;exit\; | ||

'''NOTE:''' any ";" or " ' " after "-r" should be pre-appended by "\". | '''NOTE:''' any ";" or " ' " after "-r" should be pre-appended by "\". | ||

Line 94: | Line 95: | ||

#SBATCH --error=outputfile.txt | #SBATCH --error=outputfile.txt | ||

+ | module load matlab | ||

matlab -r TestCluster\;exit\; | matlab -r TestCluster\;exit\; | ||

'''NOTE:''' any ";" or " ' " after "-r" should be pre-appended by "\". | '''NOTE:''' any ";" or " ' " after "-r" should be pre-appended by "\". | ||

Line 137: | Line 139: | ||

To run an interactive Matlab session with GUI, you should "ssh -X" to the the cluster from a Linux machine or iOS matchine with X11 enabled. Or please follow the [[X | X Apps]]. Then, run the above commands. | To run an interactive Matlab session with GUI, you should "ssh -X" to the the cluster from a Linux machine or iOS matchine with X11 enabled. Or please follow the [[X | X Apps]]. Then, run the above commands. | ||

+ | |||

+ | == Extra Notes == | ||

+ | |||

+ | When opening a parallel pool, you may receive a warning. This warning is fine to ignore, as this warning is currently a bug in the older versions of Matlab and is fixed on newer versions of Matlab. | ||

+ | |||

+ | The parallel pools help set the number of workers from the Slurm allocation, so that Matlab spawns them in the local Matlab profile as well. | ||

+ | |||

+ | To make sure Matlab runs only one instance per the job submission, the matlab -r '''matlabfilename'''\;exit\; command is used in the slurm script | ||

+ | |||

+ | The parallel pool on the cluster gives errors currently if parpool() or matlabpool is used, this can be fixed by running the Matlab 2017b module. | ||

+ | |||

+ | The pool can be called with: delete(gcp('nocreate')) "this removes previous parallel interactive sessions" | ||

+ | p=gcp "this assigns the gcp or current parallel pool instead of using p=parpool()" | ||

+ | |||

+ | |||

[[Category:Software]] | [[Category:Software]] |

## Latest revision as of 11:06, 23 January 2021

## Contents

# MATLAB licensing

The HPC cluster uses the university site license for MATLAB, which includes an unlimited number of seats and all toolboxes. More information is available here: http://software.uconn.edu/matlab/.

# Using MATLAB on the the cluster

All jobs on the the cluster must be submitted through the SLURM scheduler using `sbatch`

. Please read the SLURM Guide for more details. The preferred way to run MATLAB jobs is using a .m file. Please note that if your job uses many cores or a lot of memory it is better to submit to reserve a whole compute node (24 cores) in exclusive mode

## Loading MATLAB module

To load the MATLAB module:

$ module load matlab/2016a

You can view a list of all available MATLAB versions with:

$ module available matlab

## Submitting .m files with slurm

### Single Core job

To submit a job which uses a single core to run a matlab file **TestCluster.m**. Create a script called matlabSP.sh:

#!/bin/bash #SBATCH --ntasks=1 #SBATCH --output=outputfile.txt #SBATCH --error=outputfile.txt module load matlab matlab -singleCompThread -rTestCluster\;exit\;

**NOTE:** any ";" or " ' " after "-r" should be pre-appended by "\".

Then submit the script:

sbatch matlabSP.sh

### Multithread job

To submit a job which uses 24 computational threads on one node, create a submission script matlabMP.sh:

#!/bin/bash #SBATCH --nodes=1 #SBATCH --ntasks=24 #SBATCH --exclusive #SBATCH --output=outputfile.txt #SBATCH --error=outputfile.txt module load matlab matlab -r TestCluster\;exit\;

**NOTE:** any ";" or " ' " after "-r" should be pre-appended by "\".

Then submit the script by:

sbatch matlabMP.sh

### How to write script for matlabpool

To run a job with parfor, please rewrite TestCluster as:

num_proc=str2num(getenv('SLURM_NTASKS')); matlabpool('local',num_proc) parfor i=1:40 ... end matlabpool close;

Notice: Please use "parpool" to replace "matlabpool" if you use matlab/2016a

Then, submit your script as:

sbatch matlabMP.sh

## Interactive MATLAB use with SLURM

**NOTE:** *any* interruption to the network will cause your job to crash irrecoverably.

To run an interactive Matlab session with 24 cores, you will want to do the following:

$ module load matlab/2016a $ fisbatch --ntasks=24 --nodes=1 --exclusive FISBATCH -- the maximum time for the interactive screen is limited to 6 hours. FISBATCH -- waiting for JOBID 20248 to start on cluster=cluster and partition=general ! FISBATCH -- Connecting to head node (cn142) [xxx00000@cn142 ~]$ matlab ... # here will be the interactive commands with matlab [xxx00000@cn142 ~]$ exit [screen is terminating] Connection to cn42 closed. FISBATCH -- exiting job

**NOTE:** please **DO NOT FORGET** to **EXIT** from the nodes so that the other users can use it.

[NetID@cn142 ~]$ matlab

**NOTE:** *any* interruption to the network will cause your job to crash irrecoverably.

To run an interactive Matlab session with GUI, you should "ssh -X" to the the cluster from a Linux machine or iOS matchine with X11 enabled. Or please follow the X Apps. Then, run the above commands.

## Extra Notes

When opening a parallel pool, you may receive a warning. This warning is fine to ignore, as this warning is currently a bug in the older versions of Matlab and is fixed on newer versions of Matlab.

The parallel pools help set the number of workers from the Slurm allocation, so that Matlab spawns them in the local Matlab profile as well.

To make sure Matlab runs only one instance per the job submission, the matlab -r **matlabfilename**\;exit\; command is used in the slurm script

The parallel pool on the cluster gives errors currently if parpool() or matlabpool is used, this can be fixed by running the Matlab 2017b module.

The pool can be called with: delete(gcp('nocreate')) "this removes previous parallel interactive sessions" p=gcp "this assigns the gcp or current parallel pool instead of using p=parpool()"