| 1 |
// Copyright(C) 1999-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_Field.h" // for Field, etc |
| 8 |
#include "Ioss_Map.h" |
| 9 |
#include "Ioss_SmartAssert.h" |
| 10 |
#include "Ioss_Utils.h" // for IOSS_ERROR |
| 11 |
#include <cstddef> // for size_t |
| 12 |
#include <fmt/ostream.h> |
| 13 |
#include <numeric> |
| 14 |
#include <sstream> |
| 15 |
#include <vector> // for vector, vector<>::iterator, etc |
| 16 |
|
| 17 |
#include "Ioss_CodeTypes.h" |
| 18 |
|
| 19 |
// If defined, then only build m_reverseMap when it is used. |
| 20 |
#undef USE_LAZY_REVERSE |
| 21 |
|
| 22 |
namespace { |
| 23 |
template <typename INT> bool is_one2one(INT *ids, size_t num_to_get, size_t offset) |
| 24 |
{ |
| 25 |
bool one2one = true; |
| 26 |
INT map_offset = num_to_get > 0 ? ids[0] - 1 - offset : 0; |
| 27 |
for (size_t i = 0; i < num_to_get; i++) { |
| 28 |
if ((size_t)ids[i] != i + offset + 1 + map_offset) { |
| 29 |
one2one = false; |
| 30 |
break; |
| 31 |
} |
| 32 |
} |
| 33 |
return one2one; |
| 34 |
} |
| 35 |
} // namespace |
| 36 |
|
| 37 |
void Ioss::Map::release_memory() |
| 38 |
{ |
| 39 |
IOSS_FUNC_ENTER(m_); |
| 40 |
MapContainer().swap(m_map); |
| 41 |
MapContainer().swap(m_reorder); |
| 42 |
ReverseMapContainer().swap(m_reverse); |
| 43 |
} |
| 44 |
|
| 45 |
// Determines whether the input map is sequential (m_map[i] == i) |
| 46 |
bool Ioss::Map::is_sequential(bool check_all) const |
| 47 |
{ |
| 48 |
// Assumes the_map runs from [1..size) Slot zero will contain -1 if the |
| 49 |
// vector is sequential; 1 if not sequential, and 0 if it has not |
| 50 |
// yet been determined... |
| 51 |
// Once the the_map has been determined to be sequential/not-sequential, |
| 52 |
// slot zero is set appropriately. |
| 53 |
// 'sequential' is defined here to mean i==the_map[i] for all |
| 54 |
// 0<i<the_map.size() |
| 55 |
|
| 56 |
// Arguably, an empty map is sequential... |
| 57 |
if (m_map.empty()) { |
| 58 |
return true; |
| 59 |
} |
| 60 |
|
| 61 |
if (!check_all) { |
| 62 |
// Check slot zero... |
| 63 |
if (m_map[0] == -1) { |
| 64 |
return true; |
| 65 |
} |
| 66 |
if (m_map[0] == 1) { |
| 67 |
return false; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
IOSS_FUNC_ENTER(m_); |
| 72 |
auto &new_map = const_cast<Ioss::MapContainer &>(m_map); |
| 73 |
size_t map_size = m_map.size(); |
| 74 |
if (m_offset == -1 && map_size > 1) { |
| 75 |
m_offset = m_map[1] - 1; |
| 76 |
} |
| 77 |
for (int64_t i = 1; i < (int64_t)map_size; i++) { |
| 78 |
if (m_map[i] != i + m_offset) { |
| 79 |
new_map[0] = 1; |
| 80 |
m_offset = -1; |
| 81 |
return false; |
| 82 |
} |
| 83 |
} |
| 84 |
new_map[0] = -1; |
| 85 |
return true; |
| 86 |
} |
| 87 |
|
| 88 |
void Ioss::Map::set_size(size_t entity_count) |
| 89 |
{ |
| 90 |
IOSS_FUNC_ENTER(m_); |
| 91 |
if (m_map.empty()) { |
| 92 |
m_map.resize(entity_count + 1); |
| 93 |
set_is_sequential(true); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
void Ioss::Map::build_reverse_map() { build_reverse_map(m_map.size() - 1, 0); } |
| 98 |
void Ioss::Map::build_reverse_map_no_lock() { build_reverse_map_nl(m_map.size() - 1, 0); } |
| 99 |
void Ioss::Map::build_reverse_map(int64_t num_to_get, int64_t offset) |
| 100 |
{ |
| 101 |
IOSS_FUNC_ENTER(m_); |
| 102 |
build_reverse_map_nl(num_to_get, offset); |
| 103 |
} |
| 104 |
|
| 105 |
void Ioss::Map::build_reverse_map_nl(int64_t num_to_get, int64_t offset) |
| 106 |
{ |
| 107 |
// Stored as an unordered map -- key:global_id, value:local_id |
| 108 |
if (!is_sequential()) { |
| 109 |
#if defined MAP_USE_SORTED_VECTOR |
| 110 |
ReverseMapContainer new_ids; |
| 111 |
if (m_reverse.empty()) { |
| 112 |
// This is first time that the m_reverse map is being built.. |
| 113 |
// m_map is no longer 1-to-1. |
| 114 |
// Just iterate m_map and add all values that are non-zero |
| 115 |
new_ids.reserve(m_map.size() - 1); |
| 116 |
for (size_t i = 1; i < m_map.size(); i++) { |
| 117 |
if (m_map[i] != 0) { |
| 118 |
new_ids.emplace_back(m_map[i], i); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
else { |
| 123 |
new_ids.reserve(num_to_get); |
| 124 |
for (int64_t i = 0; i < num_to_get; i++) { |
| 125 |
int64_t local_id = offset + i + 1; |
| 126 |
new_ids.emplace_back(m_map[local_id], local_id); |
| 127 |
|
| 128 |
if (m_map[local_id] <= 0) { |
| 129 |
std::ostringstream errmsg; |
| 130 |
fmt::print( |
| 131 |
errmsg, |
| 132 |
"\nERROR: {0} map detected non-positive global id {1} for {0} with local id {2} " |
| 133 |
"on processor {3}.\n", |
| 134 |
m_entityType, m_map[local_id], local_id, m_myProcessor); |
| 135 |
IOSS_ERROR(errmsg); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
// new_ids is a vector of pairs <global_id, local_id> |
| 141 |
Ioss::sort(new_ids); |
| 142 |
|
| 143 |
int64_t new_id_min = new_ids.empty() ? 0 : new_ids.front().first; |
| 144 |
int64_t old_id_max = m_reverse.empty() ? 0 : m_reverse.back().first; |
| 145 |
if (new_ids.size() + 1 == m_map.size()) { |
| 146 |
SMART_ASSERT(m_reverse.empty() || m_reverse.size() + 1 == m_map.size()); |
| 147 |
new_ids.swap(m_reverse); |
| 148 |
} |
| 149 |
else if (new_id_min > old_id_max) { |
| 150 |
m_reverse.insert(m_reverse.end(), new_ids.begin(), new_ids.end()); |
| 151 |
} |
| 152 |
else { |
| 153 |
// Copy reverseElementMap to old_ids, empty reverseElementMap. |
| 154 |
ReverseMapContainer old_ids; |
| 155 |
old_ids.swap(m_reverse); |
| 156 |
SMART_ASSERT(m_reverse.empty()); |
| 157 |
|
| 158 |
// Merge old_ids and new_ids to reverseElementMap. |
| 159 |
m_reverse.reserve(old_ids.size() + new_ids.size()); |
| 160 |
std::merge( |
| 161 |
old_ids.begin(), old_ids.end(), new_ids.begin(), new_ids.end(), |
| 162 |
std::inserter(m_reverse, m_reverse.begin()), |
| 163 |
[](const Ioss::IdPair &lhs, const Ioss::IdPair &rhs) { return lhs.first < rhs.first; }); |
| 164 |
} |
| 165 |
|
| 166 |
// Check for duplicate ids... |
| 167 |
// Maybe debug only... |
| 168 |
verify_no_duplicate_ids(m_reverse); |
| 169 |
#else |
| 170 |
if (m_reverse.empty()) { |
| 171 |
// This is first time that the m_reverse map is being built.. |
| 172 |
// m_map is no longer 1-to-1. |
| 173 |
// Just iterate m_map and add all values that are non-zero |
| 174 |
m_reverse.max_load_factor(0.95); |
| 175 |
m_reverse.reserve(m_map.size()); |
| 176 |
for (size_t i = 1; i < m_map.size(); i++) { |
| 177 |
if (m_map[i] != 0) { |
| 178 |
bool ok = m_reverse.insert({m_map[i], i}).second; |
| 179 |
if (!ok) { |
| 180 |
std::ostringstream errmsg; |
| 181 |
fmt::print( |
| 182 |
errmsg, |
| 183 |
"\nERROR: Duplicate {0} global id detected on processor {1}, filename '{2}'.\n" |
| 184 |
" Global id {3} assigned to local {0}s {4} and {5}.\n", |
| 185 |
m_entityType, m_myProcessor, m_filename, m_map[i], i, m_reverse[m_map[i]]); |
| 186 |
IOSS_ERROR(errmsg); |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
else { |
| 192 |
for (int64_t i = 0; i < num_to_get; i++) { |
| 193 |
int64_t local_id = offset + i + 1; |
| 194 |
bool ok = m_reverse.insert({m_map[local_id], local_id}).second; |
| 195 |
if (!ok) { |
| 196 |
if (local_id != m_reverse[m_map[local_id]]) { |
| 197 |
std::ostringstream errmsg; |
| 198 |
fmt::print( |
| 199 |
errmsg, |
| 200 |
"\nERROR: Duplicate {0} global id detected on processor {1}, filename '{2}'.\n" |
| 201 |
" Global id {3} assigned to local {0}s {4} and {5}.\n", |
| 202 |
m_entityType, m_myProcessor, m_filename, m_map[local_id], local_id, |
| 203 |
m_reverse[m_map[local_id]]); |
| 204 |
IOSS_ERROR(errmsg); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
if (m_map[local_id] <= 0) { |
| 209 |
std::ostringstream errmsg; |
| 210 |
fmt::print( |
| 211 |
errmsg, |
| 212 |
"\nERROR: {0} map detected non-positive global id {1} for {0} with local id {2} " |
| 213 |
"on processor {3}.\n", |
| 214 |
m_entityType, m_map[local_id], local_id, m_myProcessor); |
| 215 |
IOSS_ERROR(errmsg); |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
#if IOSS_DEBUG_OUTPUT |
| 220 |
fmt::print("[{}] ({}) Map Size = {}\n", m_myProcessor, m_entityType, m_map.size()); |
| 221 |
fmt::print("[{}] ({}) Size = {}\n", m_myProcessor, m_entityType, m_reverse.size()); |
| 222 |
fmt::print("[{}] ({}) Bucket Count = {}\n", m_myProcessor, m_entityType, |
| 223 |
m_reverse.bucket_count()); |
| 224 |
fmt::print("[{}] ({}) Load Factor = {}\n", m_myProcessor, m_entityType, |
| 225 |
m_reverse.load_factor()); |
| 226 |
fmt::print("[{}] ({}) Max Load Factor = {}\n\n", m_myProcessor, m_entityType, |
| 227 |
m_reverse.max_load_factor()); |
| 228 |
#endif |
| 229 |
#endif |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
#if defined MAP_USE_SORTED_VECTOR |
| 234 |
void Ioss::Map::verify_no_duplicate_ids(std::vector<Ioss::IdPair> &reverse_map) |
| 235 |
{ |
| 236 |
// Check for duplicate ids... |
| 237 |
auto dup = std::adjacent_find( |
| 238 |
reverse_map.begin(), reverse_map.end(), |
| 239 |
[](const Ioss::IdPair &lhs, const Ioss::IdPair &rhs) { return lhs.first == rhs.first; }); |
| 240 |
if (dup != reverse_map.end()) { |
| 241 |
auto other = dup + 1; |
| 242 |
std::ostringstream errmsg; |
| 243 |
fmt::print(errmsg, |
| 244 |
"\nERROR: Duplicate {0} global id detected on processor {1}, filename '{2}'.\n" |
| 245 |
" Global id {3} assigned to local {0}s {4} and {5}.\n", |
| 246 |
m_entityType, m_myProcessor, m_filename, (*dup).first, (*dup).second, |
| 247 |
(*other).second); |
| 248 |
IOSS_ERROR(errmsg); |
| 249 |
} |
| 250 |
} |
| 251 |
#endif |
| 252 |
|
| 253 |
template IOSS_EXPORT bool Ioss::Map::set_map(int *ids, size_t count, size_t offset, |
| 254 |
bool in_define_mode); |
| 255 |
template IOSS_EXPORT bool Ioss::Map::set_map(int64_t *ids, size_t count, size_t offset, |
| 256 |
bool in_define_mode); |
| 257 |
|
| 258 |
template <typename INT> |
| 259 |
bool Ioss::Map::set_map(INT *ids, size_t count, size_t offset, bool in_define_mode) |
| 260 |
{ |
| 261 |
IOSS_FUNC_ENTER(m_); |
| 262 |
if (in_define_mode && is_sequential()) { |
| 263 |
// If the current map is one-to-one, check whether it will be one-to-one |
| 264 |
// after adding these ids... |
| 265 |
bool one2one = is_one2one(ids, count, offset); |
| 266 |
if (one2one) { |
| 267 |
// Further checks on how ids fit into previously set m_map entries (if any) |
| 268 |
if (count > 0) { |
| 269 |
INT tmp_offset = ids[0] - 1 - offset; |
| 270 |
if (tmp_offset < 0 || (m_offset >= 0 && tmp_offset != m_offset)) { |
| 271 |
one2one = false; |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
if (!one2one) { |
| 277 |
// Up to this point, the id map has been one-to-one. Once we |
| 278 |
// apply these `ids` to `m_map`, the map will no |
| 279 |
// longer be one-to-one. The main consequence of this is that we |
| 280 |
// now need an explicit reverseMap. The reverseMap is built |
| 281 |
// incrementally with the current range of 'ids', but before |
| 282 |
// that can be done, need to build a reverseMap of the current |
| 283 |
// one-to-one data... |
| 284 |
set_is_sequential(false); |
| 285 |
#if !defined USE_LAZY_REVERSE |
| 286 |
if (m_map.size() - 1 > count) { |
| 287 |
build_reverse_map_nl(m_map.size() - 1, 0); |
| 288 |
} |
| 289 |
#endif |
| 290 |
m_offset = 0; |
| 291 |
} |
| 292 |
else { |
| 293 |
// Map is sequential beginning at ids[0] |
| 294 |
if (count > 0) { |
| 295 |
m_offset = ids[0] - 1 - offset; |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
// Determine if `changed` which means an entity was redefined... |
| 301 |
// This is used to determine whether a `reorder` map is needed. |
| 302 |
bool changed = false; // True if redefining an entry |
| 303 |
for (size_t i = 0; i < count; i++) { |
| 304 |
int64_t local_id = offset + i + 1; |
| 305 |
SMART_ASSERT((size_t)local_id < m_map.size())(local_id)(m_map.size()); |
| 306 |
if (m_map[local_id] > 0 && m_map[local_id] != ids[i]) { |
| 307 |
changed = true; |
| 308 |
break; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
#if defined USE_LAZY_REVERSE |
| 313 |
// Build this now before we redefine an entry |
| 314 |
if (!in_define_mode && changed) { |
| 315 |
build_reverse_map_nl(m_map.size() - 1, 0); |
| 316 |
} |
| 317 |
#endif |
| 318 |
|
| 319 |
for (size_t i = 0; i < count; i++) { |
| 320 |
int64_t local_id = offset + i + 1; |
| 321 |
SMART_ASSERT((size_t)local_id < m_map.size())(local_id)(m_map.size()); |
| 322 |
|
| 323 |
if (ids[i] <= 0) { |
| 324 |
std::ostringstream errmsg; |
| 325 |
fmt::print(errmsg, |
| 326 |
"\nERROR: {} mapping routines detected non-positive global id {}" |
| 327 |
" for local id {} on processor {}, filename '{}'.\n", |
| 328 |
m_entityType, ids[i], local_id, m_myProcessor, m_filename); |
| 329 |
IOSS_ERROR(errmsg); |
| 330 |
} |
| 331 |
|
| 332 |
m_map[local_id] = ids[i]; |
| 333 |
if (local_id != ids[i] - m_offset) { |
| 334 |
set_is_sequential(false); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
if (in_define_mode) { |
| 339 |
if (changed) { |
| 340 |
m_reverse.clear(); |
| 341 |
} |
| 342 |
#if !defined USE_LAZY_REVERSE |
| 343 |
build_reverse_map_nl(count, offset); |
| 344 |
#endif |
| 345 |
} |
| 346 |
else if (changed) { |
| 347 |
// Build the reorderEntityMap which does a direct mapping from |
| 348 |
// the current topologies local order to the local order |
| 349 |
// stored in the database if these two orders are different, that |
| 350 |
// is if the ids order was redefined after the STATE_MODEL |
| 351 |
// phase... This is 0-based and used for |
| 352 |
// remapping output and input TRANSIENT fields. |
| 353 |
build_reorder_map_nl(offset, count); |
| 354 |
} |
| 355 |
return changed; |
| 356 |
} |
| 357 |
|
| 358 |
void Ioss::Map::set_default(size_t count, size_t offset) |
| 359 |
{ |
| 360 |
IOSS_FUNC_ENTER(m_); |
| 361 |
m_map.resize(count + 1); |
| 362 |
std::iota(m_map.begin() + 1, m_map.end(), 1 + offset); |
| 363 |
m_offset = -1 * static_cast<int64_t>(offset); |
| 364 |
set_is_sequential(true); |
| 365 |
} |
| 366 |
|
| 367 |
#ifndef DOXYGEN_SKIP_THIS |
| 368 |
template IOSS_EXPORT void Ioss::Map::reverse_map_data(int *data, size_t count) const; |
| 369 |
template IOSS_EXPORT void Ioss::Map::reverse_map_data(int64_t *data, size_t count) const; |
| 370 |
#endif |
| 371 |
|
| 372 |
template <typename INT> void Ioss::Map::reverse_map_data(INT *data, size_t count) const |
| 373 |
{ |
| 374 |
IOSS_FUNC_ENTER(m_); |
| 375 |
if (!is_sequential()) { |
| 376 |
for (size_t i = 0; i < count; i++) { |
| 377 |
INT global_id = data[i]; |
| 378 |
data[i] = (INT)global_to_local_nl(global_id, true); |
| 379 |
} |
| 380 |
} |
| 381 |
else if (m_offset != 0) { |
| 382 |
for (size_t i = 0; i < count; i++) { |
| 383 |
data[i] -= m_offset; |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
void Ioss::Map::reverse_map_data(void *data, const Ioss::Field &field, size_t count) const |
| 389 |
{ |
| 390 |
if (field.get_type() == Ioss::Field::INTEGER) { |
| 391 |
int *connect = static_cast<int *>(data); |
| 392 |
reverse_map_data(connect, count); |
| 393 |
} |
| 394 |
else { |
| 395 |
auto *connect = static_cast<int64_t *>(data); |
| 396 |
reverse_map_data(connect, count); |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
#ifndef DOXYGEN_SKIP_THIS |
| 401 |
template IOSS_EXPORT void Ioss::Map::map_data(int *data, size_t count) const; |
| 402 |
template IOSS_EXPORT void Ioss::Map::map_data(int64_t *data, size_t count) const; |
| 403 |
#endif |
| 404 |
|
| 405 |
template <typename INT> void Ioss::Map::map_data(INT *data, size_t count) const |
| 406 |
{ |
| 407 |
IOSS_FUNC_ENTER(m_); |
| 408 |
if (!is_sequential()) { |
| 409 |
for (size_t i = 0; i < count; i++) { |
| 410 |
data[i] = (INT)m_map[data[i]]; |
| 411 |
} |
| 412 |
} |
| 413 |
else if (m_offset != 0) { |
| 414 |
for (size_t i = 0; i < count; i++) { |
| 415 |
data[i] += m_offset; |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
void Ioss::Map::map_data(void *data, const Ioss::Field::BasicType type, size_t count) const |
| 421 |
{ |
| 422 |
if (type == Ioss::Field::INTEGER) { |
| 423 |
int *datum = static_cast<int *>(data); |
| 424 |
map_data(datum, count); |
| 425 |
} |
| 426 |
else { |
| 427 |
auto *datum = static_cast<int64_t *>(data); |
| 428 |
map_data(datum, count); |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
void Ioss::Map::map_data(void *data, const Ioss::Field &field, size_t count) const |
| 433 |
{ |
| 434 |
map_data(data, field.get_type(), count); |
| 435 |
} |
| 436 |
|
| 437 |
#ifndef DOXYGEN_SKIP_THIS |
| 438 |
template IOSS_EXPORT void Ioss::Map::map_implicit_data(int *data, size_t count, |
| 439 |
size_t offset) const; |
| 440 |
template IOSS_EXPORT void Ioss::Map::map_implicit_data(int64_t *data, size_t count, |
| 441 |
size_t offset) const; |
| 442 |
#endif |
| 443 |
|
| 444 |
template <typename INT> |
| 445 |
void Ioss::Map::map_implicit_data(INT *ids, size_t count, size_t offset) const |
| 446 |
{ |
| 447 |
// Map the "local" ids (offset+1..offset+count) to the global ids. The local |
| 448 |
// ids are implicit |
| 449 |
if (is_sequential()) { |
| 450 |
for (size_t i = 0; i < count; i++) { |
| 451 |
ids[i] = static_cast<INT>(m_offset + offset + 1 + i); |
| 452 |
} |
| 453 |
} |
| 454 |
else { |
| 455 |
for (size_t i = 0; i < count; i++) { |
| 456 |
ids[i] = static_cast<INT>(m_map[offset + 1 + i]); |
| 457 |
} |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
void Ioss::Map::map_implicit_data(void *data, const Ioss::Field &field, size_t count, |
| 462 |
size_t offset) const |
| 463 |
{ |
| 464 |
IOSS_FUNC_ENTER(m_); |
| 465 |
if (field.get_type() == Ioss::Field::INTEGER) { |
| 466 |
map_implicit_data(static_cast<int *>(data), count, offset); |
| 467 |
} |
| 468 |
else { |
| 469 |
map_implicit_data(static_cast<int64_t *>(data), count, offset); |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
template IOSS_EXPORT size_t Ioss::Map::map_field_to_db_scalar_order(double *variables, |
| 474 |
std::vector<double> &db_var, |
| 475 |
size_t begin_offset, |
| 476 |
size_t count, size_t stride, |
| 477 |
size_t offset); |
| 478 |
template IOSS_EXPORT size_t Ioss::Map::map_field_to_db_scalar_order(int *variables, |
| 479 |
std::vector<double> &db_var, |
| 480 |
size_t begin_offset, |
| 481 |
size_t count, size_t stride, |
| 482 |
size_t offset); |
| 483 |
template IOSS_EXPORT size_t Ioss::Map::map_field_to_db_scalar_order(int64_t *variables, |
| 484 |
std::vector<double> &db_var, |
| 485 |
size_t begin_offset, |
| 486 |
size_t count, size_t stride, |
| 487 |
size_t offset); |
| 488 |
|
| 489 |
template <typename T> |
| 490 |
size_t Ioss::Map::map_field_to_db_scalar_order(T *variables, std::vector<double> &db_var, |
| 491 |
size_t begin_offset, size_t count, size_t stride, |
| 492 |
size_t offset) |
| 493 |
{ |
| 494 |
IOSS_FUNC_ENTER(m_); |
| 495 |
size_t num_out = 0; |
| 496 |
if (!m_reorder.empty()) { |
| 497 |
size_t k = offset; |
| 498 |
for (size_t j = begin_offset; j < count * stride; j += stride) { |
| 499 |
// Map to storage location. |
| 500 |
int64_t where = m_reorder[k++] - offset; |
| 501 |
if (where >= 0) { |
| 502 |
SMART_ASSERT(where < (int64_t)count)(where)(count); |
| 503 |
db_var[where] = variables[j]; |
| 504 |
num_out++; |
| 505 |
} |
| 506 |
} |
| 507 |
} |
| 508 |
else { |
| 509 |
size_t k = 0; |
| 510 |
for (size_t j = begin_offset; j < count * stride; j += stride) { |
| 511 |
// Map to storage location. |
| 512 |
db_var[k++] = variables[j]; |
| 513 |
} |
| 514 |
num_out = count; |
| 515 |
} |
| 516 |
return num_out; |
| 517 |
} |
| 518 |
|
| 519 |
void Ioss::Map::build_reorder_map_nl(int64_t start, int64_t count) |
| 520 |
{ |
| 521 |
// This routine builds a map that relates the current node id order |
| 522 |
// to the original node ordering in affect at the time the file was |
| 523 |
// created. That is, the node map used to define the topology of the |
| 524 |
// model. Now, if there are changes in node ordering at the |
| 525 |
// application level, we build the node reorder map to map the |
| 526 |
// current order into the original order. An added complication is |
| 527 |
// that this is more than just a reordering... It may be that the |
| 528 |
// application has 'ghosted' nodes that it doesn't want to put out on |
| 529 |
// the database, so the reorder map must handle a node that is not |
| 530 |
// in the original mesh and map that to an invalid value (currently |
| 531 |
// using -1 as invalid value...) |
| 532 |
|
| 533 |
// Note: To further add confusion, the reorder map is 0-based |
| 534 |
// and the reverse map and 'map' are 1-based. This is |
| 535 |
// just a consequence of how they are intended to be used... |
| 536 |
// |
| 537 |
// `start` is based on a 0-based array -- start of the reorderMap to build. |
| 538 |
|
| 539 |
if (m_reorder.empty()) { |
| 540 |
// See if actually need a reorder map first... |
| 541 |
bool need_reorder_map = false; |
| 542 |
if (m_reverse.empty()) { |
| 543 |
need_reorder_map = true; |
| 544 |
} |
| 545 |
else { |
| 546 |
int64_t my_end = start + count; |
| 547 |
for (int64_t i = start; i < my_end; i++) { |
| 548 |
int64_t global_id = m_map[i + 1]; |
| 549 |
int64_t orig_local_id = global_to_local_nl(global_id) - 1; |
| 550 |
|
| 551 |
// The reordering should only be a permutation of the original |
| 552 |
// ordering within this entity block... |
| 553 |
SMART_ASSERT(orig_local_id >= start && orig_local_id <= my_end) |
| 554 |
(orig_local_id)(start)(my_end); |
| 555 |
if (i != orig_local_id) { |
| 556 |
need_reorder_map = true; |
| 557 |
break; |
| 558 |
} |
| 559 |
} |
| 560 |
} |
| 561 |
if (need_reorder_map) { |
| 562 |
int64_t map_size = m_map.size() - 1; |
| 563 |
m_reorder.resize(map_size); |
| 564 |
// If building a partial reorder map, assume all entries |
| 565 |
// are a direct 1-1 and then let the partial fills overwrite |
| 566 |
// if needed. |
| 567 |
std::iota(m_reorder.begin(), m_reorder.end(), 0); |
| 568 |
} |
| 569 |
else { |
| 570 |
return; |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
int64_t my_end = start + count; |
| 575 |
for (int64_t i = start; i < my_end; i++) { |
| 576 |
int64_t global_id = m_map[i + 1]; |
| 577 |
int64_t orig_local_id = global_to_local_nl(global_id) - 1; |
| 578 |
|
| 579 |
// The reordering should only be a permutation of the original |
| 580 |
// ordering within this entity block... |
| 581 |
SMART_ASSERT(orig_local_id >= start && orig_local_id <= my_end)(orig_local_id)(start)(my_end); |
| 582 |
m_reorder[i] = orig_local_id; |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
// Node and Element mapping functions. The ExodusII database |
| 587 |
// stores ids in a local-id system (1..NUMNP), (1..NUMEL) but |
| 588 |
// Sierra wants entities in a global system. These routines |
| 589 |
// take care of the mapping from local <-> global |
| 590 |
|
| 591 |
int64_t Ioss::Map::global_to_local(int64_t global, bool must_exist) const |
| 592 |
{ |
| 593 |
IOSS_FUNC_ENTER(m_); |
| 594 |
return global_to_local_nl(global, must_exist); |
| 595 |
} |
| 596 |
|
| 597 |
int64_t Ioss::Map::global_to_local_nl(int64_t global, bool must_exist) const |
| 598 |
{ |
| 599 |
int64_t local = global; |
| 600 |
#if defined USE_LAZY_REVERSE |
| 601 |
if (!is_sequential() && m_reverse.empty() && m_reorder.empty()) { |
| 602 |
auto *new_this = const_cast<Ioss::Map *>(this); |
| 603 |
new_this->build_reverse_map_no_lock(); |
| 604 |
} |
| 605 |
#endif |
| 606 |
|
| 607 |
if (!is_sequential() && !m_reverse.empty()) { |
| 608 |
// Possible for !is_sequential() which means non-one-to-one, but |
| 609 |
// reverseMap is empty (which implied one-to-one) if the ORIGINAL mapping defined |
| 610 |
// during dbState == STATE_MODEL was one-to-one, but there is a |
| 611 |
// reordering which is due to new id ordering defined after STATE_MODEL... |
| 612 |
#if defined MAP_USE_SORTED_VECTOR |
| 613 |
auto iter = std::lower_bound( |
| 614 |
m_reverse.begin(), m_reverse.end(), global, |
| 615 |
[](const Ioss::IdPair &lhs, int64_t val) -> bool { return lhs.first < val; }); |
| 616 |
if (iter != m_reverse.end() && iter->first == global) { |
| 617 |
local = iter->second; |
| 618 |
} |
| 619 |
else { |
| 620 |
local = 0; |
| 621 |
} |
| 622 |
#else |
| 623 |
auto iter = m_reverse.find(global); |
| 624 |
if (iter != m_reverse.end()) { |
| 625 |
local = iter->second; |
| 626 |
} |
| 627 |
else { |
| 628 |
local = 0; |
| 629 |
} |
| 630 |
#endif |
| 631 |
} |
| 632 |
else if (!must_exist && global > static_cast<int64_t>(m_map.size()) - 1) { |
| 633 |
local = 0; |
| 634 |
} |
| 635 |
else { |
| 636 |
local = global - m_offset; |
| 637 |
} |
| 638 |
if (local > static_cast<int64_t>(m_map.size()) - 1) { |
| 639 |
std::ostringstream errmsg; |
| 640 |
fmt::print(errmsg, |
| 641 |
"ERROR: Ioss Mapping routines detected {0} with global id equal to {1} returns a " |
| 642 |
"local id of {2} which is\n" |
| 643 |
"larger than the local {0} count {5} on processor {3}, filename '{4}'.\n" |
| 644 |
"This should not happen, please report.\n", |
| 645 |
m_entityType, global, local, m_myProcessor, m_filename, m_map.size() - 1); |
| 646 |
IOSS_ERROR(errmsg); |
| 647 |
} |
| 648 |
else if (local <= 0 && must_exist) { |
| 649 |
std::ostringstream errmsg; |
| 650 |
fmt::print(errmsg, |
| 651 |
"ERROR: Ioss Mapping routines could not find a {0} with global id equal to {1} in " |
| 652 |
"the {0} map\n" |
| 653 |
"on processor {2}, filename '{3}'.\n" |
| 654 |
"This should not happen, please report.\n", |
| 655 |
m_entityType, global, m_myProcessor, m_filename); |
| 656 |
IOSS_ERROR(errmsg); |
| 657 |
} |
| 658 |
return local; |
| 659 |
} |