Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tpetra: Throw in Map ctor when unrecognized TPETRA_-prefixed environment variables are set #12944

Merged
merged 3 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 47 additions & 43 deletions packages/tpetra/core/src/Tpetra_Details_Behavior.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
// clang-format on
#include <algorithm> // std::transform
#include <array>
#include <cctype> // std::toupper
#include <cstdlib> // std::getenv
#include <cctype> // std::toupper
#include <cstdlib> // std::getenv
#include <functional>
#include <map>
#include <stdexcept>
Expand Down Expand Up @@ -129,8 +129,7 @@ constexpr const std::string_view HIERARCHICAL_UNPACK =
"TPETRA_HIERARCHICAL_UNPACK";
constexpr const std::string_view SKIP_COPY_AND_PERMUTE =
"TPETRA_SKIP_COPY_AND_PERMUTE";
constexpr const std::string_view FUSED_RESIDUAL =
"TPETRA_FUSED_RESIDUAL";
constexpr const std::string_view FUSED_RESIDUAL = "TPETRA_FUSED_RESIDUAL";
constexpr const std::string_view OVERLAP = "TPETRA_OVERLAP";
constexpr const std::string_view SPACES_ID_WARN_LIMIT =
"TPETRA_SPACES_ID_WARN_LIMIT";
Expand Down Expand Up @@ -158,9 +157,8 @@ constexpr const auto RECOGNIZED_VARS = make_array(
MULTIVECTOR_USE_MERGE_PATH, VECTOR_DEVICE_THRESHOLD,
HIERARCHICAL_UNPACK_BATCH_SIZE, HIERARCHICAL_UNPACK_TEAM_SIZE,
USE_TEUCHOS_TIMERS, USE_KOKKOS_PROFILING, DEBUG, VERBOSE, TIMING,
HIERARCHICAL_UNPACK, SKIP_COPY_AND_PERMUTE, FUSED_RESIDUAL,
OVERLAP, SPACES_ID_WARN_LIMIT,
TIME_KOKKOS_DEEP_COPY, TIME_KOKKOS_DEEP_COPY_VERBOSE1,
HIERARCHICAL_UNPACK, SKIP_COPY_AND_PERMUTE, FUSED_RESIDUAL, OVERLAP,
SPACES_ID_WARN_LIMIT, TIME_KOKKOS_DEEP_COPY, TIME_KOKKOS_DEEP_COPY_VERBOSE1,
TIME_KOKKOS_DEEP_COPY_VERBOSE2, TIME_KOKKOS_FENCE, TIME_KOKKOS_FUNCTIONS);

// clang-format off
Expand Down Expand Up @@ -423,44 +421,51 @@ T idempotentlyGetEnvironmentVariable(
// clang-format on

void Behavior::reject_unrecognized_env_vars() {
const char prefix[] = "Tpetra::Details::Behavior: ";
char **env;

static bool once = false;

if (!once) {
const char prefix[] = "Tpetra::Details::Behavior: ";
char **env;
#if defined(WIN) && (_MSC_VER >= 1900)
env = *__p__environ();
env = *__p__environ();
#else
env = environ; // defined at the top of this file as extern char **environ;
env = environ; // defined at the top of this file as extern char **environ;
#endif
for (; *env; ++env) {

std::string name;
std::string value;
const std::string_view ev(*env);

// split name=value on the first =, everything before = is name
split(
ev,
[&](const std::string &s) {
if (name.empty()) {
name = s;
} else {
value = s;
}
},
'=');

if (name.size() >= BehaviorDetails::RESERVED_PREFIX.size() &&
name.substr(0, BehaviorDetails::RESERVED_PREFIX.size()) ==
BehaviorDetails::RESERVED_PREFIX) {
const auto it = std::find(BehaviorDetails::RECOGNIZED_VARS.begin(),
BehaviorDetails::RECOGNIZED_VARS.end(), name);
TEUCHOS_TEST_FOR_EXCEPTION(
it == BehaviorDetails::RECOGNIZED_VARS.end(), std::out_of_range,
prefix << "Environment "
"variable \""
<< name << "\" (prefixed with \""
<< BehaviorDetails::RESERVED_PREFIX
<< "\") is not a recognized Tpetra variable.");
for (; *env; ++env) {

std::string name;
std::string value;
const std::string_view ev(*env);

// split name=value on the first =, everything before = is name
split(
ev,
[&](const std::string &s) {
if (name.empty()) {
name = s;
} else {
value = s;
}
},
'=');

if (name.size() >= BehaviorDetails::RESERVED_PREFIX.size() &&
name.substr(0, BehaviorDetails::RESERVED_PREFIX.size()) ==
BehaviorDetails::RESERVED_PREFIX) {
const auto it = std::find(BehaviorDetails::RECOGNIZED_VARS.begin(),
BehaviorDetails::RECOGNIZED_VARS.end(), name);
TEUCHOS_TEST_FOR_EXCEPTION(
it == BehaviorDetails::RECOGNIZED_VARS.end(), std::out_of_range,
prefix << "Environment "
"variable \""
<< name << "\" (prefixed with \""
<< BehaviorDetails::RESERVED_PREFIX
<< "\") is not a recognized Tpetra variable.");
}
}

once = true;
}
}

Expand Down Expand Up @@ -681,8 +686,7 @@ bool Behavior::fusedResidual() {
static bool value_ = defaultValue;
static bool initialized_ = false;
return idempotentlyGetEnvironmentVariable(
value_, initialized_, BehaviorDetails::FUSED_RESIDUAL,
defaultValue);
value_, initialized_, BehaviorDetails::FUSED_RESIDUAL, defaultValue);
}

bool Behavior::overlapCommunicationAndComputation() {
Expand Down
6 changes: 6 additions & 0 deletions packages/tpetra/core/src/Tpetra_Map_def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ namespace Tpetra {
directory_ (new Directory<LocalOrdinal, GlobalOrdinal, Node> ())
{
Tpetra::Details::initializeKokkos ();
Tpetra::Details::Behavior::reject_unrecognized_env_vars();
}


Expand Down Expand Up @@ -222,6 +223,7 @@ namespace Tpetra {
std::cerr << os.str();
}
Tpetra::Details::initializeKokkos ();
Tpetra::Details::Behavior::reject_unrecognized_env_vars();

// In debug mode only, check whether numGlobalElements and
// indexBase are the same over all processes in the communicator.
Expand Down Expand Up @@ -400,6 +402,7 @@ namespace Tpetra {
std::cerr << os.str();
}
Tpetra::Details::initializeKokkos ();
Tpetra::Details::Behavior::reject_unrecognized_env_vars();

// Global sum of numLocalElements over all processes.
// Keep this for later debug checks.
Expand Down Expand Up @@ -896,6 +899,7 @@ namespace Tpetra {
std::cerr << os.str();
}
Tpetra::Details::initializeKokkos ();
Tpetra::Details::Behavior::reject_unrecognized_env_vars();
Tpetra::Details::ProfilingRegion pr(funcName);
checkMapInputArray ("(GST, const GO[], LO, GO, comm)",
indexList, static_cast<size_t> (indexListSize),
Expand Down Expand Up @@ -940,6 +944,7 @@ namespace Tpetra {
std::cerr << os.str();
}
Tpetra::Details::initializeKokkos ();
Tpetra::Details::Behavior::reject_unrecognized_env_vars();
Tpetra::Details::ProfilingRegion pr(funcName);
const size_t numLclInds = static_cast<size_t> (entryList.size ());
checkMapInputArray ("(GST, ArrayView, GO, comm)",
Expand Down Expand Up @@ -1008,6 +1013,7 @@ namespace Tpetra {
std::cerr << os.str();
}
Tpetra::Details::initializeKokkos ();
Tpetra::Details::Behavior::reject_unrecognized_env_vars();
Tpetra::Details::ProfilingRegion pr(funcName);
checkMapInputArray ("(GST, Kokkos::View, GO, comm)",
entryList.data (),
Expand Down
Loading