110. User-Defined Reduction Operations

PreviousUpNext
Up: Global Reduction Operations Next: Example of User-defined Reduce Previous: MINLOC and MAXLOC

MPI_OP_CREATE(user_fn, commute, op)
IN user_fn user defined function (function)
IN commute true if commutative; false otherwise.
OUT op operation (handle)

int MPI_Op_create(MPI_User_function* user_fn, int commute, MPI_Op* op)

MPI_Op_create(user_fn, commute, op, ierror)
PROCEDURE(MPI_User_function) :: user_fn
LOGICAL, INTENT(IN) :: commute
TYPE(MPI_Op), INTENT(OUT) :: op
INTEGER, OPTIONAL, INTENT(OUT) :: ierror
MPI_OP_CREATE( USER_FN, COMMUTE, OP, IERROR)
EXTERNAL USER_FN
LOGICAL COMMUTE
INTEGER OP, IERROR

MPI_OP_CREATE binds a user-defined reduction operation to an op handle that can subsequently be used in MPI_REDUCE, MPI_ALLREDUCE, MPI_REDUCE_SCATTER_BLOCK, MPI_REDUCE_SCATTER, MPI_SCAN, MPI_EXSCAN, all nonblocking variants of those (see Section Nonblocking Collective Operations ), and MPI_REDUCE_LOCAL. The user-defined operation is assumed to be associative. If commute = true, then the operation should be both commutative and associative. If commute = false, then the order of operands is fixed and is defined to be in ascending, process rank order, beginning with process zero. The order of evaluation can be changed, taking advantage of the associativity of the operation. If commute = true then the order of evaluation can be changed, taking advantage of commutativity and associativity.

The argument user_fn is the user-defined function, which must have the following four arguments: invec, inoutvec, len, and datatype.

The ISO C prototype for the function is the following.

typedef void MPI_User_function(void* invec, void* inoutvec, int *len, MPI_Datatype *datatype);

The Fortran declarations of the user-defined function user_fn appear below.

SUBROUTINE MPI_User_function(invec, inoutvec, len, datatype)
USE, INTRINSIC :: ISO_C_BINDING, ONLY : C_PTR
TYPE(C_PTR), VALUE :: invec, inoutvec
INTEGER :: len
TYPE(MPI_Datatype) :: datatype
SUBROUTINE USER_FUNCTION(INVEC, INOUTVEC, LEN, DATATYPE)
<type> INVEC(LEN), INOUTVEC(LEN)
INTEGER LEN, DATATYPE

The datatype argument is a handle to the data type that was passed into the call to MPI_REDUCE. The user reduce function should be written such that the following holds: Let u[0], ... , u[len-1] be the len elements in the communication buffer described by the arguments invec, len and datatype when the function is invoked; let v[0], ... , v[len-1] be len elements in the communication buffer described by the arguments inoutvec, len and datatype when the function is invoked; let w[0], ... , w[len-1] be len elements in the communication buffer described by the arguments inoutvec, len and datatype when the function returns; then w[i] = u[i]°v[i], for i=0 , ... , len-1, where ° is the reduce operation that the function computes.

Informally, we can think of invec and inoutvec as arrays of len elements that user_fn is combining. The result of the reduction overwrites values in inoutvec, hence the name. Each invocation of the function results in the pointwise evaluation of the reduce operator on len elements: i.e., the function returns in inoutvec[i] the value Image file , for Image file , where ° is the combining operation computed by the function.


Rationale.

The len argument allows MPI_REDUCE to avoid calling the function for each element in the input buffer. Rather, the system can choose to apply the function to chunks of input. In C, it is passed in as a reference for reasons of compatibility with Fortran.

By internally comparing the value of the datatype argument to known, global handles, it is possible to overload the use of a single user-defined function for several, different data types. ( End of rationale.)
General datatypes may be passed to the user function. However, use of datatypes that are not contiguous is likely to lead to inefficiencies.

No MPI communication function may be called inside the user function. MPI_ABORT may be called inside the function in case of an error.


Advice to users.

Suppose one defines a library of user-defined reduce functions that are overloaded: the datatype argument is used to select the right execution path at each invocation, according to the types of the operands. The user-defined reduce function cannot ``decode'' the datatype argument that it is passed, and cannot identify, by itself, the correspondence between the datatype handles and the datatype they represent. This correspondence was established when the datatypes were created. Before the library is used, a library initialization preamble must be executed. This preamble code will define the datatypes that are used by the library, and store handles to these datatypes in global, static variables that are shared by the user code and the library code.

The Fortran version of MPI_REDUCE will invoke a user-defined reduce function using the Fortran calling conventions and will pass a Fortran-type datatype argument; the C version will use C calling convention and the C representation of a datatype handle. Users who plan to mix languages should define their reduction functions accordingly. ( End of advice to users.)

Advice to implementors.

We outline below a naive and inefficient implementation of MPI_REDUCE not supporting the ``in place'' option.


      MPI_Comm_size(comm, &groupsize); 
      MPI_Comm_rank(comm, &rank); 
      if (rank > 0) { 
          MPI_Recv(tempbuf, count, datatype, rank-1,...); 
          User_reduce(tempbuf, sendbuf, count, datatype); 
      } 
      if (rank < groupsize-1) { 
          MPI_Send(sendbuf, count, datatype, rank+1, ...); 
      } 
      /* answer now resides in process groupsize-1 ... now send to root 
       */ 
      if (rank == root) { 
          MPI_Irecv(recvbuf, count, datatype, groupsize-1,..., &req); 
      } 
      if (rank == groupsize-1) { 
          MPI_Send(sendbuf, count, datatype, root, ...); 
      } 
      if (rank == root) { 
          MPI_Wait(&req, &status); 
      } 
The reduction computation proceeds, sequentially, from process 0 to process groupsize-1. This order is chosen so as to respect the order of a possibly non-commutative operator defined by the function User_reduce(). A more efficient implementation is achieved by taking advantage of associativity and using a logarithmic tree reduction. Commutativity can be used to advantage, for those cases in which the commute argument to MPI_OP_CREATE is true. Also, the amount of temporary buffer required can be reduced, and communication can be pipelined with computation, by transferring and reducing the elements in chunks of size len <count.

The predefined reduce operations can be implemented as a library of user-defined operations. However, better performance might be achieved if MPI_REDUCE handles these functions as a special case. ( End of advice to implementors.)

MPI_OP_FREE(op)
INOUT op operation (handle)

int MPI_Op_free(MPI_Op *op)

MPI_Op_free(op, ierror)
TYPE(MPI_Op), INTENT(INOUT) :: op
INTEGER, OPTIONAL, INTENT(OUT) :: ierror
MPI_OP_FREE(OP, IERROR)
INTEGER OP, IERROR

Marks a user-defined reduction operation for deallocation and sets op to MPI_OP_NULL.


PreviousUpNext
Up: Global Reduction Operations Next: Example of User-defined Reduce Previous: MINLOC and MAXLOC


110.1. Example of User-defined Reduce

PreviousUpNext
Up: User-Defined Reduction Operations Next: All-Reduce Previous: User-Defined Reduction Operations

It is time for an example of user-defined reduction. The example in this section uses an intracommunicator.


Example Compute the product of an array of complex numbers, in C.

typedef struct { 
    double real,imag; 
} Complex; 
 
/* the user-defined function 
 */ 
void myProd(void *inP, void *inoutP, int *len, MPI_Datatype *dptr) 
{ 
    int i; 
    Complex c; 
    Complex *in = (Complex *)inP, *inout = (Complex *)inoutP; 
 
    for (i=0; i< *len; ++i) { 
        c.real = inout->real*in->real - 
                   inout->imag*in->imag; 
        c.imag = inout->real*in->imag + 
                   inout->imag*in->real; 
        *inout = c; 
        in++; inout++; 
    } 
} 
 
/* and, to call it... 
 */ 
... 
 
    /* each process has an array of 100 Complexes 
     */ 
    Complex a[100], answer[100]; 
    MPI_Op myOp; 
    MPI_Datatype ctype; 
 
    /* explain to MPI how type Complex is defined 
     */ 
    MPI_Type_contiguous(2, MPI_DOUBLE, &ctype); 
    MPI_Type_commit(&ctype); 
    /* create the complex-product user-op 
     */ 
    MPI_Op_create( myProd, 1, &myOp ); 
 
    MPI_Reduce(a, answer, 100, ctype, myOp, root, comm); 
 
    /* At this point, the answer, which consists of 100 Complexes, 
     * resides on process root 
     */ 


Example How to use the mpi_f08 interface of the Fortran MPI_User_function.

  subroutine my_user_function( invec, inoutvec, len, type ) 
    use, intrinsic :: iso_c_binding, only : c_ptr, c_f_pointer 
    use mpi_f08 
    type(c_ptr), value :: invec, inoutvec 
    integer :: len 
    type(MPI_Datatype) :: type 
    real, pointer :: invec_r(:), inoutvec_r(:) 
    if (type%MPI_VAL == MPI_REAL%MPI_VAL) then 
       call c_f_pointer(invec, invec_r, (/ len /) ) 
       call c_f_pointer(inoutvec, inoutvec_r, (/ len /) ) 
       inoutvec_r = invec_r + inoutvec_r 
    end if 
  end subroutine 


PreviousUpNext
Up: User-Defined Reduction Operations Next: All-Reduce Previous: User-Defined Reduction Operations


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

(Unofficial) MPI-3.1 of June 4, 2015
HTML Generated on June 4, 2015