IOSS 2.0

Ioss_SerializeIO.h
Go to the documentation of this file.
1 // Copyright(C) 1999-2017 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 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //
12 // * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following
14 // disclaimer in the documentation and/or other materials provided
15 // with the distribution.
16 //
17 // * Neither the name of NTESS nor the names of its
18 // contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #ifndef IOSS_Ioss_SerializeIO_h
33 #define IOSS_Ioss_SerializeIO_h
34
35 #include <Ioss_CodeTypes.h>
36
37 namespace Ioss {
38 class DatabaseIO;
39 } // namespace Ioss
40
41 namespace Ioss {
42
43 /**
44 * @brief Class <code>SerializeIO</code> is a sentry class which performs serialization
45 * for mesh database I/O.
46 *
47 * This sentry guards serialization of parallel I/O routines. At construction, it
48 * blocks the processes via an MPI barrier, releasing them to execute in groups specified
49 * by <code>s_groupSize</code>. At destruction, it continues to block via MPI barriers
50 * until all the processor have been released by the constructor.
51 *
52 * In the case where the constructor is called, and the sentry is already active and owned
53 * by the processes group, the constructor and destructor simply fall through since the
54 * serialization is already in place at a higher level.
55 *
56 * \note All ranks must call the SerializeIO constructor synchronously.
57 * \note It is recommended to use RAII and keep the area protected by the SerializeIO as small as
58 * possible.
59 *
60 * The flow is that the ranks are split into groups of the specified size. Assume 3 ranks of group
61 * size 1.
62 *
63 * * First time through,
64 * - rank 0 falls through and
65 * - ranks 1, 2 sit at the barrier
66 * - rank 0 hits the destructor and then all 3 ranks are in the barrier so they all go to next
67 * step
68 * - (rank 1,2 in constructor, rank 0 in destructor)
69 * * `s_owner` is now equal to `m_groupRank` on rank 1, so it falls out of the do while;
70 * - rank 2 still in the constructor do while Barrier
71 * - rank 0 in the destructor do while Barrier
72 * - rank 1 does its work and calls destructor;
73 * - all ranks in Barrier, so they go to next step.
74 * * `s_owner` now equal to `m_groupRank` on rank 2, so if falls out of the do while;
75 * - ranks 0,1 in destructor do while at the Barrier
76 * - rank 2 does it work and calls destructor
77 * - all ranks are now in the destructor Barrier, so they go to next step
78 * - all ranks clear the Destructor and go to next step.
79 *
80 */
81 class SerializeIO
82 {
83 public:
84 /**
85 * Creates a new <code>SerializeIO</code> instance.
86 *
87 * @param database_io a <code>DatabaseIO</code> variable ...
88 */
89 explicit SerializeIO(const DatabaseIO *database_io);
90 SerializeIO(const SerializeIO &from) = delete;
91 SerializeIO &operator=(const SerializeIO &from) = delete;
92 ~SerializeIO();
93
94 inline static int getOwner() { return s_owner; }
95
96 inline static int getRank() { return s_rank; }
97
98 inline static int getSize() { return s_size; }
99
100 inline static int getGroupRank() { return s_groupRank; }
101
102 inline static int getGroupSize() { return s_groupSize; }
103
104 static void setGroupFactor(int factor);
105
106 inline static bool isEnabled() { return s_groupFactor != 0; }
107
108 inline static bool inBarrier() { return s_owner != -1; }
109
110 inline static bool inMyGroup() { return s_owner == s_groupRank; }
111
112 private:
113 const DatabaseIO *m_databaseIO; ///< Database I/O pointer
114 #if defined(IOSS_THREADSAFE)
115 static std::mutex m_;
116 #endif
117 bool m_activeFallThru; ///< No barriers since my group is running
118
119 static int s_groupFactor; ///< Grouping factor
120 static int s_size; ///< Number of processors
121 static int s_rank; ///< My processor rank
122 static int s_groupSize; ///< Number of groups
123 static int s_groupRank; ///< My group rank
124 static int s_owner; ///< Group currently running
125 };
126
127 } // namespace Ioss
128
129 #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:81
Ioss::SerializeIO::s_groupRank
static int s_groupRank
My group rank.
Definition: Ioss_SerializeIO.h:123
Ioss::SerializeIO::getGroupSize
static int getGroupSize()
Definition: Ioss_SerializeIO.h:102
Ioss
The main namespace for the Ioss library.
Definition: Ioad_DatabaseIO.C:66
Ioss::SerializeIO::inMyGroup
static bool inMyGroup()
Definition: Ioss_SerializeIO.h:110
Ioss::SerializeIO::~SerializeIO
~SerializeIO()
Definition: Ioss_SerializeIO.C:87
Ioss::SerializeIO::getGroupRank
static int getGroupRank()
Definition: Ioss_SerializeIO.h:100
Ioss::SerializeIO::inBarrier
static bool inBarrier()
Definition: Ioss_SerializeIO.h:108
Ioss::SerializeIO::setGroupFactor
static void setGroupFactor(int factor)
Definition: Ioss_SerializeIO.C:113
Ioss::SerializeIO::s_size
static int s_size
Number of processors.
Definition: Ioss_SerializeIO.h:120
Ioss::SerializeIO::isEnabled
static bool isEnabled()
Definition: Ioss_SerializeIO.h:106
Ioss::SerializeIO::s_groupSize
static int s_groupSize
Number of groups.
Definition: Ioss_SerializeIO.h:122
Ioss::DatabaseIO
An input or output Database.
Definition: Ioss_DatabaseIO.h:82
Ioss::SerializeIO::m_activeFallThru
bool m_activeFallThru
No barriers since my group is running.
Definition: Ioss_SerializeIO.h:117
Ioss::SerializeIO::SerializeIO
SerializeIO(const DatabaseIO *database_io)
Definition: Ioss_SerializeIO.C:54
Ioss::SerializeIO::m_databaseIO
const DatabaseIO * m_databaseIO
Database I/O pointer.
Definition: Ioss_SerializeIO.h:113
Ioss::SerializeIO::getRank
static int getRank()
Definition: Ioss_SerializeIO.h:96
Ioss::SerializeIO::s_owner
static int s_owner
Group currently running.
Definition: Ioss_SerializeIO.h:124
Ioss::SerializeIO::s_rank
static int s_rank
My processor rank.
Definition: Ioss_SerializeIO.h:121
Ioss::SerializeIO::getSize
static int getSize()
Definition: Ioss_SerializeIO.h:98
Ioss::SerializeIO::operator=
SerializeIO & operator=(const SerializeIO &from)=delete
Ioss::SerializeIO::s_groupFactor
static int s_groupFactor
Grouping factor.
Definition: Ioss_SerializeIO.h:119
Ioss_CodeTypes.h
Ioss::SerializeIO::getOwner
static int getOwner()
Definition: Ioss_SerializeIO.h:94