Skip to end of metadata
Go to start of metadata

This quick start will guide you in running MATLAB for the first time on the HCC supercomputers. It will first describe a serial MATLAB job, including a submit script to the supercomputer. Then, it will describe how to use parallel directives in MATLAB on the supercomputer. The sample scripts and output files can be downloaded from <matlab_dir.zip> and <parallel_matlab_dir.zip>.

Login to a HCC Cluster (Tusker or Sandhills) 

Log in to a HCC cluster through PuTTY (For Windows Users) or Terminal (For Mac/Linux Users) and make a subdirectory called matlab_dir under the $WORK directory.

$ cd $WORK
$ mkdir matlab_dir

Creating the M-Scripts

In the subdirectory matlab_dir, save all the relevant m-scripts. Here, two sample m-scripts, serial_main.m and integration.m, are created. The main program (serial_main.m) links to a function file (integration.m) to execute the computation.

serial_main.m
%This MATLAB program ("serial_main.m") plots a quadratic function x^2 and computes
%its integral value form N1 to N2. The function plot is saved as a .pdf file.
%The integral is evaluated through calling a customized function file 
%"integration.m", and the result is printed to the screen (MATLAB command window).
%Finally, all the generated variables in the workspaced is saved to a .mat file.

clear;
clc;
N1 = -10;                               %input1
N2 = 10;                                %input2
integral_value = integration(N1,N2)		%Call function file integration.m to compute the integral
x = linspace(N1,N2,100);	
figure;
plot(x, x.^2,'-k.');
print(gcf,'data_plot.pdf','-dpdf');   	%Save the figure as a .pdf file
save('data.mat');						%Save the workpace data
integration.m
function output = integration(input1, input2)
fun = @(x) x.^2;
output = integral(fun,input1,input2);
end

Creating a Submit Script

Create a submit script serial_matlab.submit to request one core (default) and 1-min run time on the supercomputer. The name of the main program serial_main.m enters at the last line.

serial_matlab.submit

#!/bin/sh
#SBATCH --mem-per-cpu=1024
#SBATCH --time=00:01:00
#SBATCH --job-name=MATLAB
#SBATCH --error=MATLAB.%J.err
#SBATCH --output=MATLAB.%J.out

 

module load matlab/r2016a

matlab -nodisplay -r "serial_main, quit"


Submit the Job

The job can be submitted through the command sbatch. The job status can be monitored by entering squeue with the -u option.

$ sbatch serial_matlab.submit
$ squeue -u <username>

Sample Output

A integral value is computed and printed to the .out file (see below). The plot is saved to a .pdf file, data_plot.pdf. The workspace is saved to a .mat file, data.mat.

MATLAB.out
                            < M A T L A B (R) >
                  Copyright 1984-2012 The MathWorks, Inc.
                    R2012b (8.0.0.783) 64-bit (glnxa64)
                              August 22, 2012
    ----------------------------------------------------
	Your MATLAB license will expire in 52 days.
	Please contact your system administrator or
	MathWorks to renew this license.
    ----------------------------------------------------
 
To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.
 
integral_value =
  666.6667

 

Parallel MATLAB Job

The following describes how to use the MATLAB Parallel Computing Toolbox on the HCC supercomputers.

Creating the M-Scripts

Under the $WORK directory, create a subdirectory named parallel_matlab_dir. Save all the relevant m-scripts to parallel_matlab_dir. Here a m-script paralle_main.m is created to make use of the MATLAB Parallel Computing Toolbox on a HCC supercomputer. Note that the system variable SLURM_TASKS_PER_NODE need to be invoked in the m-script.

parallel_main.m
%This MATALB program ("parallel_main.m") calls for a number of MATLAB workers
%according to that specified in the slurm submit scritp ('#SBATCH --ntasks_per_node').
%Each MATLAB worker is asked to print to the screen (MATLAB command) their 
%assigned for-loop index "i". 

number = getenv('SLURM_NTASKS_PER_NODE');
matlabpool(number);
parfor i=1:matlabpool('size')   
   i
end
matlabpool close;

Creating a Submit Script

For parallel MATLAB jobs, two additional lines ( --nodes, --ntasks-per-node) need to be added to the serial-job submit script. Here, the submit script parallel_matlab.submit requests for 5 cores within the same node. Again, the filename of the main program goes to the last line.

parallel_matlab.submit

#!/bin/sh
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=5
#SBATCH --mem-per-cpu=1024
#SBATCH --time=00:05:00
#SBATCH --job-name=MATLAB
#SBATCH --error=MATLAB.%J.err
#SBATCH --output=MATLAB.%J.out

 

module load matlab/r2016a

matlab -nodisplay -r "parallel_main, quit"

 

Submit the Job

The job can be submitted through the command sbatch. The job status can be monitored by entering squeue with the -u option.

$ sbatch parallel_matlab.submit
$ squeue -u <username>

Sample Output

The assigned for-loop index are printed to the screen (.out file). The random order of the printed numbers indicates that each value is generated by a different core. 

MATLAB.out
                            < M A T L A B (R) >
                  Copyright 1984-2012 The MathWorks, Inc.
                    R2012b (8.0.0.783) 64-bit (glnxa64)
                              August 22, 2012
    ----------------------------------------------------
	Your MATLAB license will expire in 51 days.
	Please contact your system administrator or
	MathWorks to renew this license.
    ----------------------------------------------------
 
To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.
 
Starting matlabpool using the 'local' profile ... connected to 5 workers.
ans =
     1

ans =
     4

ans =
     5

ans =
     2

ans =
     3
Sending a stop signal to all the workers ... stopped.
  • No labels