mirror of
https://github.com/OPM/opm-simulators.git
synced 2025-02-25 18:55:30 -06:00
Merge pull request #5516 from GitPaean/avoiding_unnecessary_exception
avoiding unnecessary exception from std::stoi("")
This commit is contained in:
commit
6507bba434
@ -38,6 +38,7 @@
|
|||||||
#include <dune/common/parallel/mpihelper.hh>
|
#include <dune/common/parallel/mpihelper.hh>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <charconv>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
@ -376,23 +377,30 @@ namespace Opm {
|
|||||||
// OMP_NUM_THREADS is set or command line --threads-per-process used.
|
// OMP_NUM_THREADS is set or command line --threads-per-process used.
|
||||||
// Issue a warning if both OMP_NUM_THREADS and --threads-per-process are set,
|
// Issue a warning if both OMP_NUM_THREADS and --threads-per-process are set,
|
||||||
// but let the environment variable take precedence.
|
// but let the environment variable take precedence.
|
||||||
const int default_threads = 2;
|
constexpr int default_threads = 2;
|
||||||
const int requested_threads = Parameters::get<TypeTag, Parameters::ThreadsPerProcess>();
|
const int requested_threads = Parameters::get<TypeTag, Parameters::ThreadsPerProcess>();
|
||||||
|
int threads = requested_threads > 0 ? requested_threads : default_threads;
|
||||||
|
|
||||||
const char* env_var = getenv("OMP_NUM_THREADS");
|
const char* env_var = getenv("OMP_NUM_THREADS");
|
||||||
int omp_num_threads = -1;
|
if (env_var) {
|
||||||
try {
|
int omp_num_threads = -1;
|
||||||
omp_num_threads = std::stoi(env_var ? env_var : "");
|
auto result = std::from_chars(env_var, env_var + std::strlen(env_var), omp_num_threads);
|
||||||
// Warning in 'Main.hpp', where this code is duplicated
|
if (result.ec == std::errc() && omp_num_threads > 0) {
|
||||||
// if (requested_threads > 0) {
|
// Set threads to omp_num_threads if it was successfully parsed and is positive
|
||||||
// OpmLog::warning("Environment variable OMP_NUM_THREADS takes precedence over the --threads-per-process cmdline argument.");
|
threads = omp_num_threads;
|
||||||
// }
|
// Warning in 'Main.hpp', where this code is duplicated
|
||||||
} catch (const std::invalid_argument& e) {
|
// if (requested_threads > 0) {
|
||||||
omp_num_threads = requested_threads > 0 ? requested_threads : default_threads;
|
// OpmLog::warning("Environment variable OMP_NUM_THREADS takes precedence over the --threads-per-process cmdline argument.");
|
||||||
|
// }
|
||||||
|
} else {
|
||||||
|
OpmLog::warning("Invalid value for OMP_NUM_THREADS environment variable.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// We are not limiting this to the number of processes
|
// We are not limiting this to the number of processes
|
||||||
// reported by OpenMP as on some hardware (and some OpenMPI
|
// reported by OpenMP as on some hardware (and some OpenMPI
|
||||||
// versions) this will be 1 when run with mpirun
|
// versions) this will be 1 when run with mpirun
|
||||||
omp_set_num_threads(omp_num_threads);
|
omp_set_num_threads(threads);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
using ThreadManager = GetPropType<TypeTag, Properties::ThreadManager>;
|
using ThreadManager = GetPropType<TypeTag, Properties::ThreadManager>;
|
||||||
|
@ -77,6 +77,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <charconv>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -713,28 +714,28 @@ private:
|
|||||||
// This function is called before the parallel OpenMP stuff gets initialized.
|
// This function is called before the parallel OpenMP stuff gets initialized.
|
||||||
// That initialization happens after the deck is read and we want this message.
|
// That initialization happens after the deck is read and we want this message.
|
||||||
// Hence we duplicate the code of setupParallelism to get the number of threads.
|
// Hence we duplicate the code of setupParallelism to get the number of threads.
|
||||||
static bool first_time = true;
|
static bool first_time = true;
|
||||||
const int default_threads = 2;
|
constexpr int default_threads = 2;
|
||||||
const int requested_threads = Parameters::get<TypeTag, Parameters::ThreadsPerProcess>();
|
const int requested_threads = Parameters::get<TypeTag, Parameters::ThreadsPerProcess>();
|
||||||
threads = requested_threads > 0 ? requested_threads : default_threads;
|
threads = requested_threads > 0 ? requested_threads : default_threads;
|
||||||
|
|
||||||
const char* env_var = getenv("OMP_NUM_THREADS");
|
const char* env_var = getenv("OMP_NUM_THREADS");
|
||||||
int omp_num_threads = -1;
|
if (env_var) {
|
||||||
try {
|
int omp_num_threads = -1;
|
||||||
if (env_var) {
|
auto result = std::from_chars(env_var, env_var + std::strlen(env_var), omp_num_threads);
|
||||||
omp_num_threads = std::stoi(env_var);
|
const bool can_output = first_time && FlowGenericVanguard::comm().rank() == 0;
|
||||||
|
if (result.ec == std::errc() && omp_num_threads > 0) {
|
||||||
|
// Set threads to omp_num_threads if it was successfully parsed and is positive
|
||||||
|
threads = omp_num_threads;
|
||||||
|
if (can_output && requested_threads > 0) {
|
||||||
|
std::cout << "Warning: Environment variable OMP_NUM_THREADS takes precedence over the --threads-per-process cmdline argument."
|
||||||
|
<< std::endl;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (can_output) {
|
||||||
|
std::cout << ("Warning: Invalid value for OMP_NUM_THREADS environment variable.") << std::endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (const std::invalid_argument&) {
|
|
||||||
// Do nothing if an invalid_argument exception is caught
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set threads to omp_num_threads if it was successfully parsed and is positive
|
|
||||||
if (omp_num_threads > 0) {
|
|
||||||
if (first_time && FlowGenericVanguard::comm().rank() == 0 && requested_threads > 0) {
|
|
||||||
std::cout << "Warning: Environment variable OMP_NUM_THREADS takes precedence over the --threads-per-process cmdline argument."
|
|
||||||
<< std::endl;
|
|
||||||
}
|
|
||||||
threads = omp_num_threads;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
first_time = false;
|
first_time = false;
|
||||||
|
Loading…
Reference in New Issue
Block a user