本章中的所有示例仅使用一个连接所有进程的通信器。但是,可以创建更多的通信器来链接进程的子集。这对于不需要由所有进程执行的集体操作特别有用。
示例 47.15。使用多个通信器
- #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;
- boost::mpi::communicator local = world.split(world.rank() < 2 ? 99 : 100);
- std::string s;
- if (world.rank() == 0)
- s = "Hello, world!";
- boost::mpi::broadcast(local, s, 0);
- std::cout << world.rank() << ": " << s << '\n';
- }
示例 47.15 使用函数 boost::mpi::broadcast()。此函数发送字符串“Hello, world!”从等级为 0 的进程到链接到本地通信器的所有进程。等级为 0 的进程也必须链接到该通信器。
本地通信