Collectives

C | Fortran-2008 | Fortran-90

MPI_Gather

Definition

MPI_Gather collects data from all processes in a given communicator and concatenates them in the given buffer on the specified process. The concatenation order follows that of the ranks. This is a collective operation; all processes in the communicator must invoke this routine. Other variants of MPI_Gather are MPI_Gatherv, MPI_Allgather and MPI_Allgatherv. Refer to MPI_Igather to see the non-blocking counterpart of MPI_Gather.

Copy

Feedback

int MPI_Gather(void* buffer_send,
               int count_send,
               MPI_Datatype datatype_send,
               void* buffer_recv,
               int count_recv,
               MPI_Datatype datatype_recv,
               int root,
               MPI_Comm communicator);

Parameters

buffer_send

The buffer containing the data to send. The “in place” option for intra-communicators is specified by passing MPI_IN_PLACE as the value of buffer_send at the root. In such a case, count_send and datatype_send 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.

count_send

The number of elements in the send buffer.

datatype_send

The type of one send buffer element.

buffer_recv

The buffer in which store the gathered data for the root process. For other processes, the receiving parameters like this one are ignored.

count_recv

The number of elements per message received, not the total number of elements to receive from all processes altogether. For non-root processes, the receiving parameters like this one are ignored.

datatype_recv

The type of one receive buffer element. For non-root processes, the receiving parameters like this one are ignored.

root

The rank of the root process, which will collect the data gathered.

communicator

The communicator in which the gather takes place.

Return value

The error code returned from the gathering.

Example

Copy

Feedback

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

/**
 * @brief Illustrates how to use a gather.
 * @details This application is meant to be run with 4 MPI processes. Every MPI
 * process begins with a value, then MPI process 0 is picked to gather all these
 * values and print them. It can be visualised as follows:
 *
 * +-----------+ +-----------+ +-----------+ +-----------+
 * | Process 0 | | Process 1 | | Process 2 | | Process 3 |
 * +-+-------+-+ +-+-------+-+ +-+-------+-+ +-+-------+-+ 
 *   | Value |     | Value |     | Value |     | Value |   
 *   |   0   |     |  100  |     |  200  |     |  300  |   
 *   +-------+     +-------+     +-------+     +-------+   
 *            \            |     |            /
 *             \           |     |           /
 *              \          |     |          /
 *               \         |     |         /
 *                \        |     |        /
 *                 \       |     |       /
 *                +-----+-----+-----+-----+
 *                |  0  | 100 | 200 | 300 |
 *                +-----+-----+-----+-----+
 *                |       Process 0       |
 *                +-----------------------+
 **/
int main(int argc, char* argv[])
{
    MPI_Init(&argc, &argv);

    // Get number of processes and check that 4 processes are used
    int size;
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    if(size != 4)
    {
        printf("This application is meant to be run with 4 MPI processes.\n");
        MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
    }

    // Determine root's rank
    int root_rank = 0;

    // Get my rank
    int my_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

    // Define my value
    int my_value = my_rank * 100;
    printf("Process %d, my value = %d.\n", my_rank, my_value);

    if(my_rank == root_rank)
    {
        int buffer[4];
        MPI_Gather(&my_value, 1, MPI_INT, buffer, 1, MPI_INT, root_rank, MPI_COMM_WORLD);
        printf("Values collected on process %d: %d, %d, %d, %d.\n", my_rank, buffer[0], buffer[1], buffer[2], buffer[3]);
    }
    else
    {
        MPI_Gather(&my_value, 1, MPI_INT, NULL, 0, MPI_INT, root_rank, MPI_COMM_WORLD);
    }

    MPI_Finalize();

    return EXIT_SUCCESS;
}