Distributed Memory Programming With: Peter Pacheco
Distributed Memory Programming With: Peter Pacheco
Distributed Memory Programming With: Peter Pacheco
Peter Pacheco
Chapter 3
Distributed Memory
Programming with
MPI
(a classic)
5
Identifying MPI processes
Common practice to identify processes by
nonnegative integer ranks.
6
Our first MPI program
7
Compilation
8
Execution
mpiexec -n 1 ./mpi_hello
mpiexec -n 4 ./mpi_hello
9
Execution
mpiexec -n 1 ./mpi_hello
mpiexec -n 4 ./mpi_hello
10
MPI Programs
Written in C.
Has main.
Uses stdio.h, string.h, etc.
Need to add mpi.h header file.
Identifiers defined by MPI start with
“MPI_”.
First letter following underscore is
uppercase.
For function names and MPI-defined types.
Helps to avoid confusion.
11
Copyright © 2010, Elsevier Inc. All rights Reserved 12
MPI Components
MPI_Init
Tells MPI to do all the necessary setup.
MPI_Finalize
Tells MPI we’re done, so clean up anything
allocated for this program.
13
Basic Outline
14
Communicators
Called MPI_COMM_WORLD.
15
Communicators
my rank
(the process making this call)
16
SPMD
Single-Program Multiple-Data
We compile one program.
Process 0 does something different.
Receives messages and prints them while the
other processes do the work.
17
Communication
r
MPI_Send
src = q
MPI_Recv
dest = r
MPI_Status*
unpredictable output
44
MPI_Reduce
45
18 and 28 lines are replaced with MPI_reduce 46
Predefined reduction operators in MPI
47
Collective vs. Point-to-Point Communications
48
Collective vs. Point-to-Point Communications
b seems to be 3 but it is 4.
d seems to be 4 but it is 5
52
MPI_Allreduce
64
Gather
Collect all of the components of the vector onto
process 0, and then process 0 can process all of
the components.
65
Print a distributed vector
69
Matrix-vector multiplication
i-th component of y
Dot product of the ith
row of A with x.
stored as
75
Derived datatypes
Used to represent any collection of data items in
memory by storing both the types of the items
and their relative locations in memory.
The idea is that if a function that sends data
knows this information about a collection of data
items, it can collect the items from memory
before they are sent.
Similarly, a function that receives data can
distribute the items into their correct destinations
in memory when they’re received.
86
Elapsed parallel time
87
Elapsed serial time
In this case, you don’t need to link in the MPI
libraries.
Returns time in microseconds elapsed from
some point in the past.
88
Elapsed serial time
89
MPI_Barrier
Ensures that no process will return from calling it
until every process in the communicator has
started calling it.
90
MPI_Barrier
91
Run-times of serial and parallel
matrix-vector multiplication
(Seconds)
92
Speedup
100
Serial bubble sort
101
Odd-even transposition sort
A sequence of phases.
Even phases, compare swaps:
102
Example
Start: 5, 9, 4, 3
Even phase: compare-swap (5,9) and (4,3)
getting the list 5, 9, 3, 4
Odd phase: compare-swap (9,3)
getting the list 5, 3, 9, 4
Even phase: compare-swap (5,3) and (9,4)
getting the list 3, 5, 4, 9
Odd phase: compare-swap (5,4)
getting the list 3, 4, 5, 9
103
Serial odd-even transposition sort
104
Communications among tasks in odd-even sort
105
Parallel odd-even transposition sort
106
Pseudo-code
107
Compute_partner
108
Safety in MPI programs
109
HERE……….
111
Safety in MPI programs
(see pseudo-code)
112
Safety in MPI programs
A program that relies on MPI provided
buffering is said to be unsafe.
113
MPI_Ssend
An alternative to MPI_Send defined by the MPI
standard.
The extra “s” stands for synchronous and
MPI_Ssend is guaranteed to block until the
matching receive starts.
114
Restructuring communication
115
MPI_Sendrecv
An alternative to scheduling the communications
ourselves.
Carries out a blocking send and a receive in a
single call.
The dest and the source can be the same or
different.
Especially useful because MPI schedules the
communications so that the program won’t hang
or crash.
125