# MATLAB

SCRP nodes have MATLAB 2022a installed.

**Note:** running MATLAB’s graphical user interface (GUI) remotely is generally not as smooth as running it locally,
so you might want to consider installing MATLAB on your own computer under CUHK’s campus-wide license.

### Option 1: JupyterHub

- Navigate to one of the following URLs on a browser:
- Choose one of the following under
*Notebook*:`MATLAB GUI [↗]`

launches MATLAB’s graphical user interface in your browser.`Matlab [conda env:matlab]`

creates a new MATLAB jupyter notebook.

### Option 2: Remote Desktop

- Connect to a login node through remote desktop.
- Launch from the pull-down menu on the top-right corner, Applications > Statistics > MATLAB.

### Option 3: SSH

#### Prerequisite

To ensure MATLAB’s GUI runs smoothly, consider using an X-server with hardware OpenGL support. We recommend using MobaXterm or VcXsrv on Windows over the public-domain version of Xming for this reason. On the other hand, if you find MATLAB unstable with hardware OpenGL, you might want to try using the default software OpenGL option.

Hardware OpenGL acceleration is off by default in MobaXterm. Enable it before you connect to the cluster.

#### Using MATLAB over SSH

All the instructions below assume you have connected to a login node through SSH. See Account and Access for details.

Start MATLAB:

```
matlab -nosoftwareopengl
```

If the MATLAB GUI does not launch, make sure you have an X server running on your computer. If you are using MobaXterm, make sure the built-in X server is enabled. If you are using a standalone SSH client like PuTTY, you will need to install a separate X server (e.g. VcXsrv on Windows or XQuartz on Mac OS).

The `-nosoftwareopengl`

flag forces MATLAB to use hardware OpenGL on your computer if available. To check if hardare OpenGL is being used, type in MATLAB:

```
opengl info
```

If hardware OpenGL is used, the command should return your graphics card model and driver version. For example:

```
Version: '1.4 (4.6.0 NVIDIA 432.00)'
Vendor: 'NVIDIA Corporation'
Renderer: 'GeForce GTX 1070/PCIe/SSE2'
```

### Switch between Different MATLAB Versions

You can switch between different MATLAB versions in a terminal using Environment Modules. Follow the instructions here.

### Running on a Compute Node - Short Duration

You should run your job on a compute node if you need more processing power. MATLAB by default uses all physical CPU cores it has access to.

#### Running MATLAB without GUI

For computationally-intensive job, it is best to run MATLAB *without* a GUI.
You can do so by running the following command in a terminal:

```
compute matlab -nodisplay -batch "run('/home/users/testuser/mycode.m')"
```

This launches MATLAB’s GUI on a compute node with four logical CPUs and 8GB of RAM, for a duration of 24 hours.

If you need GPU, prepend `gpu`

instead:

```
gpu matlab -nodisplay -batch "run('/home/users/testuser/mycode.m')"
```

You can request more logical CPUs with the `-c`

option, more memory with the `--mem`

option,
more time with the `-t`

option and specify GPU model with the `--gpus-per-task`

option.
For example, to request 16 CPUs, 40G of memory and one RTX 3090 GPU for three days:

```
compute -c 16 --mem=40G --gpus-per-task=rtx3090 -t 3-0 matlab ...
```

See `compute`

for a full list of options,
or `srun`

and `sbatch`

for maximum flexibility.

#### Running MATLAB with GUI

*Jupyter*

To run MATLAB’s GUI through a browser on a compute node, follow the instructions here.

*Remote Desktop*

To run MATLAB GUI on a compute node in remote desktop, launch Applications > Slurm (*x* cores) > MATLAB,
where *x* is the number of desirable cores.

*SSH*

To run MATLAB GUI on a compute node in a terminal, run:

```
# CPU only
compute matlab -nosoftwareopengl
# With GPU
gpu matlab -nosoftwareopengl
```

To check if a GPU is truly available, type in MATLAB:

```
gpuDeviceCount
```

which outputs the number of GPU you have access to.

### Running on a Compute Node - Long Duration

Codes that take a long time to run is best initiated through Slurm’s `sbatch`

command,
which allows you to submit a job without the need to wait for it to execute.
Your code will run even if you disconnect from the cluster.

As an example, let us use MATLAB’s `parfor`

to execute multiple eigenvalue discomposition
simultaneously. Here is our MATLAB code:

```
% Number of workers
poolsize = 8
% Set maximum available workers to CPU core count
local = parcluster('local')
local.NumWorkers = feature('numcores')
saveProfile(local)
% Set up pool of workers
local.parpool(poolsize)
% Actual computation
tic
n = 200;
A = 500;
a = zeros(1,n);
parfor i = 1:n
a(i) = max(abs(eig(rand(A))));
end
toc
```

It is crucial that you set `local.NumWorkers`

to a number as least as big as the desired pool size
and initiate a `parpool`

manually, otherwise the code might not execute.

We will first need to write a batch script. In this example we will name the script `my_job.sh`

,
but you can obviously choose any name you want.

```
#!/bin/bash
#SBATCH --job-name=eigen-test
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=8
matlab -nodisplay -batch "run('/home/users/testuser/mycode.m')"
```

The `#SBATCH`

comments specify various options. In this example, we are requesting eight logical CPUs
for a single task.

We can now submit our job:

```
sbatch my_job.sh
```

`sbatch`

have many options. For example, you can run multiple jobs concurrently in one `sbatch`

request.
See the page on Slurm for details.