Skip to content

Commit

Permalink
Merge pull request #5454 from vkip/omp_num_threads
Browse files Browse the repository at this point in the history
Handle empty or invalid OMP_NUM_THREADS by Flow default
  • Loading branch information
bska authored Jul 12, 2024
2 parents 93e06d2 + a078eaa commit 7789551
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
33 changes: 20 additions & 13 deletions opm/simulators/flow/FlowMain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,20 +383,27 @@ namespace Opm {
mpi_size_ = comm.size();

#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
if (!getenv("OMP_NUM_THREADS"))
{
int threads = 2;
const int requested_threads = Parameters::get<TypeTag, Parameters::ThreadsPerProcess>();
if (requested_threads > 0)
threads = requested_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(threads);
// 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.
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 : "");
// 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
// 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);
#endif

using ThreadManager = GetPropType<TypeTag, Properties::ThreadManager>;
Expand Down
25 changes: 14 additions & 11 deletions opm/simulators/flow/Main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -713,21 +713,24 @@ class Main
// This function is called before the parallel OpenMP stuff gets initialized.
// 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.
if (std::getenv("OMP_NUM_THREADS")) {
threads = omp_get_max_threads();
}
else {
threads = 2;

const int input_threads = Parameters::get<TypeTag, Parameters::ThreadsPerProcess>();

if (input_threads > 0)
threads = input_threads;
static bool first_time = true;
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;
}
} catch (const std::invalid_argument& e) {
omp_num_threads = requested_threads > 0 ? requested_threads : default_threads;
}
threads = omp_num_threads;
first_time = false;
#else
threads = 1;
#endif

return threads;
}

Expand Down

0 comments on commit 7789551

Please sign in to comment.