avoiding unnecessary exception from std::stoi("")

in FlowMain.hpp
This commit is contained in:
Kai Bao 2024-08-07 15:16:33 +02:00
parent a6006b0c93
commit 52da2c9dfe

View File

@ -378,21 +378,30 @@ namespace Opm {
// but let the environment variable take precedence.
const int default_threads = 2;
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");
int omp_num_threads = -1;
try {
omp_num_threads = std::stoi(env_var ? env_var : "");
if (env_var) {
omp_num_threads = std::stoi(env_var);
}
} 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.");
// OpmLog::warning("Environment variable OMP_NUM_THREADS takes precedence over the --threads-per-process cmdline argument.");
// }
} catch (const std::invalid_argument& e) {
omp_num_threads = requested_threads > 0 ? requested_threads : default_threads;
}
// 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
omp_set_num_threads(omp_num_threads);
omp_set_num_threads(threads);
#endif
using ThreadManager = GetPropType<TypeTag, Properties::ThreadManager>;