| 1 |
// Copyright(C) 1999-2021, 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 |
|
| 7 |
#include "Ioss_IOFactory.h" |
| 8 |
#include "Ioss_ParallelUtils.h" |
| 9 |
#include "Ioss_Utils.h" // for IOSS_ERROR |
| 10 |
#include "Ioss_Version.h" |
| 11 |
#include <fmt/format.h> |
| 12 |
#include <fmt/ostream.h> |
| 13 |
#include <fmt/ranges.h> |
| 14 |
#include <map> // for _Rb_tree_iterator, etc |
| 15 |
#include <ostream> // for basic_ostream, etc |
| 16 |
#include <set> |
| 17 |
#include <string> // for char_traits, string, etc |
| 18 |
|
| 19 |
#include "Ioss_CodeTypes.h" |
| 20 |
#include "Ioss_DBUsage.h" // for DatabaseUsage |
| 21 |
#include "Ioss_PropertyManager.h" |
| 22 |
#if defined(SEACAS_HAVE_MPI) |
| 23 |
#include "Ioss_Decomposition.h" |
| 24 |
#endif |
| 25 |
|
| 26 |
namespace { |
| 27 |
#if defined(IOSS_THREADSAFE) |
| 28 |
std::mutex m_; |
| 29 |
#endif |
| 30 |
|
| 31 |
int describe_nl(Ioss::IOFactoryMap *registry, Ioss::NameList *names) |
| 32 |
{ |
| 33 |
int count = 0; |
| 34 |
Ioss::IOFactoryMap::const_iterator I; |
| 35 |
for (I = registry->begin(); I != registry->end(); ++I) { |
| 36 |
names->push_back((*I).first); |
| 37 |
++count; |
| 38 |
} |
| 39 |
return count; |
| 40 |
} |
| 41 |
} // namespace |
| 42 |
|
| 43 |
namespace Ioss { |
| 44 |
class DatabaseIO; |
| 45 |
|
| 46 |
using IOFactoryValuePair = IOFactoryMap::value_type; |
| 47 |
} // namespace Ioss |
| 48 |
|
| 49 |
/** \brief Create an IO database. |
| 50 |
* |
| 51 |
* This is the public interface method for creating an Ioss::DatabaseIO object. |
| 52 |
* If Ioss is invoked with more than one process, type == "exodus", db_usage == Ioss::READ_MODEL, |
| 53 |
* and properties contains the property DECOMPOSITION_METHOD, which is not set to EXTERNAL, |
| 54 |
* then a parallel decompose-on-the-fly (dof) style Exodus database will be created. The mesh in a |
| 55 |
* single |
| 56 |
* Exodus file will be decomposed according to DECOMPOSITION_METHOD. Otherwise, if the number of |
| 57 |
* processes is greater than one, then a file-per-process (fpp) style Exodus database will be |
| 58 |
* created. |
| 59 |
* In this case, Ioss expects the mesh in p Exodus files, where p is the number of processes in |
| 60 |
* communicator. |
| 61 |
* |
| 62 |
* \param[in] type The database file format. Use Ioss::IOFactory::describe to list formats known to |
| 63 |
* Ioss. |
| 64 |
* \param[in] filename The name of the database file to read from or write to. |
| 65 |
* \param[in] db_usage Specifies whether the database will be used for input, normal output, |
| 66 |
* restart output, etc. |
| 67 |
* \param[in] communicator The MPI communicator. |
| 68 |
* \param[in] properties The property manager associated with the database. |
| 69 |
* \returns A pointer to the newly-constructed Ioss::DatabaseIO object, or NULL if unsuccessful. |
| 70 |
*/ |
| 71 |
Ioss::DatabaseIO *Ioss::IOFactory::create(const std::string &type, const std::string &filename, |
| 72 |
Ioss::DatabaseUsage db_usage, Ioss_MPI_Comm communicator, |
| 73 |
const Ioss::PropertyManager &properties) |
| 74 |
{ |
| 75 |
IOSS_FUNC_ENTER(m_); |
| 76 |
Ioss::DatabaseIO *db = nullptr; |
| 77 |
auto iter = registry()->find(type); |
| 78 |
if (iter == registry()->end()) { |
| 79 |
if (registry()->empty()) { |
| 80 |
std::ostringstream errmsg; |
| 81 |
fmt::print(errmsg, "ERROR: No database types have been registered.\n" |
| 82 |
" Was Ioss::Init::Initializer() called?\n\n"); |
| 83 |
IOSS_ERROR(errmsg); |
| 84 |
} |
| 85 |
else { |
| 86 |
std::ostringstream errmsg; |
| 87 |
fmt::print(errmsg, "ERROR: The database type '{}' is not supported.\n", type); |
| 88 |
Ioss::NameList db_types; |
| 89 |
describe_nl(registry(), &db_types); |
| 90 |
fmt::print(errmsg, "\nSupported database types:\n\t{}\n\n", |
| 91 |
fmt::join(db_types.begin(), db_types.end(), " ")); |
| 92 |
IOSS_ERROR(errmsg); |
| 93 |
} |
| 94 |
} |
| 95 |
else { |
| 96 |
auto my_props(properties); |
| 97 |
my_props.add(Property("database_type", type)); |
| 98 |
|
| 99 |
Ioss::ParallelUtils pu(communicator); |
| 100 |
pu.add_environment_properties(my_props); |
| 101 |
if (my_props.exists("SHOW_CONFIG")) { |
| 102 |
static bool output = false; |
| 103 |
if (!output && pu.parallel_rank() == 0) { |
| 104 |
output = true; |
| 105 |
std::string config = show_configuration(); |
| 106 |
Ioss::OUTPUT() << config; |
| 107 |
} |
| 108 |
} |
| 109 |
Ioss::IOFactory *factory = (*iter).second; |
| 110 |
db = factory->make_IO(filename, db_usage, communicator, my_props); |
| 111 |
} |
| 112 |
return db; |
| 113 |
} |
| 114 |
|
| 115 |
/** \brief Get the names of database formats known to IOSS. |
| 116 |
* |
| 117 |
* \param[out] names The list of known database format names. |
| 118 |
* \returns The number of known database formats. |
| 119 |
*/ |
| 120 |
int Ioss::IOFactory::describe(NameList *names) |
| 121 |
{ |
| 122 |
IOSS_FUNC_ENTER(m_); |
| 123 |
return describe_nl(registry(), names); |
| 124 |
} |
| 125 |
|
| 126 |
/** \brief Get the names of database formats known to IOSS. |
| 127 |
* |
| 128 |
* \returns The list of known database format names. |
| 129 |
*/ |
| 130 |
Ioss::NameList Ioss::IOFactory::describe() |
| 131 |
{ |
| 132 |
Ioss::NameList names; |
| 133 |
describe(&names); |
| 134 |
return names; |
| 135 |
} |
| 136 |
|
| 137 |
std::string Ioss::IOFactory::show_configuration() |
| 138 |
{ |
| 139 |
std::stringstream config; |
| 140 |
fmt::print(config, "IOSS Library Version '{}'\n\n", Ioss::Version()); |
| 141 |
NameList db_types = describe(); |
| 142 |
fmt::print(config, "Supported database types:\n\t{}\n", fmt::join(db_types, ", ")); |
| 143 |
|
| 144 |
#if defined(SEACAS_HAVE_MPI) |
| 145 |
fmt::print(config, "\nSupported decomposition methods:\n\t{}\n", |
| 146 |
fmt::join(Ioss::valid_decomp_methods(), ", ")); |
| 147 |
{ |
| 148 |
char version[MPI_MAX_LIBRARY_VERSION_STRING]; |
| 149 |
int length = 0; |
| 150 |
MPI_Get_library_version(version, &length); |
| 151 |
fmt::print(config, "MPI Version: {}\n", version); |
| 152 |
} |
| 153 |
#endif |
| 154 |
|
| 155 |
fmt::print(config, "\nThird-Party Library Configuration Information:\n\n"); |
| 156 |
|
| 157 |
// Each database type may appear multiple times in the registry |
| 158 |
// due to aliasing (i.e. exodus, genesis, exodusII, ...) |
| 159 |
// Iterate registry and get only print config for a single |
| 160 |
// instance... |
| 161 |
std::set<IOFactory *> unique_facs; |
| 162 |
|
| 163 |
for (const auto &db : *registry()) { |
| 164 |
auto result = unique_facs.insert(db.second); |
| 165 |
if (result.second) { |
| 166 |
config << db.second->show_config(); |
| 167 |
} |
| 168 |
} |
| 169 |
return config.str(); |
| 170 |
} |
| 171 |
|
| 172 |
Ioss::IOFactory::IOFactory(const std::string &type) |
| 173 |
{ |
| 174 |
registry()->insert(IOFactoryValuePair(type, this)); |
| 175 |
} |
| 176 |
|
| 177 |
void Ioss::IOFactory::alias(const std::string &base, const std::string &syn) |
| 178 |
{ |
| 179 |
Ioss::IOFactory *factory = (*registry()->find(base)).second; |
| 180 |
registry()->insert(IOFactoryValuePair(syn, factory)); |
| 181 |
} |
| 182 |
|
| 183 |
Ioss::IOFactoryMap *Ioss::IOFactory::registry() |
| 184 |
{ |
| 185 |
static IOFactoryMap registry_; |
| 186 |
return ®istry_; |
| 187 |
} |
| 188 |
|
| 189 |
/** \brief Empty method. |
| 190 |
*/ |
| 191 |
void Ioss::IOFactory::clean() {} |