c++openmpiboost-serializationboost-mpi

boost::mpi and boost::serialization errors


I use openmpi and linux mint, consider the following example:

#include <boost/mpi.hpp>
#include <iostream>
#include <string>
#include <boost/serialization/string.hpp>

namespace mpi = boost::mpi;
using namespace std;

int main()
{
    mpi::environment env;
    mpi::communicator world;

    if (world.rank() == 0)
    {
        world.send(1, 0, std::string("3664010"));
        while (1)
        {
             world.send(1, 0, std::string("3664012"));
             sleep(1);
        }
     }
     else
     {
        std::string msg;
        string dst;
        bool first = true;

        while (1)
        {
            world.recv(0, 0, msg);
            if (first) {dst = msg;first = false;}
            std::cout << "slave received=" << dst << " msg=" << msg << std::endl;
        }
     }

    return 0;
}

Compilation: mpic++ -std=c++0x test.cc -lboost_serialization -lboost_mpi
Run: mpirun -np 2 ./a.out
Output: slave received=3664010 msg=3664010
slave received=3664012 msg=3664012
slave received=3664012 msg=3664012

Bug is only reproduced when all message have equal length. If second message for example will be "3664012andmore" everything work fine:
slave received=3664010 msg=3664010
slave received=3664010 msg=3664012andmore
slave received=3664010 msg=3664012andmore
slave received=3664010 msg=3664012andmore

It looks like dst and msg use the same memory buffer. And they start to use different memory buffer only if strings length are different. I use following workaround(msg = string()) to tell compiler that msg is changed:

std::cout << "slave received=" << dst << " msg=" << msg << std::endl;
msg = string();

And it works fine. Are there any better solutions? Thank you.


Solution

  • Problem was solved by using gcc c++11 without string cow