MATLAB
MATLAB is a commercial language for matrix computations and other arithmetic and scientific computations. It can be used in GUI or command line mode.External Links:
MATLAB website
MATLAB Tutorials
MATLAB R2024a (and Above) Required Procedures
If you use MATLAB R2024a (module avail matlab to see the versions available), there are some special procedures that must be performed.
Loading MATLAB
There are various versions of MATLAB on Hazel. To see the various versions available, typemodule avail matlabTo use the default version, enter
module load matlab
Running MATLAB
Do not use MATLAB on a login node. To use MATLAB interactively for test and debug, either use an interactive session or use the HPC-VCL node. To confirm that the session is not on a login node, type hostname.
Batch MATLAB
Here is an example batch script. For more information on submitting batch scripts, see the documentation on running jobs.
To run MATLAB in batch mode using a MATLAB script called script.m, create a text file called submit.sh containing:
#!/bin/bash
#BSUB -W 20
#BSUB -n 1
#BSUB -o out.%J
#BSUB -e err.%J
##BSUB -x #Use exclusive only if necessary, uncomment if job spawns additional threads
module load matlab
matlab -nodisplay -nosplash -nodesktop -singleCompThread -r "run('script.m');exit;"
The job can be submitted as
bsub < submit.sh
The script submit.sh requests 1 core for a maximum of 20 minutes. Check the documentation on running jobs to customize the batch script.
Interactive MATLAB
HPC-VCL
MATLAB can be used on the HPC-VCL image in GUI mode. Older versions of MATLAB may not work on the current HPC-VCL image. To run on the HPC-VCL, go to the VCL site and reserve a HPC-VCL image, once the image is ready log in, open a terminal, and type:module load matlab matlabSee “Troubleshooting tips for MATLAB” section at bottom of this page for common GUI issues.
Interactive Compute Node
An interactive session on a compute node may be used for short debugging or in order to prepare a production batch script. Interactive sessions must be kept to a minimum and only used when necessary. Nodes left idle or underutilized by long running interactive sessions may be terminated.
The following requests an interactive session with 1 core for 10 minutes and then opens MATLAB in non-display mode.
bsub -Is -n 1 -W 10 -x bash module load matlab matlab -nodisplay -nosplash -nodesktop -singleCompThreadTo exit MATLAB, type
exit. To exit the interactive session, type exit
Parallel MATLAB with shared memory
Using MATLAB in shared memory means that the program functions on one node; there is no communication across nodes.
By default, parallel MATLAB functions create as many tasks as there are physical cores available. To avoid overloading the node, the user must either limit the number of cores by changing the default behavior in the MATLAB script (maxNumCompThreads(N)) or adjust the bsub script to ensure the reservation of an entire node.
One possibility is to specify the minimum number of processors needed and reserve the entire node using -x. The following reserves a node with at least 8 cores, specifies that all cores be on one node, and reserves the whole node. If a node with more cores happens to be reserved, e.g. a node with 12 cores, then MATLAB will use all 12 cores. The -x ensures that LSF does not assign additional jobs to the node.#BSUB -n 8 #BSUB -R "span[hosts=1]" #BSUB -xAnother method is to be more precise in specifying the resources. The following reserves 8 cores and specifies an 8 core node, which is a node with two quad core processors:
#BSUB -n 8 #BSUB -R "span[hosts=1]" #BSUB -R "select[qc]"
See the example below on how to specify the number of processes to be used as an argument to a MATLAB function.
MATLAB support for GPUs
MATLAB versions R2018a and newer support the newer model GPUs (RTX 2080, P100, GTX 1080, and K20m). MATLAB versions R2017a and older support the older model GPUs (M2070, M2070q, and M2090). See the MATLAB GPU Support by Release page for more details.Here are examples from batch scripts that use MATLAB on different GPU models on Hazel.
#!/bin/bash
#BSUB -n 1
#BSUB -W 10
#BSUB -q gpu
#BSUB -R "select[m2070]"
#BSUB -gpu "num=1:mode=shared:mps=no"
#BSUB -o m2070_out.%J
#BSUB -e m2070_err.%J
module load matlab/R2017b
matlab -nodisplay -nosplash -nodesktop -singleCompThread -r "run('gpu.m');exit;"
or
#!/bin/bash
#BSUB -n 1
#BSUB -W 10
#BSUB -q gpu
#BSUB -R "select[rtx2080]"
#BSUB -gpu "num=1:mode=shared:mps=yes"
#BSUB -o rtx2080_out.%J
#BSUB -e rtx2080_err.%J
module load matlab/R2019b
matlab -nodisplay -nosplash -nodesktop -singleCompThread -r "run('gpu.m');exit;"
Note that the older GPUs (M2070, M2070Q, M2090) do not support NVIDIA's mps (mps=no), while the newer GPU nodes (RTX 2080, GTX 1080, P100, K20m) do support mps(mps=yes).
Example MATLAB batch scripts
Here are some sample runs, each having a matlab script (*.m), a submit script (*.sh), and the resulting standard out and standard error files.Matrix multiply with random numbers
Creates and multiplies matrices. Submit usingbsub < rand.sh . There were no errors.
Integer factorization - shared memory and parpool
This is an example from the official MATLAB documentation. It has been modified into a function in order to limit the number of processors used in the parallel pool via the call to MATLAB in the LSF script. Submit usingbsub < int_fun.sh .
FFT on a GPU
Compares timing for the calculation of FFT's with and without GPU acceleration. Submit usingbsub < gpu_m2070.sh . There were no errors.
MATLAB Parallel Server
MATLAB Parallel Server is available for users that need to run MATLAB jobs that require more processors than are available on a single node. Please contact us for more details about this.
Troubleshooting tips for MATLAB
Here are a few troubleshooting tips:- Make sure the home directory is not close to the quota limit. MATLAB may produce large temporary files, so if the home directory is close to the limit, MATLAB could fail with cryptic error messages.
-
Users have reported that the presence of ~/.matlab files from previous jobs may cause issues. In that case, remove these files:
rm -R ~/.matlab -
If you are unable to open up files (script files or otherwise) from within MATLAB GUI started directly from an HPC-VCL, please try this workaround:
- Once your reservation starts at https://vcl.ncsu.edu, click “Connect!”, then click “Get RDP file”, then open up the .rdp file in the appropriate application (more information here).
- Once the .rdp opens up and you see your virtual desktop, open up the Terminal application.
- Follow directions under “Memory or compute intensive applications requiring remote display” at this webpage.
-
On the compute node (after “bsub-ing” into compute node), type
module load matlaband thenmatlab. The GUI will open, and you should be able to open up files from within MATLAB.
- If your MATLAB GUI is blank or distorted, try these tips under “Blank or distorted GUIs for Mac users, especially MATLAB” at this page.
Last modified: November 03 2025 23:36:07.