From 3d794d2d80339b2be1e22baad13f99b2b81d91e5 Mon Sep 17 00:00:00 2001 From: Kai Bao Date: Thu, 8 Aug 2024 16:10:27 +0200 Subject: [PATCH] using std::from_chars instead of std::stoi --- opm/simulators/flow/FlowMain.hpp | 29 +++++++++++++------------- opm/simulators/flow/Main.hpp | 35 ++++++++++++++++---------------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/opm/simulators/flow/FlowMain.hpp b/opm/simulators/flow/FlowMain.hpp index db2251599..8d1d72ac9 100644 --- a/opm/simulators/flow/FlowMain.hpp +++ b/opm/simulators/flow/FlowMain.hpp @@ -38,6 +38,7 @@ #include #endif +#include #include #include @@ -376,28 +377,26 @@ namespace Opm { // 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, // but let the environment variable take precedence. - const int default_threads = 2; + constexpr int default_threads = 2; const int requested_threads = Parameters::get(); int threads = requested_threads > 0 ? requested_threads : default_threads; const char* env_var = getenv("OMP_NUM_THREADS"); - int omp_num_threads = -1; - try { - if (env_var) { - omp_num_threads = std::stoi(env_var); + if (env_var) { + int omp_num_threads = -1; + auto result = std::from_chars(env_var, env_var + std::strlen(env_var), omp_num_threads); + 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; + // Warning in 'Main.hpp', where this code is duplicated + // if (requested_threads > 0) { + // 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."); } - } catch (const std::invalid_argument& e) { - // 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) { - threads = omp_num_threads; - // Warning in 'Main.hpp', where this code is duplicated - // if (requested_threads > 0) { - // OpmLog::warning("Environment variable OMP_NUM_THREADS takes precedence over the --threads-per-process cmdline argument."); - // } - } // We are not limiting this to the number of processes // reported by OpenMP as on some hardware (and some OpenMPI // versions) this will be 1 when run with mpirun diff --git a/opm/simulators/flow/Main.hpp b/opm/simulators/flow/Main.hpp index 991320833..cd1e460cb 100644 --- a/opm/simulators/flow/Main.hpp +++ b/opm/simulators/flow/Main.hpp @@ -77,6 +77,7 @@ #endif #include +#include #include #include #include @@ -713,28 +714,28 @@ private: // This function is called before the parallel OpenMP stuff gets initialized. // 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. - static bool first_time = true; - const int default_threads = 2; + static bool first_time = true; + constexpr int default_threads = 2; const int requested_threads = Parameters::get(); threads = requested_threads > 0 ? requested_threads : default_threads; const char* env_var = getenv("OMP_NUM_THREADS"); - int omp_num_threads = -1; - try { - if (env_var) { - omp_num_threads = std::stoi(env_var); + if (env_var) { + int omp_num_threads = -1; + auto result = std::from_chars(env_var, env_var + std::strlen(env_var), omp_num_threads); + 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;