How to send and receive string using MPI

Viewed 20317

I am trying to send and recieve string using MPI but results are hopless.

Send function:

MPI_Send(&result, result.size(), MPI_CHAR, 0, 0, MPI_COMM_WORLD);

And the recv function:

    MPI_Recv(&result,      /* message buffer */
        128,                 /* one data item */
        MPI_CHAR,        /* of type char real */
        MPI_ANY_SOURCE,    /* receive from any sender */
        MPI_ANY_TAG,       /* any type of message */
        MPI_COMM_WORLD,    /* default communicator */
        &status);          /* info about the received message */

Where result is a string.

I didn't get any error but program doesn't want to finish.

2 Answers

I was having similar problem where I had to pass a string between two processes in MPI C. My solution, aligned to your problem, is the following:

MPI_Send(&result, strlen(result)+1, MPI_CHAR, 1, 0, MPI_COMM_WORLD); // Destination: Process 1

On the other side, process 0 will have the following MPI_Recv function:

MPI_Recv(&result, 100, MPI_CHAR, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); // Source: Process 0 and considering 100 as the maximum string length

<string.h> has to be added in the header.

Related