Differences between revisions 20 and 22 (spanning 2 versions)
Revision 20 as of 2021-08-05 12:13:30
Size: 6528
Editor: jbjohnston
Comment:
Revision 22 as of 2021-08-05 12:16:21
Size: 6652
Editor: jbjohnston
Comment:
Deletions are marked like this. Additions are marked like this.
Line 15: Line 15:
matlab -nodisplay matlab -nodisplay -nosplash
Line 31: Line 31:
matlab -nodisplay matlab -nodisplay -nosplash
Line 178: Line 178:

Please also note that if a value is not specified for "parpool" MATLAB will default to 12 workers.

MATLAB

Overview

Oakland University has obtained a campus wide license (CWL) for MATLAB. This permits any user to run MATLAB on Matilda without special permission or license files. This page describes some of the ways in which MATLAB can be run on the cluster.

Initial Cluster Configuration

In order to use parallel work and batch modes it is necessary to import cluster configuration settings into your MATLAB profile the first time you run it.

Please use the following:

module load MATLAB
matlab -nodisplay -nosplash
>>configCluster

Note that if you run the "configCluster" MATLAB command more than once, it will erase and regenerate your cluster profile information, so it is only necessary to do this once.

Interactively Scheduled

A scheduled interactive job can be run on one of the cluster nodes using something like the following:

srun -N 1 -c 1 -t 30:00 --pty /bin/bash --login

Once the session has started, simply load MATLAB and launch the application:

module load MATLAB
matlab -nodisplay -nosplash

Please see our documentation for more information on scheduling interactive jobs.

Interactive Parallel

You can start an interactive MATLAB job and specify the total number of parallel workers to use during your run. This can be accomplished using something like the following:

module load MATLAB
matlab -nodisplay
>>c=parcluster;
>>p=c.parpool(40)

The command sequence above provides a handle to the cluster resource (parcluster), and then allocates 40 workers (parpool). These workers can run on a single node or across multiple nodes depending on the request and cluster scheduling. From here, you can execute MATLAB commands or scripts using a default set of cluster parameters.

To see the default cluster parameters use the following command:

>>c.AdditionalProperties

Various other parameters can be added or modified such as walltime, memory usage, GPUs, etc. For example:

>>c.AdditionalProperties.Walltime = '5:00:00';
>>c.AdditionalProperties.MemUsage = '4000';
>>c.AdditionalProperties.GpusPerNode = 1;

When finished, you can terminate this job using:

>>p.delete

Asynchronous Interactive Batch Jobs

It is also possible to submit asynchronous batch jobs to the cluster through the MATLAB interactive interface. This can be a handy way to submit a series of jobs to Matilda without having to create independent job scripts for each analysis task. MATLAB can be run from the login node or a scheduled interactive session and the jobs submitted as shown below.

The "batch" command will return a job object which is used to access the output of the submitted job. For example:

module load MATLAB
matlab -nodisplay
>>c=parcluster;
>>j=c.batch(@pwd, 1, {})

In the example above we obtain a handle to the cluster (parcluster) as we did in the previous example. The batch command launches the "@pwd" command (present working directory) with "1" output argument and no input parameters "{}".

Another example might involve running a MATLAB function which might look something like the following:

j=c.batch('sphere',1, {})

The command above submits a MATLAB job for the script "sphere" to the Matilda cluster.

In this final example, we run a MATLAB script as a batch job:

j=c.batch('myscript')

To query the state of any job:

>>j.State

If the "state" is "finished" you can fetch the results:

>>j.fetchOutputs{:}

After you're finished, make sure to delete the job results:

>>j.delete

To retrieve a list of currently running or completed jobs, call "parcluster" to retrieve the cluster object. This object stores an array of jobs that were run, are running, or are queued to run:

>>c=parcluster;
>>jobs=c.Jobs
>>j2=c.Jobs(2);

The example above will fetch the second job and display it.

We can view our job results as well:

>>j2.fetchOutputs

Please note that ending a command with the semicolon (;) will suppress output.

SLURM Job Script

MATLAB runs can also be accomplished using conventional job scripts. MATLAB commands should be constructed to suppress GUI display components. MATLAB scripts should be coded to display or print results to output. Below is an example job script where we run the MATLAB script "mysphere" and view the slurm*.out file for the results:

#SAMPLE JOB SCRIPT
#!/bin/bash
#SBATCH --job-name=matlabTest
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=1
#SBATCH --time=00:05:00

cd ~
module load MATLAB
matlab -singleCompThread -nodisplay -nosplash -r mysphere

For reference, the MATLAB script "mysphere.m" contains the following:

[x,y,z] = sphere; 
r = 2;
surf(x*r,y*r,z*r)
axis equal
A = 4*pi*r^2;
V = (4/3)*pi*r^3;
disp(A)
disp(V)

We can also run multi-worker parallel jobs using SLURM job scripts. For example, assume we have the following script "for_loop.m":

poolobj = parpool(4);
fprintf('Number of workers: %g\n', poolobj.NumWorkers);

tic
n = 200;
A = 500;
a = zeros(n);
parfor i = 1:n
    a(i) = max(abs(eig(rand(A))));
end
toc

In the above script, we specify we want 4 workers, so our job script would look something like the following:

# Sample job script
#!/bin/bash
#SBATCH --job-name=matlabTest
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=5
#SBATCH --time=00:05:00

cd ~
module load MATLAB
matlab -nodisplay -nosplash -r for_loop

Note that in the job script example above, we specified 5 CPUs instead of 4. This is because one (1) CPU is needed to manage the 4 workers (N + 1) for a total of 5.

Please also note that if a value is not specified for "parpool" MATLAB will default to 12 workers.

More Information

For more information on parallel computing with MATLAB, refer to the references below:


CategoryHPC