异步数据交换,除了阻塞函数 send() 和 recv() 之外,Boost.MPI 还支持与成员函数 isend() 和 irecv() 的异步数据交换。名称以 i 开头,表示函数立即返回。
示例 47.7。使用 irecv() 异步接收数据
- #include <boost/mpi.hpp>
- #include <boost/serialization/string.hpp>
- #include <string>
- #include <iostream>
-
- int main(int argc, char *argv[])
- {
- boost::mpi::environment env{argc, argv};
- boost::mpi::communicator world;
- if (world.rank() == 0)
- {
- std::string s;
- boost::mpi::request r = world.irecv(boost::mpi::any_source, 16, s);
- if (r.test())
- std::cout << s << '\n';
- else
- r.cancel();
- }
- else
- {
- std::string s = "Hello, world!";
- world.send(0, 16, s);
- }
- }
示例 47.7 使用阻塞函数 send() 发送字符串“Hello, world!”但是,数据是通