7.9.4. MINLOC and MAXLOC

PreviousUpNext
Up: Global Reduction Operations Next: User-Defined Reduction Operations Previous: Signed Characters and Reductions

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:

Image file

where

Image file

and

Image file

MPI_MINLOC is defined similarly:

Image file

where

Image file

and

Image file

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 Image file 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.

Image file

Image file

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.

Image file

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_typedatatype of the value in pair (handle)
IN index_typedatatype of the index in pair (handle)
OUT pair_typedatatype of the value-index pair (handle)
C binding
int MPI_Type_get_value_index(MPI_Datatype value_type, MPI_Datatype index_type, MPI_Datatype *pair_type)
Fortran 2008 binding
MPI_Type_get_value_index(value_type, index_type, pair_type, ierror)

TYPE(MPI_Datatype), INTENT(IN) :: value_type, index_type
TYPE(MPI_Datatype), INTENT(OUT) :: pair_type
INTEGER, OPTIONAL, INTENT(OUT) :: ierror
Fortran binding
MPI_TYPE_GET_VALUE_INDEX(VALUE_TYPE, INDEX_TYPE, PAIR_TYPE, IERROR)

INTEGER VALUE_TYPE, INDEX_TYPE, PAIR_TYPE, IERROR

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.

Image file


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.

Image file


Example Same example, in Fortran.

Image file


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.

Image file


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.)


PreviousUpNext
Up: Global Reduction Operations Next: User-Defined Reduction Operations Previous: Signed Characters and Reductions


Return to MPI-4.1 Standard Index
Return to MPI Forum Home Page

(Unofficial) MPI-4.1 of November 2, 2023
HTML Generated on November 19, 2023