Professional Documents
Culture Documents
Lec 9 DR Marwa Abbas
Lec 9 DR Marwa Abbas
MPI
▪ Fortran and C interfaces (C++
Drivers
deprecated)
▪ Features for supporting parallel Hard-
libraries ware
▪ Library:
▪ C: error = MPI_Xxxx(...);
int MPI_Init(int *argc, char *argv);
▪ Arrays:
▪ C: indexed from 0
▪ C routines
▪ return an int — may be ignored
int MPI_Finalize();
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
▪ Running
▪ Details are implementation specific
▪ Startup wrappers:
▪ mpirun, mpiexec
Parallel Programming 2020 2020-11-30 21
Compiling and running the code
▪ Compiling/linking
▪ Headers and libs must be found by
compiler
▪ Most implementations provide
wrapper scripts, e.g., $ mpiCC -o hello hello.cc
▪ mpicc / mpiCC $ mpirun -np 4 ./hello
▪ Behave like normal
compilers/linkers Hello World! I am 3 of 4
Hello World! I am 1 of 4
Hello World! I am 0 of 4
▪ Running Hello World! I am 2 of 4
▪ Details are implementation specific
▪ Startup wrappers:
▪ mpirun, mpiexec
Parallel Programming 2020 2020-11-30 22
MPI point-to-point communication
Point-to-point communication: message envelope
▪ Which process is sending the message?
▪ Where is the data on the sending process?
▪ What kind of data is being sent?
Message
▪ How much data is there?
rank 𝑖 rank 𝑗
sender receiver
▪ Data types
▪ Basic
▪ MPI derived types
8 binary digits
At completion
▪ Send buffer can be reused as you see fit
▪ Status of destination is unknown – the message could be anywhere
Parallel Programming 2020 2020-11-30 30
Buffered send
MPI_send(buf, count, datatype,
blocking
dest, tag, comm)
Rank 0 Rank 1
app MPI MPI app
Bsend
blocking
copy msg
completion post send
wait for Recv
recv post recv
send completes
when message has msg msg
been copied transfer transfer
completion
predictable & no
synchronization Caveat: comes at the cost of
additional copy operations
Rank 0 Rank 1
app MPI MPI app
Ssend
post send
blocking
blocking
completion msg msg
transfer transfer
▪ Actual source and tag values are available in the status object:
MPI_Status s;
MPI_Recv(buf, count, datatype, MPI_ANY_SOURCE,
MPI_ANY_TAG, MPI_COMM_WORLD, &s);
printf(“Received from rank %d with tag %d\n”,
s.MPI_SOURCE, s.MPI_TAG);
int count;
MPI_Get_count(&s, MPI_DOUBLE, &count);
▪ Send/receive buffer may safely be reused after the call has completed
▪ MPI_Send() must have a specific target/tag, MPI_Recv() does not
▪ So far no explicit synchronization!
Parallel Programming 2020 2020-11-30 37
Example: parallel integration in MPI
MPI_Init(&argc, &argv);
MPI_Status status; // rank 0 collects partial results
MPI_Comm_size(MPI_COMM_WORLD, &size); if(0==rank) {
MPI_Comm_rank(MPI_COMM_WORLD, &rank); res = psum; // local result
// integration limits for(int i=1; i<size; ++i) {
double a=0., b=2., res=0., tmp; MPI_Recv(tmp, // receive buffer
// limits for "me" 1, // array length
mya = a + rank * (b-a)/size; MPI_DOUBLE, // data type
myb = mya + (b-a)/size; i, // rank of source
// integrate f(x) over my own chunk 0, // tag (unused here)
psum = integrate(mya,myb); MPI_COMM_WORLD,
&status); //status object
𝑏 res += tmp;
Task: calculate∫ 𝑓 𝑥 𝑑𝑥 using }
𝑎
printf(“Result: %f\n”, res);
(existing) function integrate(x,y) } else { // ranks != 0 send results to rank 0
MPI_Send(psum, // send buffer
1, // message length
▪ Split up interval [a,b] into equal MPI_DOUBLE,// data type
disjoint chunks 0, // rank of destination
0, // tag (unused here)
▪ Compute partial results in parallel MPI_COMM_WORLD);
▪ Collect global sum at rank 0 }
MPI_Finalize();
Parallel Programming 2020 2020-11-30 38
Example: parallel integration in MPI
MPI_Init(&argc, &argv);
MPI_Status status;
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
// integration limits
double a=0., b=2., res=0., tmp;
// limits for "me"
mya = a + rank * (b-a)/size; 𝑏
Task: calculate∫ 𝑓 𝑥 𝑑𝑥 using
myb = mya + (b-a)/size; 𝑎
(existing) function integrate(x,y)
// integrate f(x) over my own chunk
psum = integrate(mya,myb); ▪ Split up interval [a,b] into equal
disjoint chunks
▪ Compute partial results in parallel
▪ Collect global sum at rank 0
▪ Gathering results from processes is a very common task in MPI – there are more
efficient and elegant ways to do this (see later).
▪ This is a reduction operation (summation). There are more efficient and elegant
ways to do this (see later).
▪ The “master” process waits for one receive operation to be completed before the
next one is initiated. There are more efficient ways...
▪ Every process has its own res variable, but only the master process actually uses
it → it’s typical for MPI codes to use more memory than actually needed.
MPI_Barrier(comm);
▪ Ranks only return from call after every rank has called
the function
count = 3
MPI_Bcast(buffer, 3, MPI_INT, 1, MPI_COMM_WORLD)
buffer 1 2 3 1 2 3 1 2 3 1 2 3
root int
rank 0 1 2 3
sendbuf 1 2 3 4
recvbuf
sendbuf 1 2 3 4
recvbuf 1 2 3 4
root int
rank 0 1 2 3
sendbuf 1 2 3 4
recvbuf
sendbuf 1 2 3 4
recvbuf 1 2 3 4
rank 0 1 2 3
sendbuf 0 1 2 3
sendcount 1 1 1 1
recvbuf
recvcount 1 1 1 1
recvbuf 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3
rank 0 1 2 3
sendbuf 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
sendcount 1 1 1 1
recvbuf
recvcount 1 1 1 1
recvbuf 0 4 8 12 1 5 9 13 2 6 10 14 3 7 11 15
max()
max()
max()
max()
count = 4
▪ If all ranks require result, use MPI_Allreduce() op = MPI_MAX
recvbuf 8 9 6 8
▪ If the 12 predefined ops are not enough use on root
MPI_Op_create/MPI_Op_free to create own ones
Parallel Programming 2020 2020-12-14 59
Global operations – predefined operators
Name Operation Name Operation
MPI_SUM Sum MPI_PROD Product
MPI_MAX Maximum MPI_MIN Minimum
MPI_LAND Logical AND MPI_BAND Bit-AND
MPI_LOR Logical OR MPI_BOR Bit-OR
MPI_LXOR Logical XOR MPI_BXOR Bit-XOR
MPI_MAXLOC Maximum+Position MPI_MINLOC Minimum+Position
▪ Timing functions
▪ Global operations
▪ Reduce, allreduce, there are more…