Matlab use cases

Some use cases describing how to offload MATLAB computation to the cluster.

Use Matlab on a compute node with a scheduled batch script
A shared cluster environment like Cheaha uses a job scheduler to run tasks on the cluster to provide optimal resource sharing among users. Cheaha uses a job scheduling system call Slurm to schedule and manage jobs. A user needs to tell Slurm about resource requirements (e.g. CPU, memory) so that it can schedule jobs effectively. These resource requirements along with actual application code can be specified in a single file commonly referred as 'Job Script/File'. Following is a simple job script that prints job number and hostname.

Following is a sample script to generate Fibonacci series from 1-10 and save the output %% Job Test on Cheaha with SLURM - Test1Fib.m % Submit Job with matlabSubmit.sbatch % Output Files out.mat % Create Fibonacci Series upto 10

% clear all % close all

limit = 10       % Fibonacci series from 1 to limit c(1)=0;          % first element c(2)=1;          % second element

for i=3:limit c(i)=c(i-1)+c(i-2); end c                 % Displays the output on screen save('out1.mat')  % Saves the workspace to mat file exit

Following is the matlabSubmit.sbatch Please edit the email address and the processor cores/memory requirement according to your jobs
 * 1) !/bin/bash
 * 2) SBATCH --job-name=matTest
 * 3) SBATCH --output=res.txt
 * 4) SBATCH --ntasks=1
 * 5) SBATCH --partition=express
 * 6) SBATCH --time=10:00
 * 7) SBATCH --mem-per-cpu=100
 * 8) SBATCH --mail-type=FAIL
 * 9) SBATCH --mail-user=YOUR_EMAIL_ADDRESS
 * 1) SBATCH --mail-user=YOUR_EMAIL_ADDRESS

module load rc/matlab/R2016b srun matlab -nodesktop -nodisplay -r Test1Fib srun sleep 60

The above job can be submitted by $ sbatch matlabSubmit.sbatch Submitted batch job 359705

You can check the job status using squeue command $ squeue -u $USER 359705  express     matTest tanthony  R       0:03      1 c0005

This shows that the job is running on compute node c0005 The working directory should have two new files res.txt and out1.mat

Use Matlab on a compute node via VNC
This use case creates an ordinary Matlab session, just like you would on your desktop, however, instead of running MATLAB you your desktop, you run MATLAB on a compute node of the cluster and access it's GUI via your cluster desktop.

This is useful in situations where your personal desktop or laptop is not powerful enough to run your Matlab code or you simply want to offload a long-running computation so you can free your desktop or laptop for other activities.

Connect to Your Cluster Desktop
The first step is to connect to an existing cluster desktop session via VNC. Please follow the steps for setting up a VNC session to create your cluster desktop and connect to it.

Open a Terminal
The second step is to open a terminal window on your cluster desktop. Once you are connected to your cluster desktop, start the Terminal application from the cluster desktop menu Applications->Accessories->Terminal.

Reserve your Computer
The third step is to request a computer on which you can run Matlab. In your cluster desktop terminal window, submit a resource request to reserve a compute node for running Matlab sinteractive --ntasks=1 --mem-per-cpu=1024 --time=4:00:00 --partition=medium The above resource request asks for a computer with 1 gigabyte of RAM for 4 hours. You can adjust these values to suit your needs but you should try to make reasonable estimates to ensure that resources are shared fairly. WARNING: the specified time limit is a hard boundary and can't be changed after you start Matlab. When this time limit is reached, your Matlab session will be automatically stopped, without warning, by the cluster resource manager.

sinteractive --ntasks=1 --cpus-per-task=4 --mem-per-cpu=1024 --time=8:00:00 --partition=medium The above resource request asks for a computer with 4 cores and 1 gigabyte of RAM/core (total 4GB ram) for 8 hours.

Start Matlab
The fourth and final step is to start Matlab on the compute resource you have reserved. In the same terminal window from which you made the resource request (ie. ran "qlogin"), run the following commands to start matlab. module load rc/matlab/R2016b matlab

Matlab will be running your reserved compute resource and can be controlled interactively via the VNC session. This Matlab session works just like the Matlab sessions on your personal desktop or laptop. When you are finished using Matlab, simply exit the application as you normally would. Your compute resource will automatically return to the resource pool at the end of the your requested reservation time or you can type "exit" to return it immediately.