Rookie HPC

Synchronisation

C | FORTRAN-legacy | FORTRAN-2008

MPI_Waitall

Definition

MPI_Waitall is a version of MPI_Wait that can be applied to an array of request handlers. This version waits until all the non-blocking routines concerned complete. Other versions are MPI_Wait, MPI_Waitany and MPI_Waitsome.

Copy

Feedback

int MPI_Waitall(int count,
                MPI_Request requests[],
                MPI_Status statuses[]);

Parameters

count
Number of request handlers to wait on, as well as the number of statuses contained in the array passed (if MPI_STATUSES_IGNORE is not passed).
requests
The request handlers on the non-blocking routines to wait on.
statuses
The array in which write the statuses returned by the non-blocking routines concerned. It can be MPI_STATUSES_IGNORE if statuses are unused.

Returned value

The error code returned from the waitings.

MPI_SUCCESS
The routine successfully completed.

Example

Copy

Feedback

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

/**
 * @brief Illustrates how to wait for the completion of multiple non-blocking
 * operations.
 * @details This program is meant to be run with 3 processes: a sender and two
 * receivers.
 **/
int main(int argc, char* argv[])
{
    MPI_Init(&argc, &argv);

    // Get the number of processes and check only 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 processes.\n");
        MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
    }

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

    if(my_rank == 0)
    {
        // The "master" MPI process sends the message.
        int buffer[2] = {12345, 67890};
        MPI_Request requests[2];
        printf("MPI process %d sends the values %d & %d.\n", my_rank, buffer[0], buffer[1]);
        MPI_Isend(&buffer[0], 1, MPI_INT, 1, 0, MPI_COMM_WORLD, &requests[0]);
        MPI_Isend(&buffer[1], 1, MPI_INT, 2, 0, MPI_COMM_WORLD, &requests[1]);

        // Wait for both routines to complete
        MPI_Waitall(2, requests, MPI_STATUSES_IGNORE);
        printf("Process %d: both messages have been sent.\n", my_rank);
    }
    else
    {
        // The "slave" MPI processes receive the message.
        int received;
        MPI_Recv(&received, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
        printf("Process %d received value %d.\n", my_rank, received);
    }

    MPI_Finalize();

    return EXIT_SUCCESS;
}