42#include "TpetraCore_config.h"
50#include "Teuchos_TestForException.hpp"
51#include "Teuchos_OrdinalTraits.hpp"
57namespace BehaviorDetails {
58std::map<std::string, std::map<std::string, bool> > namedVariableMap_;
59bool verboseDisabled_ =
false;
60bool timingDisabled_ =
false;
65 enum EnvironmentVariableState
67 EnvironmentVariableIsSet_ON,
68 EnvironmentVariableIsSet_OFF,
69 EnvironmentVariableIsSet,
70 EnvironmentVariableIsNotSet
76 std::string stringToUpper (std::string s)
78 std::transform (s.begin (), s.end (), s.begin (),
79 [] (
unsigned char c) { return std::toupper (c); });
84 split(
const std::string& s,
85 std::function<
void(
const std::string&)> f,
88 typedef std::string::size_type size_type;
89 size_type cur_pos, last_pos=0, length=s.length();
90 while(last_pos < length + 1)
92 cur_pos = s.find_first_of(sep, last_pos);
93 if(cur_pos == std::string::npos)
97 if(cur_pos!=last_pos) {
98 auto token = std::string(s.data()+last_pos, (size_type)cur_pos-last_pos);
101 last_pos = cur_pos + 1;
106 EnvironmentVariableState
107 environmentVariableState(
const std::string& environmentVariableValue)
109 std::string v = stringToUpper(environmentVariableValue);
110 if (v ==
"1" || v ==
"YES" || v ==
"TRUE" || v ==
"ON")
112 return EnvironmentVariableIsSet_ON;
113 else if (v ==
"0" || v ==
"NO" || v ==
"FALSE" || v ==
"OFF")
115 return EnvironmentVariableIsSet_OFF;
117 return EnvironmentVariableIsSet;
121 setEnvironmentVariableMap (
const char environmentVariableName[],
122 std::map<std::string,std::map<std::string, bool> >& valsMap,
123 const bool defaultValue)
131 valsMap[environmentVariableName] = map<string,bool>{{
"DEFAULT", defaultValue}};
133 const char* varVal = getenv (environmentVariableName);
134 if (varVal ==
nullptr) {
141 const string varStr(varVal);
142 vector<string> names;
143 split(varStr, [&](
const string& x){names.push_back(x);});
144 for (
auto const& name: names) {
145 auto state = environmentVariableState(name);
146 if (state == EnvironmentVariableIsSet_ON) {
149 valsMap[environmentVariableName][
"DEFAULT"] =
true;
151 else if (state == EnvironmentVariableIsSet_OFF) {
154 valsMap[environmentVariableName][
"DEFAULT"] =
false;
159 valsMap[environmentVariableName][name] =
true;
166 getEnvironmentVariableAsBool (
const char environmentVariableName[],
167 const bool defaultValue)
169 const char* varVal = std::getenv (environmentVariableName);
171 bool retVal = defaultValue;
172 if (varVal !=
nullptr) {
173 auto state = environmentVariableState(std::string(varVal));
174 if (state == EnvironmentVariableIsSet_ON) retVal =
true;
175 else if (state == EnvironmentVariableIsSet_OFF) retVal =
false;
181 getEnvironmentVariableAsSize(
const char environmentVariableName[],
182 const size_t defaultValue)
184 const char prefix[] =
"Tpetra::Details::Behavior: ";
186 const char* varVal = std::getenv(environmentVariableName);
187 if (varVal ==
nullptr) {
193 long long val = std::stoll(stringToUpper(varVal));
194 if (val <
static_cast<long long>(0)) {
196 return std::numeric_limits<size_t>::max();
203 if (
sizeof(
long long) >
sizeof(
size_t)) {
207 constexpr long long maxSizeT =
208 static_cast<long long>(std::numeric_limits<size_t>::max());
209 TEUCHOS_TEST_FOR_EXCEPTION
210 (val > maxSizeT, std::out_of_range, prefix <<
"Environment "
211 "variable \"" << environmentVariableName <<
"\" has a "
212 "value " << val <<
" larger than the largest size_t value "
215 return static_cast<size_t>(val);
220 idempotentlyGetEnvironmentVariableAsBool (
bool& value,
222 const char environmentVariableName[],
223 const bool defaultValue)
226 value = getEnvironmentVariableAsBool (environmentVariableName,
234 idempotentlyGetNamedEnvironmentVariableAsBool (
const char name[],
236 const char environmentVariableName[],
237 const bool defaultValue)
239 using BehaviorDetails::namedVariableMap_;
241 setEnvironmentVariableMap (environmentVariableName,
246 auto thisEnvironmentVariableMap = namedVariableMap_[environmentVariableName];
247 auto thisEnvironmentVariable = thisEnvironmentVariableMap.find(name);
248 if (thisEnvironmentVariable != thisEnvironmentVariableMap.end())
249 return thisEnvironmentVariable->second;
250 return thisEnvironmentVariableMap[
"DEFAULT"];
254 idempotentlyGetEnvironmentVariableAsSize
257 const char environmentVariableName[],
258 const size_t defaultValue)
261 value = getEnvironmentVariableAsSize(environmentVariableName,
268 constexpr bool debugDefault () {
269#ifdef HAVE_TPETRA_DEBUG
276 constexpr bool verboseDefault () {
280 constexpr bool timingDefault () {
284 constexpr bool assumeMpiIsGPUAwareDefault () {
285#ifdef TPETRA_ASSUME_GPU_AWARE_MPI
292 constexpr bool cudaLaunchBlockingDefault () {
296 constexpr bool hierarchicalUnpackDefault () {
308 static bool initialized_ =
false;
317 if (BehaviorDetails::verboseDisabled_)
return false;
319 constexpr char envVarName[] =
"TPETRA_VERBOSE";
323 static bool initialized_ =
false;
332 if (BehaviorDetails::timingDisabled_)
return false;
334 constexpr char envVarName[] =
"TPETRA_TIMING";
338 static bool initialized_ =
false;
347 constexpr char envVarName[] =
"TPETRA_ASSUME_GPU_AWARE_MPI";
351 static bool initialized_ =
false;
360 constexpr char envVarName[] =
"CUDA_LAUNCH_BLOCKING";
364 static bool initialized_ =
false;
376 const char*
varVal = std::getenv (
"MM_TAFC_OptimizationCoreCount");
387 constexpr char envVarName[] =
"TPETRA_VERBOSE_PRINT_COUNT_THRESHOLD";
391 static bool initialized_ =
false;
398 constexpr char envVarName[] =
"TPETRA_ROW_IMBALANCE_THRESHOLD";
402 static bool initialized_ =
false;
409 constexpr char envVarName[] =
"TPETRA_MULTIVECTOR_USE_MERGE_PATH";
413 static bool initialized_ =
false;
420 constexpr char envVarName[] =
"TPETRA_VECTOR_DEVICE_THRESHOLD";
424 static bool initialized_ =
false;
431 constexpr char envVarName[] =
"TPETRA_HIERARCHICAL_UNPACK_BATCH_SIZE";
433#ifdef HAVE_TPETRA_INST_CUDA
440 static bool initialized_ =
false;
447 constexpr char envVarName[] =
"TPETRA_HIERARCHICAL_UNPACK_TEAM_SIZE";
448#ifdef HAVE_TPETRA_INST_CUDA
451 const size_t defaultValue (Teuchos::OrdinalTraits<size_t>::invalid ());
455 static bool initialized_ =
false;
462 constexpr char envVarName[] =
"TPETRA_USE_TEUCHOS_TIMERS";
466 static bool initialized_ =
false;
473 constexpr char envVarName[] =
"TPETRA_USE_KOKKOS_PROFILING";
477 static bool initialized_ =
false;
488 static bool initialized_ =
false;
497 if (BehaviorDetails::verboseDisabled_)
return false;
499 constexpr char envVarName[] =
"TPETRA_VERBOSE";
502 static bool initialized_ =
false;
510 BehaviorDetails::verboseDisabled_ =
false;
514 BehaviorDetails::verboseDisabled_ =
true;
519 if (BehaviorDetails::timingDisabled_)
return false;
521 constexpr char envVarName[] =
"TPETRA_TIMING";
524 static bool initialized_ =
false;
532 BehaviorDetails::timingDisabled_ =
false;
536 BehaviorDetails::timingDisabled_ =
true;
541 constexpr char envVarName[] =
"TPETRA_HIERARCHICAL_UNPACK";
545 static bool initialized_ =
false;
554 constexpr char envVarName[] =
"TPETRA_SKIP_COPY_AND_PERMUTE";
558 static bool initialized_ =
false;
565 constexpr char envVarName[] =
"TPETRA_OVERLAP";
569 static bool initialized_ =
false;
576 constexpr char envVarName[] =
"TPETRA_SPACES_ID_WARN_LIMT";
580 static bool initialized_ =
false;
587 constexpr char envVarName[] =
"TPETRA_TIME_KOKKOS_DEEP_COPY";
591 static bool initialized_ =
false;
Declaration of Tpetra::Details::Behavior, a class that describes Tpetra's behavior.
Struct that holds views of the contents of a CrsMatrix.
static bool timing()
Whether Tpetra is in timing mode.
static void enable_verbose_behavior()
Enable verbose mode, programatically.
static void disable_timing()
Disable timing, programatically.
static bool cudaLaunchBlocking()
Whether the CUDA_LAUNCH_BLOCKING environment variable has been set.
static bool timeKokkosDeepCopy()
Add Teuchos timers for all host calls to Kokkos::deep_copy(). This is especially useful for identifyi...
static bool hierarchicalUnpack()
Unpack rows of a matrix using hierarchical unpacking.
static size_t spacesIdWarnLimit()
Warn if more than this many Kokkos spaces are accessed.
static bool assumeMpiIsGPUAware()
Whether to assume that MPI is CUDA aware.
static bool debug()
Whether Tpetra is in debug mode.
static int TAFC_OptimizationCoreCount()
MPI process count above which Tpetra::CrsMatrix::transferAndFillComplete will attempt to do advanced ...
static bool overlapCommunicationAndComputation()
Overlap communication and computation.
static void enable_timing()
Enable timing, programatically.
static bool profilingRegionUseTeuchosTimers()
Use Teuchos::Timer in Tpetra::ProfilingRegion.
static bool profilingRegionUseKokkosProfiling()
Use Kokkos::Profiling in Tpetra::ProfilingRegion.
static bool verbose()
Whether Tpetra is in verbose mode.
static bool useMergePathMultiVector()
Whether to use the cuSPARSE merge path algorithm to perform sparse matrix-multivector products,...
static size_t multivectorKernelLocationThreshold()
the threshold for transitioning from device to host
static size_t verbosePrintCountThreshold()
Number of entries below which arrays, lists, etc. will be printed in debug mode.
static size_t hierarchicalUnpackBatchSize()
Size of batch for hierarchical unpacking.
static void disable_verbose_behavior()
Disable verbose mode, programatically.
static size_t rowImbalanceThreshold()
Threshold for deciding if a local matrix is "imbalanced" in the number of entries per row....
static bool skipCopyAndPermuteIfPossible()
Skip copyAndPermute if possible.
static size_t hierarchicalUnpackTeamSize()
Size of team for hierarchical unpacking.
Implementation details of Tpetra.
Namespace Tpetra contains the class and methods constituting the Tpetra library.