# MATLAB

# Using MATLAB on Compute Canada clusters

There are two main ways of using MATLAB on Compute Canada clusters. The first one involves bringing your own license, typically owned by your institution, faculty, department or lab. The second one involves compiling your code into a binary, which you can then run using the MATLAB Compiler Runtime (MCR) libraries.

## Using your own license

Compute Canada is a hosting provider for MATLAB. This means that we have MATLAB installed on our clusters, but we do not provide a generic license accessible to everyone. However, many institutions, faculty or department already have licenses that can be used on our cluster. In general, any research license can be used on our clusters, and more precisely, any "Total Academic Headcount" license is allowed to be used on our infrastructure. We have received written confirmation from MathWorks that this is allowed by their licenses. This is however not obvious in the wording of the license. If the person managing your license is arguing that it cannot be used outside of your campus, you should encourage them to reach out to their MathWorks representative to confirm with them.

Once the legal aspects are worked out, there will be remaining technical aspects. Namely, the license server on your end will need to be reachable by our compute nodes. This will require our technical team to get in touch with the technical people managing your license software. In some cases, this has already been done. You should then be able to load the MATLAB module, and it should find its license automatically. If this is not the case, please write to technical support, so that we can arrange this for you.

**Important:** Like any other intensive job, you must always run MATLAB code within a job that you will have submitted to the scheduler. For instructions on using the scheduler, please see the Running jobs page.

A simple SLURM script that you can use to submit a matlab script example, named cosplot.m - see the section below, is as follows:

**File :**matlab_slurm.sl

```
#!/bin/bash -l
#SBATCH --job-name=matlab_test
#SBATCH --account=def-bmoa #adjust this to match the accounting group you are using to submit jobs
#SBATCH --time=0-03:00 #adjust this to match the walltime of your job
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=1 #adjust this if you are using PCT
#SBATCH --mem=4000 #adjust this according to your the memory requirement per node you need
#SBATCH --mail-user=bmoa@uvic.ca #adjust this to match your email address
#SBATCH --mail-type=ALL
#Load the appropriate matlab module
module load matlab/2018a
#Remove -singleCompThread if you are using PCT
matlab -nodisplay -singleCompThread -r "cosplot"
```

You can then submit the job using the usual `sbatch` command as follows:

```
[name@server ~]$ sbatch matlab_slurm.sl
```

## Using the MATLAB Compiler Runtime libraries

This requires you to have access to the MATLAB Compiler on a Linux platform. Note that Compute Canada's hosting provider license agreement does not allow us to get the Compiler. See documentation for the Compiler at the MathWorks website. Here is an example MATLAB file function.

**File :**cosplot.m

```
function cosplot()
% MATLAB file example to approximate a sawtooth
% with a truncated Fourier expansion.
nterms=5;
fourbypi=4.0/pi;
np=100;
y(1:np)=pi/2.0;
x(1:np)=linspace(-2.0*pi,2*pi,np);
for k=1:nterms
twokm=2*k-1;
y=y-fourbypi*cos(twokm*x)/twokm^2;
end
%(The following commands for generating graphics output work
% with MATLAB 7 on glacier but produce empty plots with MATLAB 6
% on some other clusters.)
plot(x,y)
print -dpsc matlab_test_plot.ps
quit
end
```

To compile it, you would use the command

```
[name@yourserver ~]$ mcc -m -R -nodisplay cosplot.m
```

This will produce a binary named `cosplot`, as well as a wrapper script. To run the binary on Compute Canada servers, you will only require the binary. The wrapper script, named `run_cosplot.sh`, will not work as is on our servers, because MATLAB assumes that some libraries can be found in specific locations. Instead, we provide a different wrapper script, called `run_mcr_binary.sh` which sets the correct paths.

On one of our servers, load an MCR module corresponding to the MATLAB version you used to build the executable:

```
[name@server ~]$ module load mcr/R2018a
```

Run the following command:

```
[name@server ~]$ setrpaths.sh --path cosplot
```

then use your binary as so:

```
[name@server ~]$ run_mcr_binary.sh cosplot
```

You will only need to run the `setrpaths.sh` command once for each compiled binary. The `run_mcr_binary.sh` will instruct you to run it if it detects that it has not been done.

**Important:** Like any other intensive job, you must always run MCR code within a job that you will have submitted to the scheduler. For instructions on using the scheduler, please see the Running jobs page.