Merge pull request #5856 from michal-toth/bugfix/thread-spawn

Bugfix/thread spawn
This commit is contained in:
Markus Blatt 2025-01-16 17:12:26 +01:00 committed by GitHub
commit 2bbf48c39c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 46 deletions

View File

@ -261,9 +261,9 @@ void BlackoilModelParameters<Scalar>::registerParameters()
Parameters::Hide<Parameters::DebugEmitCellPartition>();
// if openMP is available, determine the number threads per process automatically.
// if openMP is available, use two threads per mpi rank by default
#if _OPENMP
Parameters::SetDefault<Parameters::ThreadsPerProcess>(-1);
Parameters::SetDefault<Parameters::ThreadsPerProcess>(2);
#endif
}

View File

@ -101,12 +101,11 @@ namespace Opm {
("Developer option to see whether logging was on non-root processors. "
"In that case it will be appended to the *.DBG or *.PRT files");
ThreadManager::registerParameters();
Simulator::registerParameters();
// register the base parameters
registerAllParameters_<TypeTag>(/*finalizeRegistration=*/false);
Simulator::registerParameters();
detail::hideUnusedParameters<Scalar>();
Parameters::endRegistration();
@ -156,6 +155,9 @@ namespace Opm {
}
}
// set the maximum limit on OMP threads
setMaxThreads();
return status;
}
@ -278,12 +280,18 @@ 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.
// Issue a warning if both OMP_NUM_THREADS and --threads-per-process are set,
// but let the environment variable take precedence.
constexpr int default_threads = 2;
const bool isSet = Parameters::IsSet<Parameters::ThreadsPerProcess>();
const int requested_threads = Parameters::Get<Parameters::ThreadsPerProcess>();
int threads = requested_threads > 0 ? requested_threads : default_threads;
@ -294,19 +302,19 @@ namespace Opm {
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.");
// }
if (isSet) {
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
// reported by OpenMP as on some hardware (and some OpenMPI
// versions) this will be 1 when run with mpirun
omp_set_num_threads(threads);
// Requesting -1 thread will let OMP automatically deduce the number
// but setting OMP_NUM_THREADS takes precedence.
if (env_var || !(isSet && requested_threads == -1)) {
omp_set_num_threads(threads);
}
#endif
using TM = GetPropType<TypeTag, Properties::ThreadManager>;

View File

@ -703,42 +703,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;
}
}
}
first_time = false;
return omp_get_max_threads();
#else
threads = 1;
return 1;
#endif
return threads;
}
#if HAVE_DAMARIS