Ginkgo  Generated from pipelines/3136675824 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/diagonal.hpp>
14 #include <ginkgo/core/matrix/identity.hpp>
15 #include <ginkgo/core/matrix/multivector.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 
46 template <typename ValueType = default_precision, typename IndexType = int32>
47 class ScaledReordered : public LinOp {
48  GKO_ASSERT_SUPPORTED_VALUE_AND_INDEX_TYPE;
49 
50 public:
51  using value_type = ValueType;
52  using index_type = IndexType;
53  using ReorderingBaseFactory =
56 
57  std::shared_ptr<const LinOp> get_system_matrix() const
58  {
59  return system_matrix_;
60  }
61 
62  std::shared_ptr<const LinOp> get_inner_operator() const
63  {
64  return inner_operator_;
65  }
66 
68  {
73  std::shared_ptr<const LinOpFactory> GKO_FACTORY_PARAMETER_SCALAR(
74  inner_operator, nullptr);
75 
81  std::shared_ptr<const ReorderingBaseFactory>
82  GKO_FACTORY_PARAMETER_SCALAR(reordering, nullptr);
83 
87  std::shared_ptr<const matrix::Diagonal<value_type>>
88  GKO_FACTORY_PARAMETER_SCALAR(row_scaling, nullptr);
89 
93  std::shared_ptr<const matrix::Diagonal<value_type>>
94  GKO_FACTORY_PARAMETER_SCALAR(col_scaling, nullptr);
95  };
98 
99 protected:
103  explicit ScaledReordered(std::shared_ptr<const Executor> exec)
104  : LinOp(std::move(exec)), permutation_array_{exec}
105  {}
106 
107  explicit ScaledReordered(const Factory* factory,
108  std::shared_ptr<const LinOp> system_matrix)
109  : LinOp(factory->get_executor(), system_matrix->get_size()),
110  parameters_{factory->get_parameters()},
112  factory->get_executor())),
113  permutation_array_{factory->get_executor()}
114  {
115  // For now only support square matrices.
116  GKO_ASSERT_IS_SQUARE_MATRIX(system_matrix);
117 
118  auto exec = this->get_executor();
119 
120  as<ConvertibleTo<matrix::Csr<ValueType, IndexType>>>(system_matrix)
121  ->convert_to(system_matrix_);
122 
123  // Scale the system matrix if scaling coefficients are provided
124  if (parameters_.row_scaling) {
125  GKO_ASSERT_EQUAL_DIMENSIONS(parameters_.row_scaling,
126  system_matrix_);
127  row_scaling_ = parameters_.row_scaling;
128  row_scaling_->apply(system_matrix_, system_matrix_);
129  }
130  if (parameters_.col_scaling) {
131  GKO_ASSERT_EQUAL_DIMENSIONS(parameters_.col_scaling,
132  system_matrix_);
133  col_scaling_ = parameters_.col_scaling;
134  col_scaling_->rapply(system_matrix_, system_matrix_);
135  }
136 
137  // If a reordering factory is provided, generate the reordering and
138  // permute the system matrix accordingly.
139  if (parameters_.reordering) {
140  auto reordering = parameters_.reordering->generate(system_matrix_);
141  permutation_array_ = reordering->get_permutation_array();
142  system_matrix_ = as<matrix::Csr<ValueType, IndexType>>(
143  system_matrix_->permute(&permutation_array_));
144  }
145 
146  // Generate the inner operator with the scaled and reordered system
147  // matrix. If none is provided, use the Identity.
148  if (parameters_.inner_operator) {
149  inner_operator_ =
150  parameters_.inner_operator->generate(system_matrix_);
151  } else {
153  exec, this->get_size()[0]);
154  }
155  }
156 
157  void apply_impl(const AbstractMultiVector* b,
158  AbstractMultiVector* x) const override;
159 
160  void apply_impl(const AbstractMultiVector* alpha,
161  const AbstractMultiVector* b,
162  const AbstractMultiVector* beta,
163  AbstractMultiVector* x) const override;
164 
176  void set_cache_to(const AbstractMultiVector* b,
177  const AbstractMultiVector* x) const
178  {
179  if (cache_.inner_b == nullptr ||
180  cache_.inner_b->get_size() != b->get_size()) {
181  const auto size = b->get_size();
183  this->get_executor(), size);
185  this->get_executor(), size);
186  cache_.intermediate = matrix::MultiVector<value_type>::create(
187  this->get_executor(), size);
188  }
189  cache_.inner_b->copy_from(as<matrix::MultiVector<ValueType>>(b));
190  if (inner_operator_->apply_uses_initial_guess()) {
191  cache_.inner_x->copy_from(as<matrix::MultiVector<ValueType>>(x));
192  }
193  }
194 
195 private:
196  std::shared_ptr<matrix::Csr<ValueType, IndexType>> system_matrix_{};
197  std::shared_ptr<const LinOp> inner_operator_{};
198  std::shared_ptr<const matrix::Diagonal<value_type>> row_scaling_{};
199  std::shared_ptr<const matrix::Diagonal<value_type>> col_scaling_{};
200  array<index_type> permutation_array_{};
201 
212  mutable struct cache_struct {
213  cache_struct() = default;
214 
215  ~cache_struct() = default;
216 
217  cache_struct(const cache_struct&) {}
218 
219  cache_struct(cache_struct&&) noexcept {}
220 
221  cache_struct& operator=(const cache_struct&) { return *this; }
222 
223  cache_struct& operator=(cache_struct&&) noexcept { return *this; }
224 
225  std::unique_ptr<matrix::MultiVector<value_type>> inner_b{};
226  std::unique_ptr<matrix::MultiVector<value_type>> inner_x{};
227  std::unique_ptr<matrix::MultiVector<value_type>> intermediate{};
228  } cache_;
229 };
230 
231 
232 } // namespace reorder
233 } // namespace experimental
234 } // namespace gko
235 
236 
237 #endif // GKO_PUBLIC_CORE_REORDER_SCALED_REORDERED_HPP_
gko::matrix::Csr
CSR is a matrix format which stores only the nonzero coefficients by compressing each row of the matr...
Definition: matrix.hpp:30
gko::LinOp
Definition: lin_op.hpp:163
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:94
gko::AbstractFactory
The AbstractFactory is a generic interface template that enables easy implementation of the abstract ...
Definition: abstract_factory.hpp:77
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:74
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:88
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:469
gko::as
temporary_conversion< T > as(temporary_conversion< U > &&obj)
Performs polymorphic type conversion of a temporary_conversion.
Definition: temporary_conversion.hpp:376
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:19
gko::experimental::reorder::ScaledReordered
Provides an interface to wrap reorderings like Rcm and diagonal scaling like equilibration around a L...
Definition: scaled_reordered.hpp:47
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:1192
gko::LinOp::operator=
LinOp & operator=(const LinOp &)=default
Copy-assigns a LinOp.
gko::experimental::reorder::ScaledReordered::Factory
Definition: scaled_reordered.hpp:96
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:82
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:418
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:304
gko::matrix::MultiVector::create
static std::unique_ptr< MultiVector > create(std::shared_ptr< const Executor > exec, const dim< 2 > &size={}, size_type stride=0)
Creates an uninitialized MultiVector of the specified size.
gko::PolymorphicObject::get_executor
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition: polymorphic_object.hpp:69
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:259