到目前为止介绍的功能共享一对一的关系:即一个进程发送和一个进程接收。链接是通过标签建立的。本节介绍在多个进程中调用相同参数但执行不同操作的函数。对于一个进程,函数可能会发送数据,对于另一个进程,它可能会接收数据。这些功能称为集体操作。
示例 47.9。使用 gather() 从多个进程接收数据
- #include <boost/mpi.hpp>
- #include <boost/serialization/string.hpp>
- #include <vector>
- #include <string>
- #include <iterator>
- #include <algorithm>
- #include <iostream>
-
- int main(int argc, char *argv[])
- {
- boost::mpi::environment env{argc, argv};
- boost::mpi::communicator world;
- if (world.rank() == 0)
- {
- std::vector<std::string> v;
- boost::mpi::gather<std::string>(world, "", v, 0);
- std::ostream_iterator<std::string> out{std::cout, "\n"};
- std::copy(v.begin(), v.end(), out);
- }
- else if (world.rank() == 1)
- {
- boost::mpi::gather(world, std::string{"Hello, world!"}, 0);
- }
- else if (world.rank() == 2)
- {
- boo