提升 MPI World Communicator 复制

Boost MPI World Communicator duplication

我正在寻找与此处提出的问题类似的内容:MPI communicator as global variable

那里给出的答案基本上是 MPI_COMM_WORLD 是一个全局变量。

Boost 很好地包装了它的通信器,并在构造 boost::mpi::communicator 类型的对象时默认为 MPI_COMM_WORLD 生成一个通信器包装器。 [Boost MPI tutorial]

我的问题:

如果我想访问通信器给出的东西,例如进程的等级,我可以调用

boost::mpi::communicator world;
int mpi_rank  = world.rank();

这也将在 main.cpp 之外的单独函数中工作,等等。如果我在那里使用相同的构造函数,我会在每种情况下得到相同的 mpi_rank 值吗?更具体地说,如果我在新的 file/function 中构造一个新的 boost::mpi::communicator 对象,具有 rank = 0 的进程是否仍然具有 rank = 0?从 Boost 文档中我似乎不清楚。

简单回答:看起来是的。

示例程序(没有相关的 #includes,语法很好):

int main()
{
    boost::mpi::communicator main_comm;
    int mpi_rank = main_comm.rank();
    myClass myObj;
    myObj.test(mpi_rank);
}

// Separate file
class myClass
{
    void test(int i) {
        boost::mpi::communicator local;
        int local_rank = local.rank();
        std::cout << "local rank = " << local_rank 
                  << ", i(main rank) = " << i 
                  << std::endl;
    }
};

这个程序总是打印相同的数字两次(总是 == 10 次左右,有 4 个进程。无论如何都不是详尽的测试,但至少对我来说是有说服力的)

确认您已经测试过的内容。是的。一旦您设置了 MPI 通信器(或在您的情况下使用 MPI_COMM_WORLD),该通信器中的进程等级将不会在整个程序中改变。甚至跨越 files/functions.