IOSS  2.0
Ioss_SerializeIO.h
Go to the documentation of this file.
1 // Copyright(C) 1999-2020 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6 #ifndef IOSS_Ioss_SerializeIO_h
7 #define IOSS_Ioss_SerializeIO_h
8 
9 #include <Ioss_CodeTypes.h>
10 
11 namespace Ioss {
12  class DatabaseIO;
13 } // namespace Ioss
14 
15 namespace Ioss {
16 
17  /**
18  * @brief Class <code>SerializeIO</code> is a sentry class which performs serialization
19  * for mesh database I/O.
20  *
21  * This sentry guards serialization of parallel I/O routines. At construction, it
22  * blocks the processes via an MPI barrier, releasing them to execute in groups specified
23  * by <code>s_groupSize</code>. At destruction, it continues to block via MPI barriers
24  * until all the processor have been released by the constructor.
25  *
26  * In the case where the constructor is called, and the sentry is already active and owned
27  * by the processes group, the constructor and destructor simply fall through since the
28  * serialization is already in place at a higher level.
29  *
30  * \note All ranks must call the SerializeIO constructor synchronously.
31  * \note It is recommended to use RAII and keep the area protected by the SerializeIO as small as
32  * possible.
33  *
34  * The flow is that the ranks are split into groups of the specified size. Assume 3 ranks of group
35  * size 1.
36  *
37  * * First time through,
38  * - rank 0 falls through and
39  * - ranks 1, 2 sit at the barrier
40  * - rank 0 hits the destructor and then all 3 ranks are in the barrier so they all go to next
41  * step
42  * - (rank 1,2 in constructor, rank 0 in destructor)
43  * * `s_owner` is now equal to `m_groupRank` on rank 1, so it falls out of the do while;
44  * - rank 2 still in the constructor do while Barrier
45  * - rank 0 in the destructor do while Barrier
46  * - rank 1 does its work and calls destructor;
47  * - all ranks in Barrier, so they go to next step.
48  * * `s_owner` now equal to `m_groupRank` on rank 2, so if falls out of the do while;
49  * - ranks 0,1 in destructor do while at the Barrier
50  * - rank 2 does it work and calls destructor
51  * - all ranks are now in the destructor Barrier, so they go to next step
52  * - all ranks clear the Destructor and go to next step.
53  *
54  */
56  {
57  public:
58  /**
59  * Creates a new <code>SerializeIO</code> instance.
60  *
61  * @param database_io a <code>DatabaseIO</code> variable ...
62  */
63  explicit SerializeIO(const DatabaseIO *database_io);
64  SerializeIO(const SerializeIO &from) = delete;
65  SerializeIO &operator=(const SerializeIO &from) = delete;
66  ~SerializeIO();
67 
68  inline static int getOwner() { return s_owner; }
69 
70  inline static int getRank() { return s_rank; }
71 
72  inline static int getSize() { return s_size; }
73 
74  inline static int getGroupRank() { return s_groupRank; }
75 
76  inline static int getGroupSize() { return s_groupSize; }
77 
78  static void setGroupFactor(int factor);
79 
80  inline static bool isEnabled() { return s_groupFactor != 0; }
81 
82  inline static bool inBarrier() { return s_owner != -1; }
83 
84  inline static bool inMyGroup() { return s_owner == s_groupRank; }
85 
86  private:
87  const DatabaseIO *m_databaseIO; ///< Database I/O pointer
88 #if defined(IOSS_THREADSAFE)
89  static std::mutex m_;
90 #endif
91  bool m_activeFallThru; ///< No barriers since my group is running
92 
93  static int s_groupFactor; ///< Grouping factor
94  static int s_size; ///< Number of processors
95  static int s_rank; ///< My processor rank
96  static int s_groupSize; ///< Number of groups
97  static int s_groupRank; ///< My group rank
98  static int s_owner; ///< Group currently running
99  };
100 
101 } // namespace Ioss
102 
103 #endif // IOSS_Ioss_SerializeIO_h
Ioss::SerializeIO
Class SerializeIO is a sentry class which performs serialization for mesh database I/O.
Definition: Ioss_SerializeIO.h:56
Ioss::SerializeIO::s_groupRank
static int s_groupRank
My group rank.
Definition: Ioss_SerializeIO.h:97
Ioss::SerializeIO::getGroupSize
static int getGroupSize()
Definition: Ioss_SerializeIO.h:76
Ioss
The main namespace for the Ioss library.
Definition: Ioad_DatabaseIO.C:40
Ioss::SerializeIO::inMyGroup
static bool inMyGroup()
Definition: Ioss_SerializeIO.h:84
Ioss::SerializeIO::~SerializeIO
~SerializeIO()
Definition: Ioss_SerializeIO.C:61
Ioss::SerializeIO::getGroupRank
static int getGroupRank()
Definition: Ioss_SerializeIO.h:74
Ioss::SerializeIO::inBarrier
static bool inBarrier()
Definition: Ioss_SerializeIO.h:82
Ioss::SerializeIO::setGroupFactor
static void setGroupFactor(int factor)
Definition: Ioss_SerializeIO.C:87
Ioss::SerializeIO::s_size
static int s_size
Number of processors.
Definition: Ioss_SerializeIO.h:94
Ioss::SerializeIO::isEnabled
static bool isEnabled()
Definition: Ioss_SerializeIO.h:80
Ioss::SerializeIO::s_groupSize
static int s_groupSize
Number of groups.
Definition: Ioss_SerializeIO.h:96
Ioss::DatabaseIO
An input or output Database.
Definition: Ioss_DatabaseIO.h:59
Ioss::SerializeIO::m_activeFallThru
bool m_activeFallThru
No barriers since my group is running.
Definition: Ioss_SerializeIO.h:91
Ioss::SerializeIO::SerializeIO
SerializeIO(const DatabaseIO *database_io)
Definition: Ioss_SerializeIO.C:28
Ioss::SerializeIO::SerializeIO
SerializeIO(const SerializeIO &from)=delete
Ioss::SerializeIO::m_databaseIO
const DatabaseIO * m_databaseIO
Database I/O pointer.
Definition: Ioss_SerializeIO.h:87
Ioss::SerializeIO::getRank
static int getRank()
Definition: Ioss_SerializeIO.h:70
Ioss::SerializeIO::s_owner
static int s_owner
Group currently running.
Definition: Ioss_SerializeIO.h:98
Ioss::SerializeIO::s_rank
static int s_rank
My processor rank.
Definition: Ioss_SerializeIO.h:95
Ioss::SerializeIO::getSize
static int getSize()
Definition: Ioss_SerializeIO.h:72
Ioss::SerializeIO::operator=
SerializeIO & operator=(const SerializeIO &from)=delete
Ioss::SerializeIO::s_groupFactor
static int s_groupFactor
Grouping factor.
Definition: Ioss_SerializeIO.h:93
Ioss_CodeTypes.h
Ioss::SerializeIO::getOwner
static int getOwner()
Definition: Ioss_SerializeIO.h:68