Sending

C | Fortran-2008 | Fortran-90

MPI_Send

Definition

MPI_Send is the standard send in MPI. Behind the scenes, it will issue a buffered send MPI_Bsend or a synchronous send MPI_Ssend. This decision will be based on whether the buffer attached for buffered sends contains enough free space for the message to send. If there is enough space, the buffered send MPI_Bsend will be issued, otherwise it will revert to a synchronous send MPI_Ssend. MPI implementations may provide a buffer by default, therefore not having explicitly assigned a buffer for buffered send does not guarantee that an MPI_Send will issue an MPI_Ssend. Tip: this specificity of MPI_Send to implicitly issue a buffered send or a synchronous send can result in deadlocks that are challenging to find out for beginners. In this case, explicitly issuing synchronous sends MPI_Ssend instead of standard sends MPI_Send may greatly help verify code correctness.
Other variants of MPI_Send are MPI_Ssend, MPI_Bsend and MPI_Rsend. See MPI_Isend for the non-blocking counterpart of MPI_Send.

Copy

Feedback

int MPI_Send(const void* buffer,
             int count,
             MPI_Datatype datatype,
             int recipient,
             int tag,
             MPI_Comm communicator);

Parameters

buffer

The buffer to send.

count

The number of elements to send.

datatype

The type of one buffer element.

recipient

The rank of the recipient MPI process.

tag

The tag to assign to the message.

communicator

The communicator in which the standard send takes place.

Return value

The error code returned from the standard send.

Example

Copy

Feedback

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

/**
 * @brief Illustrates how to send a message in a blocking fashion.
 * @details This program is meant to be run with 2 processes: a sender and a
 * receiver.
 **/
int main(int argc, char* argv[])
{
    MPI_Init(&argc, &argv);

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

    // 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:
        {
            int buffer_sent = 12345;
            printf("MPI process %d sends value %d.\n", my_rank, buffer_sent);
            MPI_Send(&buffer_sent, 1, MPI_INT, RECEIVER, 0, MPI_COMM_WORLD);
            break;
        }
        case RECEIVER:
        {
            int received;
            MPI_Recv(&received, 1, MPI_INT, SENDER, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
            printf("MPI process %d received value: %d.\n", my_rank, received);
            break;
        }
    }

    MPI_Finalize();

    return EXIT_SUCCESS;
}