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