Distributed Memory Programming With MPI: Peter Pacheco
Distributed Memory Programming With MPI: Peter Pacheco
Distributed Memory Programming With MPI: Peter Pacheco
Peter Pacheco
Chapter 3
Distributed Memory
Programming with
MPI
# Chapter Subtitle
Roadmap
Hello World!
(a classic)
Copyright 2010, Elsevier Inc. All rights Reserved
You can Link with the libraries -llmpe -lmpe to enable logging and the MPE
environment. Then run the program as usual and a log file will be produced. The
log file can be visualized using the jumpshot program that comes bundled with
MPICH2.
Compilation
wrapper script to compile
source file
10
Execution
mpiexec -n <number of processes> <executable>
mpiexec -n 1 ./mpi_hello
run with 1 process
mpiexec -n 4 ./mpi_hello
run with 4 processes
11
Execution
mpiexec -n 1 ./mpi_hello
12
MPI Programs
Written in C.
Has main.
Uses stdio.h, string.h, etc.
13
MPI Components
MPI_Init
MPI_Finalize
14
Basic Outline
15
Communicators
Called MPI_COMM_WORLD.
16
Communicators
my rank
(the process making this call)
17
#include mpi.h
#include <stdio.h>
#include <math.h>
#define MAXSIZE 1000
void main(int argc, char *argv)
{
int myid, numprocs;
int data[MAXSIZE], i, x, low, high, myresult, result;
char fn[255];
char *fp;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
if (myid == 0) { /* Open input file and initialize data */
strcpy(fn,getenv(HOME));
strcat(fn,/MPI/rand_data.txt);
if ((fp = fopen(fn,r)) == NULL) {
printf(Cant open the input file: %s\n\n, fn);
exit(1);
}
for(i = 0; i < MAXSIZE; i++) fscanf(fp,%d, &data[i]);
}
MPI_Bcast(data, MAXSIZE, MPI_INT, 0, MPI_COMM_WORLD); /* broadcast data */
x = n/nproc; /* Add my portion Of data */
low = myid * x;
high = low + x;
for(i = low; i < high; i++)
myresult += data[i];
printf(I got %d from %d\n, myresult, myid); /* Compute global sum */
MPI_Reduce(&myresult, &result, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
if (myid == 0) printf(The sum is %d.\n, result);
MPI_Finalize();
}
18
SPMD
Single-Program Multiple-Data
We compile one program.
Process 0 does something different.
19
Communication
20
Data types
21
Communication
22
Message matching
r
MPI_Send
src = q
MPI_Recv
dest = r
23
Receiving messages
24
status_p argument
How receiver finds out the sender, tag if they are not needed
by the receiver
MPI_Status*
MPI_Status* status;
status.MPI_SOURCE
status.MPI_TAG
MPI_SOURCE
MPI_TAG
MPI_ERROR
25
26
27
28
29
30
One trapezoid
31
32
33
Parallel pseudo-code
34
35
36
37
38
39
unpredictable output
40
Input
41
42
COLLECTIVE
COMMUNICATION
Copyright 2010, Elsevier Inc. All rights Reserved
43
Tree-structured communication
1. In the first phase:
(a) Process 1 sends to 0, 3 sends to 2, 5 sends to 4, and
7 sends to 6.
(b) Processes 0, 2, 4, and 6 add in the received values.
(c) Processes 2 and 6 send their new values to
processes 0 and 4, respectively.
(d) Processes 0 and 4 add the received values into their
new values.
2. (a) Process 4 sends its newest value to process 0.
(b) Process 0 adds the received value to its newest
value.
44
45
An alternative tree-structured
global sum
46
MPI_Reduce
47
48
49
50
51
52
Example (1)
53
Example (2)
54
Example (3)
55
MPI_Allreduce
56
57
58
Broadcast
59
A tree-structured broadcast.
60
61
Data distributions
62
63
64
Partitioning options
Block partitioning
Cyclic partitioning
Block-cyclic partitioning
65
Parallel implementation of
vector addition
66
Scatter
67
68
Gather
69
70
71
Allgather
72
73
Derived datatypes
74
Derived datatypes
75
MPI_Type create_struct
76
MPI_Get_address
77
MPI_Type_commit
78
MPI_Type_free
79
80
81
82
PERFORMANCE EVALUATION
83
84
85
86
87
MPI_Barrier
88
MPI_Barrier
89
(Seconds)
90
Speedup
91
Efficiency
92
93
94
Scalability
95
Scalability
96
A PARALLEL SORTING
ALGORITHM
Copyright 2010, Elsevier Inc. All rights Reserved
97
Sorting
98
99
A sequence of phases.
Even phases, compare swaps:
100
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
Copyright 2010, Elsevier Inc. All rights Reserved
101
102
103
104
Pseudo-code
105
Compute_partner
106
107
108
109
110
MPI_Ssend
111
Restructuring communication
112
MPI_Sendrecv
113
MPI_Sendrecv
114
115
116
117
118
119
120
121