Table of Contents

MPI Point-to-point communication

Point-to-point communication sends messages between two processes. MPI_Send transmits a buffer to a target rank; MPI_Recv receives from a source rank. Both specify buffer, element count, datatype, peer rank, message tag, and communicator.

if (rank == 0) {
    int x = 42;
    MPI_Send(&x, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
} else if (rank == 1) {
    int x;
    MPI_Recv(&x, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
    printf("received %d\n", x);
}

MPI_ANY_SOURCE and MPI_ANY_TAG are wildcards that match any sender or tag in MPI_Recv. When wildcards are used, the actual source, tag, and element count are returned in the MPI_Status struct. MPI_Sendrecv combines a send and a receive in a single call, which is the simplest way to avoid deadlock in patterns where every process must both send and receive simultaneously.