10.2.5.2 Support for Size-specific MPI-/ Datatypes

MPI-/ provides named datatypes corresponding to optional Fortran 77 numeric types that contain explicit byte lengths -- MPI_REAL4, MPI_INTEGER8, etc. This section describes a mechanism that generalizes this model to support all Fortran numeric intrinsic types.

We assume that for each typeclass (integer, real, complex) and each word size there is a unique machine representation. For every pair (typeclass, n) supported by a compiler, MPI-/ must provide a named size-specific datatype. The name of this datatype is of the form MPI_$<$TYPE$>$n in C and Fortran and of the form MPI::$<$TYPE$>$n in C++ where $<$TYPE$>$ is one of REAL, INTEGER and COMPLEX, and n is the length in bytes of the machine representation. This datatype locally matches all variables of type (typeclass, n). The list of names for such types includes:

   
MPI_REAL4   
MPI_REAL8
MPI_REAL16   
MPI_COMPLEX8
MPI_COMPLEX16
MPI_COMPLEX32
MPI_INTEGER1
MPI_INTEGER2   
MPI_INTEGER4
MPI_INTEGER8   
MPI_INTEGER16
In MPI-/ these datatypes are all optional and correspond to the optional, nonstandard declarations supported by many Fortran compilers. In MPI-//, one datatype is required for each representation supported by the compiler. To be backward compatible with the interpretation of these types in MPI-/, we assume that the nonstandard declarations REAL*n, INTEGER*n, always create a variable whose representation is of size n. All these datatypes are predefined.

The following functions allow a user to obtain a size-specific MPI-/ datatype for any intrinsic Fortran type.



MPI_SIZEOF(x, size)

IN
x a Fortran variable of numeric intrinsic type (choice)
OUT
size size of machine representation of that type (integer)

MPI_SIZEOF(X, SIZE, IERROR) <type> X
INTEGER SIZE, IERROR



This function returns the size in bytes of the machine representation of the given variable. It is a generic Fortran routine and has a Fortran binding only.

Advice to users. This function is similar to the C and C++ sizeof operator but behaves slightly differently. If given an array argument, it returns the size of the base element, not the size of the whole array.(End of advice to users.)

Rationale. This function is not available in other languages because it would not be useful. (End of rationale.)



MPI_TYPE_MATCH_SIZE(typeclass, size, type)

IN
typeclass generic type specifier (integer)
IN
size size, in bytes, of representation (integer)
OUT
type datatype with correct type, size (handle)

int MPI_Type_match_size(int typeclass, int size, MPI_Datatype *type)



MPI_TYPE_MATCH_SIZE(TYPECLASS, SIZE, TYPE, IERROR) INTEGER TYPECLASS, SIZE, TYPE, IERROR



int MPI::Datatype::Match_size(int typeclass, int size)



static MPI::Datatype

typeclass is one of MPI_TYPECLASS_REAL, MPI_TYPECLASS_INTEGER and MPI_TYPECLASS_COMPLEX, corresponding to the desired typeclass. The function returns an MPI-/ datatype matching a local variable of type (typeclass, size).

This function returns a reference (handle) to one of the predefined named datatypes, not a duplicate. This type cannot be freed. MPI_TYPE_MATCH_SIZE can be used to obtain a size-specific type that matches a Fortran numeric intrinsic type by first calling MPI_SIZEOF in order to compute the variable size, and then calling MPI_TYPE_MATCH_SIZE to find a suitable datatype. In C and C++, one can use the C function sizeof(), instead of MPI_SIZEOF. In addition, for variables of default kind the variable's size can be computed by a call to MPI_TYPE_GET_EXTENT, if the typeclass is known. It is erroneous to specify a size not supported by the compiler.

Rationale. This is a convenience function. Without it, it can be tedious to find the correct named type. See note to implementors below. (End of rationale.)
Advice to implementors. This function could be implemented as a series of tests.
int MPI_Type_match_size(int typeclass, int size, MPI_Datatype *rtype)
{
  switch(typeclass) {
      case MPI_TYPECLASS_REAL: switch(size) {
        case 4: *rtype = MPI_REAL4; return MPI_SUCCESS;
        case 8: *rtype = MPI_REAL8; return MPI_SUCCESS;
        default: error(...);
      }
      case MPI_TYPECLASS_INTEGER: switch(size) {
         case 4: *rtype = MPI_INTEGER4; return MPI_SUCCESS;         
         case 8: *rtype = MPI_INTEGER8; return MPI_SUCCESS;
         default: error(...);       }
     ... etc ... 
   }
}
(End of advice to implementors.)

MPI-Standard for MARMOT