Professional Documents
Culture Documents
William Gropp
Argonne National Laboratory Mathematics and Computer Science Division
Overview
Introduction to MPI
What it is Where it came from Basic MPI communication Some simple example Some simple exercises More advanced MPI communication A non-trivial exercise Looking to the future: some features from MPI-2
end computing (over 329 of top 500; all 1998 Gordon Bell Prize finalists; most highest-performing applications)
is to simplify programming by hiding details of memory locality and management (parallel programming for the masses)
4
Department of Energy
particular process
Collective operations
On arbitrary set of processes
Data distribution
Also managed by programmer
Message passing model doesnt get in the way It doesnt help either
5
University of Chicago Department of Energy
Department of Energy
parallelism System manages memory and (usually) thread scheduling Named variables refer to the same storage
University of Chicago
9
Department of Energy
University of Chicago
What is MPI?
A message-passing library specification
extended message-passing model not a language or compiler specification not a specific implementation or product
For parallel computers, clusters, and heterogeneous networks Full-featured Designed to provide access to advanced parallel hardware for end users, library writers, and tool developers
10
University of Chicago Department of Energy
11
Department of Energy
MPI References
The Standard itself:
at http://www.mpi-forum.org All MPI official releases, in both
University of Chicago
Books on MPI
Using MPI: Portable Parallel Programming with the Message-Passing Interface (2nd edition), by Gropp, Lusk, and Skjellum, MIT Press, 1999. MPI: The Complete Reference - Vol 1 The MPI Core, by Snir, Otto, Huss-Lederman, Walker, and Dongarra, MIT Press, 1998. MPI: The Complete Reference - Vol 2 The MPI Extensions, by Gropp, Huss-Lederman, Lumsdaine, Lusk, Nitzberg, Saphir, and Snir, MIT Press, 1998. Designing and Building Parallel Programs, by Ian Foster, Addison-Wesley, 1995. Parallel Programming with MPI, by Peter Pacheco, Morgan-Kaufmann, 1997.
14
University of Chicago Department of Energy
mpi.h for C mpif.h for Fortran 77 and 90 MPI module for Fortran 90 (optional)
First Program:
Create 4 processes in a simple MPI job Write out process number Write out some variables (illustrate
15
University of Chicago Department of Energy
processes. MPI_Comm_rank reports the rank, a number between 0 and size-1, identifying the calling process
University of Chicago
16
Department of Energy
Hello (C)
#include "mpi.h" #include <stdio.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( "I am %d of %d\n", rank, size ); MPI_Finalize(); return 0; }
17
University of Chicago Department of Energy
Hello (Fortran)
program main include 'mpif.h' integer ierr, rank, size call MPI_INIT( ierr ) call MPI_COMM_RANK( MPI_COMM_WORLD, rank, ierr ) call MPI_COMM_SIZE( MPI_COMM_WORLD, size, ierr ) print *, 'I am ', rank, ' of ', size call MPI_FINALIZE( ierr ) end
18
University of Chicago Department of Energy
Hello (C++)
#include "mpi.h" #include <iostream.h> int main( int argc, char *argv[] ) { int rank, size; MPI::Init( &argc, &argv ); rank = MPI_COMM_WORLD.Get_rank(); size = MPI_COMM_WORLD.Get_size(); cout << "I am " << rank << " of " << size << endl; MPI::Finalize(); return 0; }
19
University of Chicago Department of Energy
of either MPI-1 or MPI-2 output order is undefined (may be interleaved by character, line, or blocks of characters),
A consequence of the requirement that non-MPI statements execute independently
University of Chicago
20
Department of Energy
In general, starting an MPI program is dependent on the implementation of MPI you are using, and might require various scripts, program arguments, and/or environment variables. mpiexec <args> is part of MPI-2, as a recommendation, but not a requirement, for implementors. Many parallel systems use a batch environment to share resources among users
The specific commands to run a program on a parallel system are
22
Department of Energy
A group and context together form a communicator A process is identified by its rank in the group associated with a communicator There is a default communicator whose group contains all initial processes, called MPI_COMM_WORLD
23
University of Chicago Department of Energy
MPI Datatypes
The data in a message to send or receive is described by a triple (address, count, datatype), where An MPI datatype is recursively defined as:
predefined, corresponding to a data type from the
language (e.g., MPI_INT, MPI_DOUBLE) a contiguous array of MPI datatypes a strided block of datatypes an indexed array of blocks of datatypes an arbitrary structure of datatypes
There are MPI functions to construct custom datatypes, in particular ones for subarrays
24
University of Chicago Department of Energy
MPI Tags
Messages are sent with an accompanying user-defined integer tag, to assist the receiving process in identifying the message Messages can be screened at the receiving end by specifying a specific tag, or not screened by specifying MPI_ANY_TAG as the tag in a receive Some non-MPI message-passing systems have called tags message types. MPI calls them tags to avoid confusion with datatypes
25
University of Chicago Department of Energy
The message buffer is described by (start, count, datatype). The target process is specified by dest, which is the rank of the target process in the communicator specified by comm. When this function returns, the data has been delivered to the system and the buffer can be reused. The message may not have been received by the target process.
26
University of Chicago Department of Energy
27
University of Chicago Department of Energy
Send-Receive Summary
Send to matching Receive
A(10) B(20)
MPI_Send( A, 10, MPI_DOUBLE, 1, ) MPI_Recv( B, 20, MPI_DOUBLE, 0, )
Datatype Contexts
Basic for heterogeneity Derived for non-contiguous Message safety for libraries Robustness and correctness
28
Department of Energy
Buffering
University of Chicago
29
Department of Energy
In Fortran:
integer recvd_tag, recvd_from, recvd_count integer status(MPI_STATUS_SIZE) call MPI_RECV(..., MPI_ANY_SOURCE, MPI_ANY_TAG, .. status, ierr) tag_recvd = status(MPI_TAG) recvd_from = status(MPI_SOURCE) call MPI_GET_COUNT(status, datatype, recvd_count, ierr)
32
University of Chicago Department of Energy
used by other libraries. this can be defeated by use of wild card tags.
User-defined tags still provided in MPI for user convenience in organizing 33 University of Chicago Department of Energy application
Use mpirun np # -nolocal a.out for your clusters, e.g. mpirun np 3 nolocal cpi
34
University of Chicago Department of Energy
MPI is Simple
Many parallel programs can be written using just these six functions, only two of which are non-trivial:
MPI_INIT MPI_FINALIZE MPI_COMM_SIZE MPI_COMM_RANK MPI_SEND MPI_RECV
35
University of Chicago Department of Energy
36
University of Chicago Department of Energy
37
Department of Energy
Example: PI in C - 1
#include "mpi.h" #include <math.h> int main(int argc, char *argv[]) { int done = 0, n, myid, numprocs, i, rc; double PI25DT = 3.141592653589793238462643; double mypi, pi, h, sum, x, a; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&numprocs); MPI_Comm_rank(MPI_COMM_WORLD,&myid); while (!done) { if (myid == 0) { printf("Enter the number of intervals: (0 quits) "); scanf("%d",&n); } MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD); if (n == 0) break;
38
University of Chicago Department of Energy
Example: PI in C - 2
h = 1.0 / (double) n; sum = 0.0; for (i = myid + 1; i <= n; i += numprocs) { x = h * ((double)i - 0.5); sum += 4.0 / (1.0 + x*x); } mypi = h * sum; MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); if (myid == 0) printf("pi is approximately %.16f, Error is .16f\n", pi, fabs(pi - PI25DT)); } MPI_Finalize(); return 0; }
39
University of Chicago Department of Energy
Example: PI in Fortran - 1
program main include mpif.h integer done, n, myid, numprocs, i, rc double pi25dt, mypi, pi, h, sum, x, z data done/.false./ data PI25DT/3.141592653589793238462643/ call MPI_Init(ierr) call MPI_Comm_size(MPI_COMM_WORLD,numprocs, ierr ) call MPI_Comm_rank(MPI_COMM_WORLD,myid, ierr) do while (.not. done) if (myid .eq. 0) then print *,Enter the number of intervals: (0 quits) read *, n endif call MPI_Bcast(n, 1, MPI_INTEGER, 0, * MPI_COMM_WORLD, ierr ) if (n .eq. 0) goto 10
40
University of Chicago Department of Energy
Example: PI in Fortran - 2
= 1.0 / n sum = 0.0 do i=myid+1,n,numprocs x = h * (i - 0.5); sum += 4.0 / (1.0 + x*x); enddo mypi = h * sum call MPI_Reduce(mypi, pi, 1, MPI_DOUBLE_PRECISION, * MPI_SUM, 0, MPI_COMM_WORLD, ierr ) if (myid .eq. 0) then print *, "pi is approximately , pi, * , Error is , abs(pi - PI25DT) enddo 10 continue call MPI_Finalize( ierr ) end
41
University of Chicago Department of Energy
Example: PI in C++ - 1
#include "mpi.h" #include <math.h> int main(int argc, char *argv[]) { int done = 0, n, myid, numprocs, i, rc; double PI25DT = 3.141592653589793238462643; double mypi, pi, h, sum, x, a; MPI::Init(&argc,&argv); numprocs = MPI_COMM_WORLD.Get_size(); myid = MPI_COMM_WORLD.Get_rank(); while (!done) { if (myid == 0) { printf("Enter the number of intervals: (0 quits) "); scanf("%d",&n); } MPI_COMM_WORLD.Bcast(&n, 1, MPI_INT, 0 ); if (n == 0) break;
42
University of Chicago Department of Energy
Example: PI in C++ - 2
h = 1.0 / (double) n; sum = 0.0; for (i = myid + 1; i <= n; i += numprocs) { x = h * ((double)i - 0.5); sum += 4.0 / (1.0 + x*x); } mypi = h * sum; MPI_COMM_WORLD.Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0); if (myid == 0) printf("pi is approximately %.16f, Error is .16f\n", pi, fabs(pi - PI25DT)); } MPI::Finalize(); return 0; }
43
University of Chicago Department of Energy
MPI_SUCCESS
In Fortran:
mpif.h must be included, or use MPI module All MPI calls are to subroutines, with a place
C++ bindings, and Fortran-90 issues, are 44 University of Chicago Department of Energy part of MPI-2.
45
University of Chicago Department of Energy
Exercises
Modify hello program so that each process sends the name of the machine it is running on to process 0, which prints it.
See source of cpi or fpi in
Do this in such a way that the hosts are printed in rank order
University of Chicago
46
Department of Energy
47
University of Chicago Department of Energy
Buffers
When you send data, where does it go? One possibility is:
Process 0 User data Local buffer the network Local buffer User data
48
University of Chicago Department of Energy
Process 1
Avoiding Buffering
It is better to avoid copies:
Process 0 User data the network User data This requires that MPI_Send wait on delivery, or that MPI_Send return before transfer is complete, and we wait later.
University of Chicago
Process 1
49
Department of Energy
buffer is full (available for use). MPI_Send does not complete until the buffer is empty (available for use).
Sources of Deadlocks
Send a large message from process 0 to process 1
If there is insufficient storage at the destination, the
send must wait for the user to provide the memory space (through a receive)
What happens with this code? Process 0 Process 1 Send(1) Send(0) Recv(1) Recv(0) This is called unsafe because it depends on the availability of system buffers in which to store the data sent until it can be received
University of Chicago
51
Department of Energy
53
Department of Energy
54
Department of Energy
55
Department of Energy
Multiple Completions
It is sometimes desirable to wait on multiple requests:
MPI_Waitall(count, array_of_requests, array_of_statuses) MPI_Waitany(count, array_of_requests, &index, &status) MPI_Waitsome(count, array_of_requests, array_of indices, array_of_statuses)
Communication Modes
MPI provides multiple modes for sending messages:
Synchronous mode (MPI_Ssend): the send does not
complete until a matching receive has begun. (Unsafe programs deadlock.) Buffered mode (MPI_Bsend): the user supplies a buffer to the system for its use. (User allocates enough memory to make an unsafe program safe. Ready mode (MPI_Rsend): user guarantees that a matching receive has been posted.
Allows access to fast protocols undefined behavior if matching receive not posted
Non-blocking versions (MPI_Issend, etc.) MPI_Recv receives messages sent in any mode.
58
University of Chicago Department of Energy
Persistent requests
Useful for repeated communication
patterns Some systems can exploit to reduce latency and increase performance
University of Chicago
59
Department of Energy
MPI_Sendrecv
Allows simultaneous send and receive Everything else is general.
Send and receive datatypes (even type
signatures) may be different Can use Sendrecv with plain Send or Recv (or Irecv or Ssend_init, ) More general than send left Process 0 Process 1 SendRecv(1) SendRecv(0)
60
University of Chicago Department of Energy
Synchronization
MPI_Barrier( comm ) Blocks until all processes in the group of the communicator comm call it. Almost never required in a parallel program
Occasionally useful in measuring
Synchronization (Fortran)
MPI_Barrier( comm, ierr ) Blocks until all processes in the group of the communicator comm call it.
63
University of Chicago Department of Energy
Synchronization (C++)
comm.Barrier(); Blocks until all processes in the group of the communicator comm call it.
64
University of Chicago Department of Energy
P0 P1 P2 P3
A A A A
P0 P1 P2 P3
ABCD
Scatter
Gather
A B C D
65
Department of Energy
University of Chicago
Comments on Broadcast
All collective operations must be called by all processes in the communicator MPI_Bcast is called by both the sender (called the root process) and the processes that are to receive the broadcast
MPI_Bcast is not a multi-send root argument is the rank of the sender; this tells
Example of orthogonallity of the MPI design: MPI_Recv need not test for multisend
66
University of Chicago Department of Energy
A B C D
Allgather
AB AB AB AB
C C C C
D D D D
P0 P1 P2 P3
A0 A1 A2 A3 B0 B1 B2 B3 C0 C1 C2 C3 D0 D1 D2 D3
A0 B0 C0 D0
Alltoall
A1 B1 C1 D1 A2 B2 C2 D2 A3 B3 C3 D3
67
University of Chicago
Department of Energy
Collective Computation
P0 P1 P2 P3
A B C D
ABCD
Reduce
P0 P1 P2 P3
A B C D
Scan
A AB ABC ABCD
68
Department of Energy
University of Chicago
71
University of Chicago Department of Energy
1/slope=Bandwidth
Message Size
Department of Energy
The value returned by a single call to MPI_Wtime has little value. Times in general are local, but an implementation might offer synchronized times. See attribute MPI_WTIME_IS_GLOBAL.
University of Chicago Department of Energy
Measuring Performance
Using MPI_Wtime
timers are not continuous MPI_Wtick
MPI Profiling interface provides a way to easily instrument the MPI calls in an application Performance measurement tools for MPI
University of Chicago Department of Energy
Use MPI_Wtick to discover clock resolution Use getrusage to get other effects (e.g., context switches, paging)
University of Chicago Department of Energy
do i=1,maxloop
<operation to be timed>
enddo
time = (MPI_Wtime() - t1)/maxloop if (time .lt. tfinal) tfinal = time
enddo
Use MPI_Wtick to discover clock resolution Use getrusage to get other effects (e.g., context switches, paging)
University of Chicago Department of Energy
Underestimates by MPI_Wtick, over by cost of calling MPI_Wtime Correcting MPI_Wtime by subtracting average of MPI_Wtime calls overestimates MPI_Wtime Code not paged in (always run at least twice) Minimums not what users see Tests with 2 processors may not be representative T3D has processors in pairs, pingpong give 130 MB/sec for 2 but 75 MB/sec for 4 (for MPI_Ssend)
University of Chicago
Department of Energy
Underestimates by MPI_Wtick, over by cost of calling MPI_Wtime Correcting MPI_Wtime by subtracting average of MPI_Wtime calls overestimates MPI_Wtime Code not paged in (always run at least twice) Minimums not what users see Tests with 2 processors may not be representative T3D has processors in pairs, pingpong give 130 MB/sec for 2 but 75 MB/sec for 4 (for MPI_Ssend)
University of Chicago
Department of Energy
83
University of Chicago Department of Energy
starting the test How repeatable are your measurements? How does the performance compare to the performance of other operations (e.g., memcpy, floating multiply)?
84
University of Chicago Department of Energy
Logfile
Jumpshot
Display
85
University of Chicago Department of Energy
86
Department of Energy
University of Chicago
Department of Energy
Not included:
Sending initial work to all processes Deciding when to set done
88
University of Chicago Department of Energy
from master) An alternative would be a test for a nonblocking barrier (which MPI doesnt have)
89
University of Chicago Department of Energy
Master may fall behind in servicing requests for work if many processes ask in a very short interval Presented with many requests, master may not evenly respond
90
University of Chicago Department of Energy
MPI_Cancel
Last Irecv may never match; remove it with MPI_Cancel MPI_Test_cancelled required on IBM (!), then
MPI_Request_free
91
Department of Energy
University of Chicago
that the data itself doesnt move until the matching receive is issued, but that is the easiest way to implement the synchronous send operations Replace MPI_Send in worker with
MPI_Ssend for blocking MPI_Issend for nonblocking (even less synchronization)
92
University of Chicago Department of Energy
mesh-based computations Not a good algorithm (well see better ways later) Not the best decomposition (more scalable decompositions are more complex to program unless you use higher-level libraries)
94
University of Chicago Department of Energy
University of Chicago
96
Department of Energy
98
University of Chicago Department of Energy
sends/receives
99
University of Chicago Department of Energy
101
University of Chicago Department of Energy
Thread Interfaces
POSIX pthreads Windows
Kernel threads User threads called fibers
Java
First major language with threads Provides memory synchronization model: methods
(procedures) declared synchronized executed by one thread at a time (dont mention Ada, which has tasks)
OpenMP
Mostly directive-based parallel loops Some thread features (lock/unlock) http://www.openmp.org
103
University of Chicago Department of Energy
Coexist with compiler (thread) parallelism for SMPs MPI could have defined the same modes on a communicator basis (more natural, and MPICH will do this through attributes)
104
University of Chicago Department of Energy
Whats in MPI-2
Extensions to the message-passing model
Dynamic process management One-sided operations (remote memory access) Parallel I/O Thread support
105
University of Chicago Department of Energy
memory and file layout communicators to separate application-level message passing from I/O-related message passing non-blocking operations
I.e., lots of MPI-like machinery We will discuss a high-level approach to using MPI-IO
University of Chicago
106
Department of Energy
Retaining look and feel of MPI-1 Dealing with subtle memory behavior issues: cache coherence, sequential consistency
University of Chicago
107
Department of Energy
Get
Put window
Process 2 Process 3
= address spaces
University of Chicago
= window object
108
Department of Energy
one-sided operations
109
University of Chicago Department of Energy
MPI_Win_free deallocates window object MPI_Put moves data from local memory to remote memory MPI_Get retrieves data from remote memory into local memory MPI_Accumulate updates remote memory using local values Data movement operations are non-blocking Subsequent synchronization on window object needed to ensure operation is complete
110
University of Chicago Department of Energy
111
Department of Energy
112
University of Chicago Department of Energy
Point-to-point
/* Create communicator for separate context for processes 0 and 1 */ MPI_Comm_rank( MPI_COMM_WORLD, &rank ); MPI_Comm_split( MPI_COMM_WORLD, rank <= 1, rank, &comm ); /* Only processes 0 and 1 execute the rest of this */ if (rank > 1) return; /* Process 0 sends and Process 1 receives */ if (rank == 0) { MPI_Isend( outbuf, n, MPI_INT, 1, 0, comm, &request ); } else if (rank == 1) { MPI_Irecv( inbuf, n, MPI_INT, 0, 0, comm, &request ); } /* Allow other operations to proceed (communication or computation) */ /* Complete the operation */ MPI_Wait( &request, &status ); /* Free communicator */ MPI_Comm_free( &comm );
113
University of Chicago Department of Energy
RMA
/* Create memory window for separate context for processes 0 and 1 */ MPI_Comm_rank( MPI_COMM_WORLD, &rank ); MPI_Comm_split( MPI_COMM_WORLD, rank <= 1, rank, &comm ); if (rank == 0) MPI_Win_create( NULL, 0, sizeof(int), MPI_INFO_NULL, comm, &win ); else if (rank == 1 ) MPI_Win_create( inbuf, n * sizeof(int), sizeof(int), MPI_INFO_NULL, comm, &win ); /* Only processes 0 and 1 execute the rest of this */ if (rank > 1) return; /* Process 0 puts into process 1 */ MPI_Win_fence( 0, win ); if (rank == 0) MPI_Put( outbuf, n, MPI_INT, 1, 0, n, MPI_INT, win ); /* Allow other operations to proceed (communication or computation) */ /* Complete the operation */ MPI_Win_fence( 0, win ); /* Free the window */ MPI_Win_free( &win );
University of Chicago
114
Department of Energy
Using MPI_Win_fence
MPI_Win_create( A, ..., &win ); MPI_Win_fence( 0, win ); if (rank == 0) { /* Process 0 puts data into many local windows */ MPI_Put( ... , win ); MPI_Put( ... , win ); } /* This fence completes the MPI_Put operations initiated by process 0 */ MPI_Win_fence( 0, win ); /* All processes initiate access to some window to extract data */ MPI_Get( ... , win ); /* The following fence completes the MPI_Get operations */ MPI_Win_fence( 0, win ); /* After the fence, processes can load and store into A, the local window */ A[rank] = 4; printf( "A[%d] = %d\n", 0, A[0] ); MPI_Win_fence( 0, win ); /* We need a fence between stores and RMA operations */ MPI_Put( ... , win ); /* The following fence completes the preceding Put */ MPI_Win_fence( 0, win ); 115
University of Chicago Department of Energy
products, and algorithms with surface/volume behavior Issues are similar to unstructured grid problems (but harder to illustrate)
116
University of Chicago Department of Energy
117
Department of Energy
from SGI and HP, parts coming soon from IBM) MPI RMA an important part of the spectacular results on the Earth Simulator Most of dynamic and one sided in LAM, WMPI, MPICH2
University of Chicago
119
Department of Energy
MPICH Goals
Complete MPI implementation Portable to all platforms supporting the messagepassing model High performance on high-performance hardware As a research project:
exploring tradeoff between portability and performance removal of performance gap between user level (MPI) and
hardware capabilities
As a software project:
a useful free implementation for most machines a starting point for vendor proprietary implementations
120
University of Chicago Department of Energy
MPICH2
All-new implementation is our vehicle for research in
Thread safety and efficiency (e.g., avoid thread locks) Optimized MPI datatypes Optimized Remote Memory Access (RMA) High Scalability (64K MPI processes and more) Exploiting Remote Direct Memory Access (RDMA) capable networks (Myrinet) All of MPI-2, including dynamic process management, parallel I/O, RMA Clusters IBM BG/L New interconnect technologies (Myrinet) Cray Red Storm (under discussion) Others Many vendors start with MPICH in crafting custom, optimized MPIs
121
Department of Energy
Parallel Environments
University of Chicago
122
University of Chicago Department of Energy
MPICH2:
www.mcs.anl.gov/mpi/mpich2
123
University of Chicago Department of Energy
124
Department of Energy
environments Retains the file format so netCDF and PnetCDF applications can share files
126
University of Chicago Department of Energy
I/O in netCDF
Original netCDF
Parallel read
All processes read the file independently No possibility of collective optimizations
P0
P1
P2
P3
Sequential write
Parallel writes are carried out by shipping data to a single process
netCDF
PnetCDF
Parallel read/write to shared netCDF file Built on top of MPI-IO which utilizes optimal I/O facilities of the parallel file system and MPI-IO implementation Allows for MPI-IO hints and datatypes for further optimization
University of Chicago
P0
P1
P2
P3
Parallel netCDF
unstructured grids)
131
University of Chicago Department of Energy
Structure of PETSc
PETSc PDE Application Codes ODE Integrators
Visualization
Nonlinear Solvers, Interface Unconstrained Minimization Linear Solvers Preconditioners + Krylov Methods Object-Oriented Grid Matrices, Vectors, Indices Management Profiling Interface Computation and Communication Kernels MPI, MPI-IO, BLAS, LAPACK
133
University of Chicago Department of Energy
Time Steppers
Backward Pseudo Time Euler Stepping Other
Preconditioners
Additive Schwartz Block Jacobi Jacobi ILU ICC LU (Sequential only) Others
Matrices
Compressed Sparse Row (AIJ) Blocked Compressed Sparse Row (BAIJ) Block Diagonal (BDIAG) Dense Matrix-free Other
Distributed Arrays
Indices
Index Sets
Block Indices Stride Other
Vectors
134
University of Chicago Department of Energy
PETSc
PC
Application Initialization
KSP
Function Evaluation User code Jacobian Evaluation PETSc code PostProcessing
135
Department of Energy
University of Chicago
select many different sparse data structures Linear system solved with any user-selected Krylov iterative method and preconditioner provided by PETSc, including GMRES with ILU, BiCGstab with Additive Schwarz, etc. Complete performance analysis built-in
PETSC objects hide details of distributed data structures and function parameters
/* Get the mesh size. Use 10 by default */ n = 10; PetscOptionsGetInt( PETSC_NULL, "-n", &n, 0 ); /* Get the process decomposition. Default it the same as without DAs */ px = 1; PetscOptionsGetInt( PETSC_NULL, "-px", &px, 0 ); MPI_Comm_size( PETSC_COMM_WORLD, &worldSize ); py = worldSize / px;
/* Create a distributed array */ DACreate2d( PETSC_COMM_WORLD, DA_NONPERIODIC, DA_STENCIL_STAR, n, n, px, py, 1, 1, 0, 0, &grid ); /* Form the matrix and the vector corresponding to the DA */ A = FormLaplacianDA2d( grid, n ); b = FormVecFromFunctionDA2d( grid, n, func ); VecDuplicate( b, &x );
PETSc provides routines to create, allocate, and manage distributed data structures
SLESCreate( PETSC_COMM_WORLD, &sles ); SLESSetOperators( sles, A, A, DIFFERENT_NONZERO_PATTERN ); SLESSetFromOptions( sles ); PETSc provides SLESSolve( sles, b, x, &its ); routines that solve PetscPrintf( PETSC_COMM_WORLD, "Solution is:\n" ); VecView( x, PETSC_VIEWER_STDOUT_WORLD ); PetscPrintf( PETSC_COMM_WORLD, "Required %d iterations\n", its ); MatDestroy( A ); VecDestroy( b ); VecDestroy( x ); SLESDestroy( sles ); DADestroy( grid ); PetscFinalize( ); return 0; }
PETSc provides coordinated I/O (behavior is as-if a single process), including the output of the distributed vec object
/* -*- Mode: C; c-basic-offset:4 ; -*- */ #include "petsc.h" #include "petscvec.h" #include "petscda.h" /* Form a vector based on a function for a 2-d regular mesh on the unit square */ Vec FormVecFromFunctionDA2d( DA grid, int n, double (*f)( double, double ) ) { Vec V; int is, ie, js, je, in, jn, i, j; double h; double **vval; h = 1.0 / (n + 1); DACreateGlobalVector( grid, &V ); DAVecGetArray( grid, V, (void **)&vval );
/* Get global coordinates of this patch in the DA grid */ DAGetCorners( grid, &is, &js, 0, &in, &jn, 0 ); ie = is + in - 1; je = js + jn - 1; for (i=is ; i<=ie ; i++) { for (j=js ; j<=je ; j++){ vval[j][i] = (*f)( (i + 1) * h, (j + 1) * h ); } } DAVecRestoreArray( grid, V, (void **)&vval ); return V; }
DAGetMatrix( grid, MATMPIAIJ, &A ); /* Get global coordinates of this patch in the DA grid */ DAGetCorners( grid, &is, &js, 0, &in, &jn, 0 ); ie = is + in - 1; je = js + jn - 1;
for (i=is; i<=ie; i++) { for (j=js; j<=je; j++){ row.j = j; row.i = i; nelm = 0; if (j - 1 > 0) { vals[nelm] = oneByh2; cols[nelm].j = j - 1; cols[nelm++].i = i;} if (i - 1 > 0) { vals[nelm] = oneByh2; cols[nelm].j = j; cols[nelm++].i = i - 1;} vals[nelm] = - 4 * oneByh2; cols[nelm].j = j; cols[nelm++].i = i; if (i + 1 < n - 1) { vals[nelm] = oneByh2; cols[nelm].j = j; cols[nelm++].i = i + 1;} if (j + 1 < n - 1) { vals[nelm] = oneByh2; cols[nelm].j = j + 1; cols[nelm++].i = i;} MatSetValuesStencil( A, 1, &row, nelm, cols, vals, INSERT_VALUES ); } } MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY); MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY); return A; }
Just the usual code for setting the elements of the sparse matrix (the complexity comes, as it often does, from the boundary conditions
-ksp_max_it <max_iters> -ksp_gmres_restart <restart> -pc_asm_overlap <overlap> -pc_asm_type [basic,restrict,interpolate,none] etc ...
145
University of Chicago Department of Energy
5 0 1 2 3 4
Global: each process stores a unique local set of vertices (and each vertex is owned by exactly one process)
Local: each process stores a unique local set of vertices as well as ghost nodes from neighboring processes
146
University of Chicago Department of Energy
Global Representation:
0 1 2 3 4 5
Proc 0
Proc 0
6 7 8 9 10 11
Proc 1
Proc 1
6 7 8 3 4 5 0 1 2 6 7 8 3 4 5 0 1 2
Local Representations:
Proc 1
3 4 5 6 7 8 9 10 11
0 1 2 3 4 5 6 7 8 Proc 0
Proc 0
0 1 2 3 4
0 1 2 3 4
5 6 7 8
5 6 7 8
147
University of Chicago Department of Energy
Update ghostpoints (scatter global vector into local parts, including ghost points)
DAGlobalToLocalBegin(DA, ) DAGlobalToLocalEnd(DA,)
University of Chicago
148
Department of Energy
Distributed Arrays
Data layout and ghost values
Proc 10
Proc 10
Proc 0 Proc 1
Proc 0 Proc 1
Box-type stencil
Star-type stencil
149
University of Chicago Department of Energy
1)
150
University of Chicago Department of Energy
DACreate2d(,*DA)
DACreate2d(MPI_Comm comm, DAPeriodicType wrap, DAStencilType stencil_type, int M,int N,int m,int n,int dof, int s,const int lx[],const int ly[], DA *inra)
DA_[NON,X,Y,XY]PERIODIC DA_STENCIL_[STAR,BOX] number of grid points in x- and y-directions processes in x- and y-directions degrees of freedom per node stencil width Number of nodes for each cell (use PETSC_NULL for the
default) as tensor-product
and specifies how to update values in the local vector, local_vec (a pre-existing local vector)
DAGlobalToLocal End(DA,)
Takes same arguments
152
University of Chicago Department of Energy
153
University of Chicago Department of Energy
26 27 28 21 22 23 16 17 18 11 12 13 6 7 8 1 2 3
Proc 0
29 30 24 25 19 20 14 15 9 10 4 5
Proc1
22 23 24 19 20 21 16 17 18 7 8 9 4 5 6 1 2 3
Proc 0
29 30 27 28 25 26 14 15 12 13 10 11
Proc1
154
Department of Energy
Can convert between various global numbering schemes using AO (Application Orderings)
DAGetAO(DA da, AO *ao); AO usage explained in next section
Some utilities (e.g., VecView()) automatically handle this mapping for global vectors attained from DAs
155
University of Chicago Department of Energy
Other Libraries
Many libraries exist
Freely available libraries PETSc, ScaLAPACK, FFTW, HDF5, DOUG, GeoFEM, MP_SOLVE, MpCCI, PARASOL, ParMETIS, Prometheus, PSPASES, PLAPACK, S+, TCGMSG-MPI, Trilinos, SPRNG, TAO, Commercially supported libraries E.g., NAG, IBM PESSL, IMSL, More at www.mcs.anl.gov/mpi/libraries.html
These provide the building blocks for building large, complex, and efficient programs
156
University of Chicago Department of Energy
Conclusion
MPI provides a well-developed, efficient and portable model for programming parallel computers Just as important, MPI provides features that enable and encourage the construction of software components. Parallel applications can often be quickly built using these components Even when no available component meets your needs, using component-oriented design can simplify the process of writing and debugging an application with MPI.
158
University of Chicago Department of Energy
Visit
www.mcs.anl.gov/mpi/tutorials/mpiexmpl/index.html Or on the CD: mpi-exercises/index.html
University of Chicago
159
Department of Energy
Appendix
The following slides provide more information about some features of MPI, including: Send modes in MPI Collectives and pipelining Defining collective combiner operations Parallel netCDF details Fairness in master-worker programs Why contexts? Using the MPI profiling interface
160
University of Chicago Department of Energy
Buffered Mode
When MPI_Isend is awkward to use (e.g. lots of small messages), the user can provide a buffer for the system to store messages that cannot immediately be sent.
int bufsize; char *buf = malloc( bufsize ); MPI_Buffer_attach( buf, bufsize ); ... MPI_Bsend( ... same as MPI_Send ... ) ... MPI_Buffer_detach( &buf, &bufsize );
MPI_Buffer_detach waits for completion. Performance depends on MPI implementation and size of message.
161
University of Chicago Department of Energy
MPI_Buffer_detach waits for completion. Performance depends on MPI implementation and size of message.
162
University of Chicago Department of Energy
Buffering is limited
Processor 0 i=1 MPI_Bsend MPI_Recv i=2 MPI_Bsend i=2 Bsend fails because first Bsend has not been able to deliver the data
University of Chicago
164
Department of Energy
if (rank .eq. 0) read *, a call mpi_bcast( a, n, MPI_INTEGER, 0, & comm, ierr ) enddo Assume that Bcast takes nlogp time. Then this takes m n log p time (see Tuesday for a better way to bcast).
if (rank .eq. 0) then read *, a call mpi_send(a, n, type, 1, 0, comm,ierr) else call mpi_recv(a,n,type,rank-1, 0, comm,status,ierr) call mpi_send(a,n,type,next, 0, comm,ierr) endif enddo
167
University of Chicago Department of Energy
Pipeline
stops
MPI does not require that the same input give the same output
Implementations are encouraged but not required to
provide exactly the same output given the same input Round-off error may cause slight differences
Allreduce does guarantee that the same value is received by all processes for each call Why didnt MPI mandate determinism?
Not all applications need it Implementations can use deferred synchronization
170
Department of Energy
University of Chicago
FLASH
FLASH is an astrophysics simulation code from the University of Chicago ASCI Center Fluid dynamics code using adaptive mesh refinement (AMR) Runs on systems with thousands of nodes Has been using HDF5 for checkpointing and other output
174
University of Chicago Department of Energy
8x8x8 Blocks
University of Chicago
16x16x16 Blocks
175
Department of Energy
176
University of Chicago Department of Energy
Has also been tested on IBM SP and NEC S Cluster PnetCDF PnetCDF ROMIO ROMIO PVFS PVFS
University of Chicago
Department of Energy
178
University of Chicago Department of Energy
MPICH2
Complete redesign! Same goals as for MPICH:
Complete implementation (this time with
MPI-2) Portable Making best use of hardware (highperformance when possible) Starting point for vendors
Also:
Enhanced fault handling
179
Department of Energy
180
University of Chicago Department of Energy
MatCreate(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE ,n,n,&A); MatSetFromOptions(A); /* (code to assemble matrix not shown) */ Indicate whether the preconditioner VecCreate(PETSC_COMM_WORLD,&x); has the same nonzero pattern as the VecSetSizes(x,PETSC_DECIDE, n); matrix each time a system is solved. VecSetFromOptions (x); This default works with all VecDuplicate(x,&b); preconditioners. Other values (e.g., /* (code to assemble RHS vector not shown)*/ SAME_NONZERO_PATTERN)
can be used for particular
SLESCreate(PETSC_COMM_WORLD ,&sles); preconditioners. Ignored when SLESSetOperators(sles,A,A,DIFFERENT_NONZERO_PATTERN); solving only one system SLESSetFromOptions(sles); SLESSolve(sles,b,x,&its); SLESDestroy(sles);
181
University of Chicago Department of Energy
MatCreate( PETSC_COMM_WORLD,PETSC_DECIDE,n,n,A,ierr ) MatSetFromOptions( A, ierr ) VecCreate( PETSC_COMM_WORLD,x,ierr ) VecSetSizes( x, PETSC_DECIDE, n, ierr ) VecSetFromOptions( x, ierr ) VecDuplicate( x,b,ierr )
University of Chicago
Set level of fill for ILU Set SOR iterations Set SOR parameter Set additive Schwarz variant Set subdomain solver options
-pc_ilu_levels <levels> -pc_sor_its <its> -pc_sor_omega <omega> -pc_asm_type [basic, restrict,interpolate,none] -sub_pc_type <pctype> -sub_ksp_type <ksptype> -sub_ksp_rtol <rtol>
Runtime Option
-ksp_type [cg,gmres,bcgs, tfqmr,cgs,] -ksp_monitor, ksp_xmonitor, -ksp_truemonitor, ksp_xtruemonitor
KSPSetTolerances( ) -ksp_rtol <rt> -ksp_atol <at> Set convergence -ksp_max_its <its> tolerances KSPGMRESSetRestart( ) -ksp_gmres_restart <restart> Set GMRES restart parameter Set orthogonalization KSPGMRESSet -ksp_unmodifiedgramschmidt Orthogonalization( ) -ksp_irorthog routine for GMRES
Matrices
What are PETSc matrices?
MatSetType(Mat,MatType)
where MatType is one of
8 default sparse AIJ: MPIAIJ, SEQAIJ 8 block sparse AIJ (for multi-component PDEs): MPIAIJ, SEQAIJ 8 symmetric block sparse AIJ: MPISBAIJ, SAEQSBAIJ 8 block diagonal: MPIBDIAG, SEQBDIAG 8 dense: MPIDENSE, SEQDENSE 8 matrix-free 8 etc.
Matrix-vector multiplication
MatMult()
Matrix viewing
MatView()
A matrix is defined by its properties, the operations that you can perform with it.
Not by its data structures
186
University of Chicago Department of Energy
Matrix Assembly
Same form as for PETSc Vectors: MatSetValues(Mat,)
number of rows to insert/add indices of rows and columns number of columns to insert/add values to add mode: [INSERT_VALUES,ADD_VALUES]
MatAssemblyBegin(Mat) MatAssemblyEnd(Mat)
187
University of Chicago Department of Energy
MatSetFromOptions(A); /* mesh interior */ Let PETSc decide value[0] = -1.0; value[1] = 2.0; value[2] = -1.0; to allocate matrix if (rank == 0) { /* Only one process creates matrix */ for (i=1; i<n-2; i++) { across processes column[0] = i-1; column[1] = i; column[2] = i+1; MatSetValues(A,1,&i,3,column,value,INSERT_VALUES); } } /* also must set boundary points (code for global row 0 and n-1 omitted) */ MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY); MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
how
188
University of Chicago Department of Energy
MatGetOwnershipRange(Mat A, int *rstart, int *rend) rstart: first locally owned row of global matrix rend -1: last locally owned row of global matrix
189
University of Chicago Department of Energy
MatSetFromOptions(A); MatGetOwnershipRange(A,&start,&end); Let PETSc decide /* mesh interior */ to allocate matrix istart = start; if (start == 0) istart = 1; iend = end; if (iend == n-1) iend = n-2; across processes value[0] = -1.0; value[1] = 2.0; value[2] = -1.0; for (i=istart; i<iend; i++) { column[0] = i-1; column[1] = i; column[2] = i+1; MatSetValues(A,1,&i,3,column,value,INSERT_VALUES); } /* also must set boundary points (code for global row 0 and n-1 omitted) */ MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY); MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
University of Chicago
how
190
Department of Energy
Communication
Geometric Data stencil [implicit] Data Structure Ghost Point Creation Data Structures DACreate( ) DA Ghost Point Updates
DAGlobalToLocal( )
structured meshes
191
University of Chicago Department of Energy
Fairness
MPI makes no guarantee, other than that all messages will be received eventually. The program could
Receive all from process 1, then all from
process 2, etc. That order would starve processes 2 and higher of work in a master/worker method
195
University of Chicago Department of Energy
Why Contexts?
Parallel libraries require isolation of messages from one another and from the user that cannot be adequately handled by tags. The following examples are due to Marc Snir.
Sub1 and Sub2 are from different libraries Sub1(); Sub2(); Sub1a and Sub1b are from the same library Sub1a(); Sub2(); Sub1b();
University of Chicago
Department of Energy
Send(1)
Send(0)
University of Chicago
Department of Energy
Send(1)
Send(0)
University of Chicago
Send(0) Recv(1)
Recv(any) Sub1b
199
University of Chicago Department of Energy
Send(0) Recv(1)
Recv(any) Sub1b
University of Chicago
Some libraries already make use of PMPI Some MPI implementations have PMPI bugs
PMPI_Wtime() returns 0 PMPI in a separate library that some
University of Chicago
Department of Energy
Profiling Interface
Call MPI_Send
MPI_Send
MPI_Send PMPI_Send
202
University of Chicago Department of Energy
204
Department of Energy
206
University of Chicago Department of Energy
__LINE__
207
University of Chicago Department of Energy
Paragraph
Department of Energy