AMDiS  0.1
The Adaptive Multi-Dimensional Simulation Toolbox
Environment.hpp
1 #pragma once
2 
3 // std c++ headers
4 #include <cassert>
5 #include <string>
6 
7 #include <dune/common/parallel/mpihelper.hh>
8 
9 namespace AMDiS
10 {
12 
20  {
21  // internal static container holding a pointer to the Dune::MPIHelper.
22  struct Mpi
23  {
24  static Mpi& instance()
25  {
26  static Mpi mpi;
27  return mpi;
28  }
29 
30  void registerMpiHelper(Dune::MPIHelper& mpiHelper)
31  {
32  mpiHelper_ = &mpiHelper;
33  }
34 
35  int rank()
36  {
37  assert(mpiHelper_ != nullptr);
38  return mpiHelper_->rank();
39  }
40 
41  int size()
42  {
43  assert(mpiHelper_ != nullptr);
44  return mpiHelper_->size();
45  }
46 
47  Dune::MPIHelper& mpiHelper()
48  {
49  return *mpiHelper_;
50  }
51 
52  private:
53  Dune::MPIHelper* mpiHelper_ = nullptr;
54  };
55 
56  public:
58  Environment(std::string const& initFileName = "");
59 
62  Environment(int& argc, char**& argv, std::string const& initFileName = "");
63 
65  ~Environment();
66 
68  static int mpiRank()
69  {
70  return Mpi::instance().rank();
71  }
72 
74  static int mpiSize()
75  {
76  return Mpi::instance().size();
77  }
78 
80  static Dune::MPIHelper& mpiHelper()
81  {
82  return Mpi::instance().mpiHelper();
83  }
84 
86  static typename Dune::MPIHelper::MPICommunicator comm()
87  {
88  return Dune::MPIHelper::getCommunicator();
89  }
90 
91  private:
92 #if AMDIS_HAS_PETSC
93  bool petscInitialized_ = false;
94 #endif
95  };
96 
97 } // end namespace AMDiS
static Dune::MPIHelper & mpiHelper()
Return a reference to the stored MPIHelper.
Definition: Environment.hpp:80
Contains all classes needed for solving linear and non linear equation systems.
Definition: AdaptBase.hpp:6
Establishes an environment for sequential and parallel AMDiS programs.
Definition: Environment.hpp:19
~Environment()
Finishes MPI and PETSc.
Definition: Environment.cpp:44
static int mpiSize()
Return the MPI_Size of the group created by Dune::MPIHelper.
Definition: Environment.hpp:74
Environment(std::string const &initFileName="")
Create an environment without mpi initialization, with a fixed initfile given as string.
Definition: Environment.cpp:15
static Dune::MPIHelper::MPICommunicator comm()
Return the MPI_Comm object (or a fake communicator)
Definition: Environment.hpp:86
static int mpiRank()
Return the MPI_Rank of the current processor.
Definition: Environment.hpp:68