所以我将这个简单的 MPI 示例放在一起。我在测试 boost::mpi::request 时看到一些我无法解释的奇怪行为。具体来说,如果您删除第二个循环的注释,它将永远旋转。对 boost::mpi::request 的测试是否只返回 true 一次?如果是这样,哪些状态正在更新?我浏览了 Boost 的 MPI 和可选代码,但我无法解释我所看到的内容。
(当然,对于外行来说,您需要使用带有两个节点的 mpiexec 来自己运行它。)
# include "stdafx.h"
# include <boost/serialization/string.hpp>
# include <boost/mpi.hpp>
# include <windows.h>
# include <iostream>
# include <boost/mpi.hpp>
# include <boost/optional.hpp>
int main(int argc, char *argv[]);
int main(int argc, char *argv[])
{
boost::mpi::environment m_env;
boost::mpi::communicator m_world;
if (m_world.rank() == 0)
{
m_world.send(1,0, std::string("hi!"));
}
else
{
std::shared_ptr<std::string> rcv = std::shared_ptr<std::string>(new std::string());
boost::mpi::request x = m_world.irecv(0, 0, *rcv);
while (!x.test())
{
Sleep(10);
}
//while (!x.test())
//{
// Sleep(10);
//}
std::cout << *rcv;
}
}
最佳答案
答案在文档中,类似于:
/**
* Determine whether the communication associated with this request
* has completed successfully. If so, returns the @c status object
* describing the communication. Otherwise, returns an empty @c
* optional<> to indicate that the communication has not completed
* yet. Note that once @c test() returns a @c status object, the
* request has completed and @c wait() should not be called.
*/
optional<status> test();
然后看docs for the underlying MPI_Test function :
A call to MPI_TEST returns flag = true if the operation identified by request is complete. In such a case, the status object is set to contain information on the completed operation; if the communication object was created by a nonblocking send or receive, then it is deallocated and the request handle is set to MPI_REQUEST_NULL.
One is allowed to call MPI_TEST with a null or inactive request argument. In such a case the operation returns with flag = true and empty status.
所以我们看到的是 Boost MPI 的 test()
方法返回 optional<status>
, 和 MPI_Test()
只能返回一次状态(之后,请求被销毁)。 MPI_Test()
重复调用会返回 flag = true
,但这不是您要检查的内容。如果你真的需要这个模式,你可以调用 MPI_Test()
你自己并使用返回的标志而不是状态。或者只是在你的应用程序中做簿记,不要调用 boost::mpi::request::test()
在同一个请求上两次。
查看此问题的另一种方法是您正在使用 test()
的结果在 bool 上下文中,您希望它像 flag
一样工作的 MPI_Test()
, 但实际上它像 status
一样工作, 它的 bool 性质只是一种幻觉。
关于c++ - 我对 boost::mpi::request 缺少什么?测试似乎改变了状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43739482/