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

  1. 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 to discovery-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.

  2. Then, load the matlab module into the working environment. You can load any version of the matlab 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.

  3. 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

  1. Create a new folder called matlab in your home directory.

    mkdir matlab && cd matlab
  2. Create the script file script.sh and the MatLab file sample.m and then copy paste the codes in script.sh and sample.m tabs respectively.

    vi script.sh
    vi sample.m
    • script.sh

    • 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 the srun 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.

  3. 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
  4. 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 the cat 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.

  1. Ensure that X11 server is running on your local machine. See how to do this here → Run X11 Server

  2. 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

  3. Set up the OpenGL Remote Acceleration environmental variable on the remote server(Discovery). To see how to do this, visit → Enable OpenGL Remote Acceleration

  4. Create a directory called matlabProject and navigate to it

    mkdir matlabProject && cd matlabProject
  5. 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.

  6. Next, to launch MatLab GUI, run the matlab command.

    [vaduaka@discovery-c1 matlabProject]$ matlab

    Output:

    MatLab-window-1.png

    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.