| 1 |
// Copyright(C) 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 |
|
| 7 |
#include "Ioss_ChangeSetFactory.h" |
| 8 |
#include "Ioss_DBUsage.h" |
| 9 |
#include "Ioss_DatabaseIO.h" |
| 10 |
#include "Ioss_DynamicTopology.h" |
| 11 |
#include "Ioss_DynamicTopologyNotifier.h" |
| 12 |
#include "Ioss_DynamicTopologyObserver.h" |
| 13 |
#include "Ioss_FileInfo.h" |
| 14 |
#include "Ioss_IOFactory.h" |
| 15 |
#include "Ioss_ParallelUtils.h" |
| 16 |
#include "Ioss_Region.h" |
| 17 |
|
| 18 |
#include <fmt/core.h> |
| 19 |
#include <fmt/format.h> |
| 20 |
#include <fmt/ostream.h> |
| 21 |
|
| 22 |
#include <assert.h> |
| 23 |
#include <climits> |
| 24 |
#include <cstddef> |
| 25 |
#include <functional> |
| 26 |
#include <sstream> |
| 27 |
|
| 28 |
namespace Ioss { |
| 29 |
|
| 30 |
bool DynamicTopologyObserver::needs_new_output_file() const |
| 31 |
{ |
| 32 |
// See what type of topology modification has occurred. If a |
| 33 |
// simple REORDER, then we don't need a new file, just have to |
| 34 |
// inform database of new order; otherwise, need a new file (for |
| 35 |
// ExodusII). Baseline implementation |
| 36 |
|
| 37 |
if ((get_topology_modification() & Ioss::TOPOLOGY_HADAPT) || |
| 38 |
(get_topology_modification() & Ioss::TOPOLOGY_CREATEFACE) || |
| 39 |
(get_topology_modification() & Ioss::TOPOLOGY_CREATEELEM) || |
| 40 |
(get_topology_modification() & Ioss::TOPOLOGY_CREATENODE) || |
| 41 |
(get_topology_modification() & Ioss::TOPOLOGY_UNKNOWN) || |
| 42 |
(get_topology_modification() & Ioss::TOPOLOGY_SHUFFLE)) { |
| 43 |
// See if database has been written to since being opened... |
| 44 |
if (get_cumulative_topology_modification() != 0) { |
| 45 |
return true; |
| 46 |
} |
| 47 |
} |
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
void DynamicTopologyObserver::verify_region_is_registered() const |
| 52 |
{ |
| 53 |
if (nullptr == m_region) { |
| 54 |
std::ostringstream errmsg; |
| 55 |
fmt::print(errmsg, "ERROR: A region has not been registered with the " |
| 56 |
"Dynamic Topology Observer.\n\n"); |
| 57 |
IOSS_ERROR(errmsg); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
void DynamicTopologyObserver::register_region(Region *region) |
| 62 |
{ |
| 63 |
if (nullptr != region && nullptr != m_region && region != m_region) { |
| 64 |
std::ostringstream errmsg; |
| 65 |
fmt::print(errmsg, "ERROR: Attempt to re-register different region on " |
| 66 |
"Dynamic Topology Observer.\n\n"); |
| 67 |
IOSS_ERROR(errmsg); |
| 68 |
} |
| 69 |
|
| 70 |
m_region = region; |
| 71 |
} |
| 72 |
|
| 73 |
void DynamicTopologyObserver::register_notifier(DynamicTopologyNotifier *notifier) |
| 74 |
{ |
| 75 |
if (nullptr != notifier && nullptr != m_notifier && notifier != m_notifier) { |
| 76 |
std::ostringstream errmsg; |
| 77 |
fmt::print(errmsg, "ERROR: Attempt to re-register different notifier on " |
| 78 |
"Dynamic Topology Observer.\n\n"); |
| 79 |
IOSS_ERROR(errmsg); |
| 80 |
} |
| 81 |
|
| 82 |
m_notifier = notifier; |
| 83 |
} |
| 84 |
|
| 85 |
void DynamicTopologyObserver::set_cumulative_topology_modification(unsigned int type) |
| 86 |
{ |
| 87 |
m_cumulativeTopologyModification = type; |
| 88 |
} |
| 89 |
|
| 90 |
unsigned int DynamicTopologyObserver::get_cumulative_topology_modification() const |
| 91 |
{ |
| 92 |
return m_cumulativeTopologyModification; |
| 93 |
} |
| 94 |
|
| 95 |
unsigned int DynamicTopologyObserver::get_topology_modification() const |
| 96 |
{ |
| 97 |
return m_topologyModification; |
| 98 |
} |
| 99 |
|
| 100 |
void DynamicTopologyObserver::set_topology_modification_nl(unsigned int type) |
| 101 |
{ |
| 102 |
m_topologyModification |= type; |
| 103 |
m_cumulativeTopologyModification |= type; |
| 104 |
} |
| 105 |
|
| 106 |
void DynamicTopologyObserver::sync_topology_modification(unsigned int modFlag, |
| 107 |
unsigned int cumulativeModFlag) |
| 108 |
{ |
| 109 |
m_topologyModification = modFlag; |
| 110 |
m_cumulativeTopologyModification = cumulativeModFlag; |
| 111 |
} |
| 112 |
|
| 113 |
void DynamicTopologyObserver::set_topology_modification(unsigned int type) |
| 114 |
{ |
| 115 |
if (!(m_topologyModification & type)) { |
| 116 |
set_topology_modification_nl(type); |
| 117 |
|
| 118 |
if (nullptr != m_notifier) { |
| 119 |
for (auto &observer : m_notifier->get_observers()) { |
| 120 |
observer->set_topology_modification_nl(type); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
void DynamicTopologyObserver::reset_topology_modification() |
| 127 |
{ |
| 128 |
m_topologyModification = TOPOLOGY_SAME; |
| 129 |
} |
| 130 |
|
| 131 |
void DynamicTopologyObserver::reset_topology_modification_all() |
| 132 |
{ |
| 133 |
if (m_topologyModification != TOPOLOGY_SAME) { |
| 134 |
reset_topology_modification(); |
| 135 |
|
| 136 |
if (nullptr != m_notifier) { |
| 137 |
for (auto &observer : m_notifier->get_observers()) { |
| 138 |
observer->reset_topology_modification(); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
bool DynamicTopologyObserver::is_topology_modified() const |
| 145 |
{ |
| 146 |
return m_topologyModification != TOPOLOGY_SAME; |
| 147 |
} |
| 148 |
|
| 149 |
const ParallelUtils &DynamicTopologyObserver::util() const |
| 150 |
{ |
| 151 |
verify_region_is_registered(); |
| 152 |
return m_region->get_database()->util(); |
| 153 |
} |
| 154 |
|
| 155 |
void DynamicTopologyObserver::synchronize_topology_modified_flags() |
| 156 |
{ |
| 157 |
verify_region_is_registered(); |
| 158 |
int num_processors = m_region->get_database()->parallel_size(); |
| 159 |
// Synchronize the topology flags between all processors in case |
| 160 |
// it has not been set consistently. |
| 161 |
if (num_processors > 1) { |
| 162 |
static unsigned int buffer[2]; |
| 163 |
buffer[0] = m_cumulativeTopologyModification; |
| 164 |
buffer[1] = m_topologyModification; |
| 165 |
|
| 166 |
util().attribute_reduction(2 * sizeof(unsigned int), reinterpret_cast<char *>(buffer)); |
| 167 |
|
| 168 |
m_cumulativeTopologyModification = buffer[0]; |
| 169 |
m_topologyModification = buffer[1]; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
int DynamicTopologyObserver::get_cumulative_topology_modification_field() |
| 174 |
{ |
| 175 |
verify_region_is_registered(); |
| 176 |
const std::string variable_name = topology_modification_change_name(); |
| 177 |
|
| 178 |
int ivalue = 0; |
| 179 |
|
| 180 |
if (m_region->field_exists(variable_name)) { |
| 181 |
Field topo_field = m_region->get_field(variable_name); |
| 182 |
if (topo_field.get_type() == Field::INTEGER) { |
| 183 |
m_region->get_field_data(variable_name, &ivalue, sizeof(int)); |
| 184 |
} |
| 185 |
else { |
| 186 |
double value; |
| 187 |
m_region->get_field_data(variable_name, &value, sizeof(double)); |
| 188 |
ivalue = (int)value; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
int num_processors = m_region->get_database()->parallel_size(); |
| 193 |
// Synchronize the value between all processors in case |
| 194 |
// it has not been set consistently. |
| 195 |
if (num_processors > 1) { |
| 196 |
unsigned int buffer[1]; |
| 197 |
buffer[0] = ivalue; |
| 198 |
|
| 199 |
util().attribute_reduction(sizeof(unsigned int), reinterpret_cast<char *>(buffer)); |
| 200 |
|
| 201 |
ivalue = (int)buffer[0]; |
| 202 |
} |
| 203 |
|
| 204 |
m_cumulativeTopologyModification = ivalue; |
| 205 |
|
| 206 |
return ivalue; |
| 207 |
} |
| 208 |
|
| 209 |
void DynamicTopologyObserver::define_model() {} |
| 210 |
|
| 211 |
void DynamicTopologyObserver::write_model() {} |
| 212 |
|
| 213 |
void DynamicTopologyObserver::define_transient() {} |
| 214 |
|
| 215 |
void DynamicTopologyObserver::initialize_region() |
| 216 |
{ |
| 217 |
if (nullptr != m_region) { |
| 218 |
m_region->reset_region(); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
} // namespace Ioss |