MPI_Guide_C++
MPI_Guide_C++
Header File
All program units that make MPI calls must include the mpi.h header file. This file defines a number
of MPI constants as well as providing the MPI function prototypes. All MPI constants and procedures
use the MPI namespace.
#include “mpi.h”
MPI::COMM_WORLD
MPI::PROC_NULL
MPI::ANY_SOURCE
MPI::ANY_TAG
MPI::INT
MPI::SHORT
MPI::LONG
MPI::LONG_LONG_INT
MPI::UNSIGNED
MPI::UNSIGNED_LONG
MPI::UNSIGNED_SHORT
MPI::FLOAT
MPI::DOUBLE
MPI::LONG_DOUBLE
MPI::CHAR
MPI::UNSIGNED_CHAR
MPI::BYTE
MPI::PACKED
The Essential MPI Procedures
The C++ bindings typically pass arguments by reference and return the value of interest, if it is a single
variable. The function prototypes indicate how the parameters are to be declared, but if a parameter
specified as a pointer but is declared as a variable, an ampersand must be prepended in the method call.
MPI::Init
void MPI::Init()
example
MPI_Init(argc, argv);
MPI_Comm_rank
This routine obtains the rank of the calling process within the specified communicator group.
example
my_rank = MPI::COMM_WORLD.Get_rank();
MPI_Comm_size
This procedure obtains the number of processes in the specified communicator group.
example
np = MPI::COMM_WORLD.Get_size();
MPI_Finalize
The MPI_Finalize routine cleans up the MPI state in preparation for the processes to exit.
void MPI::Finalize()
example
MPI::Finalize();
MPI_Abort
This routine shuts down MPI in the event of an abnormal termination. It should be called when an
error condition is detected, and in general the communicator should always be MPI_COMM_WORLD.
example
MPI::COMM_WORLD.Abort(errcode);
MPI_Bcast
This procedure broadcasts a buffer from a sending process to all other processes.
example
int myval;
MPI_Reduce
The MPI_Reduce function sends the local value(s) to a specified root node and applies an operator on
all data in order to produce a global result, e.g. the sum of all the values on all processes.
MPI::Comm::Reduce operators
MPI::MAX
MPI::MIN
MPI::SUM
MPI::PROD
MPI::MAXLOC
MPI::MINLOC
MPI::LAND
MPI_BAND
MPI::LOR
MPI::BOR
MPI::LXOR
MPI::BXOR
MPI_Barrier
The MPI_Barrier function causes all processes to pause until all members of the specified
communicator group have called the procedure.
example
MPI::COMM_WORLD.Barrier();
MPI_Send
MPI_Recv
or
examples
The pattern of exchanging data between two processes simultaneously is so common that a routine has
been provided to handle the exchange directly.
or
example
MPI_Gather
This routine collects data from each processor onto a root process, with the final result stored
in rank order. The same number of items is sent from each process. The count of items
received is the count sent by a single process, not the aggregate size, but the receive buffer
must be declared to be of a size to contain all the data.
int sendarr[100];
int root=0;
int *recvbuf;
MPI::Comm::Gather is limited to receiving the same count of items from each process,
and only the root process has all the data. If all processes need the aggregate data,
MPI_Allgather should be used.
If a different count must be sent from each process, the routine is MPI_GATHERV. This has
a more complex syntax and the reader is referred to MPI reference books. Similar to
GATHER/ALLGATHER, there is also an MPI::Comm::Allgatherv.
MPI_Scatter
This routine distributes data from a root process to the processes in a communicator group.
The same count of items is sent to each process.
int recvarr[100];
int root=0;
int *sendbuf;
#include <iostream.h>
#include "mpi.h"
if ( rank == 0 ) {
cout << "Running on "<< npes << " Processes "<< endl;
}
cout << "Greetings from process " << rank << endl;
MPI::Finalize();
}