[mpich-discuss] a question about MPI_Send & MPI_Recv

samantha lin wl8150 at googlemail.com
Tue Aug 18 18:10:30 CDT 2009


Hi,
I got a simple example program from a book, PPMPI. The purpose of the
program:
Send a message from all processes with rank != 0 to process 0. Process 0
prints the messages received.

The code is basically like the following:

int main(int argc, char* argv[])
{
....
char        message[100];  /* storage for message  */
...
    if (my_rank != 0) {
        /* Create message */
        sprintf(message, "Greetings from process %d!",
            my_rank);
        printf("len = %d\n", strlen(message));
        dest = 0;
        /* Use strlen+1 so that '\0' gets transmitted */
        MPI_Send(message, strlen(message)+1, MPI_CHAR, dest, tag,
MPI_COMM_WORLD);
    } else { /* my_rank == 0 */
        for (source = 1; source < p; source++) {
            MPI_Recv(message, 100, MPI_CHAR, source, tag, MPI_COMM_WORLD,
&status);
            printf("%s\n", message);
        }
    }
...
}

The program can compile and run successfully.
But count size of sent message, strlen(message)+1, is somehow different from
that of received message,100.
As this is synchronous send and receive, the receiver is supposed to wait
until it receives enough data.
Does anyone know how this is implemented in MPI? Or I misunderstand the
functions?

Sam
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/mpich-discuss/attachments/20090819/a61ea3a6/attachment.htm>


More information about the mpich-discuss mailing list