Rookie HPC

About

Docs

Tools

Tests

MPI_Allgather

Definition

MPI_Allgather is a variant of MPI_Gather; it collects data from all processes in a given communicator. Unlike MPI_Gather however, the data collected during an MPI_Allgather is not stored in the receive buffer of a specific process but in that of each process instead. MPI_Allgather is a collective operation; all processes in the communicator must invoke this routine. Other variants of MPI_Allgather are MPI_Gather, MPI_Gatherv and MPI_Allgatherv. Refer to MPI_Iallgather to see the non-blocking counterpart of MPI_Allgather.

Copy

Feedback

int MPI_Allgather(void* buffer_send,
                  int count_send,
                  MPI_Datatype datatype_send,
                  void* buffer_recv,
                  const int* count_recv,
                  const int* displacements,
                  MPI_Datatype datatype_recv,
                  MPI_Comm communicator);

Parameters

buffer_send
The buffer containing the data to send.
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
An array containing the number of elements in the message to receive from each process, not the total number of elements to receive from all processes altogether. For non-root processes, the receiving parameters like this one are ignored.
displacements
An array containing the displacement to apply to the message received by each process. Displacements are expressed in number of elements, not bytes. 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.
communicator
The communicator in which the gather takes place.

Returned value

MPI_SUCCESS
The routine successfully completed.

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 3 MPI processes. Every MPI
 * process begins with a value, then every MPI process collects the entirety of
 * the data gathered and prints them. It can be visualised as follows:
 *
 * +-----------+           +-----------+           +-----------+
 * | Process 0 |           | Process 1 |           | Process 2 |
 * +-+-------+-+           +-+-------+-+           +-+-------+-+
 *   | Value |               | Value |               | Value |
 *   |   0   |               |  100  |               |  200  |
 *   +-------+               +-------+               +-------+
 *     | | |  _________________| | |_________________  | | |
 *     | | |_|___________________|_____________      | | | |
 *     | |___|_____________      |             |     | | | |
 *     |     |      _______|_____|_____________|_____|_| | |
 *     |     |     |       |     |      _______|_____|___| |
 *     |     |     |       |     |     |       |     |     |
 *  +-----+-----+-----+ +-----+-----+-----+ +-----+-----+-----+
 *  |  0  | 100 | 200 | |  0  | 100 | 200 | |  0  | 100 | 200 |
 *  +-----+-----+-----+ +-----+-----+-----+ +-----+-----+-----+
 *  |    Process 0    | |    Process 1    | |    Process 2    |
 *  +-----------------+ +-----------------+ +-----------------+
 **/
int main(int argc, char* argv[])
{
    MPI_Init(&argc, &argv);

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

    // 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);

    int buffer[3];
    MPI_Allgather(&my_value, 1, MPI_INT, buffer, 1, MPI_INT, MPI_COMM_WORLD);
    printf("Values collected on process %d: %d, %d, %d.\n", my_rank, buffer[0], buffer[1], buffer[2]);

    MPI_Finalize();

    return EXIT_SUCCESS;
}