Matlab

Interactive Mode

  1. SSH onto the mercury login node ssh <BoothID>@mercury.chicagobooth.edu

  2. Request an interactive session on a compute node srun --pty bash --login

  3. Load the module with the desired version of matlab module load matlab/R2021b

  4. Start the matlab interpreter by typing: matlab

Batch Mode

To run a matlab script in batch mode, generate a submission script similar to the one below, and run by typing sbatch submit.sh.

submit.sh
#!/bin/bash

#SBATCH --account=phd
#SBATCH --mem=2G
#SBATCH --time=0-01:00:00
#SBATCH --job-name=example_job

# Load the module with the desired version of matlab
module load matlab/R2023b

# [optional] - load knitro if using the optimization package
module load knitro/14.0/14.0.0

# run matlab script named myscript.m
srun matlab -nodisplay -nojvm < myscript.m

parallel matlab

script.m
 1 % Get the number of cores allocated to your job
 2 num_cores = str2num(getenv('SLURM_CPUS_PER_TASK'))
 3
 4 % Start a pool of workers
 5 c = parcluster;
 6 poolobj = parpool(c, num_cores);
 7
 8 % Distribute tasks among workers in parallel
 9 parfor ix = 1:poolobj.NumWorkers
10     taskID = get(getCurrentTask,'ID');
11     workerPID = get(getCurrentWorker, 'ProcessId')
12     fprintf('Worker %d has taskID %d and PID %d \n:', ix, taskID, workerPID)
13 end
14
15 % Shut down pool of workers
16 delete(poolobj)

gpu matlab

There are 3 options for performing GPU computations in Matlab. Option #1 is simplest and commonly used, while #3 is most difficult and highest performance.

  1. Use one of the numerous built-in Matlab functions that support gpuArray data. In Matlab, methods(‘gpuArray’) lists all methods with class gpuArray support.

  2. Implement a custom Matlab function of element-wise operations to be performed on the GPU, via arrayfun or bsxfun.

  3. Create a custom GPU kernel that will process Matlab’s gpuArray data.

The basic method (option #1) for transforming a CPU-based Matlab program into one that runs on the GPU using the supported built-in Matlab functions is:

  1. Move the input data from Matlab to GPU using the gpuArray function, or directly create the input data on the GPU using static gpuArray methods.

  2. Use exactly the same processing code as for the CPU implementation.

  3. Move the results back into Matlab memory using the gather function.

There are many good articles on the Internet on how to use a GPU in Matlab. Here are some starters: