matlab

  • About: MATLAB (matrix laboratory) is a multi-paradigm numerical computing environment and fourth-generation programming language
  • Category: Mathematics
  • URL: http://www.mathworks.com/products/matlab/
  • Interactive executable: matlab
module (bold = default) build dependencies
matlab/r2011a x64
matlab/r2013a x64
matlab/r2014a x64
matlab/r2014b x64
matlab/r2015a x64
matlab/r2015b x64
matlab/r2016a x64

Running Matlab Interactively

Use the interactive command to get connected to a compute node with X11 forwarding enabled.

interactive -t 2:00:00 -n 1 -c 1  

Running Matlab in Batch Mode

To run matlab in batch mode, you should create an sbatch script (matlab.slurm), ie.

#/bin/bash
#SBATCH -t 2:00:00
#SBATCH -n 1
#SBATCH -c 1
module load matlab
srun matlab -nodesktop -r matlab_script

and then submit the job using sbatch, ie.

sbatch matlab.slurm 

An alternative approach it to use a generic script that you put in your home directory

#/bin/bash
module load matlab
srun matlab -nodesktop -r "$1"

and then submit the job using sbatch, ie.

sbatch -p standard -t 2:00:00 -n 1 -c 1 matlab.slurm matlab_script

Using Job Arrays to do embarrassingly parallel computations

To run multiple instances of a matlab function, but with different parameters (in this case 1,2,3,5,7,8,9), you can use a job array. For example, we can modify matlab.slurm as follows:

#!/bin/bash
#SBATCH --array=1-3,5,7-9
#SBATCH -t 2:00:00 
#SBATCH -n 1
#SBATCH -c 1
srun matlab -singleCompThread -r "matlab_function($SLURM_ARRAY_TASK_ID)"

Note you can also access the SLURM_ARRAY_TASK_ID from within a matlab script if you prefer, using matlab's getenv function.

getenv('SLURM_ARRAY_TASK_ID')

Running Matlab in parallel

Matlab is capable of using multiple cores on a single node in several ways depending on the types of calculations in your matlab routines.

Automatic threading of vector operations

Some types of vector operations are automatically parallelized, you just need to modify your batch script to ask for more cores per task. (There are 24 cores per node on the standard and interactive queues, but you should adjust this to match the number of cores per node consistent with whatever queue you are using). Note that if you run matlab without -singleCompThread, it will try to use as many cores as it can.

#!/bin/bash
#SBATCH -t 2:00:00 
#SBATCH -n 1
#SBATCH -c 24
srun matlab -nodesktop -r matlab_script

Using the Parallel Computing Toolbox

Matlab's parallel computing toolbox allows you to explicitly parallelize your code using parfor loops, or spmd instructions. The parallel computing toolbox limits you to only using cores on a single node, but since there are 24 cores per node, you can still get significant speedup. Your slurm script would look the same as above, but your matlab code would need to be modified to open and close the matlab pool with proper environment variables as wellas introducing parfor loops or spmd sections. Here is a sample matlab script.

 pc=parcluster('local')
 JOB_ID=getenv('SLURM_JOBID')
 CPUS=getenv('SLURM_CPUS_PER_TASK')
 pc.JobStorageLocation=strcat('/local_scratch/',JOB_ID)
 matlabpool(pc,CPUS)
 parfor i=1:100
    myfunc(i)
 end
 matlabpool close
 exit

Note that we have used the slurm environment to setup the matlabpool storage location and size appropriately.

Using the Distributed Compute Server

In order to open up a pool of matlab workers beyond the number of cores on a single node, you will need to use the distributed compute server. There are two scripts (slurm_mdce_start and slurm_mdce_stop) that will respectively startup and shutdown a matlab distributed compute server inside of your job allocation. You also will need to adjust your max user process limit (ie ulimit -u) to 8192.

Batch mode

Here is an example slurm script that you can submit using sbatch

#!/bin/bash
#SBATCH -J Matlab_mdce
#SBATCH --nodes 2
#SBATCH --ntasks-per-node=24
#SBATCH -t 20
#SBATCH -p debug
#SBATCH -o matlab.log
ulimit -u 8192
module load matlab
slurm_mdce_start
matlab -nodisplay -r "mdce_script"
slurm_mdce_stop

The slurm_mdce_start script also generates a $JOBID.profile file for use by matlab to determine how to open up the distributed pool of workers. Just use the mdce_profile function to load this profile, that you then pass to matlabpool.

slurm_profile=mdce_profile()
nworkers=str2num(getenv('SLURM_NTASKS'))
parpool(slurm_profile, nworkers)

Interactive mode

If you want to do this interactively, then use the interactive script. ie

interactive -J Matlab_mdce --nodes 2 --ntasks-per-node=24 -t 20 -p debug

and then once you get a prompt on a compute node you can run the rest of the commands

ulimit -u 8192
module load matlab
slurm_mdce_start
matlab 

This should start up matlab and then on the matlab prompt you can connect to the pool using

slurm_profile=mdce_profile()
nworkers=str2num(getenv('SLURM_NTASKS'))
parpool(slurm_profile, nworkers)

After you exit matlab, don't forget to shutdown the matlab distributed compute engine and exit your job

slurm_mdce_stop
exit    

Also please note that there are 256 worker licenses available for the distributed compute server, so don't use this method unless you need to use more than 24 cores. While it is unlikely, you might find that the licenses are not available when your job is ready to run. We are working on determining the best way to handle this, but please let us know if you get errors regarding the lack of available licenses.

Matlab ToolBoxes

  • SIMULINK
  • Bioinformatics_Toolbox
  • Communication_Toolbox
  • Video_and_Image_Blockset
  • Control_Toolbox
  • Curve_Fitting_Toolbox
  • Signal_Blocks
  • Data_Acq_Toolbox
  • Database_Toolbox
  • Datafeed_Toolbox
  • Econometrics_Toolbox
  • RTW_Embedded_Coder
  • Filter_Design_HDL_Coder
  • Fin_Instruments_Toolbox
  • Financial_Toolbox
  • Fixed_Point_Toolbox
  • Fuzzy_Toolbox
  • GADS_Toolbox
  • EDA_Simulator_Link
  • Image_Acquisition_Toolbox
  • Image_Toolbox
  • Instr_Control_Toolbox
  • MATLAB_Excel_Builder
  • MATLAB_Builder_for_Java
  • MATLAB_Builder_for_dot_Net
  • MATLAB_Coder
  • Compiler
  • MATLAB_Report_Gen
  • MAP_Toolbox
  • MPC_Toolbox
  • Neural_Network_Toolbox
  • OPC_Toolbox
  • Optimization_Toolbox
  • Distrib_Computing_Toolbox
  • PDE_Toolbox
  • RF_Toolbox
  • Robust_Toolbox
  • Signal_Toolbox
  • SimBiology
  • SimEvents
  • SimMechanics
  • Simscape
  • Real-Time_Workshop
  • Simulink_Control_Design
  • Simulink_Design_Optim
  • XPC_Target
  • Excel_Link
  • Stateflow
  • Statistics_Toolbox
  • Symbolic_Toolbox
  • Identification_Toolbox

also see Using Software.