# MATLAB on the Della cluster #### Author: [Niall Coffey](https://hackmd.io/@E-DkQCMtRKuwn1bRWnXebA), [Josh Rines](https://hackmd.io/@aVQeIpHASA2aT-h20vDcnw) First of all, many questions can be answered at [MATLAB on the HPC Clusters](https://researchcomputing.princeton.edu/support/knowledge-base/matlab). This short page is meant to give quick tips for using MATLAB on Princeton HPC clusters. I recommend writing the code outside of the Della GUI, and then uploading and running parallelized code on the Della GUI. ## Access - 2 Options: GUI or no GUI ## GUI - [Click](https://mydella.princeton.edu/pun/sys/dashboard/), sign in with your NetID, interface the directory from Files tab, launch sessions from My Interactive Sessions tab. You may also interface the directory by SSHing in with `ssh netid@della.princeton.edu` and entering your password. - I discovered this by a typo when trying to access [MATLAB on the HPC Clusters, GUI on Tigressdata](https://researchcomputing.princeton.edu/support/knowledge-base/matlab#tigressdata). Since I don't actually know where tigressdata is stored (you cannot access it from mydella.princeton.edu), if you use `ssh -X <netID>@della.princeton.edu`, and move to the folder of interest, you can run the Matlab GUI with seemingly no wait time or session end time (this comes with former method as you specify the number of hours you want). I've attached a screenshot below of what it looks like to show that the graphics aren't too bad.![](https://i.imgur.com/U8PObrZ.jpg) ## No GUI You can access Matlab on the cluster without a GUI as well. One way is to use MobaXterm (or terminal), the other is to use VS Code. I'd recommend VS Code, but if you're proficient with Vim then go for it. The screenshot below is the same project but on VS Code. A fun fact is that if you have both of these open simultaneously, the "live" version will override. Thus, any edits and saves that you make in the GUI will appear in the VS Code version, but not vice versa.![](https://i.imgur.com/bHVjKjx.jpg) Josh recommends using VS Code to use MATLAB on the cluster as well. ### Connecting VS Code to Della cluster (NOTE: Here's a useful tutorial on how to use keys to authorize VS Code without entering password each ssh connection log-in: https://www.youtube.com/watch?v=PDVnUErS_us). In order to use MATLAB on Della with VS Code, you will need to download the MATLAB extension for VSC, which can be found at the VS Code Marketplace [here](https://marketplace.visualstudio.com/items?itemName=Gimly81.matlab). To ssh connect to a host such as the Della cluster, click the bottom left corner of the window (the green button with the arrows pointing at each other) and select "Connect to Host...". If this is the first time you are connecting to a new host, you will need to click "Add New SSH Hosts..." and then type "ssh <username>@della.princeton.edu" (or "ssh <username>@gpu-della.princeton.edu" if you are using or GPUs) in order to add the Della host to your config file. You can check your config file by clicking "Connect to Host..." and then "Configure SSH Hosts...". The config file should look something like the following:![](https://i.imgur.com/CtkEeu0.png) Now you will be able to select which host you want to connect to (Della in this case). To connect, you will need to enter your Princeton University password when prompted to do so. This is the same password as you normally use to access things on campus (VPN, email, etc.). ### Running MATLAB Once you have connected, you can open a folder by clicking on the folder icon at the top left of the window (above the magnifying glass icon), and open your MATLAB files for editing. You also can access a terminal by clicking the "Terminal" drop-down in the drop-down options bar at the top of your screen. In this terminal, if you would like the equivalent of a the MATLAB Command Window, type "module load matlab/R2020a", press enter, then type "matlab -nodesktop -nosplash". The terminal window will become a MATLAB Command Window. To exit this, type "exit". ### Submitting MATLAB jobs with Slurm To submit a MATLAB job with a Slurm file, create a .slurm file in your current working directory. A sample .slurm file, which runs a script called "hello_world.m" is below. #!/bin/bash #SBATCH --job-name=matlab # create a short name for your job #SBATCH --nodes=1 # node count #SBATCH --ntasks=1 # total number of tasks across all nodes #SBATCH --cpus-per-task=1 # cpu-cores per task (>1 if multi-threaded tasks) #SBATCH --mem-per-cpu=4G # memory per cpu-core (4G per cpu-core is default) #SBATCH --gres=gpu:1 # number of gpus per node #SBATCH --time=00:01:00 # total run time limit (HH:MM:SS) #SBATCH --mail-type=all # send email on job start, end and fault #SBATCH --mail-user=<YourNetID>@princeton.edu module purge module load matlab/R2019a matlab -singleCompThread -nodisplay -nosplash -r hello_world Note: only include the #SBATCH --gres==gpu:1 if you are running on the GPU. More details on Slurm can be found on the Princeton Research Computing information page [here](https://researchcomputing.princeton.edu/support/knowledge-base/matlab#slurm). ## Code Structure - Only ask for >1 cores if you are sure that your code is parallelized. - If your code is not vectorized, I would recommend vectorizing it instead of running in parallel. This is an example of [vectorization](https://www.mathworks.com/help/matlab/matlab_prog/vectorization.html). - If your code is not parallelized, a very simple way is to use [parfor](https://www.mathworks.com/help/parallel-computing/decide-when-to-use-parfor.html) instead of for loops. However, make sure that your code does not have any parts that depend on the iteration. - For example, don't use parfor if you have something like `vec = [vec, add_element];` inside your loop, which changes size with each iteration. ## Command Keys This is so strange for me that I would recommend writing/editing the scripts in your local MATLAB GUI. Undo is not Control+z, and the search function is not Control+f. ![](https://i.imgur.com/j45Oyp4.png) ## Running in Segments vs Full Script I do not know why, but I have only figured out how to run the whole script, not segments separated by`%% Header` as in local MATLAB GUI. You could use LiveScript (which has separations like Jupyter Notebook), but I would recommend against this for reproducible research.