10.1.5.0.1 Construction / Destruction

The default constructor and destructor are prototyped as follows:

int MPI::<CLASS>()



int $^\sim$MPI::<CLASS>()



In terms of construction and destruction, opaque MPI-/ user level objects behave like handles. Default constructors for all MPI-/ objects except MPI::Status create corresponding MPI::*_NULL handles. That is, when an MPI-/ object is instantiated, comparing it with its corresponding MPI::*_NULL object will return true. The default constructors do not create new MPI-/ opaque objects. Some classes have a member function Create() for this purpose.

Example 10..2   In the following code fragment, the test will return true and the message will be sent to cout.

void foo()
{
  MPI::Intracomm bar;

  if (bar == MPI::COMM_NULL) 
    cout << "bar is MPI::COMM_NULL" << endl;
}

The destructor for each MPI-/ user level object does not invoke the corresponding MPI_*_FREE function (if it exists).

Rationale. MPI_*_FREE functions are not automatically invoked for the following reasons:
  1. Automatic destruction contradicts the shallow-copy semantics of the MPI-/ classes.

  2. The model put forth in MPI-/ makes memory allocation and deallocation the responsibility of the user, not the implementation.

  3. Calling MPI_*_FREE upon destruction could have unintended side effects, including triggering collective operations (this also affects the copy, assignment, and construction semantics). In the following example, we would want neither foo_comm nor bar_comm to automatically invoke MPI_*_FREE upon exit from the function.
    void example_function() 
    {
      MPI::Intracomm foo_comm(MPI::COMM_WORLD), bar_comm;
      bar_comm = MPI::COMM_WORLD.Dup();
      // rest of function
    }
    
(End of rationale.)

MPI-Standard for MARMOT