From e7a9c4cd2198c139de9c4fc423a18375c1e5c8b0 Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Tue, 3 Sep 2024 09:52:31 +0200 Subject: [PATCH] threadmanager: remove unused typetag template parameter and move implementation to a translation unit --- CMakeLists.txt | 1 + CMakeLists_files.cmake | 1 + .../common/fvbasediscretization.hh | 2 +- opm/models/parallel/threadmanager.cpp | 93 +++++++++++++++++++ opm/models/parallel/threadmanager.hpp | 68 +------------- opm/models/utils/start.hh | 8 +- opm/simulators/flow/FlowMain.hpp | 10 +- tests/test_RestartSerialization.cpp | 2 +- tests/test_equil.cpp | 2 +- tests/test_outputdir.cpp | 4 +- 10 files changed, 114 insertions(+), 77 deletions(-) create mode 100644 opm/models/parallel/threadmanager.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index bbd7374a4..c5526517d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -407,6 +407,7 @@ if(QuadMath_FOUND) co2injection_flash_ecfv co2injection_flash_vcfv) opm_add_test(${tapp}_quad + LIBRARIES opmsimulators opmcommon EXE_NAME ${tapp}_quad SOURCES examples/${tapp}.cpp diff --git a/CMakeLists_files.cmake b/CMakeLists_files.cmake index 0d473fd17..6c78e1886 100644 --- a/CMakeLists_files.cmake +++ b/CMakeLists_files.cmake @@ -61,6 +61,7 @@ list (APPEND MAIN_SOURCE_FILES opm/models/blackoil/blackoilmicpparams.cpp opm/models/blackoil/blackoilpolymerparams.cpp opm/models/blackoil/blackoilsolventparams.cpp + opm/models/parallel/threadmanager.cpp opm/simulators/flow/ActionHandler.cpp opm/simulators/flow/Banners.cpp opm/simulators/flow/BlackoilModelParameters.cpp diff --git a/opm/models/discretization/common/fvbasediscretization.hh b/opm/models/discretization/common/fvbasediscretization.hh index df0165701..cee45056c 100644 --- a/opm/models/discretization/common/fvbasediscretization.hh +++ b/opm/models/discretization/common/fvbasediscretization.hh @@ -219,7 +219,7 @@ struct ConstraintsContext */ template struct ThreadManager -{ using type = ::Opm::ThreadManager; }; +{ using type = ::Opm::ThreadManager; }; template struct UseLinearizationLock diff --git a/opm/models/parallel/threadmanager.cpp b/opm/models/parallel/threadmanager.cpp new file mode 100644 index 000000000..f9a77afd2 --- /dev/null +++ b/opm/models/parallel/threadmanager.cpp @@ -0,0 +1,93 @@ +// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- +// vi: set et ts=4 sw=4 sts=4: +/* + This file is part of the Open Porous Media project (OPM). + + OPM is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + OPM is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with OPM. If not, see . + + Consult the COPYING file in the top-level source directory of this + module for the precise wording of the license and the list of + copyright holders. +*/ +#include +#include + +#ifdef _OPENMP +#include +#endif + +#include +#include + +namespace Opm { + +int ThreadManager::numThreads_ = 1; + +void ThreadManager::registerParameters() +{ + Parameters::Register + ("The maximum number of threads to be instantiated per process " + "('-1' means 'automatic')"); +} + +void ThreadManager::init(bool queryCommandLineParameter) +{ + if (queryCommandLineParameter) { + numThreads_ = Parameters::Get(); + + // some safety checks. This is pretty ugly macro-magic, but so what? +#if !defined(_OPENMP) + if (numThreads_ != 1 && numThreads_ != -1) { + throw std::invalid_argument("OpenMP is not available. The only valid values for " + "threads-per-process is 1 and -1 but it is "+std::to_string(numThreads_)+"!"); + } + numThreads_ = 1; +#elif !defined NDEBUG && defined DUNE_INTERFACECHECK + if (numThreads_ != 1) { + throw std::invalid_argument("You explicitly enabled Barton-Nackman interface checking in Dune. " + "The Dune implementation of this is currently incompatible with " + "thread parallelism!"); + } + numThreads_ = 1; +#else + if (numThreads_ == 0) { + throw std::invalid_argument("Zero threads per process are not possible: It must be at least 1, " + "(or -1 for 'automatic')!"); + } +#endif + +#ifdef _OPENMP + // actually limit the number of threads + if (numThreads_ > 0) { + omp_set_num_threads(numThreads_); + } +#endif + } + +#ifdef _OPENMP + // get the number of threads which are used in the end. + numThreads_ = omp_get_max_threads(); +#endif +} + +unsigned ThreadManager::threadId() +{ +#ifdef _OPENMP + return static_cast(omp_get_thread_num()); +#else + return 0; +#endif +} + +} // namespace Opm diff --git a/opm/models/parallel/threadmanager.hpp b/opm/models/parallel/threadmanager.hpp index 13e630509..8b12345d8 100644 --- a/opm/models/parallel/threadmanager.hpp +++ b/opm/models/parallel/threadmanager.hpp @@ -27,22 +27,11 @@ #ifndef OPM_THREAD_MANAGER_HPP #define OPM_THREAD_MANAGER_HPP -#ifdef _OPENMP -#include -#endif - -#include -#include -#include - -#include - namespace Opm { /*! * \brief Simplifies multi-threaded capabilities. */ -template class ThreadManager { public: @@ -58,12 +47,7 @@ public: /*! * \brief Register all run-time parameters of the thread manager. */ - static void registerParameters() - { - Parameters::Register - ("The maximum number of threads to be instantiated per process " - "('-1' means 'automatic')"); - } + static void registerParameters(); /*! * \brief Initialize number of threads used thread manager. @@ -74,43 +58,7 @@ public: * outside of this function (e.g. by OPM_NUM_THREADS or in the simulator by * the ThreadsPerProcess parameter). */ - static void init(bool queryCommandLineParameter = true) - { - if (queryCommandLineParameter) - { - numThreads_ = Parameters::Get(); - - // some safety checks. This is pretty ugly macro-magic, but so what? -#if !defined(_OPENMP) - if (numThreads_ != 1 && numThreads_ != -1) - throw std::invalid_argument("OpenMP is not available. The only valid values for " - "threads-per-process is 1 and -1 but it is "+std::to_string(numThreads_)+"!"); - numThreads_ = 1; -#elif !defined NDEBUG && defined DUNE_INTERFACECHECK - if (numThreads_ != 1) - throw std::invalid_argument("You explicitly enabled Barton-Nackman interface checking in Dune. " - "The Dune implementation of this is currently incompatible with " - "thread parallelism!"); - numThreads_ = 1; -#else - - if (numThreads_ == 0) - throw std::invalid_argument("Zero threads per process are not possible: It must be at least 1, " - "(or -1 for 'automatic')!"); -#endif - -#ifdef _OPENMP - // actually limit the number of threads - if (numThreads_ > 0) - omp_set_num_threads(numThreads_); -#endif - } - -#ifdef _OPENMP - // get the number of threads which are used in the end. - numThreads_ = omp_get_max_threads(); -#endif - } + static void init(bool queryCommandLineParameter = true); /*! * \brief Return the maximum number of threads of the current process. @@ -121,22 +69,12 @@ public: /*! * \brief Return the index of the current OpenMP thread */ - static unsigned threadId() - { -#ifdef _OPENMP - return static_cast(omp_get_thread_num()); -#else - return 0; -#endif - } + static unsigned threadId(); private: static int numThreads_; }; -template -int ThreadManager::numThreads_ = 1; - } // namespace Opm #endif // OPM_THREAD_MANAGER_HPP diff --git a/opm/models/utils/start.hh b/opm/models/utils/start.hh index 62604c471..0c9714d84 100644 --- a/opm/models/utils/start.hh +++ b/opm/models/utils/start.hh @@ -76,7 +76,7 @@ template static inline void registerAllParameters_(bool finalizeRegistration = true) { using Simulator = GetPropType; - using ThreadManager = GetPropType; + using TM = GetPropType; Parameters::Register ("An .ini file which contains a set of run-time parameters"); @@ -84,7 +84,7 @@ static inline void registerAllParameters_(bool finalizeRegistration = true) ("Print the values of the run-time parameters at the " "start of the simulation"); - ThreadManager::registerParameters(); + TM::registerParameters(); Simulator::registerParameters(); if (finalizeRegistration) { @@ -279,7 +279,7 @@ static inline int start(int argc, char **argv, bool registerParams=true) using Scalar = GetPropType; using Simulator = GetPropType; using Problem = GetPropType; - using ThreadManager = GetPropType; + using TM = GetPropType; // set the signal handlers to reset the TTY to a well defined state on unexpected // program aborts @@ -304,7 +304,7 @@ static inline int start(int argc, char **argv, bool registerParams=true) if (paramStatus == 2) return 0; - ThreadManager::init(); + TM::init(); // initialize MPI, finalize is done automatically on exit #if HAVE_DUNE_FEM diff --git a/opm/simulators/flow/FlowMain.hpp b/opm/simulators/flow/FlowMain.hpp index f9a391ef4..b9727a85c 100644 --- a/opm/simulators/flow/FlowMain.hpp +++ b/opm/simulators/flow/FlowMain.hpp @@ -38,6 +38,10 @@ #include #endif +#ifdef _OPENMP +#include +#endif + #include #include #include @@ -97,7 +101,7 @@ 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(); + ThreadManager::registerParameters(); Simulator::registerParameters(); // register the base parameters @@ -305,8 +309,8 @@ namespace Opm { omp_set_num_threads(threads); #endif - using ThreadManager = GetPropType; - ThreadManager::init(false); + using TM = GetPropType; + TM::init(false); } void mergeParallelLogFiles() diff --git a/tests/test_RestartSerialization.cpp b/tests/test_RestartSerialization.cpp index 4d3b9ec28..c69a6b539 100644 --- a/tests/test_RestartSerialization.cpp +++ b/tests/test_RestartSerialization.cpp @@ -527,7 +527,7 @@ struct AquiferFixture { "test_RestartSerialization", "--ecl-deck-file-name=GLIFT1.DATA" }; - Opm::ThreadManager::registerParameters(); + Opm::ThreadManager::registerParameters(); AdaptiveTimeStepping::registerParameters(); BlackoilModelParameters::registerParameters(); Parameters::Register("Do *NOT* use!"); diff --git a/tests/test_equil.cpp b/tests/test_equil.cpp index fdc3157bb..1971e5293 100644 --- a/tests/test_equil.cpp +++ b/tests/test_equil.cpp @@ -232,7 +232,7 @@ struct EquilFixture { #endif using namespace Opm; FlowGenericVanguard::setCommunication(std::make_unique()); - Opm::ThreadManager::registerParameters(); + Opm::ThreadManager::registerParameters(); BlackoilModelParameters::registerParameters(); AdaptiveTimeStepping::registerParameters(); Parameters::Register("Dummy added for the well model to compile."); diff --git a/tests/test_outputdir.cpp b/tests/test_outputdir.cpp index cce63c26f..d0bf0d466 100644 --- a/tests/test_outputdir.cpp +++ b/tests/test_outputdir.cpp @@ -116,7 +116,7 @@ BOOST_FIXTURE_TEST_CASE(WithOutputDir, Fixture) Opm::Parameters::reset(); - Opm::ThreadManager::registerParameters(); + Opm::ThreadManager::registerParameters(); Opm::Main main(3, const_cast(no_param), false); BOOST_CHECK_EQUAL(main.justInitialize(), EXIT_SUCCESS); @@ -154,7 +154,7 @@ BOOST_FIXTURE_TEST_CASE(NoOutputDir, Fixture) const char* no_param[] = {"test_outputdir", input_file_path.c_str(), nullptr}; Opm::Parameters::reset(); - Opm::ThreadManager::registerParameters(); + Opm::ThreadManager::registerParameters(); Opm::Main main(2, const_cast(no_param), false);