NeoFOAM
WIP Prototype of a modern OpenFOAM core
Loading...
Searching...
No Matches
ginkgo.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2024 NeoFOAM authors
3
4#pragma once
5
6#if NF_WITH_GINKGO
7
8#include <ginkgo/ginkgo.hpp>
9#include <ginkgo/extensions/kokkos.hpp>
10
15
16
17namespace NeoFOAM::la::ginkgo
18{
19
20template<typename ValueType>
21class GkoSolverBase
22{
23
24private:
25
26 std::shared_ptr<const gko::Executor> gkoExec_;
27 Dictionary solverDict_;
28
29 virtual std::shared_ptr<gko::LinOp> solverGen(
30 std::shared_ptr<const gko::Executor> exec,
31 std::shared_ptr<const gko::LinOp> mtx,
32 size_t maxIter,
33 float relTol
34 ) = 0;
35
36protected:
37
38 GkoSolverBase(Executor exec, Dictionary solverDict)
39 : gkoExec_(getGkoExecutor(exec)), solverDict_(solverDict)
40 {}
41
42public:
43
44 virtual void solve(LinearSystem<ValueType, int>& sys, Field<ValueType>& x)
45 {
46 size_t nrows = sys.rhs().size();
47
48 auto solver = solverGen(
49 gkoExec_,
50 detail::createGkoMtx(gkoExec_, sys),
51 size_t(solverDict_.get<int>("maxIters")),
52 float(solverDict_.get<double>("relTol"))
53 );
54
55 auto rhs = detail::createGkoDense(gkoExec_, sys.rhs().data(), nrows);
56 auto gkoX = detail::createGkoDense(gkoExec_, x.data(), nrows);
57 solver->apply(rhs, gkoX);
58 }
59};
60
61
62template<typename ValueType>
63class CG : public GkoSolverBase<ValueType>
64{
65
66 virtual std::shared_ptr<gko::LinOp> solverGen(
67 std::shared_ptr<const gko::Executor> exec,
68 std::shared_ptr<const gko::LinOp> mtx,
69 size_t maxIter,
70 float relTol
71 ) override
72 {
73 auto fact =
74 gko::solver::Bicgstab<ValueType>::build()
75 .with_criteria(
76 gko::stop::Iteration::build().with_max_iters(maxIter),
77 gko::stop::ResidualNorm<ValueType>::build().with_reduction_factor(relTol)
78 )
79 .on(exec);
80 return fact->generate(mtx);
81 }
82
83public:
84
85 CG(Executor exec, Dictionary solverDict) : GkoSolverBase<ValueType>(exec, solverDict) {}
86};
87
88template<typename ValueType>
89class BiCGStab : public GkoSolverBase<ValueType>
90{
91 virtual std::shared_ptr<gko::LinOp> solverGen(
92 std::shared_ptr<const gko::Executor> exec,
93 std::shared_ptr<const gko::LinOp> mtx,
94 size_t maxIter,
95 float relTol
96 )
97 {
98 auto fact =
99 gko::solver::Bicgstab<ValueType>::build()
100 .with_criteria(
101 gko::stop::Iteration::build().with_max_iters(maxIter),
102 gko::stop::ResidualNorm<ValueType>::build().with_reduction_factor(relTol)
103 )
104 .on(exec);
105 return fact->generate(mtx);
106 }
107
108public:
109
110 BiCGStab(Executor exec, Dictionary solverDict) : GkoSolverBase<ValueType>(exec, solverDict) {}
111};
112
113}
114
115#endif
void solve(Expression< typename FieldType::ElementType > &exp, FieldType &solution, scalar t, scalar dt, const Dictionary &fvSchemes, const Dictionary &fvSolution)
Definition solver.hpp:106
std::shared_ptr< gko::Executor > getGkoExecutor(Executor exec)