From 8f8af340f43564a8675ab57f863564f558467379 Mon Sep 17 00:00:00 2001 From: Timo Koch Date: Mon, 5 Sep 2022 19:14:12 +0000 Subject: [PATCH 1/3] [common] Add trait to be specialized for grids with informs about multithreading support The default implementation uses `Dune::Capabilities::viewThreadSafe` to determine whether the grid manager support multithreading. --- dumux/common/gridcapabilities.hh | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/dumux/common/gridcapabilities.hh b/dumux/common/gridcapabilities.hh index 1ed27e7dea..3806be6467 100644 --- a/dumux/common/gridcapabilities.hh +++ b/dumux/common/gridcapabilities.hh @@ -66,4 +66,26 @@ static constexpr bool canCommunicate = } // namespace Dumux +namespace Dumux::Grid::Capabilities { + +// Default implementation +// The grid capability gives an absolute guarantee +// however this does not mean that multithreading is not +// supported at all. It might still work for some special cases. +// This knowledge is encoded in specializations for the different +// grid managers, see dumux/grid/io/gridmanager_*.hh +template +struct MultithreadingSupported +{ + template + static bool eval(const GV&) // default is independent of the grid view + { return Dune::Capabilities::viewThreadSafe::v; } +}; + +template +inline bool supportsMultithreading(const GridView& gridView) +{ return MultithreadingSupported::eval(gridView); } + +} // namespace Dumux::Grid::Capabilities + #endif -- GitLab From 3f9bc0fbecc427a8bb1d40c201a6e817887445ac Mon Sep 17 00:00:00 2001 From: Timo Koch Date: Mon, 5 Sep 2022 19:26:44 +0000 Subject: [PATCH 2/3] [grid] Specialize multithreading traits for most common grid managers This is the state to the best of our knowledge for Dune 2.8 although the viewThreadSafe capability has not been specialized until recently in Dune (> 2.8) --- dumux/io/grid/gridmanager_alu.hh | 15 +++++++++++++++ dumux/io/grid/gridmanager_foam.hh | 16 ++++++++++++++++ dumux/io/grid/gridmanager_ug.hh | 16 ++++++++++++++++ dumux/io/grid/gridmanager_yasp.hh | 16 ++++++++++++++++ 4 files changed, 63 insertions(+) diff --git a/dumux/io/grid/gridmanager_alu.hh b/dumux/io/grid/gridmanager_alu.hh index 274837e55e..8efb1700a0 100644 --- a/dumux/io/grid/gridmanager_alu.hh +++ b/dumux/io/grid/gridmanager_alu.hh @@ -35,6 +35,7 @@ #endif #include +#include namespace Dumux { @@ -248,6 +249,20 @@ private: int flag_; }; +namespace Grid::Capabilities { + +// To the best of our knowledge ALUGrid is view thread-safe +// This specialization can be removed after we depend on Dune release 2.9 in which this is guaranteed by ALUGrid itself +template +struct MultithreadingSupported> +{ + template + static bool eval(const GV&) // default is independent of the grid view + { return true; } +}; + +} // end namespace Grid::Capabilities + #endif // HAVE_DUNE_ALUGRID } // end namespace Dumux diff --git a/dumux/io/grid/gridmanager_foam.hh b/dumux/io/grid/gridmanager_foam.hh index 8ca5a56332..2d78016dd0 100644 --- a/dumux/io/grid/gridmanager_foam.hh +++ b/dumux/io/grid/gridmanager_foam.hh @@ -34,6 +34,8 @@ #include #endif +#include + namespace Dumux { #if HAVE_DUNE_FOAMGRID @@ -163,6 +165,20 @@ public: } }; +namespace Grid::Capabilities { + +// To the best of our knowledge FoamGrid is view thread-safe +// This specialization can be removed after we depend on Dune release 2.9 in which this is guaranteed by FoamGrid itself +template +struct MultithreadingSupported> +{ + template + static bool eval(const GV&) // default is independent of the grid view + { return true; } +}; + +} // end namespace Grid::Capabilities + #endif // HAVE_DUNE_FOAMGRID } // end namespace Dumux diff --git a/dumux/io/grid/gridmanager_ug.hh b/dumux/io/grid/gridmanager_ug.hh index 0ac7b59aee..201a427125 100644 --- a/dumux/io/grid/gridmanager_ug.hh +++ b/dumux/io/grid/gridmanager_ug.hh @@ -33,6 +33,8 @@ #include #endif +#include + namespace Dumux { #if HAVE_DUNE_UGGRID @@ -175,6 +177,20 @@ private: } }; +namespace Grid::Capabilities { + +// To the best of our knowledge UGGrid is view thread-safe for sequential runs +// This specialization maybe be removed after we depend on Dune release 2.9 if is guaranteed by UGGrid itself by then +template +struct MultithreadingSupported> +{ + template + static bool eval(const GV& gv) // default is independent of the grid view + { return gv.comm().size() <= 1; } +}; + +} // end namespace Grid::Capabilities + #endif // HAVE_DUNE_UGGRID } // end namespace Dumux diff --git a/dumux/io/grid/gridmanager_yasp.hh b/dumux/io/grid/gridmanager_yasp.hh index 5e7450b029..88095d416c 100644 --- a/dumux/io/grid/gridmanager_yasp.hh +++ b/dumux/io/grid/gridmanager_yasp.hh @@ -33,6 +33,8 @@ #include #endif +#include + namespace Dumux { /*! @@ -434,6 +436,20 @@ private: } }; +namespace Grid::Capabilities { + +// To the best of our knowledge YaspGrid is view thread-safe +// This specialization can be removed after we depend on Dune release 2.9 and this is guaranteed by YaspGrid itself +template +struct MultithreadingSupported> +{ + template + static bool eval(const GV& gv) // default is independent of the grid view + { return true; } +}; + +} // end namespace Grid::Capabilities + } // end namespace Dumux #endif -- GitLab From 6616631209411e3b226ca37a70daef6a49e41aaa Mon Sep 17 00:00:00 2001 From: Timo Koch Date: Mon, 5 Sep 2022 19:44:31 +0000 Subject: [PATCH 3/3] [assembly] Check if grid supports multithreading --- dumux/assembly/fvassembler.hh | 4 ++++ dumux/multidomain/fvassembler.hh | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/dumux/assembly/fvassembler.hh b/dumux/assembly/fvassembler.hh index 5006331d43..c83d1ae1e1 100644 --- a/dumux/assembly/fvassembler.hh +++ b/dumux/assembly/fvassembler.hh @@ -33,6 +33,8 @@ #include #include +#include + #include #include @@ -153,6 +155,7 @@ public: { static_assert(isImplicit, "Explicit assembler for stationary problem doesn't make sense!"); enableMultithreading_ = SupportsColoring::value + && Grid::Capabilities::supportsMultithreading(gridGeometry_->gridView()) && !Multithreading::isSerial() && getParam("Assembly.Multithreading", true); @@ -177,6 +180,7 @@ public: , isStationaryProblem_(!timeLoop) { enableMultithreading_ = SupportsColoring::value + && Grid::Capabilities::supportsMultithreading(gridGeometry_->gridView()) && !Multithreading::isSerial() && getParam("Assembly.Multithreading", true); diff --git a/dumux/multidomain/fvassembler.hh b/dumux/multidomain/fvassembler.hh index 4603259c5f..2673368433 100644 --- a/dumux/multidomain/fvassembler.hh +++ b/dumux/multidomain/fvassembler.hh @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -53,6 +54,26 @@ namespace Dumux { +namespace Grid::Capabilities { + +namespace Detail { +// helper for multi-domain models +template +bool allGridsSupportsMultithreadingImpl(const T& gridGeometries, std::index_sequence) +{ + return (... && supportsMultithreading(std::get(gridGeometries)->gridView())); +} +} // end namespace Detail + +// helper for multi-domain models (all grids have to support multithreading) +template +bool allGridsSupportsMultithreading(const std::tuple& gridGeometries) +{ + return Detail::allGridsSupportsMultithreadingImpl>(gridGeometries, std::make_index_sequence()); +} + +} // end namespace Grid::Capabilities + /*! * \ingroup MultiDomain * \ingroup Assembly @@ -182,6 +203,7 @@ public: std::cout << "Instantiated assembler for a stationary problem." << std::endl; enableMultithreading_ = CouplingManagerSupportsMultithreadedAssembly::value + && Grid::Capabilities::allGridsSupportsMultithreading(gridGeometryTuple_) && !Multithreading::isSerial() && getParam("Assembly.Multithreading", true); @@ -211,6 +233,7 @@ public: std::cout << "Instantiated assembler for an instationary problem." << std::endl; enableMultithreading_ = CouplingManagerSupportsMultithreadedAssembly::value + && Grid::Capabilities::allGridsSupportsMultithreading(gridGeometryTuple_) && !Multithreading::isSerial() && getParam("Assembly.Multithreading", true); -- GitLab