42 #include "TpetraCore_config.h"
50 #include "Teuchos_TestForException.hpp"
51 #include "Teuchos_OrdinalTraits.hpp"
57 namespace BehaviorDetails {
58 std::map<std::string, std::map<std::string, bool> > namedVariableMap_;
59 bool verboseDisabled_ =
false;
60 bool timingDisabled_ =
false;
65 enum EnvironmentVariableState
67 EnvironmentVariableIsSet_ON,
68 EnvironmentVariableIsSet_OFF,
69 EnvironmentVariableIsSet,
70 EnvironmentVariableIsNotSet
76 std::string stringToUpper (std::string s)
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
273 #endif // HAVE_TPETRA_DEBUG
276 constexpr
bool verboseDefault () {
280 constexpr
bool timingDefault () {
284 constexpr
bool assumeMpiIsCudaAwareDefault () {
285 #ifdef TPETRA_ASSUME_CUDA_AWARE_MPI
289 #endif // TPETRA_ASSUME_CUDA_AWARE_MPI
292 constexpr
bool hierarchicalUnpackDefault () {
300 constexpr
char envVarName[] =
"TPETRA_DEBUG";
301 constexpr
bool defaultValue = debugDefault ();
303 static bool value_ = defaultValue;
304 static bool initialized_ =
false;
305 return idempotentlyGetEnvironmentVariableAsBool (value_,
313 if (BehaviorDetails::verboseDisabled_)
return false;
315 constexpr
char envVarName[] =
"TPETRA_VERBOSE";
316 constexpr
bool defaultValue = verboseDefault ();
318 static bool value_ = defaultValue;
319 static bool initialized_ =
false;
320 return idempotentlyGetEnvironmentVariableAsBool (value_,
328 if (BehaviorDetails::timingDisabled_)
return false;
330 constexpr
char envVarName[] =
"TPETRA_TIMING";
331 constexpr
bool defaultValue = timingDefault ();
333 static bool value_ = defaultValue;
334 static bool initialized_ =
false;
335 return idempotentlyGetEnvironmentVariableAsBool (value_,
343 constexpr
char envVarName[] =
"TPETRA_ASSUME_CUDA_AWARE_MPI";
344 constexpr
bool defaultValue = assumeMpiIsCudaAwareDefault ();
346 static bool value_ = defaultValue;
347 static bool initialized_ =
false;
348 return idempotentlyGetEnvironmentVariableAsBool (value_,
357 static int savedval=-1;
358 if(savedval!=-1)
return savedval;
359 const char* varVal = std::getenv (
"MM_TAFC_OptimizationCoreCount");
360 if (varVal ==
nullptr) {
364 savedval = std::stoi(std::string(varVal));
370 constexpr
char envVarName[] =
"TPETRA_VERBOSE_PRINT_COUNT_THRESHOLD";
371 constexpr
size_t defaultValue (200);
373 static size_t value_ = defaultValue;
374 static bool initialized_ =
false;
375 return idempotentlyGetEnvironmentVariableAsSize
376 (value_, initialized_, envVarName, defaultValue);
381 constexpr
char envVarName[] =
"TPETRA_ROW_IMBALANCE_THRESHOLD";
382 constexpr
size_t defaultValue (256);
384 static size_t value_ = defaultValue;
385 static bool initialized_ =
false;
386 return idempotentlyGetEnvironmentVariableAsSize
387 (value_, initialized_, envVarName, defaultValue);
392 constexpr
char envVarName[] =
"TPETRA_MULTIVECTOR_USE_MERGE_PATH";
393 constexpr
bool defaultValue =
false;
395 static bool value_ = defaultValue;
396 static bool initialized_ =
false;
397 return idempotentlyGetEnvironmentVariableAsBool
398 (value_, initialized_, envVarName, defaultValue);
403 constexpr
char envVarName[] =
"TPETRA_VECTOR_DEVICE_THRESHOLD";
404 constexpr
size_t defaultValue (10000);
406 static size_t value_ = defaultValue;
407 static bool initialized_ =
false;
408 return idempotentlyGetEnvironmentVariableAsSize
409 (value_, initialized_, envVarName, defaultValue);
414 constexpr
char envVarName[] =
"TPETRA_HIERARCHICAL_UNPACK_BATCH_SIZE";
416 #ifdef HAVE_TPETRA_INST_CUDA
417 constexpr
size_t defaultValue (16);
419 constexpr
size_t defaultValue (256);
422 static size_t value_ = defaultValue;
423 static bool initialized_ =
false;
424 return idempotentlyGetEnvironmentVariableAsSize
425 (value_, initialized_, envVarName, defaultValue);
430 constexpr
char envVarName[] =
"TPETRA_HIERARCHICAL_UNPACK_TEAM_SIZE";
431 #ifdef HAVE_TPETRA_INST_CUDA
432 const size_t defaultValue (16);
434 const size_t defaultValue (Teuchos::OrdinalTraits<size_t>::invalid ());
437 static size_t value_ = defaultValue;
438 static bool initialized_ =
false;
439 return idempotentlyGetEnvironmentVariableAsSize
440 (value_, initialized_, envVarName, defaultValue);
445 constexpr
char envVarName[] =
"TPETRA_USE_TEUCHOS_TIMERS";
446 constexpr
bool defaultValue(
false);
448 static bool value_ = defaultValue;
449 static bool initialized_ =
false;
450 return idempotentlyGetEnvironmentVariableAsBool
451 (value_, initialized_, envVarName, defaultValue);
456 constexpr
char envVarName[] =
"TPETRA_USE_KOKKOS_PROFILING";
457 constexpr
bool defaultValue(
false);
459 static bool value_ = defaultValue;
460 static bool initialized_ =
false;
461 return idempotentlyGetEnvironmentVariableAsBool
462 (value_, initialized_, envVarName, defaultValue);
468 constexpr
char envVarName[] =
"TPETRA_DEBUG";
469 constexpr
bool defaultValue =
false;
471 static bool initialized_ =
false;
472 return idempotentlyGetNamedEnvironmentVariableAsBool (name,
480 if (BehaviorDetails::verboseDisabled_)
return false;
482 constexpr
char envVarName[] =
"TPETRA_VERBOSE";
483 constexpr
bool defaultValue =
false;
485 static bool initialized_ =
false;
486 return idempotentlyGetNamedEnvironmentVariableAsBool (name,
493 BehaviorDetails::verboseDisabled_ =
false;
497 BehaviorDetails::verboseDisabled_ =
true;
502 if (BehaviorDetails::timingDisabled_)
return false;
504 constexpr
char envVarName[] =
"TPETRA_TIMING";
505 constexpr
bool defaultValue =
false;
507 static bool initialized_ =
false;
508 return idempotentlyGetNamedEnvironmentVariableAsBool (name,
515 BehaviorDetails::timingDisabled_ =
false;
519 BehaviorDetails::timingDisabled_ =
true;
524 constexpr
char envVarName[] =
"TPETRA_HIERARCHICAL_UNPACK";
525 constexpr
bool defaultValue = hierarchicalUnpackDefault();
527 static bool value_ = defaultValue;
528 static bool initialized_ =
false;
529 return idempotentlyGetEnvironmentVariableAsBool (value_,
static bool useMergePathMultiVector()
Whether to use the cuSPARSE merge path algorithm to perform sparse matrix-multivector products...
static int TAFC_OptimizationCoreCount()
MPI process count above which Tpetra::CrsMatrix::transferAndFillComplete will attempt to do advanced ...
static bool timing()
Whether Tpetra is in timing mode.
static void disable_verbose_behavior()
Disable verbose mode, programatically.
static size_t multivectorKernelLocationThreshold()
the threshold for transitioning from device to host
static bool debug()
Whether Tpetra is in debug mode.
static size_t hierarchicalUnpackTeamSize()
Size of team for hierarchical unpacking.
static bool profilingRegionUseTeuchosTimers()
Use Teuchos::Timer in Tpetra::ProfilingRegion.
static void disable_timing()
Disable timing, programatically.
static bool hierarchicalUnpack()
Unpack rows of a matrix using hierarchical unpacking.
static bool assumeMpiIsCudaAware()
Whether to assume that MPI is CUDA aware.
static void enable_timing()
Enable timing, programatically.
static bool verbose()
Whether Tpetra is in verbose mode.
static size_t rowImbalanceThreshold()
Threshold for deciding if a local matrix is "imbalanced" in the number of entries per row...
void transform(const char kernelLabel[], ExecutionSpace execSpace, GlobalDataStructure &input, GlobalDataStructure &output, UnaryFunctionType f)
For each local entry input_i of input, assign f(input_i) to the corresponding local entry output_i of...
static size_t hierarchicalUnpackBatchSize()
Size of batch for hierarchical unpacking.
static void enable_verbose_behavior()
Enable verbose mode, programatically.
static size_t verbosePrintCountThreshold()
Number of entries below which arrays, lists, etc. will be printed in debug mode.
static bool profilingRegionUseKokkosProfiling()
Use Kokkos::Profiling in Tpetra::ProfilingRegion.
Declaration of Tpetra::Details::Behavior, a class that describes Tpetra's behavior.