You are on page 1of 25

Matlab MPI

Shuxia Zhang Supercomputing Institute e-mail: szhang@msi.umn.edu help@msi.umn.edu Tel: 612-624-8858 (direct) 612-626-0802(help)

Outline: Introduction MatlabMPI functions/syntax Matlab functions How to write MatlabMPI program How to run MatlabMPI jobs Hands-on

INTRODUCTION
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

INTRODUCTION
MatlabMPI -- 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.

Requirements
- 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 % MatMPI_Delete_all - Deletes leftover MatlabMPI files More MPI related functions and example can be found: /usr/local/MatlabMPI/src

Matlab functions
Bioinformatics Toolbox Communications Toolbox Control System Toolbox Curve Fitting Toolbox Data Acquisition Toolbox Database Toolbox Datafeed Toolbox Excel Link Filter Design Toolbox Financial Toolbox Financial Derivatives Toolbox Financial Time Series Toolbox Fixed-Income Toolbox Fuzzy Logic Toolbox GARCH Toolbox Genetic Algorithm Toolbox Image Acquisition Toolbox Image Processing Toolbox Instrument Control Toolbox LMI Control Toolbox Mapping Toolbox Model-Based Calibration Toolbox Model Predictive Control Toolbox Mu-Analysis and Synthesis Toolbox Neural Network Toolbox Optimization Toolbox Partial Differential Equation (PDE) Toolbox Robust Control Toolbox Signal Processing Toolbox Spline Toolbox Statistics Toolbox Symbolic Math Toolbox System Identification Toolbox Virtual Reality 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. Examples: 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. MPI_Init; (3) Create communicator. comm = MPI_COMM_WORLD; (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

addpath /usr/local/MatlabMPI/src MPI_Init; comm = MPI_COMM_WORLD; % 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. MPI_Finalize; disp('SUCCESS'); quit Save it into a file named hello.m and run it

Examples Hello World

Examples Send - Recv


MatlabMPI program using 2 processors
source =1; 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 if (my_rank == source) D=rand(40); MPI_Send( dest, tag, comm, D); end if (my_rank == dest) A=ones(10,40); B = MPI_Recv( source, tag, comm ); C = A*B save out C -ascii end else disp(No data sent); end MPI_Finalize; disp('SUCCESS'); quit

% Matlab Program A=ones(10,40); B=rand(40); C=A * B; save out C -ascii

More Examples
/usr/local/MatlabMPI/examples

basi.m

Simple MatlabMPI program that sends data from processor 1 to processor 0. to processor 0 a few times.

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

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

More Examples
/usr/local/MatlabMPI/examples synch_start.m Function for synchronizing starts.machines.m Example script for creating a machine description. unit_test.m Wrapper for using an example as a unit test. 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 interactively?

(1) Login to Netfinity:


ssh -l temp01 nf.msi.umn.edu 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 interactively?

>> 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

Examples
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: hello.1.out hello.2.out hello.3.out

Hands on
Exercises:
(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 A=ones(10,40); B=rand(40); C=A * B; save out C -ascii (4) Copy any example from /usr/local/MatlabMPI/examples, make modifications if necessary and run the job.

References: http://www.ll.mit.edu/MatlabMPI/ http://www.msi.umn.edu/tutorial/scicomp/general/MPI/ workshop_MPI/ http://www.mathworks.com/products/ http://www.mpi-forum.org/

This room is reserved for a tutorial On June. 1 from 1:00pm to 4:00pm Thank you for your cooperation!

You might also like