Transcript Lecture 1: Overview - City University of New York
Lecture 7 The Message Passing Interface (MPI)
Parallel Computing Fall 2008
1
The Message Passing Interface
Introduction
The Message-Passing Interface (MPI)is an attempt to create a standard to allow tasks executing on multiple processors to communicate through some standardized communication primitives.
It defines a standard library for message passing that one can use to develop message-passing program using C or Fortran. The MPI standard define both the syntax and the semantics of these functional interface to message passing. MPI comes intro a variety of flavors, freely available such as LAM-MPI and MPIch, and also commercial versions such as Critical Software’s WMPI. It supporst message-passing on a variety of platforms from Linux-based or Windows-based PC to supercomputer and multiprocessor systems.
After the introduction of MPI whose functionality includes a set of 125 functions, a revision of the standard took place that added C++ support, external memory accessibility and also Remote Memory Access (similar to BSP’s put and get capability)to the standard. The resulting standard is known as MPI-2 and has grown to almost 241 functions.
2
The Message Passing Interface
A minimum set
A minimum set of MPI functions is described below. MPI functions use the prefix MPI and after the prefix the remaining keyword start with a capital letter.
A brief explanation of the primitives can be found on the textbook (beginning page 242). A more elaborate presentation is available in the optional book.
Function Class Initialization and Termination Abnormal Stop Process Control Synchronization DRMA Message Passing Standard
MPI MPI MPI MPI MPI MPI MPI MPI-2 MPI-2 MPI-2 MPI-2 MPI MPI
Function
MPI_Init MPI_Finalize MPI_Abort MPI_Comm_size MPI_Comm_rank MPI_Wtime MPI_Barrier MPI_Win_create MPI_Win_free MPI_Put MPI_Get MPI_Send MPI_Recv
Operation
Start of SPMD code End of SPMD code One process halts all Number of processes Identifier of Calling Process Local (wall-clock) time Global Synchronization Make memory info global Undo global effect Copy into remote memory Copy from remote memory Send message to remote proc.
Receive mess. from remote proc.
3
General MPI program
#include
MPI and MPI-2
Initialization and Termination
#include
Definitions are available in
5
MPI and MPI-2
Abort
int MPI_Abort(MPI_Comm comm, int errcode); Note that MPI_Abort aborts an MPI program cleanly. The first one is a communicator and second argument is an integer error code. A communicator is a collection of processes that can send messages to each other. A default communicator is MPI_COMM_WORLD, which consists of all the processes running when program execution begins.
6
MPI and MPI-2
Communicators; Process Control
Under MPI, a communication domain is a set of processors that are allowed to communicate with each other. Information about such a domain is stored in a communicator that uniquely identify the processors that participate in a communication operation.
A default communication domain is all the processors of a parallel execution; it is called MPI COMM WORLD. By using multiple communicators between possibly overlapping groups of processors we make sure that messages are not interfering with each other.
#include
7
MPI and MPI-2 Communicators; Process Control: Example
A hello world! Program in MPI is the following one.
#include
MPI
A LAM (Local Area Multicomputer)
One can compile this MPI program as follows where instead of directly issuing the local C compiler, we call the LAMMPI frontend that handles all relevant work for us. Compilation is no more complicated than the compilation of the corresponding sequential code. LAM MPI is already preinstalled. There is nothing for you to install on our system.
You compile code as follows.
% mpicc hello.c -o hello //no optimizations chosen If optimization options are required, % mpicc -O3 hello.c -o hello // -O3 optimization -O is - Oh not - zero!
In LAM MPI we can define a cluster. Just define a file of any name eg lamhosts. The first name is assigned a processor id 0, the second 1, and so on. If one machine has two CPUs, you can enter it twice in this file.
pcc16 pcc13 pcc14 Pcc15 A LAM is initiated by issuing the command % lamboot -v lamhosts A LAM is terminated by issuing the command % wipe -v lamhosts 9
MPI
LAM MPI: How to run things on a Linux cluster
A parallel program under LAM MPI can then be executed by % mpirun -np 4 ./hello The parameter to np is the number of processes that will be initiated and will be assigned one-to-one to the machines listed in lamhosts.
Hello World from process 3 of total 4 Hello World from process 1 of total 4 Hello World from process 0 of total 4 Hello World from process 2 of total 4 The order of printing can be arbitrary on the standard output.
10
Submit job to Grid Engine
(1)- created mpi program, e.g. example.c.
(2)- compiled my program, using mpicc O2 example.c -o example (3)- type command of vi submit (4)- i put this into vi submit #!/bin/bash #$ -S /bin/sh #$ -N example #$ -q default #$ -pe lammpi 4 #$ -cwd mpirun C ./example (5)- then ran chmod a+x submit (6)- then ran qsub submit (7)- ran qstat to check the status of your program 11
MPI Message-Passing primitives
#include
dtyp - datatype of each send/receive buffer element (handle) dest,src - rank of destination/source (integer) Wild-card: MPI_ANY_SOURCE for recv only. No wildcard for dest.
tag - message tag (integer). Range 0...32767.
Wild-card: MPI_ANY_TAG for recv only; send must specify tag.
comm - communicator (handle) stat - status object (Status), which can be the MPI constant; it returns the source and tag of the message that was acctually received.
MPI_STATUS_IGNORE if the return status is not desired Struct of status: status->MPI_SOURCE, status->MPI_TAG, status->MPI_ERROR 12
data type correspondence between MPI and C
MPI_CHAR --> signed char , MPI_SHORT --> signed short int , MPI_INT --> signed int MPI_LONG --> signed long int , MPI_UNSIGNED_CHAR --> unsigned char , MPI_UNSIGNED_SHORT --> unsigned short int , MPI_UNSIGNED --> unsigned int MPI_UNSIGNED_LONG --> unsigned long int , MPI_FLOAT --> float , MPI_DOUBLE --> double, MPI_LONG_DOUBLE --> long double, MPI_BYTE MPI_PACKED 13
MPI Message-Passing primitives
The MPI Send and MPI Recv functions are blocking, that is they do not return unless it is safe to modify or use the contents of the send/receive buffer. MPI also provides for non-blocking send and receive primitives. These are MPI Isend and MPI Irecv, where the I stands for Immediate. These functions allow a process to post that it wants to send to or receive from another process, and then allow the process to call a function (eg. MPI Wait to complete the send-receive pair. Non-blocking send-receives allow for the overlapping of computation/communication. Thus MPI Wait plays the role of synchronizer: the send/receive are only advisories and communication is only effected at the MPI Wait.
14
MPI
Message-Passing primitives: tag
A tag is simply an integer argument that is passed to a communication function and that can be used to uniquely identify a message. For example, in MPI if process A sends a message to process B, then in order for B to receive the message, the tag used in A's call to MPI_Send must be the same as the tag used in B's called to MPI_Recv. Thus, if the characteristics of two messages sent by A to B are identical (i.e., same count and datatype), then A and B can distinguish between the two by using different tags. For example, suppose A is sending two floats, x and y, to B. Then the processes can be sure that the values are received correctly, regardless of the order in which A sends and B receives, provided different tags are used: /* Assume system provides some buffering */ if (my_rank == A) { tag = 0; MPI_Send(&x, 1, MPI_FLOAT, B, tag, MPI_COMM_WORLD); . . . tag = 1; MPI_Send(&y, 1, MPI_FLOAT, B, tag, MPI_COMM_WORLD); } else if (my_rank == B) { tag = 1; MPI_Recv(&y, 1, MPI_FLOAT, A, tag, MPI_COMM_WORLD, &status); . . . tag = 0; MPI_Recv(&x, 1, MPI_FLOAT, A, tag, MPI_COMM_WORLD, &status); } 15
MPI
Message-Passing primitives: Communicators
Now if one message from process A to process B is being sent by the library, and another, with identical characteristics, is being sent by the user's code, unless the library developer insists that user programs refrain from using certain tag values, this approach cannot be made to work. Clearly, partitioning the set of possible tags is at best an inconvenience: if one wishes to modify an existing user code so that it can use a library that partitions tags, each message passing function in the entire user code must be checked. The solution that was ultimately decided on was the communicator. Formally, a communicator is a pair of objects: the first is a ordered collection of processes, and the second is a communicator, or its functions can create their own unique confused. group context, use the same communicator argument. Thus, a library can either require that its functions be passed a unique library-specific communicator. In either case, it is straightforward for the library or which can be viewed as a unique, system-defined tag. Every communication function in MPI takes a communicator argument, and a communication can succeed only if all the processes participating in the communication designer and the user to make certain that their messages are not 16
For example, suppose now that the user's code is sending a float, x, from process A to process B, while the library is sending a float, y, from A to B: /* Assume system provides some buffering */ void User_function(int my_rank, float* x) { MPI_Status status; if (my_rank == A) { /* MPI_COMM_WORLD is pre-defined in MPI */ MPI_Send(x, 1, MPI_FLOAT, B, 0, MPI_COMM_WORLD); } else if (my_rank == B) { MPI_Recv(x, 1, MPI_FLOAT, A, 0, MPI_COMM_WORLD, &status); } . . . } void Library_function(float* y) { MPI_Comm library_comm; MPI_Status status; int my_rank; /* Create a communicator with the same group */ /* as MPI_COMM_WORLD, but a different context */ MPI_Comm_dup(MPI_COMM_WORLD, &library_comm); /* Get process rank in new communicator */ MPI_Comm_rank(library_comm, &my_rank); if (my_rank == A) MPI_Send(y, 1, MPI_FLOAT, B, 0, library_comm); else if (my_rank == B) { MPI_Recv(y, 1, MPI_FLOAT, A, 0, library_comm, &status); } . . . } int main(int argc, char* argv[]) { . . . if (my_rank == A) { . . .
User_function(A, &x); . . . Library_function(&y); } else if (my_rank == B) { Library_function(&y); User_function(B, &x); } . . . } 17
MPI
Message-Passing primitives: User-defined datatypes
The second main innovation in MPI, user-defined datatypes, allows programmers to exploit this power, and as a consequence, to create messages consisting of logically unified sets of data rather than only physically contiguous blocks of data. Loosely, an MPI datatype is a sequence of distributed from a single message. MPI_Datatype mat_entry_mpi_t; displacements in memory together with a collection of basic datatypes (e.g., int, float, double, and char). Thus, an MPI-datatype specifies the layout in memory of data to be collected into a single message or data to be For example, suppose we specify a sparse matrix entry with the following definition. typedef struct { double entry; int row, col; } mat_entry_t; MPI provides functions for creating a variable that stores the layout in memory of a variable of type mat_entry_t. One does this by first defining an MPI datatype to be used in communication functions, and then calling various MPI functions to initialize mat_entry_mpi_t so that it contains the required layout. Then, if we define mat_entry_t x; we can send x by simply calling MPI_Send(&x, 1, mat_entry_mpi_t, dest, tag, comm); and we can receive x with a similar call to MPI_Recv. 18
MPI:
An example with the blocking operations
#include
// Parallel sum of 1 , 2 , 3, ... , N int main(int argc,char **argv){ int pid,nprocs,i,j; int sum, start, end, total; MPI_Status status; MPI_Init(argc,argv); MPI_Comm_size(MPI_COMM_WORLD,&nprocs); MPI_Comm_rank(MPI_COMM_WORLD,&pid ); sum = 0; total = 0; start = (N/nprocs)*pid +1 ; // Each processor end =(N/nprocs)*(pid+1); for(i=start;i<=end;i++) sum += i; if (pid != 0 ) { MPI_Send(&sum,1,MPI_INT,0,1,MPI_COMM_WORLD); } else { for (j=1;j /* Non-Blocking send and receive */ int MPI_Isend(void *buf, int count, MPI_Datatype dtype, int dest, int tag, MPI_Comm comm, MPI_Request *req); int MPI_Irecv(void *buf, int count, MPI_Datatype dtype, int src, int tag, MPI_Comm comm, MPI_Request *req); int MPI_Wait(MPI_Request *preq, MPI_Status *stat); #include "mpi.h" int MPI_Wait ( MPI_Request *request, MPI_Status *status) Waits for an MPI send or receive to complete Input Parameter request (handle) Output Parameter status object (Status) . May be MPI_STATUS_IGNORE. 20 #include // Parallel sum of 1 , 2 , 3, ... , N int main(int argc,char **argv){ int pid,nprocs,i,j; int sum, start, end, total; MPI_Status status; MPI_Request request; MPI_Init(argc,argv); MPI_Comm_size(MPI_COMM_WORLD,&nprocs); MPI_Comm_rank(MPI_COMM_WORLD,&pid ); sum = 0; total = 0; start = (N/nprocs)*pid +1 ; // Each processor end = (N/nprocs)*(pid+1); for(i=start;i<=end;i++) sum += i; if (pid != 0 ) { // MPI_Send(&sum,1,MPI_INT,0,1,MPI_COMM_WORLD); MPI_Isend(&sum,1,MPI_INT,0,1,MPI_COMM_WORLD,&request); MPI_Wait(&request,&status); } else { for (j=1;j 21 One simple collective operations: int MPI_Bcast(void * message, int count, MPI_Datatype datatype, int root, MPI_Comm comm) The routine MPI_Bcast sends data from one process to all others 22 Process 0 Data Present Write data to all processes Data written, unblock Process 1 Process 2 Process 3 23 Simple Program that Demonstrates MPI_Bcast: #include MPI_Bcast(&k,1,MPI_INT,0,MPI_COMM_WORLD); for(p=0; p 25 #include If you want to give the time since the beginning of the program under MPI, you may need to do something like the fragment below. double t1, mpi_basetime; MPI_Init(&argc,&argv), mpi_basetime = MPI_Wtime(); t1= (MPI_Wtime()-mpi_basetime); 26 #include MPI Barrier is just a barrier synchronization. No side effect other than the obvious is effected. 27 The RMA (Remote Memory Access)facility allowed by LAM MPI according the MPI-2 implementation is effected by MPI Put and MPI Get. There are multiple ways one can use such operations under LAM MPI. We mention only one. #include A get has a similar syntax. #include 29 MPI-2 requires that RMA requests be registered through window creation and termination operations. #include base - initial address of window (choice) size - size of window in bytes (nonnegative integer) disp_unit - local unit size for displacements, in bytes (positive integer) info - info argument (handle) comm - communicator (handle) newwin - window object returned by the call (handle) assertion - A suggestion to the communication algorithm; use 0 if you don’w know what values are acceptable. Operation MPI_Win_free frees the window object newwin and returns a null handle (equal to MPI WIN NULL) . The MPI call MPI_Win_fence(assertion, newwin) synchronizes RMA calls on newwin. The call is collective on the group of newwin. All RMA operations on newwin originating at a given process and started before the fence call will complete at that process before the fence call returns. They will be completed at their target before the fence call returns at the target. RMA operations on newwin started by a process after the fence call returns will access their target window only after MPI Win fence has been called by the target process. Calls to MPI Win fence should both precede and follow calls to put, get or accumulate that are synchronized with fence calls. MPI Win fence statements must surround an MPI-2 RMA put or get operation. 30 #include MPI_Win_create(&y,sizeof(int),1,MPI_INFO_NULL,MPI_COMM_WORLD,&win); MPI_Win_fence(0,win); if (pid == 0) { MPI_Put(&x,sizeof(int),MPI_CHAR,5,0,sizeof(int),MPI_CHAR,win); } MPI_Win_fence(0,win); MPI_Win_free(0,&win); 31 MPI_Put ***************************************** char *des,*src; int dp,off,size,K; MPI_Win_create(des,(K),1,MPI_INFO_NULL,MPI_COMM_WORLD,&win); MPI_Win_fence(0,win); if (pid == 0) { MPI_Put(src,size,MPI_CHAR,dp,off,size,MPI_CHAR,win); } MPI_Win_fence(0,win); MPI_Win_free(0,&win); MPI Get ***************************************** MPI_Win_create((src),(K),1,MPI_INFO_NULL,MPI_COMM_WORLD,&win) MPI_Win_fence(0,win); if (pid == 0) { MPI_Get(des,size,MPI_CHAR,dp,off,size,MPI_CHAR,win); } MPI_Win_fence(0,win); MPI_Win_free(0,&win); 32Non-Blocking send and Receive
MPI: An example with the non blocking operations
MPI Basic Collective Operations
MPI_Bcast
Simple Program that Demonstrates MPI_Bcast:
The Output would look like: Process 0: k= 20 before Process 0: k= 20 after Process 3: k= 10 before Process 3: k= 20 after Process 2: k= 10 before Process 2: k= 20 after Process 1: k= 10 before Process 1: k= 20 after
MPI Timing Control
MPI Barrier Synchronization
MPI-2 in LAM MPI Remote Memory Access: MPI Put
MPI-2 in LAM MPI Remote Memory Access: MPI Get
MPI-2 in LAM MPI Communication Windows
MPI-2 in LAM MPI Communication Windows:Example
MPI-2 in LAM MPI : Put and Get examples