University of Pretoria
Operational / Internal Site

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

computing:hpcc:matlab [2015/09/21 05:56] (current)
Line 1: Line 1:
 +====== MATLAB simulations with SLURM ======
 +
 +A limited number of MATLAB licenses are available on the clusters. The
 +following provides templates for a number of typical MATLAB simulations.
 +
 +===== Simple single threaded example: 1 license =====
 +
 +The following will run the **''​single_example.m''​** script in the current
 +directory, using a single MATLAB computational thread (i.e. one CPU).
 +
 +<file bash single_example.slurm>​
 +#!/bin/bash
 +#SBATCH --output=<​CHANGETHIS>​.log
 +#SBATCH --job-name=<​CHANGETHIS>​
 +#SBATCH --cpus-per-task=1
 +#SBATCH --mem-per-cpu=1000
 +#SBATCH --licenses=matlab:​1
 +#SBATCH --mail-type=END
 +#SBATCH --mail-user=<​CHANGETHIS>​
 +
 +# Run simulation
 +srun matlab -nodisplay -nosplash -nojvm -singleCompThread -r "​single_example,​ exit"
 +</​file>​
 +
 +===== Multi-threaded example: 1 license =====
 +
 +The following will run the **''​multithread_example.m''​** script in the current
 +directory, using at most 4 threads (4 CPU cores). In general most MATLAB
 +code does not scale well beyond 8 cores and hence please do not set 
 +**''​cpus-per-task''​** to more than 8.
 +
 +<file bash multithread_example.slurm>​
 +#!/bin/bash
 +#SBATCH --output=<​CHANGETHIS>​.log
 +#SBATCH --job-name=<​CHANGETHIS>​
 +#SBATCH --cpus-per-task=4
 +#SBATCH --mem-per-cpu=1000
 +#SBATCH --licenses=matlab:​1
 +#SBATCH --mail-type=END
 +#SBATCH --mail-user=<​CHANGETHIS>​
 +
 +# Run simulation
 +srun matlab -nodisplay -nosplash -nojvm -r "​multithread_example,​ exit"
 +</​file>​
 +
 +===== Passing parameters to MATLAB scripts =====
 +
 +The following, based on the previous example, how to pass parameters to
 +the MATLAB script. This assumes that the script contains a function
 +of the same name as the script. Two simulations are run sequentially.
 +<file bash multithread_example.slurm>​
 +#!/bin/bash
 +#SBATCH --output=<​CHANGETHIS>​.log
 +#SBATCH --job-name=<​CHANGETHIS>​
 +#SBATCH --cpus-per-task=4
 +#SBATCH --mem-per-cpu=1000
 +#SBATCH --licenses=matlab:​1
 +#SBATCH --mail-type=END
 +#SBATCH --mail-user=<​CHANGETHIS>​
 +
 +# Run simulations with different parameters, the integers 1 and 2 in this case.
 +srun matlab -nodisplay -nosplash -nojvm -r "​multithread_example(1);,​ exit"
 +srun matlab -nodisplay -nosplash -nojvm -r "​multithread_example(2);,​ exit"
 +</​file>​
 +
 +===== Interactive MATLAB: 1 license =====
 +
 +To assist the debugging of  scripts using the cluster licensed toolboxes, it is
 +sometime necessary to run MATLAB in interactive mode on the cluster. To do so,
 +simply run **''​matlab''​** after logging in on the head node. This will start a
 +single CPU session with 4GB of memory. Note that it will take about 30 to 50
 +seconds for the session to be allocated on the cluster and for MATLAB to start.
 +Note furthermore that the session has a hard time limit of 1 hour and will also
 +terminate after 20 minutes of inactivity.
 +
 +** Do not use the interactive session for simulations. **
 +
 +For the X11 graphics to be handled correctly, this assumes you have a X11
 +server installed on your computer and that you logged in with X11 forwarding
 +enabled.
 +
 +===== Random Number Generator Initialisation =====
 +
 +There are two random number generators (RNGs) built into MATLAB and that are used by various functions such as RAND and the functions sampling from the various distributions. The sequence of numbers produced by RNGs in MATLAB are determined by the internal state of the generators. ​ Setting the generator to the same fixed state allows computations to be repeated. ​ Setting the generator to different states leads to unique computations. Since MATLAB resets the state at start-up, the various functions using the generators will generate the **same** sequence of numbers in each session unless the
 +initial state is changed. The initial state is in turn determined by the seed of the RNG.
 +
 +For simulations running on the clusters, the following code should be executed once for every simulation run in order to ensure randomised sequences across the runs:
 +
 +<code matlab>
 +seed_fid = fopen('/​dev/​urandom'​);​
 +seed1 = fread(seed_fid,​ 1, '​uint32'​);​
 +seed2 = fread(seed_fid,​ 1, '​uint32'​);​
 +fclose(seed_fid);​
 +rand('​twister',​seed1);​
 +randn('​state',​seed2);​
 +</​code>​
 +