【问题标题】:boost mpi sends NULL messagesboost mpi 发送 NULL 消息
【发布时间】:2015-08-20 03:34:29
【问题描述】:

我正在尝试使用 boost 库向进程发送一些 MPI 消息。但是,接收方无法正确接收它们。接收者只得到 NULL,而不是真正的消息。我的代码在这里:

// Receiver side, rank = 0
boost::mpi::communicator* world
// initialization of the world, etc...

map<int, string> messageMap;
map<int, boost::mpi::request> requestMap;
for(int j=1; j<world->size(); j++){
    requestMap[j] = world->irecv(j, 0, messageMap[j]);
}

for(typename map<int, boost::mpi::request>::iterator it = requestMap.begin(); it != requestMap.end(); it++){
    it->second.wait();
    cout << "Received message from " << j << " is: " << messageMap[j] << endl;
}


// Sender side, ranks = 1, 2 and 3
boost::mpi::communicator* world
// initialization of the world, etc...
world->isend(0, 0, "1");

现在,问题是接收器的输出类似于:

Received message from 1 is: NULNUL
Received message from 2 is: NULNUL
Received message from 3 is: NULNUL

我不知道问题出在哪里。

【问题讨论】:

    标签: c++ string boost mpi


    【解决方案1】:

    您的问题出在底层boost::serialization

    在您的问题中,您发送一个字符串文字“1”,它的 C++ 类型为 const char[2]boost::serialization 将其序列化为大小为 2 的数组(而不是 std::string)。然后接收失败,因为 boost::mpi 尝试将数组反序列化为 std::string 并通过返回 null 静默失败。

    正如您已经想到的那样,解决方案是发送和接收相同的类型。在本例中为std::string

    【讨论】:

      【解决方案2】:

      我不知道为什么,但是当我像这样更改发送方时它是固定的:

      // Sender side, ranks = 1, 2 and 3
      boost::mpi::communicator* world
      // initialization of the world, etc...
      string s = "1";
      world->isend(0, 0, s);
      

      【讨论】:

      • isend(0, 0, "1") 的最后一个参数是一个指向 char 的指针,而isend(0, 0, s) 的最后一个参数是一个string 对象。
      猜你喜欢
      • 2018-05-25
      • 1970-01-01
      • 2010-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-14
      • 2016-01-08
      • 1970-01-01
      相关资源
      最近更新 更多