From cc5f362fdc1449245064daab9be7b394c80ec8a7 Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Tue, 16 Apr 2024 09:06:13 +0200 Subject: [PATCH 1/3] changed: make PreconditionerType an enum class this to avoid symbol clashes with the implementations. while at it rename it to Type as Preconditioner::PreconditionerType is redundant --- .../linalg/bda/opencl/Preconditioner.cpp | 18 ++++++++++-------- .../linalg/bda/opencl/Preconditioner.hpp | 7 ++++--- .../linalg/bda/opencl/openclSolverBackend.cpp | 2 +- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/opm/simulators/linalg/bda/opencl/Preconditioner.cpp b/opm/simulators/linalg/bda/opencl/Preconditioner.cpp index 1cf554a05..18144c3de 100644 --- a/opm/simulators/linalg/bda/opencl/Preconditioner.cpp +++ b/opm/simulators/linalg/bda/opencl/Preconditioner.cpp @@ -40,13 +40,15 @@ void Preconditioner::setOpencl(std::shared_ptr& context } template -std::unique_ptr > Preconditioner::create(PreconditionerType type, int verbosity, bool opencl_ilu_parallel) { - if (type == PreconditionerType::BILU0) { - return std::make_unique >(opencl_ilu_parallel, verbosity); - } else if (type == PreconditionerType::CPR) { - return std::make_unique >(verbosity, opencl_ilu_parallel); - } else if (type == PreconditionerType::BISAI) { - return std::make_unique >(opencl_ilu_parallel, verbosity); +std::unique_ptr> +Preconditioner::create(Type type, int verbosity, bool opencl_ilu_parallel) +{ + if (type == Type::BILU0) { + return std::make_unique >(opencl_ilu_parallel, verbosity); + } else if (type == Type::CPR) { + return std::make_unique >(verbosity, opencl_ilu_parallel); + } else if (type == Type::BISAI) { + return std::make_unique >(opencl_ilu_parallel, verbosity); } else { OPM_THROW(std::logic_error, "Invalid PreconditionerType"); } @@ -63,7 +65,7 @@ bool Preconditioner::create_preconditioner(BlockedMatrix *mat, [[may } #define INSTANTIATE_BDA_FUNCTIONS(n) \ -template std::unique_ptr > Preconditioner::create(PreconditionerType, int, bool); \ +template std::unique_ptr > Preconditioner::create(Type, int, bool); \ template void Preconditioner::setOpencl(std::shared_ptr&, std::shared_ptr&); \ template bool Preconditioner::analyze_matrix(BlockedMatrix *, BlockedMatrix *); \ template bool Preconditioner::create_preconditioner(BlockedMatrix *, BlockedMatrix *); diff --git a/opm/simulators/linalg/bda/opencl/Preconditioner.hpp b/opm/simulators/linalg/bda/opencl/Preconditioner.hpp index 4e450bf33..a81145dd3 100644 --- a/opm/simulators/linalg/bda/opencl/Preconditioner.hpp +++ b/opm/simulators/linalg/bda/opencl/Preconditioner.hpp @@ -22,12 +22,13 @@ #include +#include + namespace Opm { namespace Accelerator { - class BlockedMatrix; template @@ -51,13 +52,13 @@ protected: {}; public: - enum PreconditionerType { + enum class Type { BILU0, CPR, BISAI }; - static std::unique_ptr create(PreconditionerType type, int verbosity, bool opencl_ilu_parallel); + static std::unique_ptr create(Type type, int verbosity, bool opencl_ilu_parallel); virtual ~Preconditioner() = default; diff --git a/opm/simulators/linalg/bda/opencl/openclSolverBackend.cpp b/opm/simulators/linalg/bda/opencl/openclSolverBackend.cpp index d515137f3..7a39cf47c 100644 --- a/opm/simulators/linalg/bda/opencl/openclSolverBackend.cpp +++ b/opm/simulators/linalg/bda/opencl/openclSolverBackend.cpp @@ -65,7 +65,7 @@ openclSolverBackend::openclSolverBackend(int verbosity_, int maxit_, OPM_THROW(std::logic_error, "Error unknown value for argument --linear-solver, " + linsolver); } - using PreconditionerType = typename Preconditioner::PreconditionerType; + using PreconditionerType = typename Preconditioner::Type; if (use_cpr) { prec = Preconditioner::create(PreconditionerType::CPR, verbosity, opencl_ilu_parallel); } else if (use_isai) { From 34e94256bef12efecd16ba755372b37244799378 Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Tue, 16 Apr 2024 09:32:51 +0200 Subject: [PATCH 2/3] Preconditioner: replace if nest with switch --- .../linalg/bda/opencl/Preconditioner.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/opm/simulators/linalg/bda/opencl/Preconditioner.cpp b/opm/simulators/linalg/bda/opencl/Preconditioner.cpp index 18144c3de..d3703fcfa 100644 --- a/opm/simulators/linalg/bda/opencl/Preconditioner.cpp +++ b/opm/simulators/linalg/bda/opencl/Preconditioner.cpp @@ -18,14 +18,17 @@ */ #include -#include +#include + #include #include #include #include #include -#include + +#include +#include namespace Opm { @@ -43,15 +46,17 @@ template std::unique_ptr> Preconditioner::create(Type type, int verbosity, bool opencl_ilu_parallel) { - if (type == Type::BILU0) { + switch (type ) { + case Type::BILU0: return std::make_unique >(opencl_ilu_parallel, verbosity); - } else if (type == Type::CPR) { + case Type::CPR: return std::make_unique >(verbosity, opencl_ilu_parallel); - } else if (type == Type::BISAI) { + case Type::BISAI: return std::make_unique >(opencl_ilu_parallel, verbosity); - } else { - OPM_THROW(std::logic_error, "Invalid PreconditionerType"); } + + OPM_THROW(std::logic_error, + "Invalid preconditioner type " + std::to_string(static_cast(type))); } template From 7311948b8986f3eebab44e492c8b6257dc523b56 Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Tue, 16 Apr 2024 09:39:56 +0200 Subject: [PATCH 3/3] openCl preconditioners: make argument orders consistent --- opm/simulators/linalg/bda/opencl/CPR.cpp | 2 +- opm/simulators/linalg/bda/opencl/CPR.hpp | 3 +-- opm/simulators/linalg/bda/opencl/Preconditioner.cpp | 6 +++--- opm/simulators/linalg/bda/opencl/Preconditioner.hpp | 4 +++- opm/simulators/linalg/bda/opencl/openclSolverBackend.cpp | 6 +++--- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/opm/simulators/linalg/bda/opencl/CPR.cpp b/opm/simulators/linalg/bda/opencl/CPR.cpp index 9e63d5fe4..3c2003514 100644 --- a/opm/simulators/linalg/bda/opencl/CPR.cpp +++ b/opm/simulators/linalg/bda/opencl/CPR.cpp @@ -44,7 +44,7 @@ using Opm::OpmLog; using Dune::Timer; template -CPR::CPR(int verbosity_, bool opencl_ilu_parallel_) : +CPR::CPR(bool opencl_ilu_parallel_, int verbosity_) : Preconditioner(verbosity_), opencl_ilu_parallel(opencl_ilu_parallel_) { bilu0 = std::make_unique >(opencl_ilu_parallel, verbosity_); diff --git a/opm/simulators/linalg/bda/opencl/CPR.hpp b/opm/simulators/linalg/bda/opencl/CPR.hpp index 515000ec5..8afe7f497 100644 --- a/opm/simulators/linalg/bda/opencl/CPR.hpp +++ b/opm/simulators/linalg/bda/opencl/CPR.hpp @@ -115,8 +115,7 @@ private: void create_preconditioner_amg(BlockedMatrix *mat); public: - - CPR(int verbosity, bool opencl_ilu_parallel); + CPR(bool opencl_ilu_parallel, int verbosity); bool analyze_matrix(BlockedMatrix *mat) override; bool analyze_matrix(BlockedMatrix *mat, BlockedMatrix *jacMat) override; diff --git a/opm/simulators/linalg/bda/opencl/Preconditioner.cpp b/opm/simulators/linalg/bda/opencl/Preconditioner.cpp index d3703fcfa..4e61ad592 100644 --- a/opm/simulators/linalg/bda/opencl/Preconditioner.cpp +++ b/opm/simulators/linalg/bda/opencl/Preconditioner.cpp @@ -44,13 +44,13 @@ void Preconditioner::setOpencl(std::shared_ptr& context template std::unique_ptr> -Preconditioner::create(Type type, int verbosity, bool opencl_ilu_parallel) +Preconditioner::create(Type type, bool opencl_ilu_parallel, int verbosity) { switch (type ) { case Type::BILU0: return std::make_unique >(opencl_ilu_parallel, verbosity); case Type::CPR: - return std::make_unique >(verbosity, opencl_ilu_parallel); + return std::make_unique >(opencl_ilu_parallel, verbosity); case Type::BISAI: return std::make_unique >(opencl_ilu_parallel, verbosity); } @@ -70,7 +70,7 @@ bool Preconditioner::create_preconditioner(BlockedMatrix *mat, [[may } #define INSTANTIATE_BDA_FUNCTIONS(n) \ -template std::unique_ptr > Preconditioner::create(Type, int, bool); \ +template std::unique_ptr > Preconditioner::create(Type, bool, int); \ template void Preconditioner::setOpencl(std::shared_ptr&, std::shared_ptr&); \ template bool Preconditioner::analyze_matrix(BlockedMatrix *, BlockedMatrix *); \ template bool Preconditioner::create_preconditioner(BlockedMatrix *, BlockedMatrix *); diff --git a/opm/simulators/linalg/bda/opencl/Preconditioner.hpp b/opm/simulators/linalg/bda/opencl/Preconditioner.hpp index a81145dd3..4b535fded 100644 --- a/opm/simulators/linalg/bda/opencl/Preconditioner.hpp +++ b/opm/simulators/linalg/bda/opencl/Preconditioner.hpp @@ -58,7 +58,9 @@ public: BISAI }; - static std::unique_ptr create(Type type, int verbosity, bool opencl_ilu_parallel); + static std::unique_ptr create(Type type, + bool opencl_ilu_parallel, + int verbosity); virtual ~Preconditioner() = default; diff --git a/opm/simulators/linalg/bda/opencl/openclSolverBackend.cpp b/opm/simulators/linalg/bda/opencl/openclSolverBackend.cpp index 7a39cf47c..bc66e74c3 100644 --- a/opm/simulators/linalg/bda/opencl/openclSolverBackend.cpp +++ b/opm/simulators/linalg/bda/opencl/openclSolverBackend.cpp @@ -67,11 +67,11 @@ openclSolverBackend::openclSolverBackend(int verbosity_, int maxit_, using PreconditionerType = typename Preconditioner::Type; if (use_cpr) { - prec = Preconditioner::create(PreconditionerType::CPR, verbosity, opencl_ilu_parallel); + prec = Preconditioner::create(PreconditionerType::CPR, opencl_ilu_parallel, verbosity); } else if (use_isai) { - prec = Preconditioner::create(PreconditionerType::BISAI, verbosity, opencl_ilu_parallel); + prec = Preconditioner::create(PreconditionerType::BISAI, opencl_ilu_parallel, verbosity); } else { - prec = Preconditioner::create(PreconditionerType::BILU0, verbosity, opencl_ilu_parallel); + prec = Preconditioner::create(PreconditionerType::BILU0, opencl_ilu_parallel, verbosity); } std::ostringstream out;