Ch3 L1 PDC CS4172 Fall 2024
Ch3 L1 PDC CS4172 Fall 2024
Peter Pacheco
Chapter 3
Distributed Memory
Programming with
MPI
(a classic)
mpiexec -n 1 ./mpi_hello
mpiexec -n 4 ./mpi_hello
mpiexec -n 4 ./mpi_hello
■MPI_Finalize
■Tells MPI we’re done, so clean up anything
allocated for this program.
my rank
(the process making this call)
■ The ones we’ll use (and a few others) are listed in Table 3.1.
r
MPI_Send
src = q
MPI_Recv
dest = r
MPI_Status*
unpredictable output
78 78
Collective vs. Point-to-Point
Communications
■The arguments passed by each process to
an MPI collective communication must be
“compatible.”
P0 P1 P2 P3 P4 P5
14 39 53 120 66 29
+ + +
O(log2 P) 53 173 95
with P
processes +
226
+
321
Collective vs. Point-to-Point
Communications
■The output_data_p argument is only used
on dest_process.
P1 A … … … P1 A+B+C+D
P2 B … … …
MPI_Allreduc P2 A+B+C+D
P3 C … … … e P3 A+B+C+D
P4 D … … … P4 A+B+C+D
87
A global sum followed
by distribution of the
result.
Destinations
A common pattern to get Same data
same data to all processes, sent to all destinations
especially at the beginning
of a computation Source
Note:
•Patterns given do not mean the implementation does them as shown. Only the
final result is the same in any parallel implementation.
•Patterns do not describe the implementation. 90
Broadcast
■Data belonging to a
single process is sent
to all of the
processes in the
communicator.
102
Scatter Pattern
MPI_Bcast takes a single data element at the root process (the red box)
order of process rank. The first element (in red) goes to process zero, the
second element (in green) goes to process one, and so on.
Basic MPI scatter operation
Sending one of more contiguous elements of an array in root process to a
separate process.
Sources
Data
Destination
Data
A common pattern especially at
the end of a computation to Data collected
collect results at destination
in an array
Data
108
MPI Gather
P1 A P1 A B C D
P2 B P2
MPI_Gather
P3 C P3
P4 D P4
Print a distributed vector (1)
i-th component of y
Dot product of the ith
row of A with x.
Serial pseudo-code
stored as
P1 A P1 A B C D
P2 B P2 A B C D
MPI_Allgather
P3 C P3 A B C D
P4 D P4 A B C D
An MPI matrix-vector multiplication function (1)