c ++:MPI通信器作为全局变量 [英] c++: MPI communicator as global variable
问题描述
我需要在函数/类成员函数中访问MPI世界通信器。但是根据设计/约定,MPI环境和通信器总是在 int main()
开始时定义和初始化。
唯一的,简单的解决方案,我可以想到使用通信器的全局指针。
有人知道更好的方法吗?使用全局指针解决方案是危险的吗?
这个问题同样适用于裸骨骼MPI和Boost :: MPI(我在下面使用) p>
我提出的解决方案示例(未测试):
//全局变量。 h
extern boost :: mpi :: communicator * my_MPI_world_ptr;
和
// 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();
...
}
你是说实际的MPI MPI_COMM_WORLD
通信器(或者它的Boost封装器)?这已经是全球性的。如果使用不同的通信器将通信与正在编写的库分离,最好避免使用全局变量。在这种情况下,你可能想传递它(或指向它)的指针,并将其存储在需要它的类中。
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.
这篇关于c ++:MPI通信器作为全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!