NERSC logo National Energy Research Scientific Computing Center
  A DOE Office of Science User Facility
  at Lawrence Berkeley National Laboratory
 

Reduction

Often you will have calculated some quantity or quantities on many processors and you will need to collect or reduce that data onto one or all processors. MPI provides routines to perform these reductions.

One Collector

If each task in your job has calculated a private value for some variable, var, with the same name on all tasks, the MPI_REDUCE routine will reduce all those values, according to some operation, and store the result in a variable on one task. The syntax is

Fortran

FORTRAN_TYPE:: sendbuf, recbuf
INTEGER:: count, root, ierr

CALL MPI_REDUCE(sendbuf, recbuf, count, &
		MPI_TYPE, MPI_OP,  root, comm, ierr)

C

int MPI_Reduce( void *sendbuf, void *recbuf, int count, 
	MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm)

where

  • sendbuf : the variable to be collected from all tasks.
  • recbuf : the variable where the result of operating MPI_OP on all those values will be placed
  • MPI_OP : one of a number of functions to be performed on the values being collected.
  • root : the only task that will collect the result of the reduce command in recbuf.

Possible values for MPI_OP are:

MPI Name Operation
MPI_MAX Maximum
MPI_MIN Minimum
MPI_PROD Product
MPI_SUM Sum
MPI_LAND Logical AND
MPI_LOR Logical OR
MPI_LXOR Logical EXCLUSIVE OR
MPI_BAND Bitwise AND
MPI_BOR Bitwise OR
MPI_BXOR Bitwise EXCLUSIVE OR
MPI_MAXLOC Maximum value and location
MPI_MINLOC Minimum value and location

Everyone Collects

You may wish to have the reduction available to all tasks. The routine MPI_ALLREDUCE performs that function. It's almost the same as the basic reduce command:

Fortran

FORTRAN_TYPE:: sendbuf, recbuf
INTEGER:: count, ierr

CALL MPI_ALLREDUCE(sendbuf, recbuf, count, MPI_TYPE, MPI_OP, comm, ierr)

C

int MPI_Allreduce( void *sendbuf, void *recbuf,  int count,
	MPI_Datatype datatype, MPI_Op op, MPI_Comm comm )

The only difference between this call and MPI_REDUCE is the lack of a root task in the call statement.


LBNL Home
Page last modified: Fri, 21 May 2004 22:25:58 GMT
Page URL: http://www.nersc.gov/nusers/help/tutorials/mpi/intro/reduce.php
Web contact: webmaster@nersc.gov
Computing questions: consult@nersc.gov

Privacy and Security Notice
DOE Office of Science