Matlab

Getting Started

Matlab is available in the cluster as an Open OnDemand app or as a module.  To view the different versions available issue the spider command from the CLI:

$ module spider matlab
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 matlab:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Versions:
       matlab/R2014a
       matlab/R2015b
       matlab/R2016a
       matlab/R2017a
       matlab/R2017b
       matlab/R2018a
       matlab/R2018b
       matlab/R2019a
       matlab/R2019b
       matlab/R2020a
       matlab/R2021a
       matlab/R2021b
       matlab/R2022a-wiwic3f
       matlab/R2022a
       matlab/R2022b
       matlab/R2023a
       matlab/R2023b
       matlab/R2024a
       matlab/R2024b
       matlab/R2025a
       matlab/R2026a
       matlab/runtime-R2015a
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 For detailed information about a specific "matlab" package (including how to load the modules) use the module's full name.
 Note that names that have a trailing (E) are extensions provided by other modules.
 For example:
    $ module spider matlab/runtime-R2015a

Or to use Open OnDemand instantiate a new Interactive Desktop Session and select it from the menu:

Running in Sbatch Scripts

Matlab is fairly straight forward to run in an sbatch script.  First our example program:

% main.m 
disp('Starting calculation...');
result = 10 * 10; 
fprintf('The result is %d\n', result); exit;

Then we create our sbatch script matlab_test.sh:

#!/bin/bash 
#SBATCH --job-name=matlab_job 
#SBATCH --output=matlab_%j.out 
#SBATCH --error=matlab_%j.err 
#SBATCH --time=01:00:00 
#SBATCH --ntasks=1 
#SBATCH --cpus-per-task=4 
#SBATCH --mem=8G 

module load matlab 

# Run MATLAB without GUI or desktop
matlab -nodisplay -nosplash -r "run('main.m'); exit"

sbatch matlab_test.sh

Using GPUs

GPU use with Matlab is fairly straight forward.  In this example we request two arbitrary GPUs  First when instantiating a session request GPU resources:

salloc -n 8 -N 1 --time=2:00:00 --mem=32GB --gres=gpu:2

Or if you are using Open OnDemand:

OnDemand

Next to verify the GPU is working we will use the following example script which we call gpu_test.m:

try
    % Start a process parallel pool inside the Slurm allocation
    cluster_pool = parpool('Processes');
    fprintf('Parallel pool started with %d workers.\n', cluster_pool.NumWorkers);
    
    % Query each worker to verify separate GPU hardware assignment and Rank
    parfor i = 1:cluster_pool.NumWorkers
        % Get MATLAB's internal device tracking info
        gpu_info = gpuDevice();
        matlab_gpu_idx = gpu_info.Index; 
        
        % Read the underlying system environment variable set by Slurm
        % This shows the actual physical GPU rank/ID on the node
        slurm_gpu_rank = getenv('CUDA_VISIBLE_DEVICES');
        if isempty(slurm_gpu_rank)
            slurm_gpu_rank = 'Not Set (Check local GPU mode)';
        end
        
        fprintf('Worker %d -> MATLAB GPU Index: %d | Physical/Slurm GPU Rank: %s | Model: %s\n', ...
            i, matlab_gpu_idx, slurm_gpu_rank, gpu_info.Name);
    end
    
    % Core workload execution
    parfor i = 1:10
        raw_matrix = rand(2000, 2000);
        gpu_matrix = gpuArray(raw_matrix);
        transformed_gpu = fft(gpu_matrix);
        results(i) = gather(mean(transformed_gpu, 'all'));
    end
    
    disp('Processing batch done.');
    delete(cluster_pool);

catch ME
    fprintf('Execution failed: %s\n', ME.message);
    if ~isempty(gcp('nocreate'))
        delete(gcp);
    end
end
Next we create our SBATCH script:
#!/bin/bash 
#SBATCH --job-name=matlab_gpu_cluster 
#SBATCH --output=matlab_job_%j.out 
#SBATCH --error=matlab_job_%j.err 
#SBATCH --nodes=1 # Launching on 1 node 
#SBATCH --ntasks-per-node=1 # Single task orchestration 
#SBATCH --cpus-per-task=4 # CPU cores available to handle data prep 
#SBATCH --gres=gpu:2 # Requesting 2 physical GPUs 
#SBATCH --time=02:00:00 # Running time limit max (HH:MM:SS) 
#SBATCH --mem=32G # System memory request 

module load matlab  
matlab -batch "gpu_test"

You should receive output like:

Starting parallel pool (parpool) using the 'Processes' profile ...
Connected to the parallel pool (number of workers: 4).
Parallel pool started with 4 workers.
Worker 2 -> MATLAB GPU Index: 2 | Physical/Slurm GPU Rank: 0,1 | Model: NVIDIA GeForce RTX 2080 Ti
Worker 1 -> MATLAB GPU Index: 1 | Physical/Slurm GPU Rank: 0,1 | Model: NVIDIA GeForce RTX 2080 Ti
Worker 3 -> MATLAB GPU Index: 1 | Physical/Slurm GPU Rank: 0,1 | Model: NVIDIA GeForce RTX 2080 Ti
Worker 4 -> MATLAB GPU Index: 2 | Physical/Slurm GPU Rank: 0,1 | Model: NVIDIA GeForce RTX 2080 Ti
Processing batch done.
Parallel pool using the 'Processes' profile is shutting down.


Using Gurobi with Matlab

Gurobi for MATLAB is a high-performance engine swap for your optimization workflow. It replaces MATLAB’s native solvers with Gurobi’s industry-leading mathematical algorithms, allowing you to tackle massive, industrial-scale problems that would otherwise stall or time out.

See: Using Gurobi with Matlab for instruction.

Matlab Parallel Server

Matlab Parallel Server is a set of Matlab functions that allow you to run parallel jobs on the cluster.  In fact, you must use the Matlab Parallel Server if you want to run jobs that span multiple nodes. 

Please see: Matlab Parallel Server for instruction