The operator MPI_MINLOC is used to compute a global minimum and also an index attached to the minimum value. MPI_MAXLOC similarly computes a global maximum and index. One application of these is to compute a global minimum (maximum) and the rank of the MPI process containing this value.
The operation that defines MPI_MAXLOC is:
where
and
MPI_MINLOC is defined similarly:
where
and
Both operations are associative and commutative. Note that if MPI_MAXLOC is applied to reduce a sequence of pairs (u0, 0), (u1, 1) , ..., (un-1 , n-1), then the value returned is (u , r), where and r is the index of the first global maximum in the sequence. Thus, if each MPI process supplies a value and its rank within the group, then a reduce operation with op = MPI_MAXLOC will return the maximum value and the rank of the first MPI process with that value. Similarly, MPI_MINLOC can be used to return a minimum and its index. More generally, MPI_MINLOC computes a lexicographic minimum, where elements are ordered according to the first component of each pair, and ties are resolved according to the second component.
The reduce operation is defined to operate on arguments that consist of a pair: value and index. For both Fortran and C, types are provided to describe the pair. The potentially mixed-type nature of such arguments is a problem in older versions of Fortran. The problem is circumvented there by having the MPI-provided type consist of a pair of the same type as value, and coercing the index to this type also. In C, the MPI-provided pair type has distinct types and the index is an integer type. For named predefined pair types in C the index is of type int. For unnamed predefined pair types, other integer types are allowed as index instead. To use pair types with distinct value and index in Fortran, these types need to be defined using BIND(C) and be equivalent to the corresponding C struct.
In order to use MPI_MINLOC and MPI_MAXLOC in a reduce operation, one must provide a datatype argument that represents a pair (value and index). MPI provides nine such named predefined datatypes as well as the function MPI_TYPE_GET_VALUE_INDEX to query named and unnamed predefined types using value type and index type. The operations MPI_MAXLOC and MPI_MINLOC can be used with each of the following named datatypes.
The datatype MPI_2REAL is as if defined by the following (see Section Derived Datatypes).
MPI_Type_contiguous(2, MPI_REAL, MPI_2REAL);Similar statements apply for MPI_2INTEGER, MPI_2DOUBLE_PRECISION, and MPI_2INT.
The datatype MPI_SHORT_INT is as if defined by the following sequence of instructions.
Similar statements apply for MPI_FLOAT_INT, MPI_LONG_INT and MPI_DOUBLE_INT.
MPI_TYPE_GET_VALUE_INDEX(value_type, index_type, pair_type) | |
IN value_type | datatype of the value in pair (handle) |
IN index_type | datatype of the index in pair (handle) |
OUT pair_type | datatype of the value-index pair (handle) |
MPI_TYPE_GET_VALUE_INDEX returns a handle to a predefined datatype suitable for the use with MPI_MINLOC and MPI_MAXLOC if such a predefined type exists. If the provided combination of value_type and index_type does not match a predefined pair datatype (named or unnamed), the function will set pair_type to MPI_DATATYPE_NULL and return MPI_SUCCESS. The returned type is not a duplicate. This type cannot be freed. Types supported by the underlying compiler for which the operators MPI_MIN and MPI_MAX are defined in Section Predefined Reduction Operations are acceptable value types. Integer types supported by the underlying compiler are acceptable index types.
Advice to users.
Note that a named type handle returned by MPI_TYPE_GET_VALUE_INDEX will yield the combiner value MPI_COMBINER_NAMED when queried with MPI_TYPE_GET_ENVELOPE to ensure backward compatibility to existing behavior, whereas all unnamed type handles returned by MPI_TYPE_GET_VALUE_INDEX will yield the combiner value MPI_COMBINER_VALUE_INDEX when queried with MPI_TYPE_GET_ENVELOPE.
There is no observable difference between the named constant value used via its symbol name or via MPI_TYPE_GET_VALUE_INDEX.
Code evaluating the combiner of type handles returned from MPI_TYPE_GET_VALUE_INDEX must therefore handle both MPI_COMBINER_NAMED and MPI_COMBINER_VALUE_INDEX.
( End of advice to users.)
Example
An unnamed predefined value-index type is retrieved for use with the corresponding C struct.
If the requested value-index pair does not exist as a predefined type MPI_DATATYPE_NULL is returned.
Advice to users.
Implementations may apply certain optimizations to operations on compound types with equally sized value and index types. Such optimizations may not be applicable to operations on compound types where value and index type are of different size.
( End of advice to users.)
The following examples use intra-communicators.
Example
Each MPI process has an array of 30 doubles, in C. For each
of the 30 locations, compute the value and rank of the MPI process containing
the largest value.
Example
Same example, in Fortran.
Example
Each MPI process has a nonempty array of values.
Find the minimum global value, the rank of the MPI process that holds it
and its index on this MPI process.
Rationale.
The definition of MPI_MINLOC and MPI_MAXLOC given
here has the advantage that it does not require any special-case
handling of these two operations: they are handled like any other
reduce operation. By assigning a value other than myrank to the in.index field, a programmer can provide a different definition
of MPI_MAXLOC and MPI_MINLOC, if so desired.
The disadvantage is that values and indices have to be first
interleaved, and that indices and values have to be coerced to the
same type, in Fortran.
( End of rationale.)