| 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_CodeTypes.h" |
| 8 |
#include "Ioss_MemoryUtils.h" |
| 9 |
#include "Ioss_ParallelUtils.h" |
| 10 |
#include "Ioss_PropertyManager.h" |
| 11 |
#include "Ioss_Utils.h" |
| 12 |
#include <cstddef> |
| 13 |
#include <cstdlib> |
| 14 |
#include <fmt/ostream.h> |
| 15 |
#include <iosfwd> |
| 16 |
#include <numeric> |
| 17 |
#include <string> |
| 18 |
#include <tokenize.h> |
| 19 |
#include <vector> |
| 20 |
|
| 21 |
#include "Ioss_Property.h" |
| 22 |
|
| 23 |
#ifdef SEACAS_HAVE_MPI |
| 24 |
#include "Ioss_SerializeIO.h" |
| 25 |
#endif |
| 26 |
|
| 27 |
namespace { |
| 28 |
#ifdef SEACAS_HAVE_MPI |
| 29 |
MPI_Op which_reduction(Ioss::ParallelUtils::MinMax which) |
| 30 |
{ |
| 31 |
MPI_Op oper = MPI_MAX; |
| 32 |
if (which == Ioss::ParallelUtils::DO_MAX) { |
| 33 |
oper = MPI_MAX; |
| 34 |
} |
| 35 |
else if (which == Ioss::ParallelUtils::DO_MIN) { |
| 36 |
oper = MPI_MIN; |
| 37 |
} |
| 38 |
else if (which == Ioss::ParallelUtils::DO_SUM) { |
| 39 |
oper = MPI_SUM; |
| 40 |
} |
| 41 |
return oper; |
| 42 |
} |
| 43 |
#endif |
| 44 |
} // namespace |
| 45 |
|
| 46 |
Ioss::ParallelUtils::ParallelUtils(Ioss_MPI_Comm the_communicator) : communicator_(the_communicator) |
| 47 |
{ |
| 48 |
} |
| 49 |
|
| 50 |
void Ioss::ParallelUtils::add_environment_properties(Ioss::PropertyManager &properties) |
| 51 |
{ |
| 52 |
static bool do_print = true; // Print the first time called |
| 53 |
|
| 54 |
std::string env_props; |
| 55 |
if (get_environment("IOSS_PROPERTIES", env_props, parallel_size() > 1)) { |
| 56 |
// env_props string should be of the form |
| 57 |
// "PROP1=VALUE1:PROP2=VALUE2:..." |
| 58 |
Ioss::NameList prop_val = tokenize(env_props, ":"); |
| 59 |
|
| 60 |
int rank = parallel_rank(); |
| 61 |
for (auto &elem : prop_val) { |
| 62 |
Ioss::NameList property = tokenize(elem, "="); |
| 63 |
if (property.size() != 2) { |
| 64 |
IOSS_ERROR(fmt::format( |
| 65 |
"ERROR: Invalid property specification found in IOSS_PROPERTIES environment variable\n" |
| 66 |
" Found '{}' which is not of the correct PROPERTY=VALUE form", |
| 67 |
elem)); |
| 68 |
} |
| 69 |
std::string prop = Utils::uppercase(property[0]); |
| 70 |
std::string value = property[1]; |
| 71 |
std::string up_value = Utils::uppercase(value); |
| 72 |
bool all_digit = value.find_first_not_of("0123456789") == std::string::npos; |
| 73 |
|
| 74 |
if (do_print && rank == 0) { |
| 75 |
fmt::print(Ioss::OUTPUT(), "IOSS: Adding property '{}' with value '{}'\n", prop, value); |
| 76 |
} |
| 77 |
if (all_digit) { |
| 78 |
int int_value = std::stoi(value); |
| 79 |
properties.add(Property(prop, int_value)); |
| 80 |
} |
| 81 |
else if (up_value == "TRUE" || up_value == "YES") { |
| 82 |
properties.add(Property(prop, 1)); |
| 83 |
} |
| 84 |
else if (up_value == "FALSE" || up_value == "NO") { |
| 85 |
properties.add(Property(prop, 0)); |
| 86 |
} |
| 87 |
else { |
| 88 |
properties.add(Property(prop, value)); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
do_print = false; |
| 93 |
} |
| 94 |
|
| 95 |
bool Ioss::ParallelUtils::get_environment(const std::string &name, std::string &value, |
| 96 |
IOSS_MAYBE_UNUSED bool sync_parallel) const |
| 97 |
{ |
| 98 |
IOSS_PAR_UNUSED(sync_parallel); |
| 99 |
#ifdef SEACAS_HAVE_MPI |
| 100 |
char *result_string = nullptr; |
| 101 |
|
| 102 |
int string_length = 0; |
| 103 |
|
| 104 |
int rank = parallel_rank(); |
| 105 |
if (rank == 0) { |
| 106 |
result_string = std::getenv(name.c_str()); |
| 107 |
string_length = result_string != nullptr ? static_cast<int>(std::strlen(result_string)) : 0; |
| 108 |
} |
| 109 |
|
| 110 |
if (sync_parallel && parallel_size() > 1) { |
| 111 |
broadcast(string_length); |
| 112 |
|
| 113 |
if (string_length > 0) { |
| 114 |
std::vector<char> broadcast_string(string_length + 1); |
| 115 |
if (rank == 0) { |
| 116 |
Ioss::Utils::copy_string(Data(broadcast_string), result_string, |
| 117 |
static_cast<size_t>(string_length) + 1); |
| 118 |
} |
| 119 |
broadcast(broadcast_string); |
| 120 |
value = std::string(Data(broadcast_string)); |
| 121 |
} |
| 122 |
else { |
| 123 |
value = std::string(""); |
| 124 |
} |
| 125 |
} |
| 126 |
else { |
| 127 |
if (rank == 0) { |
| 128 |
if (string_length > 0) { |
| 129 |
value = std::string(result_string); |
| 130 |
} |
| 131 |
else { |
| 132 |
value = std::string(""); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
return string_length > 0; |
| 137 |
#else |
| 138 |
char *result_string = std::getenv(name.c_str()); |
| 139 |
if (result_string != nullptr) { |
| 140 |
value = std::string(result_string); |
| 141 |
} |
| 142 |
else { |
| 143 |
value = std::string(""); |
| 144 |
} |
| 145 |
return (result_string != nullptr); |
| 146 |
#endif |
| 147 |
} |
| 148 |
|
| 149 |
bool Ioss::ParallelUtils::get_environment(const std::string &name, int &value, |
| 150 |
IOSS_MAYBE_UNUSED bool sync_parallel) const |
| 151 |
{ |
| 152 |
std::string str_value; |
| 153 |
bool success = get_environment(name, str_value, sync_parallel); |
| 154 |
if (success) { |
| 155 |
value = std::stoi(str_value); |
| 156 |
} |
| 157 |
return success; |
| 158 |
} |
| 159 |
|
| 160 |
bool Ioss::ParallelUtils::get_environment(const std::string &name, |
| 161 |
IOSS_MAYBE_UNUSED bool sync_parallel) const |
| 162 |
{ |
| 163 |
// Return true if 'name' defined, no matter what the value. |
| 164 |
// Return false if 'name' not defined. |
| 165 |
IOSS_PAR_UNUSED(sync_parallel); |
| 166 |
#ifdef SEACAS_HAVE_MPI |
| 167 |
char *result_string = nullptr; |
| 168 |
int string_length = 0; |
| 169 |
|
| 170 |
int rank = Ioss::ParallelUtils::parallel_rank(); |
| 171 |
if (rank == 0) { |
| 172 |
result_string = std::getenv(name.c_str()); |
| 173 |
string_length = result_string != nullptr ? static_cast<int>(std::strlen(result_string)) : 0; |
| 174 |
} |
| 175 |
|
| 176 |
if (sync_parallel && parallel_size() > 1) { |
| 177 |
broadcast(string_length); |
| 178 |
} |
| 179 |
|
| 180 |
return string_length > 0; |
| 181 |
#else |
| 182 |
char *result_string = std::getenv(name.c_str()); |
| 183 |
return (result_string != nullptr); |
| 184 |
#endif |
| 185 |
} |
| 186 |
|
| 187 |
std::string Ioss::ParallelUtils::decode_filename(const std::string &filename, |
| 188 |
bool is_parallel) const |
| 189 |
{ |
| 190 |
std::string decoded_filename(filename); |
| 191 |
|
| 192 |
if (is_parallel) { |
| 193 |
// Running in parallel, assume nemesis and decode what the filename |
| 194 |
// should be for this processor. |
| 195 |
int processor = parallel_rank(); |
| 196 |
int num_processors = parallel_size(); |
| 197 |
|
| 198 |
decoded_filename = Ioss::Utils::decode_filename(filename, processor, num_processors); |
| 199 |
} |
| 200 |
return decoded_filename; |
| 201 |
} |
| 202 |
|
| 203 |
int Ioss::ParallelUtils::parallel_size() const |
| 204 |
{ |
| 205 |
if (parallelSize_ == -1) { |
| 206 |
parallelSize_ = 1; |
| 207 |
#ifdef SEACAS_HAVE_MPI |
| 208 |
if (communicator_ != Ioss::ParallelUtils::comm_null()) { |
| 209 |
MPI_Comm_size(communicator_, ¶llelSize_); |
| 210 |
} |
| 211 |
#endif |
| 212 |
} |
| 213 |
return parallelSize_; |
| 214 |
} |
| 215 |
|
| 216 |
int Ioss::ParallelUtils::parallel_rank() const |
| 217 |
{ |
| 218 |
if (parallelRank_ == -1) { |
| 219 |
parallelRank_ = 0; |
| 220 |
#ifdef SEACAS_HAVE_MPI |
| 221 |
if (communicator_ != Ioss::ParallelUtils::comm_null()) { |
| 222 |
MPI_Comm_rank(communicator_, ¶llelRank_); |
| 223 |
} |
| 224 |
#endif |
| 225 |
} |
| 226 |
return parallelRank_; |
| 227 |
} |
| 228 |
|
| 229 |
void Ioss::ParallelUtils::memory_stats(int64_t &min, int64_t &max, int64_t &avg) const |
| 230 |
{ |
| 231 |
int64_t my_memory = Ioss::MemoryUtils::get_memory_info(); |
| 232 |
min = max = avg = my_memory; |
| 233 |
#ifdef SEACAS_HAVE_MPI |
| 234 |
if (parallel_size() > 1) { |
| 235 |
min = global_minmax(my_memory, DO_MIN); |
| 236 |
max = global_minmax(my_memory, DO_MAX); |
| 237 |
avg = global_minmax(my_memory, DO_SUM); |
| 238 |
avg /= parallel_size(); // Integer truncation probably ok... |
| 239 |
} |
| 240 |
#endif |
| 241 |
} |
| 242 |
|
| 243 |
void Ioss::ParallelUtils::hwm_memory_stats(int64_t &min, int64_t &max, int64_t &avg) const |
| 244 |
{ |
| 245 |
int64_t my_memory = Ioss::MemoryUtils::get_hwm_memory_info(); |
| 246 |
min = max = avg = my_memory; |
| 247 |
#ifdef SEACAS_HAVE_MPI |
| 248 |
if (parallel_size() > 1) { |
| 249 |
min = global_minmax(my_memory, DO_MIN); |
| 250 |
max = global_minmax(my_memory, DO_MAX); |
| 251 |
avg = global_minmax(my_memory, DO_SUM); |
| 252 |
avg /= parallel_size(); // Integer truncation probably ok... |
| 253 |
} |
| 254 |
#endif |
| 255 |
} |
| 256 |
|
| 257 |
// Generate a "globally unique id" which is unique over all entities |
| 258 |
// of a specific type over all processors. |
| 259 |
// Used by some applications for uniquely identifying an entity. |
| 260 |
int64_t Ioss::ParallelUtils::generate_guid(size_t id, IOSS_MAYBE_UNUSED int rank) const |
| 261 |
{ |
| 262 |
IOSS_PAR_UNUSED(rank); |
| 263 |
#ifdef SEACAS_HAVE_MPI |
| 264 |
static size_t lpow2 = 0; |
| 265 |
if (lpow2 == 0) { |
| 266 |
lpow2 = Ioss::Utils::log_power_2(parallel_size()); |
| 267 |
} |
| 268 |
if (rank == -1) { |
| 269 |
rank = parallel_rank(); |
| 270 |
} |
| 271 |
return (id << lpow2) + rank; |
| 272 |
#else |
| 273 |
return id; |
| 274 |
#endif |
| 275 |
} |
| 276 |
|
| 277 |
void Ioss::ParallelUtils::attribute_reduction(IOSS_MAYBE_UNUSED const int length, |
| 278 |
IOSS_MAYBE_UNUSED char buffer[]) const |
| 279 |
{ |
| 280 |
IOSS_PAR_UNUSED(length); |
| 281 |
IOSS_PAR_UNUSED(buffer); |
| 282 |
#ifdef SEACAS_HAVE_MPI |
| 283 |
if (1 < parallel_size()) { |
| 284 |
static_assert(sizeof(char) == 1, ""); |
| 285 |
|
| 286 |
std::vector<char> recv_buf(length); |
| 287 |
const int success = |
| 288 |
MPI_Allreduce(buffer, Data(recv_buf), length, MPI_BYTE, MPI_BOR, communicator_); |
| 289 |
if (MPI_SUCCESS != success) { |
| 290 |
IOSS_ERROR(fmt::format("{} - MPI_Allreduce failed", __func__)); |
| 291 |
} |
| 292 |
|
| 293 |
std::memcpy(buffer, Data(recv_buf), length); |
| 294 |
} |
| 295 |
#endif |
| 296 |
} |
| 297 |
|
| 298 |
void Ioss::ParallelUtils::barrier() const |
| 299 |
{ |
| 300 |
#ifdef SEACAS_HAVE_MPI |
| 301 |
MPI_Barrier(communicator_); |
| 302 |
#endif |
| 303 |
} |
| 304 |
|
| 305 |
void Ioss::ParallelUtils::global_count(const IntVector &local_counts, |
| 306 |
IntVector &global_counts) const |
| 307 |
{ |
| 308 |
// Vector 'local_counts' contains the number of objects |
| 309 |
// local to this processor. On exit, global_counts |
| 310 |
// contains the total number of objects on all processors. |
| 311 |
// Assumes that ordering is the same on all processors |
| 312 |
global_counts.resize(local_counts.size()); |
| 313 |
#ifdef SEACAS_HAVE_MPI |
| 314 |
if (!local_counts.empty() && parallel_size() > 1) { |
| 315 |
if (Ioss::SerializeIO::isEnabled() && Ioss::SerializeIO::inBarrier()) { |
| 316 |
IOSS_ERROR(fmt::format("{} - Attempting mpi while in barrier owned by {}", __func__, |
| 317 |
Ioss::SerializeIO::getOwner())); |
| 318 |
} |
| 319 |
const int success = |
| 320 |
MPI_Allreduce((void *)Data(local_counts), Data(global_counts), |
| 321 |
static_cast<int>(local_counts.size()), MPI_INT, MPI_SUM, communicator_); |
| 322 |
if (success != MPI_SUCCESS) { |
| 323 |
IOSS_ERROR(fmt::format("{} - MPI_Allreduce failed", __func__)); |
| 324 |
} |
| 325 |
} |
| 326 |
else { |
| 327 |
// Serial run, just copy local to global... |
| 328 |
std::copy(local_counts.begin(), local_counts.end(), global_counts.begin()); |
| 329 |
} |
| 330 |
#else |
| 331 |
std::copy(local_counts.begin(), local_counts.end(), global_counts.begin()); |
| 332 |
#endif |
| 333 |
} |
| 334 |
|
| 335 |
void Ioss::ParallelUtils::global_count(const Int64Vector &local_counts, |
| 336 |
Int64Vector &global_counts) const |
| 337 |
{ |
| 338 |
// Vector 'local_counts' contains the number of objects |
| 339 |
// local to this processor. On exit, global_counts |
| 340 |
// contains the total number of objects on all processors. |
| 341 |
// Assumes that ordering is the same on all processors |
| 342 |
global_counts.resize(local_counts.size()); |
| 343 |
#ifdef SEACAS_HAVE_MPI |
| 344 |
if (!local_counts.empty() && parallel_size() > 1) { |
| 345 |
if (Ioss::SerializeIO::isEnabled() && Ioss::SerializeIO::inBarrier()) { |
| 346 |
IOSS_ERROR(fmt::format("{} - Attempting mpi while in barrier owned by {}", __func__, |
| 347 |
Ioss::SerializeIO::getOwner())); |
| 348 |
} |
| 349 |
const int success = MPI_Allreduce((void *)Data(local_counts), Data(global_counts), |
| 350 |
static_cast<int>(local_counts.size()), MPI_LONG_LONG_INT, |
| 351 |
MPI_SUM, communicator_); |
| 352 |
if (success != MPI_SUCCESS) { |
| 353 |
IOSS_ERROR(fmt::format("{} - MPI_Allreduce failed", __func__)); |
| 354 |
} |
| 355 |
} |
| 356 |
else { |
| 357 |
// Serial run, just copy local to global... |
| 358 |
std::copy(local_counts.begin(), local_counts.end(), global_counts.begin()); |
| 359 |
} |
| 360 |
#else |
| 361 |
std::copy(local_counts.begin(), local_counts.end(), global_counts.begin()); |
| 362 |
#endif |
| 363 |
} |
| 364 |
|
| 365 |
template IOSS_EXPORT int |
| 366 |
Ioss::ParallelUtils::global_minmax(int, Ioss::ParallelUtils::MinMax which) const; |
| 367 |
template IOSS_EXPORT unsigned int |
| 368 |
Ioss::ParallelUtils::global_minmax(unsigned int, Ioss::ParallelUtils::MinMax which) const; |
| 369 |
template IOSS_EXPORT int64_t |
| 370 |
Ioss::ParallelUtils::global_minmax(int64_t, Ioss::ParallelUtils::MinMax which) const; |
| 371 |
template IOSS_EXPORT double |
| 372 |
Ioss::ParallelUtils::global_minmax(double, Ioss::ParallelUtils::MinMax which) const; |
| 373 |
|
| 374 |
template <typename T> |
| 375 |
T Ioss::ParallelUtils::global_minmax(T local_minmax, |
| 376 |
IOSS_MAYBE_UNUSED Ioss::ParallelUtils::MinMax which) const |
| 377 |
{ |
| 378 |
IOSS_PAR_UNUSED(which); |
| 379 |
T minmax = local_minmax; |
| 380 |
|
| 381 |
#ifdef SEACAS_HAVE_MPI |
| 382 |
if (parallel_size() > 1) { |
| 383 |
if (Ioss::SerializeIO::isEnabled() && Ioss::SerializeIO::inBarrier()) { |
| 384 |
IOSS_ERROR(fmt::format("{} - Attempting mpi while in barrier owned by {}", __func__, |
| 385 |
Ioss::SerializeIO::getOwner())); |
| 386 |
} |
| 387 |
static T inbuf[1], outbuf[1]; |
| 388 |
inbuf[0] = local_minmax; |
| 389 |
|
| 390 |
MPI_Op oper = which_reduction(which); |
| 391 |
|
| 392 |
const int success = |
| 393 |
MPI_Allreduce((void *)&inbuf[0], &outbuf[0], 1, mpi_type(T()), oper, communicator_); |
| 394 |
if (success != MPI_SUCCESS) { |
| 395 |
IOSS_ERROR(fmt::format("{} - MPI_Allreduce failed", __func__)); |
| 396 |
} |
| 397 |
minmax = outbuf[0]; |
| 398 |
} |
| 399 |
#endif |
| 400 |
return minmax; |
| 401 |
} |
| 402 |
|
| 403 |
/// \relates Ioss::ParallelUtils::broadcast |
| 404 |
template IOSS_EXPORT void Ioss::ParallelUtils::broadcast(double &my_value, int) const; |
| 405 |
/// \relates Ioss::ParallelUtils::broadcast |
| 406 |
template IOSS_EXPORT void Ioss::ParallelUtils::broadcast(int &my_value, int) const; |
| 407 |
/// \relates Ioss::ParallelUtils::broadcast |
| 408 |
template IOSS_EXPORT void Ioss::ParallelUtils::broadcast(int64_t &my_value, int) const; |
| 409 |
|
| 410 |
namespace Ioss { |
| 411 |
template <> |
| 412 |
void ParallelUtils::broadcast(IOSS_MAYBE_UNUSED std::string &my_str, |
| 413 |
IOSS_MAYBE_UNUSED int root) const |
| 414 |
{ |
| 415 |
IOSS_PAR_UNUSED(my_str); |
| 416 |
IOSS_PAR_UNUSED(root); |
| 417 |
#ifdef SEACAS_HAVE_MPI |
| 418 |
if (parallel_size() > 1) { |
| 419 |
const int success = MPI_Bcast(const_cast<char *>(my_str.data()), (int)my_str.size() + 1, |
| 420 |
MPI_CHAR, root, communicator_); |
| 421 |
if (success != MPI_SUCCESS) { |
| 422 |
IOSS_ERROR(fmt::format("{} - MPI_Broadcast failed", __func__)); |
| 423 |
} |
| 424 |
} |
| 425 |
#endif |
| 426 |
} |
| 427 |
} // namespace Ioss |
| 428 |
|
| 429 |
template <typename T> |
| 430 |
void Ioss::ParallelUtils::broadcast(IOSS_MAYBE_UNUSED T &my_value, IOSS_MAYBE_UNUSED int root) const |
| 431 |
{ |
| 432 |
IOSS_PAR_UNUSED(my_value); |
| 433 |
IOSS_PAR_UNUSED(root); |
| 434 |
#ifdef SEACAS_HAVE_MPI |
| 435 |
if (parallel_size() > 1) { |
| 436 |
const int success = MPI_Bcast((void *)&my_value, 1, mpi_type(T()), root, communicator_); |
| 437 |
if (success != MPI_SUCCESS) { |
| 438 |
IOSS_ERROR(fmt::format("{} - MPI_Broadcast failed", __func__)); |
| 439 |
} |
| 440 |
} |
| 441 |
#endif |
| 442 |
} |
| 443 |
|
| 444 |
/// \relates Ioss::ParallelUtils::broadcast |
| 445 |
template IOSS_EXPORT void Ioss::ParallelUtils::broadcast(std::vector<double> &, int) const; |
| 446 |
/// \relates Ioss::ParallelUtils::broadcast |
| 447 |
template IOSS_EXPORT void Ioss::ParallelUtils::broadcast(std::vector<int> &, int) const; |
| 448 |
/// \relates Ioss::ParallelUtils::broadcast |
| 449 |
template IOSS_EXPORT void Ioss::ParallelUtils::broadcast(std::vector<long> &, int) const; |
| 450 |
/// \relates Ioss::ParallelUtils::broadcast |
| 451 |
template IOSS_EXPORT void Ioss::ParallelUtils::broadcast(std::vector<long long> &, int) const; |
| 452 |
/// \relates Ioss::ParallelUtils::broadcast |
| 453 |
template IOSS_EXPORT void Ioss::ParallelUtils::broadcast(std::vector<char> &, int) const; |
| 454 |
/// \relates Ioss::ParallelUtils::broadcast |
| 455 |
namespace Ioss { |
| 456 |
template <> |
| 457 |
IOSS_EXPORT void |
| 458 |
ParallelUtils::broadcast(IOSS_MAYBE_UNUSED std::vector<std::pair<int, int>> &my_value, |
| 459 |
IOSS_MAYBE_UNUSED int root) const |
| 460 |
{ |
| 461 |
IOSS_PAR_UNUSED(my_value); |
| 462 |
IOSS_PAR_UNUSED(root); |
| 463 |
#ifdef SEACAS_HAVE_MPI |
| 464 |
if (parallel_size() > 1) { |
| 465 |
const int success = MPI_Bcast(Data(my_value), (int)my_value.size() * 2, mpi_type(int(0)), |
| 466 |
root, communicator_); |
| 467 |
if (success != MPI_SUCCESS) { |
| 468 |
IOSS_ERROR(fmt::format("{} - MPI_Broadcast failed", __func__)); |
| 469 |
} |
| 470 |
} |
| 471 |
#endif |
| 472 |
} |
| 473 |
} // namespace Ioss |
| 474 |
|
| 475 |
template <typename T> |
| 476 |
void Ioss::ParallelUtils::broadcast(IOSS_MAYBE_UNUSED std::vector<T> &my_value, |
| 477 |
IOSS_MAYBE_UNUSED int root) const |
| 478 |
{ |
| 479 |
IOSS_PAR_UNUSED(my_value); |
| 480 |
IOSS_PAR_UNUSED(root); |
| 481 |
#ifdef SEACAS_HAVE_MPI |
| 482 |
if (parallel_size() > 1) { |
| 483 |
const int success = |
| 484 |
MPI_Bcast(Data(my_value), (int)my_value.size(), mpi_type(T()), root, communicator_); |
| 485 |
if (success != MPI_SUCCESS) { |
| 486 |
IOSS_ERROR(fmt::format("{} - MPI_Broadcast failed", __func__)); |
| 487 |
} |
| 488 |
} |
| 489 |
#endif |
| 490 |
} |
| 491 |
|
| 492 |
/// \relates Ioss::ParallelUtils::gather |
| 493 |
template IOSS_EXPORT void Ioss::ParallelUtils::gather(double, std::vector<double> &) const; |
| 494 |
/// \relates Ioss::ParallelUtils::gather |
| 495 |
template IOSS_EXPORT void Ioss::ParallelUtils::gather(int, std::vector<int> &) const; |
| 496 |
/// \relates Ioss::ParallelUtils::gather |
| 497 |
template IOSS_EXPORT void Ioss::ParallelUtils::gather(int64_t, std::vector<int64_t> &) const; |
| 498 |
/// \relates Ioss::ParallelUtils::all_gather |
| 499 |
template IOSS_EXPORT void Ioss::ParallelUtils::all_gather(int, std::vector<int> &) const; |
| 500 |
/// \relates Ioss::ParallelUtils::all_gather |
| 501 |
template IOSS_EXPORT void Ioss::ParallelUtils::all_gather(int64_t, std::vector<int64_t> &) const; |
| 502 |
/// \relates Ioss::ParallelUtils::all_gather |
| 503 |
template IOSS_EXPORT void Ioss::ParallelUtils::all_gather(std::vector<int> &, |
| 504 |
std::vector<int> &) const; |
| 505 |
/// \relates Ioss::ParallelUtils::all_gather |
| 506 |
template IOSS_EXPORT void Ioss::ParallelUtils::all_gather(std::vector<int64_t> &, |
| 507 |
std::vector<int64_t> &) const; |
| 508 |
|
| 509 |
template <typename T> void Ioss::ParallelUtils::gather(T my_value, std::vector<T> &result) const |
| 510 |
{ |
| 511 |
if (parallel_rank() == 0) { |
| 512 |
result.resize(parallel_size()); |
| 513 |
} |
| 514 |
#ifdef SEACAS_HAVE_MPI |
| 515 |
if (parallel_size() > 1) { |
| 516 |
const int success = MPI_Gather((void *)&my_value, 1, mpi_type(T()), (void *)Data(result), 1, |
| 517 |
mpi_type(T()), 0, communicator_); |
| 518 |
if (success != MPI_SUCCESS) { |
| 519 |
IOSS_ERROR(fmt::format("{} - MPI_Gather failed", __func__)); |
| 520 |
} |
| 521 |
} |
| 522 |
else { |
| 523 |
result[0] = my_value; |
| 524 |
} |
| 525 |
#else |
| 526 |
result[0] = my_value; |
| 527 |
#endif |
| 528 |
} |
| 529 |
|
| 530 |
template <typename T> void Ioss::ParallelUtils::all_gather(T my_value, std::vector<T> &result) const |
| 531 |
{ |
| 532 |
result.resize(parallel_size()); |
| 533 |
#ifdef SEACAS_HAVE_MPI |
| 534 |
if (parallel_size() > 1) { |
| 535 |
const int success = MPI_Allgather((void *)&my_value, 1, mpi_type(T()), (void *)Data(result), 1, |
| 536 |
mpi_type(T()), communicator_); |
| 537 |
if (success != MPI_SUCCESS) { |
| 538 |
IOSS_ERROR(fmt::format("{} - MPI_Allgather failed", __func__)); |
| 539 |
} |
| 540 |
} |
| 541 |
else { |
| 542 |
result[0] = my_value; |
| 543 |
} |
| 544 |
#else |
| 545 |
result[0] = my_value; |
| 546 |
#endif |
| 547 |
} |
| 548 |
|
| 549 |
template <typename T> |
| 550 |
void Ioss::ParallelUtils::all_gather(std::vector<T> &my_values, std::vector<T> &result) const |
| 551 |
{ |
| 552 |
result.resize(parallel_size() * my_values.size()); |
| 553 |
#ifdef SEACAS_HAVE_MPI |
| 554 |
if (parallel_size() > 1) { |
| 555 |
const int success = |
| 556 |
MPI_Allgather(Data(my_values), my_values.size(), mpi_type(T()), (void *)Data(result), |
| 557 |
my_values.size(), mpi_type(T()), communicator_); |
| 558 |
if (success != MPI_SUCCESS) { |
| 559 |
IOSS_ERROR(fmt::format("{} - MPI_Allgather failed", __func__)); |
| 560 |
} |
| 561 |
} |
| 562 |
else { |
| 563 |
result = my_values; |
| 564 |
} |
| 565 |
#else |
| 566 |
result = my_values; |
| 567 |
#endif |
| 568 |
} |
| 569 |
|
| 570 |
void Ioss::ParallelUtils::progress(std::string_view output) const |
| 571 |
{ |
| 572 |
static double begin = Utils::timer(); |
| 573 |
|
| 574 |
int64_t MiB = static_cast<int64_t>(1024) * static_cast<int64_t>(1024); |
| 575 |
int64_t min = 0, max = 0, avg = 0; |
| 576 |
memory_stats(min, max, avg); |
| 577 |
|
| 578 |
if (parallel_rank() == 0) { |
| 579 |
double diff = Utils::timer() - begin; |
| 580 |
fmt::print(Ioss::DebugOut(), " [{:.3f}] ({}MiB {}MiB {}MiB)\t{}\n", diff, min / MiB, |
| 581 |
max / MiB, avg / MiB, output); |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
/// \relates Ioss::ParallelUtils::gather |
| 586 |
template IOSS_EXPORT void Ioss::ParallelUtils::gather(std::vector<int> &my_values, |
| 587 |
std::vector<int> &result) const; |
| 588 |
/// \relates Ioss::ParallelUtils::gather |
| 589 |
template IOSS_EXPORT void Ioss::ParallelUtils::gather(std::vector<int64_t> &my_values, |
| 590 |
std::vector<int64_t> &result) const; |
| 591 |
template <typename T> |
| 592 |
void Ioss::ParallelUtils::gather(std::vector<T> &my_values, std::vector<T> &result) const |
| 593 |
{ |
| 594 |
size_t count = my_values.size(); |
| 595 |
if (parallel_rank() == 0) { |
| 596 |
result.resize(count * parallel_size()); |
| 597 |
} |
| 598 |
#ifdef SEACAS_HAVE_MPI |
| 599 |
if (parallel_size() > 1) { |
| 600 |
const int success = MPI_Gather((void *)Data(my_values), count, mpi_type(T()), |
| 601 |
(void *)Data(result), count, mpi_type(T()), 0, communicator_); |
| 602 |
if (success != MPI_SUCCESS) { |
| 603 |
IOSS_ERROR(fmt::format("{} - MPI_Gather failed", __func__)); |
| 604 |
} |
| 605 |
} |
| 606 |
else { |
| 607 |
std::copy(my_values.begin(), my_values.end(), result.begin()); |
| 608 |
} |
| 609 |
#else |
| 610 |
std::copy(my_values.begin(), my_values.end(), result.begin()); |
| 611 |
#endif |
| 612 |
} |
| 613 |
|
| 614 |
/// \relates Ioss::ParallelUtils::gather |
| 615 |
template IOSS_EXPORT int Ioss::ParallelUtils::gather(int num_vals, int size_per_val, |
| 616 |
std::vector<int> &my_values, |
| 617 |
std::vector<int> &result) const; |
| 618 |
/// \relates Ioss::ParallelUtils::gather |
| 619 |
template IOSS_EXPORT int Ioss::ParallelUtils::gather(int num_vals, int size_per_val, |
| 620 |
std::vector<char> &my_values, |
| 621 |
std::vector<char> &result) const; |
| 622 |
template <typename T> |
| 623 |
int Ioss::ParallelUtils::gather(int num_vals, IOSS_MAYBE_UNUSED int size_per_val, |
| 624 |
std::vector<T> &my_values, std::vector<T> &result) const |
| 625 |
{ |
| 626 |
IOSS_PAR_UNUSED(size_per_val); |
| 627 |
#ifdef SEACAS_HAVE_MPI |
| 628 |
std::vector<int> vals_per_proc; |
| 629 |
gather(num_vals, vals_per_proc); |
| 630 |
|
| 631 |
int tot_vals = std::accumulate(vals_per_proc.begin(), vals_per_proc.end(), 0); |
| 632 |
|
| 633 |
std::vector<int> vals_offset(vals_per_proc); |
| 634 |
std::vector<int> vals_index(vals_per_proc); |
| 635 |
|
| 636 |
int rank = parallel_rank(); |
| 637 |
assert(my_values.size() % size_per_val == 0); |
| 638 |
|
| 639 |
if (rank == 0) { |
| 640 |
Ioss::Utils::generate_index(vals_offset); |
| 641 |
for (size_t i = 0; i < vals_per_proc.size(); i++) { |
| 642 |
vals_index[i] *= size_per_val; |
| 643 |
vals_offset[i] *= size_per_val; |
| 644 |
} |
| 645 |
result.resize(tot_vals * size_per_val); |
| 646 |
} |
| 647 |
|
| 648 |
MPI_Gatherv(Data(my_values), (int)my_values.size(), mpi_type(T{}), Data(result), Data(vals_index), |
| 649 |
Data(vals_offset), mpi_type(T{}), 0, communicator()); |
| 650 |
#else |
| 651 |
int tot_vals = num_vals; |
| 652 |
result.resize(num_vals); |
| 653 |
std::copy(my_values.begin(), my_values.end(), result.begin()); |
| 654 |
#endif |
| 655 |
return tot_vals; // NOTE: Only valid on processor 0 |
| 656 |
} |