Ginkgo: ginkgo/core/reorder/scaled_reordered.hpp Source File

| Ginkgo Generated from branch based on main. Ginkgo version 1.11.0 A numerical linear algebra library targeting many-core architectures

Loading...
Searching...
No Matches
scaled_reordered.hpp
1// SPDX-FileCopyrightText: 2017 - 2025 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
20namespace gko {
21namespace experimental {
22namespace reorder {
23
24
43template <typename ValueType = default_precision, typename IndexType = int32>
44 class ScaledReordered
45 : public EnableLinOp<ScaledReordered<ValueType, IndexType>> {
46 friend class EnableLinOp<ScaledReordered, LinOp>;
47 friend class EnablePolymorphicObject<ScaledReordered, LinOp>;
48 GKO_ASSERT_SUPPORTED_VALUE_AND_INDEX_TYPE;
49
50public:
51 using value_type = ValueType;
52 using index_type = IndexType;
53 using ReorderingBaseFactory =
54 AbstractFactory<gko::reorder::ReorderingBase<IndexType>,
55 gko::reorder::ReorderingBaseArgs>;
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
67 GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
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 };
96 GKO_ENABLE_LIN_OP_FACTORY(ScaledReordered, parameters, Factory);
97 GKO_ENABLE_BUILD_METHOD(Factory);
98
99protected:
103 explicit ScaledReordered(std::shared_ptr<const Executor> exec)
104 : EnableLinOp<ScaledReordered>(std::move(exec)),
105 permutation_array_{exec}
106 {}
107
108 explicit ScaledReordered(const Factory* factory,
109 std::shared_ptr<const LinOp> system_matrix)
110 : EnableLinOp<ScaledReordered>(factory->get_executor(),
111 system_matrix->get_size()),
112 parameters_{factory->get_parameters()},
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 system_matrix_ = gko::clone(exec, system_matrix);
121
122 // Scale the system matrix if scaling coefficients are provided
123 if (parameters_.row_scaling) {
124 GKO_ASSERT_EQUAL_DIMENSIONS(parameters_.row_scaling,
125 system_matrix_);
126 row_scaling_ = parameters_.row_scaling;
127 row_scaling_->apply(system_matrix_, system_matrix_);
128 }
129 if (parameters_.col_scaling) {
130 GKO_ASSERT_EQUAL_DIMENSIONS(parameters_.col_scaling,
131 system_matrix_);
132 col_scaling_ = parameters_.col_scaling;
133 col_scaling_->rapply(system_matrix_, system_matrix_);
134 }
135
136 // If a reordering factory is provided, generate the reordering and
137 // permute the system matrix accordingly.
138 if (parameters_.reordering) {
139 auto reordering = parameters_.reordering->generate(system_matrix_);
140 permutation_array_ = reordering->get_permutation_array();
141 system_matrix_ = as<Permutable<index_type>>(system_matrix_)
142 ->permute(&permutation_array_);
143 }
144
145 // Generate the inner operator with the scaled and reordered system
146 // matrix. If none is provided, use the Identity.
147 if (parameters_.inner_operator) {
148 inner_operator_ =
149 parameters_.inner_operator->generate(system_matrix_);
150 } else {
151 inner_operator_ = gko::matrix::Identity<value_type>::create(
152 exec, this->get_size()[0]);
153 }
154 }
155
156 void apply_impl(const LinOp* b, LinOp* x) const override;
157
158 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
159 LinOp* x) const override;
160
172 void set_cache_to(const LinOp* b, const LinOp* x) const
173 {
174 if (cache_.inner_b == nullptr ||
175 cache_.inner_b->get_size() != b->get_size()) {
176 const auto size = b->get_size();
177 cache_.inner_b =
178 matrix::Dense<value_type>::create(this->get_executor(), size);
179 cache_.inner_x =
180 matrix::Dense<value_type>::create(this->get_executor(), size);
181 cache_.intermediate =
182 matrix::Dense<value_type>::create(this->get_executor(), size);
183 }
184 cache_.inner_b->copy_from(b);
185 if (inner_operator_->apply_uses_initial_guess()) {
186 cache_.inner_x->copy_from(x);
187 }
188 }
189
190private:
191 std::shared_ptr<LinOp> system_matrix_{};
192 std::shared_ptr<const LinOp> inner_operator_{};
193 std::shared_ptr<const matrix::Diagonal<value_type>> row_scaling_{};
194 std::shared_ptr<const matrix::Diagonal<value_type>> col_scaling_{};
195 array<index_type> permutation_array_{};
196
207 mutable struct cache_struct {
208 cache_struct() = default;
209
210 ~cache_struct() = default;
211
212 cache_struct(const cache_struct&) {}
213
214 cache_struct(cache_struct&&) {}
215
216 cache_struct& operator=(const cache_struct&) { return *this; }
217
218 cache_struct& operator=(cache_struct&&) { return *this; }
219
220 std::unique_ptr<matrix::Dense<value_type>> inner_b{};
221 std::unique_ptr<matrix::Dense<value_type>> inner_x{};
222 std::unique_ptr<matrix::Dense<value_type>> intermediate{};
223 } cache_;
224};
225
226
227} // namespace reorder
228} // namespace experimental
229} // namespace gko
230
231
232#endif // GKO_PUBLIC_CORE_REORDER_SCALED_REORDERED_HPP_
gko::AbstractFactory
The AbstractFactory is a generic interface template that enables easy implementation of the abstract ...
Definition abstract_factory.hpp:47
gko::EnableLinOp
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition lin_op.hpp:879
gko::EnablePolymorphicObject
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:668
gko::LinOp
Definition lin_op.hpp:117
gko::experimental::reorder::ScaledReordered::Factory
Definition scaled_reordered.hpp:96
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::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_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:280
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:445
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:394
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:1017
gko::experimental::reorder
The Reorder namespace.
Definition amd.hpp:25
gko
The Ginkgo namespace.
Definition abstract_factory.hpp:20
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:173
gko::as
std::decay_t< T > * as(U *obj)
Performs polymorphic type conversion.
Definition utils_helper.hpp:307
std
STL namespace.
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::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::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::reorder::ReorderingBaseArgs
This struct is used to pass parameters to the EnableDefaultReorderingBaseFactory::generate() method.
Definition reordering_base.hpp:65