Ginkgo  Generated from pipelines/2721898507 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
scaled_reordered.hpp
1 // SPDX-FileCopyrightText: 2017 - 2026 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_REORDER_SCALED_REORDERED_HPP_
6 #define GKO_PUBLIC_CORE_REORDER_SCALED_REORDERED_HPP_
7 
8 
9 #include <ginkgo/core/base/abstract_factory.hpp>
10 #include <ginkgo/core/base/executor.hpp>
11 #include <ginkgo/core/base/lin_op.hpp>
12 #include <ginkgo/core/base/types.hpp>
13 #include <ginkgo/core/matrix/dense.hpp>
14 #include <ginkgo/core/matrix/diagonal.hpp>
15 #include <ginkgo/core/matrix/identity.hpp>
16 #include <ginkgo/core/matrix/permutation.hpp>
17 #include <ginkgo/core/reorder/reordering_base.hpp>
18 
19 
20 namespace gko {
21 namespace experimental {
22 namespace reorder {
23 
24 
43 template <typename ValueType = default_precision, typename IndexType = int32>
44 class ScaledReordered : public LinOp {
45  GKO_ASSERT_SUPPORTED_VALUE_AND_INDEX_TYPE;
46 
47 public:
48  using value_type = ValueType;
49  using index_type = IndexType;
50  using ReorderingBaseFactory =
53 
54  std::shared_ptr<const LinOp> get_system_matrix() const
55  {
56  return system_matrix_;
57  }
58 
59  std::shared_ptr<const LinOp> get_inner_operator() const
60  {
61  return inner_operator_;
62  }
63 
65  {
70  std::shared_ptr<const LinOpFactory> GKO_FACTORY_PARAMETER_SCALAR(
71  inner_operator, nullptr);
72 
78  std::shared_ptr<const ReorderingBaseFactory>
79  GKO_FACTORY_PARAMETER_SCALAR(reordering, nullptr);
80 
84  std::shared_ptr<const matrix::Diagonal<value_type>>
85  GKO_FACTORY_PARAMETER_SCALAR(row_scaling, nullptr);
86 
90  std::shared_ptr<const matrix::Diagonal<value_type>>
91  GKO_FACTORY_PARAMETER_SCALAR(col_scaling, nullptr);
92  };
95 
96 protected:
100  explicit ScaledReordered(std::shared_ptr<const Executor> exec)
101  : LinOp(std::move(exec)), permutation_array_{exec}
102  {}
103 
104  explicit ScaledReordered(const Factory* factory,
105  std::shared_ptr<const LinOp> system_matrix)
106  : LinOp(factory->get_executor(), system_matrix->get_size()),
107  parameters_{factory->get_parameters()},
108  permutation_array_{factory->get_executor()}
109  {
110  // For now only support square matrices.
111  GKO_ASSERT_IS_SQUARE_MATRIX(system_matrix);
112 
113  auto exec = this->get_executor();
114 
115  system_matrix_ = gko::clone(exec, system_matrix);
116 
117  // Scale the system matrix if scaling coefficients are provided
118  if (parameters_.row_scaling) {
119  GKO_ASSERT_EQUAL_DIMENSIONS(parameters_.row_scaling,
120  system_matrix_);
121  row_scaling_ = parameters_.row_scaling;
122  row_scaling_->apply(system_matrix_, system_matrix_);
123  }
124  if (parameters_.col_scaling) {
125  GKO_ASSERT_EQUAL_DIMENSIONS(parameters_.col_scaling,
126  system_matrix_);
127  col_scaling_ = parameters_.col_scaling;
128  col_scaling_->rapply(system_matrix_, system_matrix_);
129  }
130 
131  // If a reordering factory is provided, generate the reordering and
132  // permute the system matrix accordingly.
133  if (parameters_.reordering) {
134  auto reordering = parameters_.reordering->generate(system_matrix_);
135  permutation_array_ = reordering->get_permutation_array();
136  system_matrix_ = as<Permutable<index_type>>(system_matrix_)
137  ->permute(&permutation_array_);
138  }
139 
140  // Generate the inner operator with the scaled and reordered system
141  // matrix. If none is provided, use the Identity.
142  if (parameters_.inner_operator) {
143  inner_operator_ =
144  parameters_.inner_operator->generate(system_matrix_);
145  } else {
147  exec, this->get_size()[0]);
148  }
149  }
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 
167  void set_cache_to(const LinOp* b, const LinOp* x) const
168  {
169  if (cache_.inner_b == nullptr ||
170  cache_.inner_b->get_size() != b->get_size()) {
171  const auto size = b->get_size();
172  cache_.inner_b =
174  cache_.inner_x =
176  cache_.intermediate =
178  }
179  cache_.inner_b->copy_from(as<Cloneable>(b));
180  if (inner_operator_->apply_uses_initial_guess()) {
181  cache_.inner_x->copy_from(as<Cloneable>(x));
182  }
183  }
184 
185 private:
186  std::shared_ptr<LinOp> system_matrix_{};
187  std::shared_ptr<const LinOp> inner_operator_{};
188  std::shared_ptr<const matrix::Diagonal<value_type>> row_scaling_{};
189  std::shared_ptr<const matrix::Diagonal<value_type>> col_scaling_{};
190  array<index_type> permutation_array_{};
191 
202  mutable struct cache_struct {
203  cache_struct() = default;
204 
205  ~cache_struct() = default;
206 
207  cache_struct(const cache_struct&) {}
208 
209  cache_struct(cache_struct&&) {}
210 
211  cache_struct& operator=(const cache_struct&) { return *this; }
212 
213  cache_struct& operator=(cache_struct&&) { return *this; }
214 
215  std::unique_ptr<matrix::Dense<value_type>> inner_b{};
216  std::unique_ptr<matrix::Dense<value_type>> inner_x{};
217  std::unique_ptr<matrix::Dense<value_type>> intermediate{};
218  } cache_;
219 };
220 
221 
222 } // namespace reorder
223 } // namespace experimental
224 } // namespace gko
225 
226 
227 #endif // GKO_PUBLIC_CORE_REORDER_SCALED_REORDERED_HPP_
gko::LinOp
Definition: lin_op.hpp:117
gko::experimental::reorder::ScaledReordered::parameters_type::col_scaling
std::shared_ptr< const matrix::Diagonal< value_type > > col_scaling
The column scaling that is to be applied to the system matrix.
Definition: scaled_reordered.hpp:91
gko::AbstractFactory
The AbstractFactory is a generic interface template that enables easy implementation of the abstract ...
Definition: abstract_factory.hpp:45
gko::matrix::Dense::create
static std::unique_ptr< Dense > create(std::shared_ptr< const Executor > exec, const dim< 2 > &size={}, size_type stride=0)
Creates an uninitialized Dense matrix of the specified size.
gko::experimental::reorder::ScaledReordered::parameters_type::inner_operator
std::shared_ptr< const LinOpFactory > inner_operator
The inner operator factory that is to be generated on the scaled and reordered system matrix.
Definition: scaled_reordered.hpp:71
gko::experimental::reorder::ScaledReordered::parameters_type::row_scaling
std::shared_ptr< const matrix::Diagonal< value_type > > row_scaling
The row scaling that is to be applied to the system matrix.
Definition: scaled_reordered.hpp:85
GKO_FACTORY_PARAMETER_SCALAR
#define GKO_FACTORY_PARAMETER_SCALAR(_name, _default)
Creates a scalar factory parameter in the factory parameters structure.
Definition: abstract_factory.hpp:437
gko::clone
detail::cloned_type< Pointer > clone(const Pointer &p)
Creates a unique clone of the object pointed to by p.
Definition: utils_helper.hpp:425
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::experimental::reorder::ScaledReordered
Provides an interface to wrap reorderings like Rcm and diagonal scaling like equilibration around a L...
Definition: scaled_reordered.hpp:44
GKO_ENABLE_LIN_OP_FACTORY
#define GKO_ENABLE_LIN_OP_FACTORY(_lin_op, _parameters_name, _factory_name)
This macro will generate a default implementation of a LinOpFactory for the LinOp subclass it is defi...
Definition: lin_op.hpp:900
gko::LinOp::operator=
LinOp & operator=(const LinOp &)=default
Copy-assigns a LinOp.
gko::experimental::reorder::ScaledReordered::Factory
Definition: scaled_reordered.hpp:93
gko::matrix::Identity::create
static std::unique_ptr< Identity > create(std::shared_ptr< const Executor > exec, dim< 2 > size)
Creates an Identity matrix of the specified size.
gko::experimental::reorder::ScaledReordered::parameters_type::reordering
std::shared_ptr< const ReorderingBaseFactory > reordering
The reordering that is to be applied to the system matrix.
Definition: scaled_reordered.hpp:79
GKO_ENABLE_BUILD_METHOD
#define GKO_ENABLE_BUILD_METHOD(_factory_name)
Defines a build method for the factory, simplifying its construction by removing the repetitive typin...
Definition: abstract_factory.hpp:386
GKO_CREATE_FACTORY_PARAMETERS
#define GKO_CREATE_FACTORY_PARAMETERS(_parameters_name, _factory_name)
This Macro will generate a new type containing the parameters for the factory _factory_name.
Definition: abstract_factory.hpp:272
gko::PolymorphicObject::get_executor
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition: polymorphic_object.hpp:62
gko::reorder::ReorderingBaseArgs
This struct is used to pass parameters to the EnableDefaultReorderingBaseFactory::generate() method.
Definition: reordering_base.hpp:63
gko::LinOp::get_size
const dim< 2 > & get_size() const noexcept
Returns the size of the operator.
Definition: lin_op.hpp:169
gko::LinOp::LinOp
LinOp(const LinOp &)=default
Copy-constructs a LinOp.