You are on page 1of 25

Matlab MPI

Shuxia Zhang
Supercomputing Institute
Tel: 612-624-8858 (direct)

MatlabMPI functions/syntax
Matlab functions
How to write MatlabMPI program
How to run MatlabMPI jobs

Message Passing Interface (MPI)

-- C, C++ and Fortran standard functions defined
for communication.
-- widely used in large scale of computations.

Matlab – High level intuitive language for numerical computations,

algorithm development, simulation, data reduction, testing
and evaluation.
-- needs an efficient mechanism for running Matlab programs
in parallel to achieve high performance
-- Memory limitation of 32-bit memory addressing

-- developed at Lincoln Laboratory, MIT
-- implements a subset of MPI
-- allows any Matlab programs to run in parallel,
speeding up calculations and addressing memory > 2 GB
on both SMP and DMP systems
-- implements the widely used MPI "look and feel" on top
of standard Matlab file i/o, resulting in a "pure" Matlab
implementation of MPI
-- is exceedingly small (~300 lines of code).
-- runs on any combination of computers that Matlab supports.
- Matlab license
A single Matlab license on SMP
Floating license on DMP
- File system visible to all processors
uses file I/O for communication,
there must be a directory that is visible to every machine
where the program is launched. The directory can be
changed within the MatlabMPI program.
- Data-transfer or communication strongly depends on the network,
which can be slow .
MatlabMPI is suitable for coarse granularity applications
MPI functions

function MPI_Init()
% Called at the beginning of an MPI program.
function size = MPI_Comm_size(comm)
% MPI_Comm_size - returns the number of processors
% in the communicator
% comm is an MPI Communicator, typically
% a copy of MPI_COMM_WORLD
function rank = MPI_Comm_rank(comm)
% MPI_Comm_rank - returns the rank of the current processor.
% rank = MPI_Comm_rank(comm)
MPI functions

function MPI_Send( dest, tag, comm, varargin )

% MPI_Send - Sends variables to dest.
% Send message containing variables to dest with a given tag
% dest can be an integer from 0 to comm_size-1
% tag can be any integer
% comm is an MPI Communicator
% MPI_Send( dest, tag, comm, sdata1, sdata2, … )
MPI functions

function varargout = MPI_Recv( source, tag, comm )

% MPI_Recv - Receives message from source.

% [var1, var2, ...] = MPI_Recv( source, tag, comm )
% Receives message from source with a given tag
% and returns the variables in the message.
% Source can be an integer from 0 to comm_size-1
% tag can be any integer
MPI functions

function MPI_Abort()
% MPI_Abort - Aborts any currently running MatlabMPI sessions.
% It will abort any currently running MatlabMPI sessions.
% by looking for leftover Matlab jobs and killing them.
% Cannot be used after MatMPI_Delete_all.

function MPI_Finalize()
% MPI_Finalize
% Called at the end of a MatlabMPI program.
MPI functions
MPI_Run( m_file, n_proc, machines )

% MPI_Run - Run m_file on multiple processors.

% Runs n_proc copies of m_file on machines,
% where machines = {}, run on a local processor.
% machines = {'machine1' 'machine2'}, run on a
% multi processors: nb01, nb02, …, nb12

% MatMPI_Delete_all - Deletes leftover MatlabMPI files

More MPI related functions and example can be found:

Matlab functions

Bioinformatics Toolbox
Communications Toolbox LMI Control Toolbox
Control System Toolbox Mapping Toolbox
Curve Fitting Toolbox Model-Based Calibration Toolbox
Data Acquisition Toolbox Model Predictive Control Toolbox
Database Toolbox Mu-Analysis and Synthesis
Datafeed Toolbox Toolbox
Excel Link Neural Network Toolbox
Filter Design Toolbox Optimization Toolbox
Financial Toolbox Partial Differential Equation
Financial Derivatives Toolbox (PDE) Toolbox
Financial Time Series Toolbox Robust Control Toolbox
Fixed-Income Toolbox Signal Processing Toolbox
Fuzzy Logic Toolbox Spline Toolbox
GARCH Toolbox Statistics Toolbox
Genetic Algorithm Toolbox Symbolic Math Toolbox
Image Acquisition Toolbox System Identification Toolbox
Image Processing Toolbox Virtual Reality Toolbox
Instrument Control Toolbox Wavelet Toolbox
Matlab functions

EVAL: execute string with MATLAB expression

DISP(X) displays the array, without printing the array name..

If X is a string, the text is displayed.


eval( MPI_Run('basic',2,{'machine1' 'machine2'}))

disp(['Hello World from rank: ',num2str(my_rank)]);

For rank 0, the output appears on the screen.

For rank 1, 2, …, the output goes to a file.
How to write
MatlabMPI code?
(1) Setup the path
addpath /usr/local/MatlabMPI/src
(2) Initialize MPI.
(3) Create communicator.
(4) Get size and rank.
size = MPI_Comm_size(comm);
my_rank = MPI_Comm_rank(comm);
(5) Do computation using matlab
(6) Do communication using MPI for exchanging data
(7) Finalize MPI application
“Hello World”
addpath /usr/local/MatlabMPI/src
% Get size and rank
size = MPI_Comm_size(comm);
my_rank = MPI_Comm_rank(comm);
disp(['Hello World from rank: ',num2str(my_rank)]);
% Finalize Matlab MPI.

Save it into a file named ‘hello.m’ and run it

Send - Recv
MatlabMPI program using 2 processors
source =1; % Matlab Program
dest = 0; % Set who is source and who is destination
tag = 1; % Create a unique tag id for this message
if(comm_size == 2) % Make sure exactly two processors B=rand(40);
if (my_rank == source) C=A * B;
D=rand(40); save out C -ascii
MPI_Send( dest, tag, comm, D);
if (my_rank == dest)
B = MPI_Recv( source, tag, comm );
C = A*B
save out C -ascii
disp(‘No data sent’);
More Examples

basi.m Simple MatlabMPI program that sends data from processor 1

to processor 0.
multi_basic.m Simple MatlabMPI program that sends data from processor 1
to processor 0 a few times.
probe.m Simple MatlabMPI program that demonstrates the using
MPI_Probe to check for incoming messages.
broadcast.m Tests MatlabMPI broadcast command.
basic_app.m Examples of the most common usages of MatlabMPI.
More Examples

basic_app2.m Examples of the most common usages of MatlabMPI.

basic_app3.m Examples of the most common usages of MatlabMPI.
basic_app4.m Examples of the most common usages of MatlabMPI.
blurimage.m MatlabMPI test parallel image processing application.
speedtest.m Times MatlabMPI for a variety of messages.
More Examples

synch_start.m Function for synchronizing

starts.machines.m Example script for creating a machine
unit_test.m Wrapper for using an example as a unit
unit_test_all.m Calls all of the examples as way of
testing the entire library.
unit_test_mcc.m Wrapper for using an example as a mcc
unit test.
How to run matlabMPI job

(1) Login to Netfinity:

ssh -l temp01
rsh nb01, rsh nb02, …, rsh nb12
(2) Load the module MatlabMPI
module load matlabMPI

%For across node runs, please add it to the .cshrc file

(3) Start Matlab

matlab –nojvm –nodisplay
How to run matlabMPI job

>> addpath /usr/local/MatlabMPI/src

>> help MatlabMPI
>> machines = {};
>> eval( MPI_Run(‘Mycode', 2,machines) )
% Mycode is a matlabMPI application code, which
% needs to exist in the same working directory
>> MatMPI_Delete_all;
% Deletes leftover MatlabMPI files.
>> quit
How to run matlabMPI job
across nodes?

>> addpath /usr/local/MatlabMPI/src

>> machines = {‘nb0x’ ‘nb02’ ‘nb03’};
% nb0x is the login node. For each user, nb0x may be
% different, e.g., nb05 for you, nb08 for him.
>> eval( MPI_Run(‘hello', 3,machines) )
% hello.m is a matlabMPI application code, which
% exists in the same working directory
>> MatMPI_Delete_all;
% Deletes leftover MatlabMPI files.
>> quit
“Hello World”
To run the job on the login node, type:
>> addpath /usr/local/MatlabMPI/src
>> eval(MPI_Run(‘hello’, 4, {}))

To run the job across nodes, type

>> eval(MPI_Run(‘hello’, 4, {‘nb0x’ ‘nb03’ ‘nb07’ ‘nb08’ }))
where nb0x is the node you login, e.g., nb02

It will create output in the directory MatMPI:

Hands on
(1) Write a MatlabMPI program to print ‘hello world’
and run it using 4 processor on the login node ;
(2) Run the above case on different nodes;
(3) Write a MatlabMPI program to solve the following problem
using 2 processes
% Matlab Program
C=A * B;
save out C -ascii
(4) Copy any example from /usr/local/MatlabMPI/examples,
make modifications if necessary and run the job.
This room is reserved for a tutorial
On June. 1 from 1:00pm to 4:00pm
Thank you for your cooperation!