Rookie HPC

About

Docs

Tools

Tests

MPI_Igather

Definition

MPI_Igather is the non-blocking version of MPI_Gather.

Copy

Feedback

int MPI_Igather(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,
                MPI_Request* request);

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
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.
request
The variable in which store the handler on the non-blocking operation.

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 in a non-blocking way.
 * @details Every MPI process begins with a value, the MPI process 0 will 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);

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

    // Request handler
    MPI_Request request;

    if(my_rank == 0)
    {
        // Get number of processes
        int size;
        MPI_Comm_size(MPI_COMM_WORLD, &size);

        int* buffer = (int*)calloc(size, sizeof(int));
        printf("Process %d, my value = %d.\n", my_rank, my_value);

        // Launch the gather
        MPI_Igather(&my_value, 1, MPI_INT, buffer, 1, MPI_INT, root_rank, MPI_COMM_WORLD, &request);

        // Do another job
        // ...

        // Wait for the scatter to complete
        MPI_Wait(&request, MPI_STATUS_IGNORE);

        for(int i = 0; i < size; i++)
        {
            printf("Value received from process %d: %d.\n", i, buffer[i]);
        }
        free(buffer);
    }
    else
    {
        printf("Process %d, my value = %d.\n", my_rank, my_value);

        // Launch the gather
        MPI_Igather(&my_value, 1, MPI_INT, NULL, 0, MPI_INT, root_rank, MPI_COMM_WORLD, &request);

        // Do another job
        // ...

        // Wait for the scatter to complete
        MPI_Wait(&request, MPI_STATUS_IGNORE);

    }

    MPI_Finalize();

    return EXIT_SUCCESS;
}