【发布时间】:2020-08-22 11:33:28
【问题描述】:
我想在我的班级中使用boost::mpi 通信器,因为我希望我的班级负责所有 MPI 调用。我使用这种样式使它们成为我班级的静态成员。
// works.cpp
// mpic++ -o works works.cpp -lboost_mpi
#include <boost/mpi.hpp>
#include <iostream>
class Example {
static boost::mpi::environment env;
static boost::mpi::communicator world;
public:
Example() {
std::cout << world.rank() << std::endl;
}
};
boost::mpi::environment Example::env;
boost::mpi::communicator Example::world;
int main() {
auto e = Example();
}
这很好用,例如,mpirun -n 4 ./works 按预期打印数字 0 到 3。后来我想为我的课程模板。一开始我还担心如何初始化我的静态成员变量,但看了这个answer,这表明一切都很好
// fails.cpp
// mpic++ -o fails fails.cpp -lboost_mpi
#include <boost/mpi.hpp>
#include <iostream>
template<typename T>
class Example {
static boost::mpi::environment env;
static boost::mpi::communicator world;
public:
Example() {
std::cout << world.rank() << std::endl;
}
};
template <typename T>
boost::mpi::environment Example<T>::env;
template <typename T>
boost::mpi::communicator Example<T>::world;
int main() {
auto e = Example<double>();
}
然而,这实际上给了我
$ mpirun -n 4 ./fails
*** The MPI_Comm_rank() function was called before MPI_INIT was invoked.
*** This is disallowed by the MPI standard.
*** Your MPI job will now abort.
...
这里出了点问题,但是什么?为什么?我想我在这里误解了一些事情。
【问题讨论】: