Debian Codegraph @

packages/seacas/libraries/ioss/src/Ioss_SerializeIO.C

raw
1 // Copyright(C) 1999-2020, 2023 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 #include "Ioss_DatabaseIO.h" // for DatabaseIO
7 #include "Ioss_ParallelUtils.h" // for ParallelUtils
8 #include "Ioss_SerializeIO.h"
9 #include "Ioss_Utils.h" // for IOSS_ERROR, Ioss::WarnOut()
10 #include <fmt/ostream.h>
11
12 #include "Ioss_CodeTypes.h" // for SEACAS_HAVE_MPI
13
14 namespace Ioss {
15
16 int SerializeIO::s_owner = -1;
17 int SerializeIO::s_rank = -1;
18 int SerializeIO::s_size = -1;
19 int SerializeIO::s_groupSize = -1;
20 int SerializeIO::s_groupRank = -1;
21 int SerializeIO::s_groupFactor = 0;
22
23 #if defined(IOSS_THREADSAFE)
24 std::mutex SerializeIO::m_;
25 #endif
26
27 SerializeIO::SerializeIO(const DatabaseIO *database_io) : m_databaseIO(database_io)
28
29 {
30 if (m_databaseIO->using_parallel_io()) {
31 return;
32 }
33 IOSS_FUNC_ENTER(m_);
34
35 const Ioss::ParallelUtils util = m_databaseIO->util();
36 if (s_rank == -1) {
37 s_rank = util.parallel_rank();
38 s_size = util.parallel_size();
39 if (s_groupFactor != 0) {
40 s_groupRank = s_rank / s_groupFactor;
41 s_groupSize = (s_size - 1) / s_groupFactor + 1;
42 }
43 }
44
45 m_activeFallThru = s_owner != -1;
46 if (!m_activeFallThru) {
47 if (s_groupFactor > 0) {
48 do {
49 util.barrier();
50 } while (++s_owner != s_groupRank);
51 m_databaseIO->openDatabase_nl();
52 }
53 else {
54 s_owner = s_groupRank;
55 }
56 }
57 }
58
59 SerializeIO::~SerializeIO()
60 {
61 if (m_databaseIO->using_parallel_io()) {
62 return;
63 }
64 try {
65 IOSS_FUNC_ENTER(m_);
66 if (!m_activeFallThru) {
67 if (s_groupFactor > 0) {
68 m_databaseIO->closeDatabase_nl();
69 s_owner = s_groupRank;
70 const Ioss::ParallelUtils util = m_databaseIO->util();
71 do {
72 util.barrier();
73 } while (++s_owner != s_groupSize);
74 s_owner = -1;
75 }
76 else {
77 s_owner = -1;
78 }
79 }
80 }
81 catch (...) {
82 }
83 }
84
85 void SerializeIO::setGroupFactor(int factor)
86 {
87 IOSS_FUNC_ENTER(m_);
88 if (s_rank != -1) {
89 fmt::print(Ioss::WarnOut(), "Mesh I/O serialization group factor cannot be changed "
90 "once serialized I/O has begun");
91 }
92 else {
93 s_groupFactor = factor;
94 }
95 }
96
97 } // namespace Ioss