The Great and Terrible implementation of MPI-2

function index


Gathers together values from a group of processes
int MPI_Gather(
  void *sendbuf,
  int sendcnt,
  MPI_Datatype sendtype,
  void *recvbuf,
  int recvcnt,
  MPI_Datatype recvtype,
  int root,
  MPI_Comm comm


[in] starting address of send buffer (choice)
[in] number of elements in send buffer (integer)
[in] data type of send buffer elements (handle)
[out] address of receive buffer (choice, significant only at root)
[in] number of elements for any single receive (integer, significant only at root)
[in] data type of recv buffer elements (significant only at root) (handle)
[in] rank of receiving process (integer)
[in] communicator (handle)


Each process (root process included) sends the contents of its send buffer to the root process. The root process receives the messages and stores them in rank order. The outcome is as if each of the n processes in the group (including the root process) had executed a call to

and the root had executed n calls to

where extent(recvtype) is the type extent obtained from a call to MPI_Type_extent().

An alternative description is that the n messages sent by the processes in the group are concatenated in rank order, and the resulting message is received by the root as if by a call to MPI_RECV(recvbuf, recvcount n, recvtype, ...).

The receive buffer is ignored for all non-root processes.

General, derived datatypes are allowed for both sendtype and recvtype. The type signature of sendcount, sendtype on process i must be equal to the type signature of recvcount, recvtype at the root. This implies that the amount of data sent must be equal to the amount of data received, pairwise between each process and the root. Distinct type maps between sender and receiver are still allowed.

All arguments to the function are significant on process root, while on other processes, only arguments sendbuf, sendcount, sendtype, root, comm are significant. The arguments root and comm must have identical values on all processes.

The specification of counts and types should not cause any location on the root to be written more than once. Such a call is erroneous.

Note that the recvcount argument at the root indicates the number of items it receives from each process, not the total number of items it receives.

The "in place" option for intracommunicators is specified by passing MPI_IN_PLACE as the value of sendbuf at the root. In such a case, sendcount and sendtype are ignored, and the contribution of the root to the gathered vector is assumed to be already in the correct place in the receive buffer

If comm is an intercommunicator, then the call involves all processes in the intercommunicator, but with one group (group A) defining the root process. All processes in the other group (group B) pass the same value in argument root, which is the rank of the root in group A. The root passes the value MPI_ROOT in root. All other processes in group A pass the value MPI_PROC_NULL in root. Data is gathered from all processes in group B to the root. The send buffer arguments of the processes in group B must be consistent with the receive buffer argument of the root.

Thread and Interrupt Safety

This routine is thread-safe. This means that this routine may be safely used by multiple threads without the need for any user-provided thread locks. However, the routine is not interrupt safe. Typically, this is due to the use of memory allocation routines such as malloc or other non-MPICH runtime routines that are themselves not interrupt-safe.

Notes for Fortran

All MPI routines in Fortran (except for MPI_WTIME and MPI_WTICK) have an additional argument ierr at the end of the argument list. ierr is an integer and has the same meaning as the return value of the routine in C. In Fortran, MPI routines are subroutines, and are invoked with the call statement. All MPI objects (e.g., MPI_Datatype, MPI_Comm) are of type INTEGER in Fortran.


All MPI routines (except MPI_Wtime and MPI_Wtick) return an error value; C routines as the value of the function and Fortran routines in the last argument. Before the value is returned, the current MPI error handler is called. By default, this error handler aborts the MPI job. The error handler may be changed with MPI_Comm_set_errhandler (for communicators), MPI_File_set_errhandler (for files), and MPI_Win_set_errhandler (for RMA windows). The MPI-1 routine MPI_Errhandler_set may be used but its use is deprecated. The predefined error handler MPI_ERRORS_RETURN may be used to cause error values to be returned. Note that MPI does not guarentee that an MPI program can continue past an error; however, MPI implementations will attempt to continue whenever possible.
No error; MPI routine completed successfully.
Invalid communicator. A common error is to use a null communicator in a call (not even allowed in MPI_Comm_rank).
Invalid count argument. Count arguments must be non-negative; a count of zero is often valid.
Invalid datatype argument. May be an uncommitted MPI_Datatype (see MPI_Type_commit).
Invalid buffer pointer. Usually a null buffer where one is not valid.

Example Code

The following sample code illustrates MPI_Gather.

#include "mpi.h"
#include <stdlib.h>
#include <stdio.h>

/* Gather data from a vector to contiguous */

int main( int argc, char **argv )
    MPI_Datatype vec;
    MPI_Comm comm;
    double *vecin, *vecout;
    int minsize = 2, count;
    int root, i, n, stride, errs = 0;
    int rank, size;
    MPI_Init( &argc, &argv );
    comm = MPI_COMM_WORLD;
    /* Determine the sender and receiver */
MPI_Comm_rank( comm, &rank );
    MPI_Comm_size( comm, &size );
for (root=0; root<size; root++) {
        for (count = 1; count < 65000; count = count * 2) {
            n = 12;
            stride = 10;
            vecin = (
double *)malloc( n * stride * size * sizeof(double) );
            vecout = (
double *)malloc( size * n * sizeof(double) );
            MPI_Type_vector( n, 1, stride, MPI_DOUBLE, &vec );
            MPI_Type_commit( &vec );

            for (i=0; i<n*stride; i++) vecin[i] =-2;
for (i=0; i<n; i++) vecin[i*stride] = rank * n + i;

            MPI_Gather( vecin, 1, vec, vecout, n, MPI_DOUBLE, root, comm );

if (rank == root) {
                for (i=0; i<n*size; i++) {
if (vecout[i] != i) {
if (errs < 10) {
                            fprintf( stderr, "vecout[%d]=%d\n", i, (
int)vecout[i] );fflush(stderr);
            MPI_Type_free( &vec );
            free( vecin );
            free( vecout );
/* do a zero length gather */
return 0;