MATLAB
Introduction
The below guide helps you to run MATLAB on Discovery.
This can be carried out in three different ways:
-
Using Open OnDemand.
-
Running MATLAB interactively using
srun
. -
Running MATLAB scripts using
SBATCH
. -
Launching the MATLAB GUI using X11 Forwarding.
Running MATLAB
Using Open OnDemand(Easier and Faster way)
-
Open OnDemand provides an easier and faster way of launching MATLAB GUI through an interactive session on the compute node.
-
Users simply need to fill a form in OnDemand web portal to request the resources and launch MATLAB, thus eliminating the need of running a series of bash commands to run MATLAB from the terminal.
-
For more information on how to launch MATLAB via Open OnDemand, refer the link → Running MATLAB using Open OnDemand.
Running MATLAB Interactively Using srun
-
Login to discovery and execute the below command:
srun -p normal --cpus-per-task 1 --mem-per-cpu 10g --ntasks=1 --time=0-1:10:00 --pty bash
Output:
srun: job 250774 queued and waiting for resources srun: job 250774 has been allocated resources [crushev@discovery-c2 ~]$
On Line number 3, you can infer that the hostname has been changed from
discovery-l1
login node todiscovery-c2
which is one of the Discovery’s compute nodes.For more information about the interactive jobs using
srun
, please refer to →Interactive Jobs using srun page. -
Then, load the
matlab
module into the working environment. You can load any version of thematlab
module as you wish. Please refer to → Module Environments in Discovery page for more information on how to load and search for modules.module load matlab/2020a
To verify that the module
matlab
has been loaded successfully into the environment, run the module list command. -
Now, launch the
matlab
module with graphical display in disabled mode.matlab -nodisplay
Output:
~~~~~~~~~ < M A T L A B (R) > Copyright 1984-2020 The MathWorks, Inc. R2020a Update 3 (9.8.0.1396136) 64-bit (glnxa64) May 27, 2020 To get started, type doc. For product information, visit www.mathworks.com. >>
You can run the MATLAB commands now and start performing your computations using command line interface.
However, you can only access and run programs in MATLAB until the Walltime you specified has been reached. |
Running MATLAB Scripts Using SBATCH
-
Create a new folder called
matlab
in your home directory.mkdir matlab && cd matlab
-
Create the script file
script.sh
and the MATLAB filesample.m
and then copy paste the codes inscript.sh
andsample.m
tabs respectively.vi script.sh vi sample.m
#!/bin/bash #SBATCH --output output-1.out #SBATCH --ntasks 1 #SBATCH --cpus-per-task 1 #SBATCH --mem-per-cpu 4G #SBATCH --time 00:10:00 #SBATCH --partition normal #Load matlab module module load matlab/2020a #Specify job steps to run srun matlab -batch "sample" echo "Job Completed Successfully"
The resources requested are 1 CPU, 4 GB memory per CPU, 10 minutes of Walltime, and ntasks as 1. Next, load the
matlab
module and specify thesrun
command to run the job on the compute nodes.%Calculating total number of people NoOfStudents = 6000; TeachingStaff = 150; NonTeachingStaff = 20; Total = NoOfStudents + TeachingStaff + NonTeachingStaff; fprintf('The total people equals %d\n',Total); %To close the matlab after the above script is executed quit
+ The above code calculates the total number of people by performing the addition of three numbers.
The MATLAB code must contain quit command at the end to close MATLAB. Otherwise, MATLAB may wait for the further input until the end of the Walltime, wasting the computing resources unnecessarily.
-
Now, both the files are ready and submit the batch script with the following command:
sbatch script.sh
After the job has been submitted, you should get an output similar to the one below but with a different job id.
Submitted batch job 253901
You can use the command below to check the progress of your submitted job in the queue.
Syntax:
squeue -u <username>
squeue -u crushev
Output:
JOBID PARTITION NAME USER ST TIME NODES NODELIST(REASON) 253902 normal script.s crushev R 0:02 1 discovery-c11
-
Once your job has completed and no longer in the queue, you can run the
ls
command to show the list of files in your working directory.ls output-1.out sample.m script.sh
Now you’d notice that there is a new file
output-1.out
and if you view its output with thecat
command, you should see something similar to the output below.cat output-1.out
*Output:*
~~~~~~~~~~ < M A T L A B (R) > Copyright 1984-2018 The MathWorks, Inc. R2018a Update 6 (9.4.0.949201) 64-bit (glnxa64) September 5, 2018 To get started, type one of these: helpwin, helpdesk, or demo. For product information, visit www.mathworks.com. The total people equals 6170 Job Completed Successfully
Launching the MATLAB GUI with X11 Forwarding
It’s possible to access MATLAB’s GUI on Discovery by using X11 Forwarding. This allows you directly access your scripts on Discovery as well as executing your scripts/programs on Discovery’s compute nodes.
First, you need to make sure you have x11 installed and running on your local machine by following the steps below.
-
Ensure that X11 server is running on your local machine. See how to do this here → Run X11 Server
-
Log in to Discovery with X11 enabled using the CLI or putty. To see how to do this, visit → Connecting to Discovery with X11-Forwarding enabled
-
Set up the OpenGL Remote Acceleration environmental variable on the remote server(Discovery). To see how to do this, visit → Enable OpenGL Remote Acceleration
-
Create a directory called
matlabProject
and navigate to itmkdir matlabProject && cd matlabProject
-
Enable X11 forwarding and run commands interactively by using the
srun
command and launching a login shell on Discovery’s compute node.srun -p normal --hint=nomultithread --cpus-per-task 1 --mem-per-cpu 10g --ntasks=1 --time=0-1:10:00 --x11 --pty bash
Output
srun: job 250724 queued and waiting for resources srun: job 250724 has been allocated resources [vaduaka@discovery-c1 matlabProject]$
If you notice line 3, the hostname got changed from
@discovery-l1
(Login node) to@discovery-c1
(compute node) and logged into one of the Discovery’s compute nodes successfully. -
Next, to launch MATLAB GUI, run the
matlab
command.[vaduaka@discovery-c1 matlabProject]$ matlab
Output:
From the output above, you can see that MATLAB’s GUI was successfully launched via x11 forwarding. However, the window will remain accessible for you to run your programs as long as the Walltime you specified hasn’t elapsed.