# Using Matlab

Matlab is installed on all the campus clusters. Here is a short tutorial to use Matlab, specifically for the Knot cluster. You can follow similar steps if you are using other clsuters.

Once you connect to a cluster (e.g. Knot) with Xwindows enabled, you can simply type /sw/bin/matlab to launch the Matlab graphical user interface. Xwindows is enabled by the -X option during ssh. For Windows, you will need a X-server installed, for example Xming. For MACs, you need XQuartz installed on your system. You can also use the NX Server.

Please do not run large calculations with this method! Use the method below to submit your script to the queuing system.

## Basic Runs: Submission to the queue

For jobs that require a large number of resources and a long runtime, submission to the queue is required. Suppose you have a script names test.m in your working directory. Here is an example job submission script to run this script using Matlab:

```
#!/bin/bash
#PBS -l nodes=1:ppn=12
#PBS -l walltime=00:10:00
cd $PBS_O_WORKDIR
module load MatLab/R2018a
matlab -nodisplay -nodesktop -nosplash < test.m
```

This job asks for 12 cores in a single compute node with a walltime of 10 minutes. $PBS_O_WORKDIR ensures that you are in the directory where the job is submitted (and where test.m is located). Let's say that the name of this script is job.submission. The final step is to imply submit to the queue by

`qsub job.submision`

## Using Matlab in parallel

The easiest way to use parallel processing in Matlab is parallelizing for loops. In this way, you can use all the cores in a given compute node in parallel. Note: Parallelization across different nodes require another toolbox of Matlab which is not existent on the Knot cluster.

A simple script that uses parfor is provided below:

```
% MATLAB example
% Maximum number of threads: Recommended number is the number of cores per node
maxNumCompThreads(12);
fileOut = fopen('out.txt','w');
if fileOut < 1
error('Cannot open output');
end
if matlabpool('size') > 0
matlabpool close;
end
matlabpool open local;
% Compute 50 random 10 dimensional vectors in parallel
randVec=zeros(50,10);
parfor j=1:50
randVec(j,:) = randn(1,10);
end
matlabpool close;
for j=1:50
fprintf(fileOut, 'Random vector %d : %s\n', j, num2str(randVec(j,:)));
end
fclose(fileOut);
```

Note: In the latest version of Matlab (2016), matlabpool is not included anymore. parfor will work automatically.

## Compiled Matlab Scripts

Another option is to compile your Matlab script. In this way, an executable is created which can be run directly. Consider the test.m example above. To create a compiled executable, create a new function that calls test.m, for example

```
function main
test;
close all;
```

Let's say that this function is saved in the script mainFun.m. To compile use the mcc compiler from Matlab:

`/sw/MatLab/R2014b/bin/mcc -mv -R -nodisplay mainFun.m`

This will create an executable mainFun and a shell srcipt run_mainFun.sh. To run it, you can use a job submission script, for example

```
#!/bin/bash
#PBS -l nodes=1:ppn=12
#PBS -l walltime=00:10:00
cd $PBS_O_WORKDIR
./run_mainFun.sh /sw/MatLab/R2014b/
```

Then, you can submit this to the queue.

Note: There are several versions of Matlab available under /sw/MatLab. Choose the one that you are interested in using.

## Matlab Troubleshooting

Some problems and their resolutions are listed here. Please e-mail for for issues you have encountered and we will post the solutions in this documentation.