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
Running MatLab scripts using
Launching the MatLab GUI using X11 Forwarding.
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.
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
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-l1login node to
discovery-c2which 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
matlabmodule into the working environment. You can load any version of the
matlabmodule 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
matlabhas been loaded successfully into the environment, run the module list command.
Now, launch the
matlabmodule with graphical display in disabled mode.
~~~~~~~~~ < M A T L A B (R) > Copyright 1984-2020 The MathWorks, Inc. R2020a Update 3 (18.104.22.1686136) 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.
Create a new folder called
matlabin your home directory.
mkdir matlab && cd matlab
Create the script file
script.shand the MatLab file
sample.mand then copy paste the codes in
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
matlabmodule and specify the
sruncommand 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:
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.
squeue -u <username>
squeue -u crushev
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
lscommand 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.outand if you view its output with the
catcommand, you should see something similar to the output below.
~~~~~~~~~~ < M A T L A B (R) > Copyright 1984-2018 The MathWorks, Inc. R2018a Update 6 (22.214.171.1249201) 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
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
matlabProjectand navigate to it
mkdir matlabProject && cd matlabProject
Enable X11 forwarding and run commands interactively by using the
sruncommand 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
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
[vaduaka@discovery-c1 matlabProject]$ matlab
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.