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.
Using tags: Tags are a way to identify types of messages. So, a receiving process can use these tags to decide what messages it wants to receive at a given time.
Deadlock or race conditions occur when the message passing cannot be completed. Consider the following and assume that the MPI_Send does not complete until the correspondingMPI_Recv is posted and visa versa. The MPI_Send commands will never be completed and the program will deadlock.
Blocking communication is done using MPI_Send() and MPI_Recv() . These functions do not return (i.e., they block) until the communication is finished.
The address of a std::string is not the same as address of the underlying C string, so sending should be fixed like this:
MPI_Send(result.c_str(), result.size(), MPI_CHAR, 0, 0, MPI_COMM_WORLD);
Receiving cannot be done as you tried to do it. You need a buffer (an array of char), which you will pass to MPI_Recv and use it later to create a std::string instance. To get a length of the string received use MPI_Get_count - this you should pass along the pointer to buffer to std::string constructor.
In order to avoid preallocating the buffer and allow it to receive a string of any size you should call MPI_Probe and MPI_Get_count to get the length first. Knowing the length you can allocate the buffer with the exact size which is necessary, and only then call MPI_Recv.
If this sounds a bit complicated then you can consider using BOOST MPI wrappers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With