Unit Iv Distributed Memory Programming With Mpi
Unit Iv Distributed Memory Programming With Mpi
MPI program execution – MPI constructs – libraries – MPI send and receive – Point-to-point and
Collective communication – MPI derived datatypes – Performance evaluation
Textbook
• MPI defines a library of functions that can be called from C, C++, and Fortran programs.
• One process to do the output, and the other processes will send it messages, which it will print
Compile:
Execution:
mpiexec -n <number of processes> <executable>
mpiexec -n 4 ./mpi_hello
• Written in C.
– Has main.
2.1 MPI_Init
– no other MPI functions should be called before the program calls MPI Init.
2.2 MPI_Finalize
– Tells MPI that any resources allocated for MPI can be freed.
– It’s also not necessary that the calls to MPI Init and MPI Finalize be in main function.
2.3 Communicators MPI_Comm_size & MPI_Comm_rank
• In MPI a communicator is a collection of processes that can send messages to each other.
• MPI_Init defines a communicator that consists of all the processes created when the program is
started.
• It is called MPI_COMM_WORLD.
• msg_buf_p - is a pointer to the block of memory containing the contents of the message.
– In the program, this is just the string containing the message, greeting.
• The second and third arguments, msg_size and msg_ type, determine the amount of data to be
sent.
• msg_size - is the number of characters in the message plus one character for the ‘\0’ character
that terminates C strings.
• msg_type - is MPI CHAR. These two arguments together tell the system that the message
contains strlen(greeting)+1 chars.
• dest - specifies the rank of the process that should receive the message.
• It can be used to distinguish messages that are otherwise identical. For example, suppose
process 1 is sending floats to process 0. Some of the floats should be printed, while others
should be used in a computation.
• Then the first four arguments to MPI Send provide no information regarding which floats should
be printed and which should be used in a computation.
• So process 1 can use, say, a tag of 0 for the messages that should be printed and a tag of 1 for
the messages that should be used in a computation.
• Since MPI provides functions for creating new communicators, this feature can be used in
complex programs to insure that messages aren’t “accidentally received” in the wrong place.
• First three arguments specify the memory available for receiving the message:
– buf_size determines the number of objects that can be stored in the block,
– source - specifies the process from which the message should be received.
– tag - should match the tag argument of the message being sent,
• The sending process will assemble the message. For example, it will add the “envelope”
information to the actual data being transmitted.
• The destination process rank, the sending process rank, the tag, the communicator, and some
information on the size of the message.
• Once the message has been assembled, there are essentially two possibilities: the sending
process can buffer the message or it can block.
• If it buffers the message, the MPI system will place the message (data and envelope) into its
own internal storage, and the call to MPI Send will return.
• If the system blocks, it will wait until it can begin transmitting the message, and the call to
MPI_Send may not return immediately.
• If we use MPI_Send, when the function returns, we don’t actually know whether the message
has been transmitted. We only know that the storage we used for the message.
• If the size of a message is less than the cutoff, it will be buffered. If the size of the message is
greater than the cutoff, MPI Send will block.
• Unlike MPI Send, MPI_Recv always blocks until a matching message has been received. Thus,
when a call to MPI_Recv returns, we know that there is a message stored in the receive buffer
(unless there’s been an error).
• There is an alternate method for receiving a message, in which the system checks whether a
matching message is available and returns, regardless of whether there is one.
• This means that if process q sends two messages to process r, then the first message sent by q
must be available to r before the second message.
• However, there is no restriction on the arrival of messages sent from different processes.
• That is, if q and t both send messages to r, then even if q sends its message before t sends its
message, there is no requirement that q’s message become available to r before t’s message.
• Then the communication channels will join each of the tasks of the first type to the single task of
the second type.
• If comm sz evenly divides n, the number of trapezoids, we can simply apply the trapezoidal rule
with n=comm sz trapezoids to each of the comm sz subintervals.
• To finish, we can have one of the processes, say process 0, add the estimates.
3. Collective communication
• Each process with rank greater than 0 is, in effect, saying “add this number into the
total(trapezoidal rule).”
• Process 0 is doing nearly all the work in computing the global sum, while the other processes are
doing almost nothing
communication functions that involve all the processes in a communicator are called collective
communications
3.2 MPI_Reduce
Example
Predefined reduction operators in MPI
3.3 MPI_Allreduce
• Useful in a situation in which all of the processes need the result of a global sum in order to
complete some larger computation.
3.4 Butterfly Structured
3.5 Broadcast
• MPI_Scatter can be used in a function that reads in an entire vector on process 0 but only sends
the needed components to each of the other processes.
3.7 Gather
• Collect all of the components of the vector onto process 0, and then process 0 can process all of
the components.
3.8 Allgather
• Concatenates the contents of each process’ send_buf_p and stores this in each process’
recv_buf_p.
• As usual, recv_count is the amount of data being received from each process.
Each process communicates using send and • All the processes in the communicator
receive function must call the same collective function.
The arguments passed by each process to an MPI • The arguments passed by each process to
communication may vary with each process an MPI collective communication must be
“compatible.”
5.Library Functions
1. MPI_Init()
2. MPI_Comm_size()
3. MPI_Comm_rank()
4. MPI_Finalize()
5. MPI_Send()
6. MPI_Receive()
7. MPI_Reduce()
8. MPI_Allreduce()
9. MPI_Bcast()
10. MPI_Scatter()
11. MPI_Gather()
12. MPI_Allgather()
13.MPI_Address()
#include <mpi.h>
Description
The address of a location in memory can be found by invoking this function. Returns the (byte) address
of location.
14.MPI_Barrier()
#include <mpi.h>
MPI_Get_version
Description
An MPI barrier completes after all group members have entered the barrier.
#include <mpi.h>
Description
Since Open MPI is MPI 3.1 compliant, this function will return a version value of 3 and a subversion value
of 1 for this release.
15.MPI_Wait
#include <mpi.h>
Input Parameter
Output Parameters
Description
PART B – 16 Marks
2.1 to 2.7
2.1 to 2.7
3.Collective Communication
3. 1 to 3.8
4.Library Functions
Chapter 5
PART A
2,Syntax