Rookie HPC

Receiving

C | FORTRAN-legacy | FORTRAN-2008

MPI_ERROR

Definition

MPI_ERROR is the member of the MPI_Status structure that contains the error code of the corresponding receive operation. Although in practice it has no use for a blocking receive (MPI_Recv) since the error code is returned directly by the routine, it does have uses for its non-blocking counterpart (MPI_Irecv) where the MPI_Status cannot be obtained until the MPI_Request collected is waited on (MPI_Wait, MPI_Waitall, MPI_Waitany, MPI_Waitsome) or tested (MPI_Test, MPI_Testall, MPI_Testany, MPI_Testsome).

Example

Copy

Feedback

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

/**
 * @brief Display the error code contained in the MPI_Status.
 **/
int main(int argc, char* argv[])
{
    MPI_Init(&argc, &argv);

    // Get my rank and do the corresponding job
    enum role_ranks { SENDER, RECEIVER };
    int my_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
    switch(my_rank)
    {
        case SENDER:
        {
            // The "master" MPI process issues the MPI_Bsend.
            int buffer_sent = 12345;
            printf("MPI process %d sends value %d.\n", my_rank, buffer_sent);
            MPI_Ssend(&buffer_sent, 1, MPI_INT, RECEIVER, 0, MPI_COMM_WORLD);
            break;
        }
        case RECEIVER:
        {
            // The "slave" MPI process receives the message.
            int buffer_received;
            MPI_Status status;
            MPI_Recv(&buffer_received, 1, MPI_INT, SENDER, 0, MPI_COMM_WORLD, &status);
            printf("MPI process %d received value %d, with error code %d.\n", my_rank, buffer_received, status.MPI_ERROR);
            break;
        }
    }

    MPI_Finalize();

    return EXIT_SUCCESS;
}