matlab mpi
TRANSCRIPT
Matlab MPI
Shuxia Zhang
Supercomputing Institute
e-mail: [email protected]
Tel: 612-624-8858 (direct)
612-626-0802(help)
Outline:
IntroductionMatlabMPI functions/syntaxMatlab functionsHow to write MatlabMPI programHow to run MatlabMPI jobsHands-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 GBon both SMP and DMP systems
-- implements the widely used MPI "look and feel" on topof standard Matlab file i/o, resulting in a "pure" Matlabimplementation 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 processorsuses 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_WORLDfunction 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 ToolboxCommunications ToolboxControl System ToolboxCurve Fitting ToolboxData Acquisition ToolboxDatabase ToolboxDatafeed ToolboxExcel LinkFilter Design ToolboxFinancial ToolboxFinancial Derivatives ToolboxFinancial Time Series ToolboxFixed-Income ToolboxFuzzy Logic ToolboxGARCH ToolboxGenetic Algorithm ToolboxImage Acquisition ToolboxImage Processing ToolboxInstrument Control Toolbox
LMI Control ToolboxMapping ToolboxModel-Based Calibration ToolboxModel Predictive Control ToolboxMu-Analysis and Synthesis ToolboxNeural Network ToolboxOptimization ToolboxPartial Differential Equation (PDE) ToolboxRobust Control ToolboxSignal Processing ToolboxSpline ToolboxStatistics ToolboxSymbolic Math ToolboxSystem Identification ToolboxVirtual Reality ToolboxWavelet 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 writeMatlabMPI 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
Examples“Hello World”
addpath /usr/local/MatlabMPI/srcMPI_Init;comm = MPI_COMM_WORLD; % Get size and ranksize = 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
ExamplesSend - Recv
MatlabMPI program using 2 processors
source =1;dest = 0; % Set who is source and who is destinationtag = 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);
endif (my_rank == dest)
A=ones(10,40);B = MPI_Recv( source, tag, comm );C = A*Bsave out C -ascii
endelse
disp(‘No data sent’);end MPI_Finalize;disp('SUCCESS');quit
% Matlab ProgramA=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.
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/usr/local/MatlabMPI/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/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 jobinteractively?
(1) Login to Netfinity:
ssh -l temp01 nf.msi.umn.edursh 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 Matlabmatlab –nojvm –nodisplay
How to run matlabMPI jobinteractively?
>> 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 jobacross 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 ProgramA=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:00pmThank you for your cooperation!