Line data Source code
1 : // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 : // SPDX-License-Identifier: Apache-2.0
3 :
4 : #ifndef PALACE_LINALG_KSP_HPP
5 : #define PALACE_LINALG_KSP_HPP
6 :
7 : #include <memory>
8 : #include <type_traits>
9 : #include "linalg/iterative.hpp"
10 : #include "linalg/operator.hpp"
11 : #include "linalg/solver.hpp"
12 :
13 : namespace palace
14 : {
15 :
16 : class FiniteElementSpaceHierarchy;
17 : class IoData;
18 :
19 : //
20 : // Linear solver class composing an iterative solver and preconditioner object.
21 : //
22 : template <typename OperType>
23 : class BaseKspSolver
24 : {
25 : static_assert(std::is_same<OperType, Operator>::value ||
26 : std::is_same<OperType, ComplexOperator>::value,
27 : "Solver can only be defined for OperType = Operator or ComplexOperator!");
28 :
29 : using VecType = typename std::conditional<std::is_same<OperType, ComplexOperator>::value,
30 : ComplexVector, Vector>::type;
31 :
32 : protected:
33 : // The actual solver and preconditioner objects.
34 : std::unique_ptr<IterativeSolver<OperType>> ksp;
35 : std::unique_ptr<Solver<OperType>> pc;
36 :
37 : // Counters for number of calls to Mult method for linear solves, and cumulative number
38 : // of iterations.
39 : mutable int ksp_mult, ksp_mult_it;
40 :
41 : // Enable timer contribution for Timer::KSP_PRECONDITIONER.
42 : bool use_timer;
43 :
44 : public:
45 : BaseKspSolver(const IoData &iodata, FiniteElementSpaceHierarchy &fespaces,
46 : FiniteElementSpaceHierarchy *aux_fespaces = nullptr);
47 : BaseKspSolver(std::unique_ptr<IterativeSolver<OperType>> &&ksp,
48 : std::unique_ptr<Solver<OperType>> &&pc);
49 :
50 0 : int NumTotalMult() const { return ksp_mult; }
51 0 : int NumTotalMultIterations() const { return ksp_mult_it; }
52 :
53 : void SetOperators(const OperType &op, const OperType &pc_op);
54 :
55 : void Mult(const VecType &x, VecType &y) const;
56 : };
57 :
58 : using KspSolver = BaseKspSolver<Operator>;
59 : using ComplexKspSolver = BaseKspSolver<ComplexOperator>;
60 :
61 : } // namespace palace
62 :
63 : #endif // PALACE_LINALG_KSP_HPP
|