Parallel Computing: Using MPI with C++

There are a number of advantages to using MPI with C++ instead of C or Fortran:

To use MPI with C++, you must configure and install MPI to use a a C++ compiler (such as GNU's g++). The file that accompanies the distribution lists the various switches you will need to set for configure, including -c++=yourCompiler, -c++linker=yourLinker, -c++flags=desiredFlags, and so on. (I don't think we got all of these set right here at Calvin, so we have to specify a few extra arguments when we compile and link, as you can see in our Makefile.)

Once that has been done, you can issue calls to MPI functions from within a C++ program, just as you would from within C or Fortran.

We think that the slick way to go is to use our MPIProcess class, which is in the file MPI_Process.h. Using this class, every MPI program has the same simple form:

   #include "MPI_Process.h"

   // ... Define MPIProcess::run() with the desired behavior ...

   int main(int argc, char ** argv)
      MPIProcess aProcess(argc, argv);   // NOTE: no ampersands needed!;
The file evenOdd.cpp contains a simple example program, and here is our Makefile which will need to be adjusted for your own system. (Netscape replaces tabs with spaces, so you will need to replace the spaces at the beginning of each line of the Makefile with tabs.)

To run the resulting program (e.g., evenOdd), use the usual mpirun command, for example:

  mpirun -np 8 evenOdd
Whatever code is used to define MPIProcess::run() will be then executed in SPMD fashion on your multiprocessor. The MPI_Process constructor takes care of all initialization, and the MPI_Process destructor takes care of all clean-up, without you having to do anything special.

At present, the MPI_Process interface includes the functions

There is also a copy-constructor provided (commented out), if some computation needs one.

This is our first step, and we hope to add more components (such as an MPI_Communicator class) in the future, as time permits. Have fun!

Calvin > CS > 374 > MPI Resources > MPI and C++
This page maintained by Joel Adams.