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 -l
  3. Load the module with the desired version of matlab module load matlab/2017b
  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/2017b

# [optional] - load knitro if using the optimization package
module load knitromatlab/10.1/10.1.0

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

parallel matlab

script.m
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
 % Get the number of cores allocated to your job
 num_cores = str2num(getenv('SLURM_CPUS_PER_TASK'))

 % Start a pool of workers
 c = parcluster;
 poolobj = parpool(c, num_cores);

 % Distribute tasks among workers in parallel
 parfor ix = 1:poolobj.NumWorkers
     taskID = get(getCurrentTask,'ID');
     workerPID = get(getCurrentWorker, 'ProcessId')
     fprintf('Worker %d has taskID %d and PID %d \n:', ix, taskID, workerPID)
 end

 % Shut down pool of workers
 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: