Tutorial on MPI: the Message-Passing Interface
Total Page:16
File Type:pdf, Size:1020Kb
Tutorial on MPI: The Message-Passing Interface Willi am Gropp NA TIO L NA LA B O E R N A N T O O G R R Y A • • U N O I G V E A R C S I I T C H Y O F Mathematics and Computer Science Division Argonne National Lab oratory Argonne, IL 60439 [email protected] 1 Course Outline Background on Parallel Computing Getting Started MPI Basics Intermediate MPI To ols for writing libraries Final comments Thanks to Rusty Lusk for some of the material in this tutorial. This tutorial maybe used in conjunction with the book \Using MPI" which contains detailed descriptions of the use of the MPI routines. Material that b eings with this symb ol is `advanced' and may b e skipp ed on a rst reading. 2 Background Parallel Computing Communicating with other pro cesses Co op erative op erations One-sided op erations The MPI pro cess 3 Parallel Computing Separate workers or pro cesses Interact by exchanging information 4 Typ es of parallel computing All use di erent data for each worker Data-parallel Same op erations on di erent data. Also called SIMD SPMD Same program, di erent data MIMD Di erent programs, di erent data SPMD and MIMD are essentially the same b ecause any MIMD can be made SPMD SIMD is also equivalent, but in a less practical sense. MPI is primaril y for SPMD/MIMD. HPF is an example of a SIMD interface. 5 Communicating with other pro cesses Data must be exchanged with other workers Co op erative | all parties agree to transfer data One sided | one worker p erforms transfer of data 6 Co op erative op erations Message-passing is an approach that makes the exchange of data co op erative. Data must b oth be explicitl y sent and received. An advantage is that any change in the receiver's memory is made with the receiver's participation. Process 0 Process 1 SEND( data ) RECV( data ) 7 One-sided op erations One-sided op erations between parallel pro cesses include remote memory reads and writes. An advantage is that data can be accessed without waiting for another pro cess Process 0 Process 1 PUT( data ) (Memory) Process 0 Process 1 (Memory) GET( data ) 8 Class Example Take a pad of pap er. Algorithm: Initialize with the numb er of neighb ors you have Compute average of your neighb or's values and subtract from your value. Make that your new value. Rep eat until done Questions 1. Howdoyou get values from your neighb ors? 2. Which step or iteration do they corresp ond to? Do you know? Do you care? 3. Howdoyou decide when you are done? 9 Hardware mo dels The previous example illustrates the hardware mo dels by how data is exchanged among workers. Distributed memory e.g., Paragon, IBM SPx, workstation network Shared memory e.g., SGI Power Challenge, Cray T3D Either may be used with SIMD or MIMD software mo dels. All memory is distributed. 10 What is MPI? A message-passing library sp eci cation { message-passing mo del { not a compiler sp eci cation { not a sp eci c pro duct Forparallel computers, clusters, and heterogeneous networks Full-featured Designed to p ermit unleash? the development of parallel software libraries Designed to provide access to advanced parallel hardware for { end users { library writers { to ol develop ers 11 Motivation for a New Design Message Passing now mature as programming paradigm { well understo o d { ecient match to hardware { many applications Vendor systems not p ortable Portable systems are mostly research projects { incomplete { lack vendor supp ort { not at most ecient level 12 Motivation cont. Few systems o er the full range of desired features. mo dularity for libraries access to p eak p erformance portability heterogeneity subgroups top ologies p erformance measurement to ols 13 The MPI Pro cess Began at Williamsburg Workshop in April, 1992 Organized at Sup ercomputing '92 Novemb er Followed HPF format and pro cess Met every six weeks fortwodays Extensive, op en email discussions Drafts, readings, votes Pre- nal draft distributed at Sup ercomputing '93 Two-month public comment p erio d Final version of draft in May, 1994 Widely availablenow on the Web, ftp sites, netlib http://www.mcs.anl.gov/mpi/index.html Public implementations available Vendor implementations coming so on 14 Who Designed MPI? Broad participation Vendors { IBM, Intel, TMC, Meiko, Cray, Convex, Ncub e Library writers { PVM, p4, Zip co de, TCGMSG, Chameleon, Express, Linda Application sp ecialists and consultants Companies Lab oratories Universities ARCO ANL UC Santa Barbara Convex GMD Syracuse U Cray Res LANL Michigan State U IBM LLNL Oregon Grad Inst Intel NOAA U of New Mexico KAI NSF Miss. State U. Meiko ORNL U of Southampton NAG PNL U of Colorado nCUBE Sandia Yale U ParaSoft SDSC UofTennessee Shell SRC UofMaryland TMC Western Mich U U of Edinburgh Cornell U. Rice U. U of San Francisco 15 Features of MPI General { Communicators combine context and group for message security { Thread safety Point-to-p oint communication { Structured bu ers and derived datatyp es, heterogeneity { Mo des: normal blo cking and non-blo cking, synchronous, ready to allow access to fast proto cols, bu ered Collective { Both built-in and user-de ned collective op erations { Large numb er of data movement routines { Subgroups de ned directly orby top ology 16 Features of MPI cont. Application-oriented pro cess top ologies { Built-in supp ort for grids and graphs uses groups Pro ling { Ho oks allow users to intercept MPI calls to install their own to ols Environmental { inquiry { error control 17 Features not in MPI Non-message-passing concepts not included: { pro cess management { remote memory transfers { active messages { threads { virtual shared memory MPI do es not address these issues, but has tried to remain compatible with these ideas e.g. thread safety as a goal, intercommunicators 18 Is MPI Large or Small? MPI is large 125 functions { MPI's extensive functionality requires many functions { Numb er of functions not necessarily a measure of complexity MPI is small 6 functions { Many parallel programs can b e written with just 6 basic functions. MPI is just right { One can access exibility when it is required. { One need not master all parts of MPI to use it. 19 Where to use MPI? You need a portable parallel program You are writing a parallel library You have irregular or dynamic data relationships that do not t a data parallel mo del Where not to use MPI: You can use HPF or a parallel Fortran 90 You don't need parallelism at all You can use librari es which may be written in MPI 20 Why learn MPI? Portable Expressive Good way to learn ab out subtle issues in parallel computing 21 Getting started Writing MPI programs Compiling and linking Running MPI programs More information { Using MPI by William Gropp, Ewing Lusk, and Anthony Skjellum, { The LAM companion to \Using MPI..." by Zdzislaw Meglicki { Designing and Building Parallel Programs by Ian Foster. { ATutorial/User's Guide for MPI byPeter Pacheco ftp://math.usfca.edu/pub/MPI/mpi.guide.ps { The MPI standard and other information is available at http://www.mcs.anl.gov/mpi. Also the source for several implementations. 22 Writing MPI programs include "mpi.h" include <stdio.h> int main argc, argv int argc; char **argv; { MPI_Init &argc, &argv ; printf "Hello world\n" ; MPI_Finalize; return 0; } 23 Commentary include "mpi.h" provides basic MPI de nitions and typ es MPI_Init starts MPI MPI_Finalize exits MPI Note that all non-MPI routines are lo cal; thus the printf run on each pro cess 24 Compiling and linking For simple programs, sp ecial compiler commands can be used. For large projects, it is b est to use a standard Make le. The MPICH implementation provides the commands mpicc and mpif77 as well as `Makefile' examples in `/usr/local/mpi/examples/Makefile.in' 25 Sp ecial compilation commands The commands mpicc -o first first.c mpif77 -o firstf firstf.f may b e used to build simple programs when using MPICH. These provide sp ecial options that exploit the pro ling features of MPI -mpilog Generate log les of MPI calls -mpitrace Trace execution of MPI calls -mpianim Real-time animation of MPI not available on all systems There are sp eci c to the MPICH implementation; other implementations mayprovide similar commands e.g., mpcc and mpxlf on IBM SP2. 26 Using Make les The le `Makefile.in' is a template Make le. The program script `mpireconfig' translates this to a Make le for a particular system. This allows you to use the same Make le for a network of workstations and a massively parallel computer, even when they use di erent compilers, librari es, and linker options. mpireconfig Makefile Note that you must have `mpireconfig' in your PATH. 27 Sample Make le.in User configur ab le options ARCH = @ARCH@ COMM = @COMM@ INSTALL_D IR = @INSTAL L_ DI R@ CC = @CC@ F77 = @F77@ CLINKER = @CLINKE R@ FLINKER = @FLINKE R@ OPTFLAGS = @OPTFLA GS @ LIB_PATH = -L$INS TA LL _D IR / li b/ $ AR CH /$ C OM M FLIB_PATH = @FLIB_PAT H_ LE AD ER @$ I NS TA LL _D IR / li b/ $ ARC H /$ C OM M LIB_LIST = @LIB_LI ST @ INCLUDE_D IR = @INCLUD E_ PA TH @ -I$INST AL L_D IR / in cl ud e End User configur ab le options 28 Sample Make le.in con't CFLAGS = @CFLAGS @ $OPTFLA GS $INCLUD E_D IR -DMPI_$ AR CH FFLAGS = @FFLAGS@ $INCLU DE _D IR $OPTFLAG S LIBS = $LIB_PA TH $LIB_LI ST FLIBS = $FLIB_ PA TH $LIB_LI ST EXECS = hello default: hello all: $EXECS hello: hello.o $INSTAL L_ DI R /i nc lu de /m pi.