Ginkgo  Generated from pipelines/2969282958 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
nullspace.hpp
1 // SPDX-FileCopyrightText: 2026 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_SOLVER_NULLSPACE_HPP_
6 #define GKO_PUBLIC_CORE_SOLVER_NULLSPACE_HPP_
7 
8 
9 #include <memory>
10 #include <vector>
11 
12 #include <ginkgo/core/base/array.hpp>
13 #include <ginkgo/core/base/dense_cache.hpp>
14 #include <ginkgo/core/base/lin_op.hpp>
15 #include <ginkgo/core/base/math.hpp>
16 #include <ginkgo/core/base/polymorphic_object.hpp>
17 #include <ginkgo/core/base/utils.hpp>
18 #include <ginkgo/core/matrix/dense.hpp>
19 
20 
21 namespace gko {
22 namespace solver {
23 
24 
58 template <typename ValueType = default_precision>
59 class Nullspace : public LinOp, public EnableCloneable<Nullspace<ValueType>> {
60  friend class EnableCloneable<Nullspace>;
61  GKO_ASSERT_SUPPORTED_VALUE_TYPE;
62 
63 public:
66 
67  using value_type = ValueType;
68  using absolute_type = remove_complex<ValueType>;
69 
84  static std::unique_ptr<Nullspace> create(
85  std::shared_ptr<const Executor> exec,
86  std::vector<std::shared_ptr<const LinOp>> basis,
87  bool contains_constant = false);
88 
104  static std::unique_ptr<Nullspace> create_from_constant(
105  std::shared_ptr<const Executor> exec, dim<2> size = {});
106 
108  bool contains_constant() const noexcept;
109 
114  size_type get_dimension() const noexcept;
115 
121  std::shared_ptr<const LinOp> get_basis() const noexcept;
122 
134  void project(ptr_param<LinOp> v) const;
135 
136  Nullspace& operator=(const Nullspace& other);
137 
138  Nullspace& operator=(Nullspace&& other);
139 
140  Nullspace(const Nullspace& other);
141 
142  Nullspace(Nullspace&& other);
143 
144 protected:
145  explicit Nullspace(std::shared_ptr<const Executor> exec);
146 
147  Nullspace(std::shared_ptr<const Executor> exec, dim<2> size,
148  std::vector<std::shared_ptr<const LinOp>> basis,
149  bool contains_constant);
150 
151  void apply_impl(const LinOp* b, LinOp* x) const override;
152 
153  void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
154  LinOp* x) const override;
155 
156 private:
157  template <typename VectorType>
158  void setup_basis(const std::vector<std::shared_ptr<const LinOp>>& basis);
159 
160  template <typename VectorType>
161  const matrix::Dense<ValueType>* get_local_basis(const VectorType* v) const;
162 
163  // stores [mean(v); V^H v] in coefficients_
164  template <typename VectorType>
165  void compute_components(const VectorType* v,
166  const matrix::Dense<ValueType>* basis_local,
167  bool has_constant) const;
168 
169  // result = v - mean - V C with the coefficients from compute_components;
170  // result may be v
171  template <typename VectorType>
172  void subtract_components(const VectorType* v, VectorType* result,
173  const matrix::Dense<ValueType>* basis_local,
174  bool has_constant) const;
175 
176  template <typename VectorType>
177  void remove_components(VectorType* v,
178  const matrix::Dense<ValueType>* basis_local,
179  bool has_constant) const;
180 
181  bool contains_constant_;
182  // orthonormal explicit basis (n x k), Dense or distributed::Vector
183  std::shared_ptr<const LinOp> basis_;
184  // scratch: (1 + k) x nrhs coefficients (means, then V^H x)
185  gko::detail::DenseCache<ValueType> coefficients_;
186  gko::detail::DenseCache<ValueType> host_coefficients_;
187  mutable array<char> reduction_tmp_;
188 };
189 
190 
191 namespace detail {
192 
193 
208 template <typename ValueType>
209 std::shared_ptr<const Nullspace<ValueType>> prepare_nullspace(
210  std::shared_ptr<const LinOp> param, const char* name, const LinOp* op,
211  size_type size, std::shared_ptr<const Executor> exec);
212 
213 
214 } // namespace detail
215 } // namespace solver
216 } // namespace gko
217 
218 
219 #endif // GKO_PUBLIC_CORE_SOLVER_NULLSPACE_HPP_
gko::log::profile_event_category::solver
Solver events.
gko::LinOp
Definition: lin_op.hpp:117
gko::matrix::Dense
Dense is a matrix format which explicitly stores all values of the matrix.
Definition: dense_cache.hpp:28
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:101
gko::solver::Nullspace
Nullspace represents the (right or left) nullspace of an operator and is the orthogonal projector on...
Definition: nullspace.hpp:59
gko::solver::Nullspace::create_from_constant
static std::unique_ptr< Nullspace > create_from_constant(std::shared_ptr< const Executor > exec, dim< 2 > size={})
Creates a Nullspace consisting only of the constant vector (e.g.
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:19
gko::array< char >
gko::dim< 2 >
gko::solver::Nullspace::get_dimension
size_type get_dimension() const noexcept
gko::ptr_param
This class is used for function parameters in the place of raw pointers.
Definition: utils_helper.hpp:43
gko::solver::Nullspace::project
void project(ptr_param< LinOp > v) const
Applies the projector in place: , for each column of v independently.
gko::solver::Nullspace::contains_constant
bool contains_constant() const noexcept
gko::EnableCloneable
This mixin is used to enable a default Cloneable::clone() implementation and similar for objects that...
Definition: polymorphic_object.hpp:376
gko::remove_complex
typename detail::remove_complex_s< T >::type remove_complex
Obtain the type which removed the complex of complex/scalar type or the template parameter of class b...
Definition: math.hpp:263
gko::solver::Nullspace::get_basis
std::shared_ptr< const LinOp > get_basis() const noexcept
gko::solver::Nullspace::create
static std::unique_ptr< Nullspace > create(std::shared_ptr< const Executor > exec, std::vector< std::shared_ptr< const LinOp >> basis, bool contains_constant=false)
Creates a Nullspace from an explicit basis.