Set maximum threads immediately after parsing paramteres. Remove hotfix.

This commit is contained in:
Michal Tóth 2025-01-14 09:54:08 +01:00
parent f267df98b4
commit eb782d9ae2
2 changed files with 10 additions and 38 deletions

View File

@ -156,6 +156,9 @@ namespace Opm {
}
}
// set the maximum limit on OMP threads
setMaxThreads();
return status;
}
@ -278,6 +281,11 @@ namespace Opm {
mpi_rank_ = comm.rank();
mpi_size_ = comm.size();
setMaxThreads();
}
static void setMaxThreads()
{
#if _OPENMP
// If openMP is available, default to 2 threads per process unless
// OMP_NUM_THREADS is set or command line --threads-per-process used.

View File

@ -702,47 +702,11 @@ private:
static int getNumThreads()
{
int threads;
#ifdef _OPENMP
// 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;
constexpr int default_threads = 2;
const int requested_threads = Parameters::Get<Parameters::ThreadsPerProcess>();
threads = requested_threads > 0 ? requested_threads : default_threads;
const char* env_var = getenv("OMP_NUM_THREADS");
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;
}
}
}
if (threads != omp_get_max_threads()) {
// Set a limit to OMP threads. Defaults can saturate the whole machine.
omp_set_num_threads(threads);
}
first_time = false;
return omp_get_max_threads();
#else
threads = 1;
return 1;
#endif
return threads;
}
#if HAVE_DAMARIS