I need the MPI world communicator to be accessible in functions/class member functions. But by design/convention, MPI environments and communicators are always defined and initialized at the beginning of int main()
.
The only, simple solution I can think of using a global pointer to the communicator.
Does anybody know of a better way? Is it dangerous to use the global pointer solution?
This problem applies equally well to bare-bones MPI, and to Boost::MPI (which I use below)
Example of my proposed solution (untested):
//globals.h
extern boost::mpi::communicator * my_MPI_world_ptr;
and
//main.cpp
...
int main(int argc, char* argv[])
{
boost::mpi::environment my_boost_mpi_env(argc, argv);
boost::mpi::communicator my_MPI_world;
my_MPI_world_ptr = &my_MPI_world;
my_MPI_rank = my_MPI_world_ptr->rank();
size_MPI_WORLD = my_MPI_world_ptr->size();
my_class an_Object;
an_Object.member_function_that_uses__MPI_world();
...
}
Do you mean the actual MPI MPI_COMM_WORLD
communicator (or the Boost wrapper of it)? That is already global. If you are using a different communicator to separate communication from a library that you are writing, it would be better to avoid using a global variable for it at all. In that case, you might want to just pass it (or a pointer to it) around and store it in the classes that need it.