boost mpi 发送 NULL 消息

boost mpi sends NULL messages

我正在尝试使用 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

我无法弄清楚问题是什么。

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

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

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

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

如您所知,解决方案是发送和接收相同的类型。在这种情况下 std::string.