MPI namespace. Most MPI objects have corresponding classes, such as Datatype for MPI_Datatype . Communicators and requests are slightly different. There is an abstract base class Comm for general communicators with four derived classes: Intracomm , Intercomm , Graphcomm , and Cartcomm . Most communicators are Intracomms ; GraphComm and CartComm are derived from Intracomm . Requests have two derived classes: Prequest for persistent requests and Grequest for generalized requests (new in MPI-2). Most MPI operations are methods on the appropriate objects; for example, most point-to-point and collective communications are methods on the communicator. A few routines, such as Init and Finalize , stand alone. A simple MPI program in C++ is shown in Figure 9.14.
#include "mpi.h"
#include <iostream.h>
int main( int argc, char *argv[] )
{
int data;
MPI::Init();
if (MPI::COMM_WORLD.Get_rank() == 0) {
// Broadcast data from process 0 to all others
cout << "Enter an int" << endl;
data << cin;
}
MPI::COMM_WORLD.Bcast( data, 1, MPI::INT, 0 );
MPI::Finalize();
return 0;
}
Figure 9.14: Simple MPI program in C++.
The C++ binding for MPI has a few quirks. One is the C++ analogue to MPI_Comm_dup . In the C++ binding, MPI::Comm is an abstract base class (ABC). Since it is impossible to create an instance of an abstract base class, there can be no general "dup" function that returns a new MPI::Comm . Since it is possible in C++ to create a reference to an ABC, however, MPI defines the routine (available only in the C++ binding) MPI::Clone that returns a reference to a new communicator.Two levels of Fortran 90 support are provided in MPI. The basic support provides an 'mpif.h ' include file. The extended support provides an MPI module. The module makes it easy to detect the two most common errors in Fortran MPI proSection 10.2.2 in the MPI-2 standard for more information). Another problem is that while Fortran 90 enables the user to define MPI interfaces in the MPI module, a different Fortran 90 interface file must be used for each combination of Fortran datatype and array dimension (scalars are different from arrays of dimension one, etc.). This leads to a Fortran 90 MPI module library that is often (depending on the Fortran 90 compiler) far larger than the entire MPI library. However, particularly during program development, the MPI module can be very helpful.