Title: Message Passing and MPI
1Message Passing and MPI
2A SOR Example
- for some number of timesteps/iterations
- for (i0 iltn i )
- for( j0 jltn, j )
- tempij 0.25
- ( gridi-1j gridi1j
- gridij-1 gridij1 )
- for( i0 iltn i )
- for( j0 jltn j )
- gridij tempij
-
3A SOR Example OpenMP
- for some number of timesteps/iterations
- pragma omp parallel for private(j)
- for (i0 iltn i )
- for( j0 jltn, j )
- tempij 0.25
- ( gridi-1j gridi1j
- gridij-1 gridij1 )
- pragma omp parallel for private(j)
- for( i0 iltn i )
- for( j0 jltn j )
- gridij tempij
-
4Shared Memory
grid
temp
1
1
2
2
3
3
4
4
proc1
proc2
proc3
proc4
5What does the user have to do?
- This is what we said for shared memory
- Decide how to decompose the computation into
parallel parts. - Create (and destroy) processes to support that
decomposition (implicitly or explicitly). - Add synchronization to make sure dependences are
covered. - Is the same true for message passing?
6Message Passing No shared memory!
mem1
mem2
mem3
mem4
proc1
proc2
proc3
proc4
network
7Data Distribution (only middle processes shown)
grid
grid
2
3
temp
temp
2
3
proc2
proc3
8Is this going to work?
- Same code as we used for shared memory
- for( ifrom iltto i)
- for( j0 jltn j )
- tempij 0.25( gridi-1j
gridi1j - gridij-1 gridij1)
9Is this going to work?
- Same code as we used for shared memory
- for( ifrom iltto i )
- for( j0 jltn j )
- tempij 0.25( gridi-1j
gridi1j - gridij-1 gridij1)
- No, we need extra boundary elements for grid.
10Data Distribution (only middle processes)
grid
grid
2
3
temp
temp
2
3
proc2
proc3
11Is this going to work?
- Same code as we used for shared memory
- for( ifrom iltto i)
- for( j0 jltn j )
- tempij 0.25( gridi-1j
gridi1j - gridij-1 gridij1)
12Is this going to work?
- Same code as we used for shared memory
- for( ifrom iltto i)
- for( j0 jltn j )
- tempij 0.25( gridi-1j
gridi1j - gridij-1 gridij1)
- No, on the next iteration we need boundary
elements from our neighbors.
13Data Communication (only middle processes)
grid
grid
proc2
proc3
14Is this now going to work?
- Same code as we used for shared memory
- for( ifrom iltto i )
- for( j0 jltn j )
- tempij 0.25( gridi-1j
gridi1j - gridij-1 gridij1)
15Is this now going to work?
- Same code as we used for shared memory
- for( ifrom iltto i )
- for( j0 jltn j )
- tempij 0.25( gridi-1j
gridi1j - gridij-1 gridij1)
- No, we need to translate the indices.
16Index Translation
- for( i0 iltn/p i)
- for( j0 jltn j )
- tempij 0.25( gridi-1j
gridi1j - gridij-1 gridij1)
- Remember, all variables are local.
17Index Translation is Optional
- Allocate the full arrays on each processor.
- Leave indices alone.
- Higher memory use.
- Sometimes necessary (see later).
18What does the user need to do?
- Divide up his program in parallel parts.
- Create and destroy processes to do above.
- Partition and distribute the data.
- Communicate data at the right time.
- (Sometimes) perform index translation.
- Still need to do synchronization?
- Sometimes, but many times goes hand in hand with
data communication.
19Message Passing Systems
- Provide process creation and destruction.
- Provide message passing facilities (send and
receive, in various flavors) to distribute and
communicate data. - Provide additional synchronization facilities.
20MPI (Message Passing Interface)
- Is the de facto message passing standard.
- Available on virtually all platforms, including
public domain versions (MPICH). - Grew out of an earlier message passing system,
PVM, now outdated.
21MPI Process Creation/Destruction
- MPI_Init( int argc, char argv )
- Initiates a computation.
- MPI_Finalize()
- Terminates a computation.
22MPI Process Identification
- MPI_Comm_size( comm, size )
- Determines the number of processes.
- MPI_Comm_rank( comm, pid ) Pid is the process
identifier of the caller.
23MPI Basic Send
- MPI_Send(buf, count, datatype, dest, tag, comm)
- buf address of send buffer
- count number of elements
- datatype data type of send buffer elements
- dest process id of destination process
- tag message tag (ignore for now)
- comm communicator (ignore for now)
24MPI Basic Receive
- MPI_Recv(buf, count, datatype, source, tag, comm,
status) - buf address of receive buffer
- count size of receive buffer in elements
- datatype data type of receive buffer elements
- source source process id or MPI_ANY_SOURCE
- tag and comm ignore for now
- status status object
25MPI Matrix Multiply (w/o Index Translation)
Willy Zwaenepoel missing initialization of a and
b
- main(int argc, char argv)
-
- MPI_Init (argc, argv)
- MPI_Comm_rank(MPI_COMM_WORLD, myrank)
- MPI_Comm_size(MPI_COMM_WORLD, p)
- from (myrank n)/p
- to ((myrank1) n)/p
- / Data distribution / ...
- / Computation / ...
- / Result gathering / ...
- MPI_Finalize()
-
26MPI Matrix Multiply (w/o Index Translation)
- / Data distribution /
- if( myrank ! 0 )
- MPI_Recv( afrom, nn/p, MPI_INT, 0, tag,
MPI_COMM_WORLD, status ) - MPI_Recv( b, nn, MPI_INT, 0, tag,
MPI_COMM_WORLD, status ) - else
- for( i1 iltp i )
- MPI_Send( afrom, nn/p, MPI_INT, i, tag,
MPI_COMM_WORLD ) - MPI_Send( b, nn, MPI_INT, I, tag,
MPI_COMM_WORLD ) -
27MPI Matrix Multiply (w/o Index Translation)
- / Computation /
- for ( ifrom iltto i)
- for (j0 jltn j)
- Cij0
- for (k0 kltn k)
- Cij AikBkj
-
28MPI Matrix Multiply (w/o Index Translation)
- / Result gathering /
- if (myrank!0)
- MPI_Send( cfrom, nn/p, MPI_INT, 0, tag,
MPI_COMM_WORLD) - else
- for (i1 iltp i)
- MPI_Recv( cfrom, nn/p, MPI_INT, i, tag,
MPI_COMM_WORLD, status)
29MPI Matrix Multiply (with Index Translation)
Willy Zwaenepoel missing initialization of a and
b
- main(int argc, char argv)
-
- MPI_Init (argc, argv)
- MPI_Comm_rank(MPI_COMM_WORLD, myrank)
- MPI_Comm_size(MPI_COMM_WORLD, p)
- from (myrank n)/p
- to ((myrank1) n)/p
- / Data distribution / ...
- / Computation / ...
- / Result gathering / ...
- MPI_Finalize()
-
30MPI Matrix Multiply (with Index Translation)
- / Data distribution /
- if( myrank ! 0 )
- MPI_Recv( a, nn/p, MPI_INT, 0, tag,
MPI_COMM_WORLD, status ) - MPI_Recv( b, nn, MPI_INT, 0, tag,
MPI_COMM_WORLD, status ) - else
- for( i1 iltp i )
- MPI_Send( afrom, nn/p, MPI_INT, i, tag,
MPI_COMM_WORLD ) - MPI_Send( b, nn, MPI_INT, I, tag,
MPI_COMM_WORLD ) -
31MPI Matrix Multiply (with Index Translation)
- / Computation /
- for ( i0 iltn/p i)
- for (j0 jltn j)
- Cij0
- for (k0 kltn k)
- Cij AikBkj
-
32MPI Matrix Multiply (with Index Translation)
- / Result gathering /
- if (myrank!0)
- MPI_Send( c, nn/p, MPI_INT, 0, tag,
MPI_COMM_WORLD) - else
- for( i1 iltp i )
- MPI_Recv( cfrom, nn/p, MPI_INT, i, tag,
MPI_COMM_WORLD, status)
33Running a MPI Program
- mpirun ltprogram_namegt ltargumentsgt
- Interacts with a daemon process on the hosts.
- Causes a Unix process to be run on each of the
hosts.