| 1 |
// Copyright(C) 2021-2025 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 "ZE_SystemInterface.h" |
| 8 |
#include "ZE_Version.h" // for qainfo |
| 9 |
|
| 10 |
#include <Ioss_GetLongOpt.h> |
| 11 |
#include <Ioss_ParallelUtils.h> |
| 12 |
#include <Ioss_Utils.h> |
| 13 |
#include <copyright.h> |
| 14 |
#include <cstdlib> // for exit, strtod, strtoul, abs, etc |
| 15 |
#include <fmt/color.h> |
| 16 |
#include <fmt/format.h> |
| 17 |
#include <tokenize.h> |
| 18 |
|
| 19 |
//! \file |
| 20 |
|
| 21 |
SystemInterface::SystemInterface(int my_rank) : myRank_(my_rank) { enroll_options(); } |
| 22 |
|
| 23 |
namespace { |
| 24 |
void parse_offset(const char *tokens, vector3d &offset, int myRank); |
| 25 |
} |
| 26 |
|
| 27 |
void SystemInterface::enroll_options() |
| 28 |
{ |
| 29 |
options_.usage("[options] -lattice <lattice_definition_file>"); |
| 30 |
|
| 31 |
options_.enroll("lattice", Ioss::GetLongOption::OptType::MandatoryValue, |
| 32 |
"Name of file to read lattice definition from. [required]", ""); |
| 33 |
|
| 34 |
options_.enroll("output", Ioss::GetLongOption::OptType::MandatoryValue, |
| 35 |
"Name of output file to create. Default is `zellij-out.e`", "zellij-out.e", |
| 36 |
nullptr, true); |
| 37 |
|
| 38 |
options_.enroll("rcb", Ioss::GetLongOption::OptType::NoValue, |
| 39 |
"Use recursive coordinate bisection method to decompose the input lattice for " |
| 40 |
"parallel output.", |
| 41 |
nullptr); |
| 42 |
options_.enroll( |
| 43 |
"rib", Ioss::GetLongOption::OptType::NoValue, |
| 44 |
"Use recursive inertial bisection method to decompose the input lattice for parallel output.", |
| 45 |
nullptr); |
| 46 |
|
| 47 |
options_.enroll("hsfc", Ioss::GetLongOption::OptType::NoValue, |
| 48 |
"Use hilbert space-filling curve method to decompose the input lattice for " |
| 49 |
"parallel output. [default]", |
| 50 |
nullptr); |
| 51 |
|
| 52 |
options_.enroll("linear", Ioss::GetLongOption::OptType::NoValue, |
| 53 |
"Use the linear method to decompose the input lattice for parallel output.\n" |
| 54 |
"\t\tElements in order first n/p to proc 0, next to proc 1.", |
| 55 |
nullptr); |
| 56 |
|
| 57 |
options_.enroll("cyclic", Ioss::GetLongOption::OptType::NoValue, |
| 58 |
"Use the cyclic method to decompose the input lattice for parallel output.\n" |
| 59 |
"\t\tElements handed out to id % proc_count", |
| 60 |
nullptr); |
| 61 |
|
| 62 |
options_.enroll("random", Ioss::GetLongOption::OptType::NoValue, |
| 63 |
"Use the random method to decompose the input lattice for parallel output.\n" |
| 64 |
"\t\tElements assigned randomly to processors in a way that preserves balance\n" |
| 65 |
"\t\t(do *not* use for a real run)", |
| 66 |
nullptr, nullptr, true); |
| 67 |
|
| 68 |
options_.enroll("ranks", Ioss::GetLongOption::OptType::MandatoryValue, |
| 69 |
"Number of ranks to decompose mesh/lattice across", "1"); |
| 70 |
|
| 71 |
options_.enroll("start_rank", Ioss::GetLongOption::OptType::MandatoryValue, |
| 72 |
"In partial output mode, start outputting decomposed files at this rank", "0"); |
| 73 |
|
| 74 |
options_.enroll("rank_count", Ioss::GetLongOption::OptType::MandatoryValue, |
| 75 |
"In partial output or subcycle modes, output this number of ranks", "0"); |
| 76 |
|
| 77 |
options_.enroll("subcycle", Ioss::GetLongOption::OptType::NoValue, |
| 78 |
"Process cells in groups of '-rank_count'. Helps minimize open files,\n" |
| 79 |
"\t\tbut is faster than only having a single file open.", |
| 80 |
nullptr); |
| 81 |
|
| 82 |
options_.enroll("offset", Ioss::GetLongOption::OptType::MandatoryValue, |
| 83 |
"Comma-separated x,y,z offset for coordinates of the output mesh.\n" |
| 84 |
"\t\tThe output coordinates will be xyz_out = xyz * scale + offset_xyz", |
| 85 |
nullptr); |
| 86 |
|
| 87 |
options_.enroll("scale", Ioss::GetLongOption::OptType::MandatoryValue, |
| 88 |
"Scale the output mesh coordinates by the specified value", "1"); |
| 89 |
|
| 90 |
options_.enroll( |
| 91 |
"minimize_open_files", Ioss::GetLongOption::OptType::OptionalValue, |
| 92 |
"Close files after accessing them to avoid issues with too many open files.\n" |
| 93 |
"\t\tIf argument is 'output' then close output, if 'unit' then close unit cells;\n" |
| 94 |
"\t\tif 'all' or no argument close all.\n" |
| 95 |
"\t\tShould not need to use this option unless you get an error message " |
| 96 |
"indicating this issue.", |
| 97 |
nullptr, "all", true); |
| 98 |
|
| 99 |
options_.enroll("ignore_sidesets", Ioss::GetLongOption::OptType::NoValue, |
| 100 |
"Do not copy any sidesets in the unit cells to the output file.", nullptr); |
| 101 |
|
| 102 |
options_.enroll("generate_sidesets", Ioss::GetLongOption::OptType::MandatoryValue, |
| 103 |
"Which surfaces on the output mesh should have sidesets generated,\n" |
| 104 |
"\t\t Valid options are:\n" |
| 105 |
"\t\t 'x' or 'i' for surface on minimum X coordinate, default name = `min_i`\n" |
| 106 |
"\t\t 'y' or 'j' for surface on minimum Y coordinate, default name = `min_j`\n" |
| 107 |
"\t\t 'z' or 'k' for surface on minimum Z coordinate, default name = `min_k`\n" |
| 108 |
"\t\t 'X' or 'I' for surface on maximum X coordinate, default name = `max_i`\n" |
| 109 |
"\t\t 'Y' or 'J' for surface on maximum Y coordinate, default name = `max_j`\n" |
| 110 |
"\t\t 'Z' or 'K' for surface on maximum Z coordinate, default name = `max_k`\n" |
| 111 |
"\t\t For example `xyXY` would generate sidesets on min/max X and Y surfaces.", |
| 112 |
""); |
| 113 |
|
| 114 |
options_.enroll( |
| 115 |
"sideset_names", Ioss::GetLongOption::OptType::MandatoryValue, |
| 116 |
"Specify names for one or more of the generated sidesets.\n" |
| 117 |
"\t\t Form is `axis:name,axis:name,...`\n" |
| 118 |
"\t\t where 'axis' is one of 'ijkIJKxyzXYZ', and 'name' is the name of the sideset.\n" |
| 119 |
"\t\t The default names are 'min_i', 'max_i', 'min_j', 'max_j', 'min_k', 'max_k'.\n" |
| 120 |
"\t\t For example `x:left,X:right` would name the sideset on the min x face 'left' and the " |
| 121 |
"max X face 'right'.", |
| 122 |
"", nullptr, true); |
| 123 |
|
| 124 |
options_.enroll("netcdf3", Ioss::GetLongOption::OptType::NoValue, |
| 125 |
"Output database will be a netcdf3 " |
| 126 |
"native classical netcdf file format (32-bit only)", |
| 127 |
nullptr); |
| 128 |
|
| 129 |
options_.enroll("netcdf4", Ioss::GetLongOption::OptType::NoValue, |
| 130 |
"Output database will be a netcdf4 " |
| 131 |
"hdf5-based file instead of the " |
| 132 |
"classical netcdf file format (default)", |
| 133 |
nullptr); |
| 134 |
|
| 135 |
options_.enroll("netcdf5", Ioss::GetLongOption::OptType::NoValue, |
| 136 |
"Output database will be a netcdf5 (CDF5) " |
| 137 |
"file instead of the classical netcdf file format", |
| 138 |
nullptr, nullptr, true); |
| 139 |
|
| 140 |
options_.enroll("32-bit", Ioss::GetLongOption::OptType::NoValue, |
| 141 |
"True if forcing the use of 32-bit integers for the output file", nullptr); |
| 142 |
|
| 143 |
options_.enroll("64-bit", Ioss::GetLongOption::OptType::NoValue, |
| 144 |
"True if forcing the use of 64-bit integers for the output file (default)", |
| 145 |
nullptr, nullptr, true); |
| 146 |
|
| 147 |
options_.enroll("zlib", Ioss::GetLongOption::OptType::NoValue, |
| 148 |
"Use the Zlib / libz compression method if compression is enabled " |
| 149 |
"(default) [exodus only].", |
| 150 |
nullptr); |
| 151 |
|
| 152 |
options_.enroll("szip", Ioss::GetLongOption::OptType::NoValue, |
| 153 |
"Use SZip compression. [exodus only, enables netcdf-4]", nullptr); |
| 154 |
|
| 155 |
options_.enroll("compress", Ioss::GetLongOption::OptType::MandatoryValue, |
| 156 |
"Specify the hdf5 zlib compression level [0..9] or szip [even, 4..32] to be used " |
| 157 |
"on the output file.", |
| 158 |
nullptr, nullptr, true); |
| 159 |
|
| 160 |
options_.enroll("separate_cells", Ioss::GetLongOption::OptType::NoValue, |
| 161 |
"Do not equivalence the nodes between adjacent unit cells.", nullptr); |
| 162 |
|
| 163 |
options_.enroll( |
| 164 |
"repeat", Ioss::GetLongOption::OptType::MandatoryValue, |
| 165 |
"Each lattice entry will be used the specified number of times as will\n" |
| 166 |
"\t\teach row in the lattice (for debugging). `-repeat 2` would double the lattice.", |
| 167 |
"1"); |
| 168 |
|
| 169 |
options_.enroll( |
| 170 |
"skip", Ioss::GetLongOption::OptType::MandatoryValue, |
| 171 |
"Skip the specified number of lattice entries and rows. For example, -skip 1\n" |
| 172 |
"\t\twould read every other entry on the row and every other row. (for debugging)", |
| 173 |
"1"); |
| 174 |
|
| 175 |
options_.enroll("help", Ioss::GetLongOption::OptType::NoValue, "Print this summary and exit", |
| 176 |
nullptr); |
| 177 |
|
| 178 |
options_.enroll("version", Ioss::GetLongOption::OptType::NoValue, "Print version and exit", |
| 179 |
nullptr); |
| 180 |
|
| 181 |
options_.enroll("debug", Ioss::GetLongOption::OptType::MandatoryValue, |
| 182 |
"debug level (values are or'd)\n" |
| 183 |
"\t\t 1 = Exodus Verbose mode.\n" |
| 184 |
"\t\t 2 = Memory and time stamp information.\n" |
| 185 |
"\t\t 4 = Verbose Unit Cell information.\n" |
| 186 |
"\t\t 8 = Verbose output of Grid finalization calculations.\n" |
| 187 |
"\t\t 16 = Put exodus library into verbose mode.\n" |
| 188 |
"\t\t 32 = Verbose decomposition information.\n" |
| 189 |
"\t\t 64 = Verbose output database summary information.\n" |
| 190 |
"\t\t 128 = Verbose sideset generation information.", |
| 191 |
"0"); |
| 192 |
|
| 193 |
options_.enroll("copyright", Ioss::GetLongOption::OptType::NoValue, |
| 194 |
"Show copyright and license data.", nullptr); |
| 195 |
} |
| 196 |
|
| 197 |
bool SystemInterface::parse_options(int argc, char **argv) |
| 198 |
{ |
| 199 |
int option_index = options_.parse(argc, argv); |
| 200 |
if (option_index < 1) { |
| 201 |
return false; |
| 202 |
} |
| 203 |
|
| 204 |
if (options_.retrieve("help") != nullptr) { |
| 205 |
if (myRank_ == 0) { |
| 206 |
options_.usage(); |
| 207 |
fmt::print("\n\tCan also set options via ZELLIJ_OPTIONS environment variable.\n" |
| 208 |
"\n\tDocumentation: " |
| 209 |
"https://sandialabs.github.io/seacas-docs/sphinx/html/index.html#zellij\n" |
| 210 |
"\n\t->->-> Send email to [email protected] for zellij support.<-<-<-\n"); |
| 211 |
} |
| 212 |
exit(EXIT_SUCCESS); |
| 213 |
} |
| 214 |
|
| 215 |
if (options_.retrieve("version") != nullptr) { |
| 216 |
// Version is printed up front, just exit... |
| 217 |
exit(0); |
| 218 |
} |
| 219 |
|
| 220 |
if (options_.retrieve("rcb") != nullptr) { |
| 221 |
decompMethod_ = "RCB"; |
| 222 |
} |
| 223 |
|
| 224 |
if (options_.retrieve("rib") != nullptr) { |
| 225 |
decompMethod_ = "RIB"; |
| 226 |
} |
| 227 |
|
| 228 |
if (options_.retrieve("hsfc") != nullptr) { |
| 229 |
decompMethod_ = "HSFC"; |
| 230 |
} |
| 231 |
|
| 232 |
if (options_.retrieve("linear") != nullptr) { |
| 233 |
decompMethod_ = "LINEAR"; |
| 234 |
} |
| 235 |
|
| 236 |
if (options_.retrieve("cyclic") != nullptr) { |
| 237 |
decompMethod_ = "CYCLIC"; |
| 238 |
} |
| 239 |
|
| 240 |
if (options_.retrieve("random") != nullptr) { |
| 241 |
decompMethod_ = "RANDOM"; |
| 242 |
} |
| 243 |
|
| 244 |
subcycle_ = (options_.retrieve("subcycle") != nullptr); |
| 245 |
|
| 246 |
equivalenceNodes_ = options_.retrieve("separate_cells") == nullptr; |
| 247 |
|
| 248 |
{ |
| 249 |
const char *temp = options_.retrieve("minimize_open_files"); |
| 250 |
if (temp != nullptr) { |
| 251 |
auto mode = Ioss::Utils::lowercase(temp); |
| 252 |
if (mode == "all") { |
| 253 |
minimizeOpenFiles_ = Minimize::ALL; |
| 254 |
} |
| 255 |
else if (mode == "unit") { |
| 256 |
minimizeOpenFiles_ = Minimize::UNIT; |
| 257 |
} |
| 258 |
else if (mode == "output") { |
| 259 |
minimizeOpenFiles_ = Minimize::OUTPUT; |
| 260 |
} |
| 261 |
else if (mode == "none") { |
| 262 |
minimizeOpenFiles_ = Minimize::NONE; |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
{ |
| 268 |
const char *temp = options_.retrieve("offset"); |
| 269 |
if (temp != nullptr) { |
| 270 |
parse_offset(temp, offset_, myRank_); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
scaleFactor_ = options_.get_option_value("scale", scaleFactor_); |
| 275 |
ranks_ = options_.get_option_value("ranks", ranks_); |
| 276 |
startRank_ = options_.get_option_value("start_rank", startRank_); |
| 277 |
rankCount_ = options_.get_option_value("rank_count", rankCount_); |
| 278 |
debugLevel_ = options_.get_option_value("debug", debugLevel_); |
| 279 |
|
| 280 |
if (options_.retrieve("copyright") != nullptr) { |
| 281 |
if (myRank_ == 0) { |
| 282 |
fmt::print("{}", copyright("2021")); |
| 283 |
} |
| 284 |
exit(EXIT_SUCCESS); |
| 285 |
} |
| 286 |
|
| 287 |
// Get options from environment variable also... |
| 288 |
char *options = getenv("ZELLIJ_OPTIONS"); |
| 289 |
if (options != nullptr) { |
| 290 |
if (myRank_ == 0) { |
| 291 |
fmt::print( |
| 292 |
"\nThe following options were specified via the ZELLIJ_OPTIONS environment variable:\n" |
| 293 |
"\t{}\n\n", |
| 294 |
options); |
| 295 |
} |
| 296 |
options_.parse(options, Ioss::GetLongOption::basename(*argv)); |
| 297 |
} |
| 298 |
|
| 299 |
outputName_ = options_.get_option_value("output", outputName_); |
| 300 |
lattice_ = options_.get_option_value("lattice", lattice_); |
| 301 |
|
| 302 |
ignoreInternalSidesets_ = options_.retrieve("ignore_sidesets") != nullptr; |
| 303 |
|
| 304 |
sidesetSurfaces_ = options_.get_option_value("generate_sidesets", sidesetSurfaces_); |
| 305 |
sidesetNames_ = options_.get_option_value("sideset_names", sidesetNames_); |
| 306 |
|
| 307 |
// Default to 64... |
| 308 |
ints32bit_ = options_.retrieve("32-bit") != nullptr; |
| 309 |
if (options_.retrieve("64-bit") != nullptr) { |
| 310 |
ints32bit_ = false; |
| 311 |
} |
| 312 |
|
| 313 |
if (options_.retrieve("netcdf3") != nullptr) { |
| 314 |
ints32bit_ = true; |
| 315 |
useNetcdf4_ = false; |
| 316 |
useNetcdf5_ = false; |
| 317 |
} |
| 318 |
|
| 319 |
if (options_.retrieve("netcdf4") != nullptr) { |
| 320 |
useNetcdf4_ = true; |
| 321 |
useNetcdf5_ = false; |
| 322 |
} |
| 323 |
|
| 324 |
if (options_.retrieve("netcdf5") != nullptr) { |
| 325 |
useNetcdf4_ = false; |
| 326 |
useNetcdf5_ = true; |
| 327 |
} |
| 328 |
|
| 329 |
if (options_.retrieve("szip") != nullptr) { |
| 330 |
szip_ = true; |
| 331 |
zlib_ = false; |
| 332 |
} |
| 333 |
zlib_ = (options_.retrieve("zlib") != nullptr); |
| 334 |
|
| 335 |
if (szip_ && zlib_) { |
| 336 |
if (myRank_ == 0) { |
| 337 |
fmt::print(stderr, fmt::fg(fmt::color::red), |
| 338 |
"\nERROR: Only one of 'szip' or 'zlib' can be specified.\n"); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
compressionLevel_ = options_.get_option_value("compress", compressionLevel_); |
| 343 |
|
| 344 |
skip_ = options_.get_option_value("skip", skip_); |
| 345 |
repeat_ = options_.get_option_value("repeat", repeat_); |
| 346 |
|
| 347 |
// Adjust start_rank and rank_count if running in parallel... |
| 348 |
Ioss::ParallelUtils pu{}; |
| 349 |
if (pu.parallel_size() > 1) { |
| 350 |
if (subcycle_) { |
| 351 |
if (myRank_ == 0) { |
| 352 |
fmt::print(stderr, fmt::fg(fmt::color::yellow), |
| 353 |
"\nWARNING: The `subcycle` option is ignored if running in parallel.\n"); |
| 354 |
} |
| 355 |
subcycle_ = false; |
| 356 |
} |
| 357 |
auto size = pu.parallel_size(); |
| 358 |
auto ranks_per_mpi = ranks_ / size; |
| 359 |
auto extra = ranks_ % size; |
| 360 |
|
| 361 |
auto my_rank = pu.parallel_rank(); |
| 362 |
if (my_rank < extra) { |
| 363 |
startRank_ = (ranks_per_mpi + 1) * my_rank; |
| 364 |
} |
| 365 |
else { |
| 366 |
startRank_ = (ranks_per_mpi + 1) * extra + ranks_per_mpi * (my_rank - extra); |
| 367 |
} |
| 368 |
rankCount_ = ranks_per_mpi + (my_rank < extra ? 1 : 0); |
| 369 |
} |
| 370 |
if ((rankCount_ == 0) || (startRank_ + rankCount_ > ranks_)) { |
| 371 |
rankCount_ = ranks_ - startRank_; |
| 372 |
} |
| 373 |
|
| 374 |
if (lattice().empty()) { |
| 375 |
if (myRank_ == 0) { |
| 376 |
fmt::print(stderr, fmt::fg(fmt::color::red), |
| 377 |
"\nERROR: Missing specification of lattice file.\n"); |
| 378 |
options_.usage(); |
| 379 |
} |
| 380 |
return false; |
| 381 |
} |
| 382 |
return true; |
| 383 |
} |
| 384 |
|
| 385 |
void SystemInterface::show_version() |
| 386 |
{ |
| 387 |
fmt::print(fmt::fg(fmt::color::cyan), |
| 388 |
"Zellij\n" |
| 389 |
"\t(A code for tiling 1 or more template databases into a single output database.)\n" |
| 390 |
"\t(Version: {}) Modified: {}\n", |
| 391 |
qainfo[2], qainfo[1]); |
| 392 |
} |
| 393 |
|
| 394 |
namespace { |
| 395 |
void parse_offset(const char *tokens, vector3d &offset, int myRank) |
| 396 |
{ |
| 397 |
// Break into tokens separated by "," |
| 398 |
if (tokens != nullptr) { |
| 399 |
std::string token_string(tokens); |
| 400 |
auto var_list = Ioss::tokenize(token_string, ","); |
| 401 |
|
| 402 |
// At this point, var_list should contain 1,2,or 3 strings |
| 403 |
// corresponding to the x, y, and z coordinate offsets. |
| 404 |
offset[0] = offset[1] = offset[2] = 0.0; |
| 405 |
|
| 406 |
std::string offx = var_list[0]; |
| 407 |
double x = std::stod(offx); |
| 408 |
offset[0] = x; |
| 409 |
|
| 410 |
if (var_list.size() >= 2) { |
| 411 |
std::string offy = var_list[1]; |
| 412 |
double y = std::stod(offy); |
| 413 |
offset[1] = y; |
| 414 |
} |
| 415 |
|
| 416 |
if (var_list.size() == 3) { |
| 417 |
std::string offz = var_list[2]; |
| 418 |
double z = std::stod(offz); |
| 419 |
offset[2] = z; |
| 420 |
} |
| 421 |
|
| 422 |
if (var_list.size() > 3) { |
| 423 |
if (myRank == 0) { |
| 424 |
fmt::print(stderr, fmt::fg(fmt::color::red), |
| 425 |
"ERROR: Incorrect number of offset components ({}) specified -- max is 3; " |
| 426 |
"ignoring extras.\n\n", |
| 427 |
var_list.size()); |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
} |
| 432 |
} // namespace |