0

I'm looking for something similar to the question asked here: MPI communicator as global variable.

The answer given there is essentially that MPI_COMM_WORLD is a global variable.

Boost wraps its communicators nicely, and generates a communicator wrapper for MPI_COMM_WORLD by default when constructing an object of type boost::mpi::communicator. [Boost MPI tutorial]

My question:

If I want to access something given by the communicator, for example a process's rank, I can call

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

This will also work in a separate function, outside of main.cpp, etc. If I use the same constructor there, will I get the same mpi_rank value in each case? More specifically, will the process with rank = 0 still have rank = 0 if I construct a new boost::mpi::communicator object in a new file/function? It doesn't seem clear to me from the Boost documentation.

Community
  • 1
  • 1
chrisb2244
  • 2,940
  • 22
  • 44

2 Answers2

0

Simple answer: Looks like yes.

Example program (without relevant #includes, nice syntax):

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;
    }
};

This program always prints the same number twice (for always == 10 times, or so, with 4 processes. Not an exhaustive test by any means, but convincing for me at least)

chrisb2244
  • 2,940
  • 22
  • 44
0

Confirming what you have already tested. Yes. Once you have set up an MPI communicator (or are using MPI_COMM_WORLD as in your case), a process' rank within that communicator will not change throughout the program. Even across files/functions.

NoseKnowsAll
  • 4,593
  • 2
  • 23
  • 44