boost :: mpi和boost ::序列化错误

时间:2015-11-25 14:41:32

标签: c++ openmpi boost-serialization boost-mpi

我使用openmpi和linux mint, 请考虑以下示例:

#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;
}

编译:mpic ++ -std = c ++ 0x test.cc -lboost_serialization -lboost_mpi
运行:mpirun -np 2 ./a.out
输出: slave received = 3664010 msg = 3664010
slave received = 3664012 msg = 3664012
slave received = 3664012 msg = 3664012

只有当所有消息长度相等时才会重现Bug。例如,如果第二条消息是&#34; 3664012及更多&#34;一切正常:
slave received = 3664010 msg = 3664010
奴隶收到= 3664010 msg = 3664012及更多
奴隶收到= 3664010 msg = 3664012及更多
奴隶收到= 3664010 msg = 3664012及更多

它看起来像dst和msg使用相同的内存缓冲区。并且只有当字符串长度不同时,它们才开始使用不同的内存缓冲区。我使用以下解决方法(msg = string())告诉编译器msg已更改:

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

它工作正常。还有更好的解决方案吗?谢谢。

1 个答案:

答案 0 :(得分:0)

通过使用没有字符串牛的gcc c ++ 11来解决问题