使用 Boost C++ 库中的 mpi 模块实现 all_gather 集体通信操作的测试
MPI(Message Passing Interface)是一个并行计算中标准的消息传递库。Boost C++ 库提供了对 MPI 接口的封装,使得在 C++ 中使用 MPI 变得更加方便。
在 MPI 中,all_gather 是一种集体通信操作,用于将每个进程中的数据收集到所有进程中,形式为“广播+收集”,常用于数据分发或结果合并等场景。
下面是使用 Boost C++ 库中的 mpi::all_gather 函数实现 all_gather 集体通信操作的示例代码:
#include <boost/mpi.hpp>
#include <iostream>
#include <vector>
namespace mpi = boost::mpi;
int main(int argc, char* argv[]) {
mpi::environment env(argc, argv);
mpi::communicator world;
// 每个进程初始化自己的数据
int my_data = world.rank() + 1;
std::vector<int> all_data;
mpi::all_gather(world, my_data,