Merge pull request #5454 from vkip/omp_num_threads

Handle empty or invalid OMP_NUM_THREADS by Flow default
This commit is contained in:
Bård Skaflestad 2024-07-12 09:07:53 +02:00 committed by GitHub
commit 7789551b74
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 24 deletions

View File

@ -383,20 +383,27 @@ namespace Opm {
mpi_size_ = comm.size(); mpi_size_ = comm.size();
#if _OPENMP #if _OPENMP
// if openMP is available, default to 2 threads per process unless // If openMP is available, default to 2 threads per process unless
// OMP_NUM_THREADS is set or command line --threads-per-process used // OMP_NUM_THREADS is set or command line --threads-per-process used.
if (!getenv("OMP_NUM_THREADS")) // Issue a warning if both OMP_NUM_THREADS and --threads-per-process are set,
{ // but let the environment variable take precedence.
int threads = 2; const int default_threads = 2;
const int requested_threads = Parameters::get<TypeTag, Parameters::ThreadsPerProcess>(); const int requested_threads = Parameters::get<TypeTag, Parameters::ThreadsPerProcess>();
if (requested_threads > 0) const char* env_var = getenv("OMP_NUM_THREADS");
threads = requested_threads; int omp_num_threads = -1;
try {
omp_num_threads = std::stoi(env_var ? env_var : "");
// 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.");
// }
} 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 // 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(threads); omp_set_num_threads(omp_num_threads);
}
#endif #endif
using ThreadManager = GetPropType<TypeTag, Properties::ThreadManager>; using ThreadManager = GetPropType<TypeTag, Properties::ThreadManager>;

View File

@ -713,21 +713,24 @@ 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 happends after the deck is read and we want this message. // That initialization happends 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.
if (std::getenv("OMP_NUM_THREADS")) { static bool first_time = true;
threads = omp_get_max_threads(); const int default_threads = 2;
const int requested_threads = Parameters::get<TypeTag, Parameters::ThreadsPerProcess>();
const char* env_var = getenv("OMP_NUM_THREADS");
int omp_num_threads = -1;
try {
omp_num_threads = std::stoi(env_var ? env_var : "");
if (first_time && requested_threads > 0 && FlowGenericVanguard::comm().rank()==0) {
std::cout << "Warning: Environment variable OMP_NUM_THREADS takes precedence over the --threads-per-process cmdline argument." << std::endl;
} }
else { } catch (const std::invalid_argument& e) {
threads = 2; omp_num_threads = requested_threads > 0 ? requested_threads : default_threads;
const int input_threads = Parameters::get<TypeTag, Parameters::ThreadsPerProcess>();
if (input_threads > 0)
threads = input_threads;
} }
threads = omp_num_threads;
first_time = false;
#else #else
threads = 1; threads = 1;
#endif #endif
return threads; return threads;
} }