13#include <ginkgo/ginkgo.hpp>
14#include <ginkgo/extensions/kokkos.hpp>
15#include <ginkgo/extensions/config/json_config.hpp>
26namespace NeoN::la::ginkgo
29std::shared_ptr<gko::Executor> getGkoExecutor(Executor exec);
31gko::config::pnode parse(
const Dictionary& dict);
34template<
typename NeoNMatrixType>
35std::shared_ptr<const gko::LinOp> createGkoMtx(
const NeoNMatrixType& mtx);
37#ifdef NF_WITH_MPI_SUPPORT
40template<
typename IndexType>
41std::shared_ptr<const gko::LinOp> createGkoMtxDist(
42 std::shared_ptr<const gko::Executor> exec,
43 const gko::experimental::mpi::communicator& comm,
44 const CSRMatrix<scalar, IndexType>& mtx,
45 const COOMatrix<scalar, IndexType>& bmtx,
46 const CommunicationPattern& commPattern,
47 std::shared_ptr<gko::experimental::distributed::index_map<label, gko::int64>>& imapCache,
48 std::shared_ptr<gko::matrix::Coo<scalar, IndexType>>& nonLocalMtxCache,
49 std::shared_ptr<const gko::LinOp>& distMtxCache,
50 const scalar*& localValPtrCache
55gko::array<T> gkoArrayView(std::shared_ptr<const gko::Executor> exec, std::span<T> values)
57 return gko::make_array_view(exec, values.size(), values.data());
62std::shared_ptr<gko::matrix::Dense<scalar>>
63gkoVecView(std::shared_ptr<const gko::Executor> exec, T* ptr, localIdx s)
65 constexpr sizeType cols = std::is_same_v<T, Vec3> ? 3 : 1;
66 auto size =
static_cast<sizeType>(s);
67 return gko::share(gko::matrix::Dense<scalar>::create(
69 gko::dim<2> {size, cols},
70 gkoArrayView<scalar>(exec, std::span<scalar> {
reinterpret_cast<scalar*
>(ptr), cols * size}),
77std::shared_ptr<const gko::matrix::Dense<scalar>>
78gkoVecView(std::shared_ptr<const gko::Executor> exec,
const T* ptr, localIdx s)
80 constexpr sizeType cols = std::is_same_v<T, Vec3> ? 3 : 1;
81 auto size =
static_cast<sizeType>(s);
82 return gko::share(gko::matrix::Dense<scalar>::create_const(
84 gko::dim<2> {size, cols},
85 gko::array<scalar>::const_view(exec, cols * size,
reinterpret_cast<const scalar*
>(ptr)),
91template<
typename InType>
92scalar retrieve(
const InType& in)
94 using vec = gko::matrix::Dense<scalar>;
95 auto host = vec::create(in->get_executor()->get_master(), gko::dim<2> {1});
105struct L1ResidualControl
109 gko::size_type maxIter;
110 gko::size_type minIter;
121struct L1ResidualResult
123 gko::size_type numIter = 0;
125 scalar finalResNorm = 0.0;
129 std::vector<scalar> perColInitNorms;
130 std::vector<scalar> perColFinalNorms;
138inline constexpr const char* l1CriterionKey =
"neon::l1ScaledResidual";
147std::shared_ptr<gko::stop::CriterionFactory> makeL1CriterionFactory(
148 std::shared_ptr<const gko::Executor> exec,
149 const L1ResidualControl& control,
150 L1ResidualResult* report
160 const gko::LinOp* mtx,
const gko::LinOp* b,
const gko::LinOp* x, scalar normFactor
169L1ResidualResult solveWithL1Stop(
170 std::shared_ptr<const gko::Executor> exec,
171 std::shared_ptr<const gko::LinOp> mtx,
172 std::shared_ptr<
const gko::matrix::Dense<scalar>> b,
173 std::shared_ptr<gko::matrix::Dense<scalar>> x,
175 const L1ResidualControl& control
178#ifdef NF_WITH_MPI_SUPPORT
185L1ResidualResult solveWithL1StopDist(
186 std::shared_ptr<const gko::Executor> exec,
187 std::shared_ptr<const gko::LinOp> mtx,
188 std::shared_ptr<
const gko::experimental::distributed::Vector<scalar>> b,
189 std::shared_ptr<gko::experimental::distributed::Vector<scalar>> x,
191 const L1ResidualControl& control
200inline bool readFlag(
const Dictionary& cfg,
const std::string& key,
bool fallback)
202 if (!cfg.contains(key))
return fallback;
203 if (cfg.isType<
bool>(key))
return cfg.get<
bool>(key);
204 if (cfg.isType<
int>(key))
return cfg.get<
int>(key) != 0;
205 if (cfg.isType<std::string>(key))
207 const std::string v = cfg.get<std::string>(key);
208 return v ==
"true" || v ==
"yes" || v ==
"on" || v ==
"1";
221inline std::optional<L1ResidualControl> readL1ResidualControl(
const Dictionary& cfg)
223 if (!readFlag(cfg,
"l1ScaledResidual",
false))
228 L1ResidualControl control {0.0, 0.0, 1000, 0, 1};
231 auto readScalar = [](
const Dictionary& d,
const std::string& key,
scalar fallback)
233 if (!d.contains(key))
return fallback;
234 if (d.isType<
int>(key))
return scalar(d.get<
int>(key));
235 if (d.isType<label>(key))
return scalar(d.get<label>(key));
236 return d.get<
scalar>(key);
238 auto readInt = [](
const Dictionary& d,
const std::string& key,
localIdx fallback)
240 if (!d.contains(key))
return fallback;
241 if (d.isType<
int>(key))
return localIdx(d.get<
int>(key));
242 if (d.isType<scalar>(key))
return localIdx(d.get<scalar>(key));
248 control.tolerance = readScalar(cfg,
"tolerance", control.tolerance);
249 control.relTol = readScalar(cfg,
"relTol", control.relTol);
250 control.maxIter = readInt(cfg,
"maxIter", control.maxIter);
254 if (cfg.contains(
"criteria"))
256 const Dictionary& criteria = cfg.subDict(
"criteria");
257 control.tolerance = readScalar(criteria,
"absolute_residual_norm", control.tolerance);
258 control.relTol = readScalar(criteria,
"initial_residual_norm", control.relTol);
259 control.maxIter = readInt(criteria,
"iteration", control.maxIter);
261 control.minIter = readInt(cfg,
"minIter", control.minIter);
262 control.checkFrequency = readInt(cfg,
"checkFrequency", control.checkFrequency);
271inline bool pnodeReferencesString(
const gko::config::pnode& node,
const std::string& name)
273 using tag = gko::config::pnode::tag_t;
274 switch (node.get_tag())
277 return node.get_string() ==
name;
279 for (
const auto& e : node.get_array())
280 if (pnodeReferencesString(e, name))
return true;
283 for (
const auto& kv : node.get_map())
284 if (pnodeReferencesString(kv.second, name))
return true;
291class GinkgoSolver :
public SolverFactory::template Register<GinkgoSolver>
294 using Base = SolverFactory::template Register<GinkgoSolver>;
298 GinkgoSolver(Executor exec,
const Dictionary& solverConfig)
299 : Base(exec), gkoExec_(getGkoExecutor(exec)), coupled_(solverConfig.get(
"coupled", false)),
300 reuseSetup_(readFlag(solverConfig,
"reuseSetup", true)),
301 l1Control_(readL1ResidualControl(solverConfig)), config_(parse(solverConfig))
309 gko::config::registry reg(mergedPgmConfigMap<scalar>());
312 l1CritFactory_ = makeL1CriterionFactory(gkoExec_, *l1Control_, &l1Report_);
313 reg.emplace(std::string(l1CriterionKey), l1CritFactory_);
314 l1InConfig_ = pnodeReferencesString(config_, l1CriterionKey);
327 reg.emplace(
"neon::pgmMerge1", makeMergedPgmFactory<scalar>(gkoExec_, 1));
328 reg.emplace(
"neon::pgmMerge2", makeMergedPgmFactory<scalar>(gkoExec_, 2));
329 reg.emplace(
"neon::pgmMerge3", makeMergedPgmFactory<scalar>(gkoExec_, 3));
330 reg.emplace(
"neon::pgmMerge4", makeMergedPgmFactory<scalar>(gkoExec_, 4));
336 const auto td = gko::config::make_type_descriptor<scalar>();
337 auto gkoSolverConfig = config_;
338 const auto& precondConfig = config_.get(
"preconditioner");
339 if (reuseSetup_ && precondConfig.get_tag() == gko::config::pnode::tag_t::map
340 && !config_.get(
"value_type"))
342 precondFactory_ = gko::config::parse(precondConfig, reg, td).on(gkoExec_);
343 auto map = config_.get_map();
344 map.erase(
"preconditioner");
345 gkoSolverConfig = gko::config::pnode(map);
347 factory_ = gko::config::parse(gkoSolverConfig, reg, td).on(gkoExec_);
350 static std::string
name() {
return "Ginkgo"; }
352 static std::string doc() {
return "TBD"; }
354 static std::string schema() {
return "none"; }
356 virtual SolverStats
solve(
357 const LinearSystem<scalar, scalar, CSRMatrix<scalar, localIdx>>& sys, Vector<scalar>& x
360 virtual SolverStats
solve(
361 const LinearSystem<Vec3, Vec3, CSRMatrix<Vec3, localIdx>>& sys, Vector<Vec3>& x
364 virtual SolverStats
solve(
365 const LinearSystem<scalar, Vec3, CSRMatrix<scalar, localIdx>, COOMatrix<scalar, localIdx>>&
370#ifdef NF_WITH_MPI_SUPPORT
371 virtual SolverStats solveDist(
372 const LinearSystem<scalar, scalar, CSRMatrix<scalar, localIdx>>& sys, Vector<scalar>& x
375 virtual SolverStats solveDist(
376 const LinearSystem<Vec3, Vec3, CSRMatrix<Vec3, localIdx>>& sys, Vector<Vec3>& x
379 virtual SolverStats solveDist(
380 const LinearSystem<scalar, Vec3, CSRMatrix<scalar, localIdx>, COOMatrix<scalar, localIdx>>&
387 virtual std::unique_ptr<SolverFactory> clone() const final
403 std::unique_ptr<gko::LinOp> generateSolver(std::shared_ptr<const gko::LinOp> mtx)
const;
405 std::shared_ptr<const gko::Executor> gkoExec_;
409 std::optional<L1ResidualControl> l1Control_;
410 gko::config::pnode config_;
411 std::shared_ptr<const gko::LinOpFactory> factory_;
414 std::shared_ptr<const gko::LinOpFactory> precondFactory_;
416 mutable std::unique_ptr<gko::LinOpFactory::ReuseData> reuseData_;
418 std::shared_ptr<gko::stop::CriterionFactory> l1CritFactory_;
421 bool l1InConfig_ =
false;
423 mutable L1ResidualResult l1Report_;
429 SolverStatsEntry withL1Report(
430 SolverStatsEntry entry,
const gko::LinOp* mtx,
const gko::LinOp* b,
const gko::LinOp* x
432#ifdef NF_WITH_MPI_SUPPORT
434 mutable std::shared_ptr<gko::experimental::distributed::index_map<label, gko::int64>>
436 mutable std::shared_ptr<gko::matrix::Coo<scalar, localIdx>> cachedNonLocalMtx_;
440 mutable std::shared_ptr<const gko::LinOp> cachedDistMtx_;
441 mutable const scalar* cachedLocalValPtr_ =
nullptr;
#define NF_ERROR_EXIT(message)
Macro for printing an error message and aborting the program.
std::optional< la::SolverStats > solve(Expression< typename VectorType::ElementType, IndexType > &exp, VectorType &solution, scalar t, scalar dt, const Dictionary &fvSchemes, const Dictionary &fvSolution, std::vector< const PostAssemblyBase< typename VectorType::ElementType, IndexType > * > p={})
void map(ContType< ValueType > &cont, const Inner inner, std::pair< localIdx, localIdx > range={0, 0})
Map a field using a specific executor.
const std::string & name(const NeoN::Document &doc)
Retrieves the name of a Document.