Download MPI: Message Passing Interface for Parallel Computing - Prof. Jingke Li and more Study notes Computer Science in PDF only on Docsity!
MPI
Jingke Li
Portland State University
Jingke Li (Portland State University) CS 415/515 MPI 1 / 35
What is MPI?
MPI = Message Passing Interface
- A message-passing library specification (i.e. not a specific product)
- For parallel computers, clusters, and heterogeneous networks
- Full-featured (total of 125 routines)
- (^) point-to-point and collective communications
- (^) with or without a topology
- (^) user-defined subgroups
- (^) Designed to provide access to advanced parallel hardware for end users, library writers, and tool developers
MPI programming style is generally SPMD — multiple instances of a single program are created and executed.
Features of MPI
- Point-to-Point Communication
- (^) Structured buffers and derived data types, heterogeneity
- (^) Modes: normal (blocking and non-blocking), synchronous, ready (to allow access to fast protocols), buffered
- (^) Collective Communication
- Both built-in and user-defined collective operations
- Large number of data movement routines
- Subgroups defined directly or by topology
- Built-in support for grids and graphs
- (^) General
- Communicators combine context and group for message security
- Profiling hooks allow users to intercept MPI calls to install their own tools
- Thread safety, error control
Jingke Li (Portland State University) CS 415/515 MPI 3 / 35
MPI for Beginners
One need not master all parts of MPI to use it. These six functions allow you to write many programs:
- MPI Init — starting MPI
- MPI Finalize — exiting MPI
- MPI Comm Size — the number of processes
- (^) MPI Comm Rank — the id of this process
- (^) MPI Send — sending a message
- (^) MPI Recv — receiving a message
A Second MPI Program
#include "mpi.h" int main(int argc, char **argv) { int rank, size; MPI Init(&argc, &argv); MPI Comm Rank(MPI COMM WORLD, &rank); MPI Comm Size(MPI COMM WORLD, &size); printf("Hello world! I’m %d of %d"n", rank, size); MPI Finalize(); return 0; }
Jingke Li (Portland State University) CS 415/515 MPI 7 / 35
What is MPI COMM WORLD?
- (^) Communication in MPI takes place with respect to communicators
- (^) MPI COMM WORLD is the default communicator, it is created when MPI is started and contains all MPI processes
- (^) MPI COMM WORLD is very useful — many applications do not need to use any other communicator
- (^) MPI Init() and MPI Finalize() must be called by all processes.
MPI Basic Send/Receive
int MPI Send(void *buf, int count, MPI Datatype type, int dest, int tag, MPI Comm comm) int MPI Recv(void *buf, int count, MPI Datatype type, int source, int tag, MPI Comm comm, MPI Status *status)
- buf — initial address of send/receive buffer
- count, type — number of and type of elements to be sent/received
- dest, source — rank of destination and source processes (a special value for source is MPI ANY SOURCE)
- tag — message tag
- comm — communicator
- status — status of the receive command; its info includes MPI SOURCE, MPI TAG, MPI ERROR, and message length
Jingke Li (Portland State University) CS 415/515 MPI 9 / 35
MPI Error Code
All MPI routines (except MPI Wtime and MPI Wtick) return an error value:
MPI_SUCCESS MPI_ERR_COMM MPI_ERR_COUNT MPI_ERR_TYPE MPI_ERR_TAG MPI_ERR_RANK MPI_ERR_INTERN
A Simple Send/Receive Program (cont.)
else if (rank == dest) { from = MPI ANY SOURCE; tag = MPI ANY TAG; count = 100; MPI Recv(data, count, MPI DOUBLE, from, tag, MPI COMM WORLD, &status); MPI Get count(&status, MPI DOUBLE, &st count); st source = status.MPI SOURCE; st tag = status.MPI TAG; printf("Status info: source = %d, tag = %d, count = %d\n", st source, st tag, st count); printf(" %d received: ", rank); for (i = 0; i < st count; i++) printf("%lf ", data[i]); printf("\n"); } MPI Finalize(); return 0; }
Jingke Li (Portland State University) CS 415/515 MPI 13 / 35
MPI Non-Blocking Send and Receive
int MPI ISend(void *buf, int count, MPI Datatype datatype, int dest, int tag, MPI COMM comm, MPI Request *request) int MPI IRecv(void *buf, int count, MPI Datatype datatype, int source, int tag, MPI COMM comm, MPI Request *request)
Both routines create allocate a request object and return a pointer to it in the request variable. This object is useful for future testing and waiting for the finish of the non-blocking operation.
int MPI Test(MPI Request *request, int *flag, MPI Status *status) int MPI Wait(MPI Request *request, MPI Status *status)
Other Send/Receive Routines
- (^) Combined send/receive: int MPI Sendrecv(void *sbuf, int scount, MPI Datatype stype, int dest, int stag, void *rbuf, int rcount, MPI Datatype rty int source, int rtag, MPI COMM comm, MPI Status *status)
- (^) Synchronous sends: int MPI Ssend(...) int MPI Issend(...)
- Ready sends: int MPI Rsend(...) int MPI Irsend(...)
- Buffered sends: int MPI Bsend(...) int MPI Ibsend(...)
Jingke Li (Portland State University) CS 415/515 MPI 15 / 35
MPI Collective Communications
MPI Collective communications are coordinated among a group of processes, as specified by communicator.
- Message tags are not used
- All collective operations are blocking
- All processes in the communicator group must call the collective operation
Categories:
- One-to-All Broadcast
- All-to-One Reduction
- Scatter and Gather
- Prefix Scan
- All-to-All Broadcast & Reduction
Prefix Scan
int MPI Scan(void* sbuf, void *rbuf, int count, MPI Datatype sdatatype, MPI Op op, MPI Comm comm)
Jingke Li (Portland State University) CS 415/515 MPI 19 / 35
All-Version and V-Version Routines
MPI ALLGATHER MPI ALLREDUCE MPI ALLTOALL
MPI ALLGATHERV MPI GATHERV MPI ALLTOALLV
- (^) All versions deliver results to all participating processes.
- (^) V versions allow the chunks to have different sizes.
- (^) The reduce routines take both built-in and user-defined reduction functions.
A Simple Collective Communication Program
#include int main(int argc, char **argv) { int rank, size, myn, i, N; double *vector, myvec, sum, mysum, total; MPI Init(&argc, &argv); MPI Comm rank(MPI COMM WORLD, &rank); MPI Comm size(MPI COMM WORLD, &size); / In the root process read the vector length, initialize the vector and determine the sub-vector sizes */ if (rank == 0) { printf("Enter the vector length : "); scanf("%d", &N); vector = (double *)malloc(sizeof(double) * N); for (i = 0, sum = 0; i < N; i++) vector[i] = 1.0; myn = N / size; } MPI Bcast(&myn, 1, MPI INT, 0, MPI COMM WORLD);
Jingke Li (Portland State University) CS 415/515 MPI 21 / 35
A Simple Collective Comm Program (cont.)
myvec = (double )malloc(sizeof(double)myn); MPI Scatter(vector, myn, MPI DOUBLE, myvec, myn, MPI DOUBLE, 0, MPI COMM WORLD); /* Find the local sum, then the global sum of the vectors / for (i = 0, mysum = 0; i < myn; i++) mysum += myvec[i]; MPI Allreduce(&mysum, &total, 1, MPI DOUBLE, MPI SUM, MPI COMM WORLD); / Multiply the local part of the vector by the global sum */ for (i = 0; i < myn; i++) myvec[i] *= total; MPI Gather(myvec, myn, MPI DOUBLE, vector, myn, MPI DOUBLE, 0, MPI COMM WORLD); if (rank == 0) for (i = 0; i < N; i++) printf("[%d] %f\n", rank, vector[i]); MPI Finalize(); return 0;
Jingke Li} (Portland State University) CS 415/515 MPI 22 / 35
Example: Writing to a File
int main(int argc, char *argv[]) { int cnt=4, buf[4], i, rank; char fname[10]; MPI File fh; MPI Status status;
MPI Init(&argc,&argv); MPI Comm rank(MPI COMM WORLD, &rank); for (i=0; i
Introduction to LAM
LAM = Local Area Multicomputer
- (^) Public domain implementation of MPI on workstation clusters
- (^) Originally written at the Ohio Supercomputing Center, now hosted at www.lam-mpi.org
- (^) Need to create daemons on each host before any programs can be run
Main Tools:
- Starting/terminating LAM daemons
- Compiling/running MPI programs
- Showing status of processes and messages
Jingke Li (Portland State University) CS 415/515 MPI 27 / 35
Booting LAM
- (^) The user creates a file (e.g. .lamhosts) listing the host machines in the cluster. # a 2-node LAM african chatham Each machine will be given a node identifier (nodeid) starting with 0 for the first listed machine, 1 for the second, etc.
- (^) The lamboot tool actually starts LAM on the specified cluster. (101)% lamboot -v LAM 7.1.2/MPI 2 C++/ROMIO - Indiana University n-1<3671> ssi:boot:base:linear: booting n0 (african) n-1<3671> ssi:boot:base:linear: booting n1 (chatham) n-1<3671> ssi:boot:base:linear: finished lamboot returns to the UNIX shell prompt. LAM does not force a canned environment or a “LAM shell”.
Executing MPI Programs with mpirun
- mpirun N program — run the program on every node
- mpirun 〈hosts〉 program — run on a set of nodes (111)% mpirun -v n0-1 n3 foo 2445 foo running on n0 (o) 361 foo running on n 757 foo running on n
- (^) mpirun -c n program — run n copies of the program (wherever LAM wants to run them; normally based on round-robin over all hosts.) (112)% mpirun -v -c 5 foo 2445 foo running on n0 (o) 361 foo running on n 1103 foo running on n 757 foo running on n 2446 foo running on n0 (o)
Jingke Li (Portland State University) CS 415/515 MPI 31 / 35
Application Schema
An application with multiple programs must be described in an application schema, a file that lists each program and its target node(s).
(121)% cat appfile
1 master, 2 slaves
n0 master n0-1 slave
Then use mpirun to execute the schema:
(122)% mpirun -v appfile 3292 master running on n0 (o) 3296 slave running on n0 (o) 412 slave running on n
Monitoring MPI Applications
- mpitask — Display the status of all MPI processes. (131)% mpitask TASK (G/L) FUNCTION PEER|ROOT TAG COMM COUNT DATATYPE 0/0 master Recv ANY ANY WORLD 1 INT 1 slave 2 slave Process rank 0 is blocked receiving a message consisting of a single integer from any source rank and any message tag, using the MPI COMM WORLD communicator. The other processes are running.
- mpimsg — Display the status of all MPI messages. (132)% mpimsg SRC (G/L) DEST (G/L) TAG COMM COUNT DATATYPE MSG 0/0 1/1 7 WORLD 4 INT n0,# A message sent by process rank 0 to process rank 1 is buffered and waiting to be received. It was sent with tag 7 using the MPI COMM WORLD communicator and contains 4 integers.
Jingke Li (Portland State University) CS 415/515 MPI 33 / 35
Cleaning LAM
The lamclean tool removes all user processes and messages without rebooting.
(141)% lamclean -v killing processes, done sweeping messages, done closing files, done sweeping traces, done
It is typical for users to mpirun a program, lamclean when it finishes, and then mpirun another program. It is not necessary to lamboot to run each user MPI program.