Ginkgo Generated from branch based on master. Ginkgo version 1.7.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
scaled_reordered.hpp
1/*******************************<GINKGO LICENSE>******************************
2Copyright (c) 2017-2023, the Ginkgo authors
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
8
91. Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
11
122. Redistributions in binary form must reproduce the above copyright
13notice, this list of conditions and the following disclaimer in the
14documentation and/or other materials provided with the distribution.
15
163. Neither the name of the copyright holder nor the names of its
17contributors may be used to endorse or promote products derived from
18this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31******************************<GINKGO LICENSE>*******************************/
32
33#ifndef GKO_PUBLIC_CORE_REORDER_SCALED_REORDERED_HPP_
34#define GKO_PUBLIC_CORE_REORDER_SCALED_REORDERED_HPP_
35
36
37#include <ginkgo/core/base/abstract_factory.hpp>
38#include <ginkgo/core/base/executor.hpp>
39#include <ginkgo/core/base/lin_op.hpp>
40#include <ginkgo/core/base/types.hpp>
41#include <ginkgo/core/matrix/dense.hpp>
42#include <ginkgo/core/matrix/diagonal.hpp>
43#include <ginkgo/core/matrix/identity.hpp>
44#include <ginkgo/core/matrix/permutation.hpp>
45#include <ginkgo/core/reorder/reordering_base.hpp>
46
47
48namespace gko {
49namespace experimental {
50namespace reorder {
51
52
71template <typename ValueType = default_precision, typename IndexType = int32>
73 : public EnableLinOp<ScaledReordered<ValueType, IndexType>> {
74 friend class EnableLinOp<ScaledReordered, LinOp>;
76
77public:
78 using value_type = ValueType;
79 using index_type = IndexType;
83
84 std::shared_ptr<const LinOp> get_system_matrix() const
85 {
86 return system_matrix_;
87 }
88
89 std::shared_ptr<const LinOp> get_inner_operator() const
90 {
91 return inner_operator_;
92 }
93
95 {
100 std::shared_ptr<const LinOpFactory> GKO_FACTORY_PARAMETER_SCALAR(
101 inner_operator, nullptr);
102
108 std::shared_ptr<const ReorderingBaseFactory>
109 GKO_FACTORY_PARAMETER_SCALAR(reordering, nullptr);
110
114 std::shared_ptr<const matrix::Diagonal<value_type>>
115 GKO_FACTORY_PARAMETER_SCALAR(row_scaling, nullptr);
116
120 std::shared_ptr<const matrix::Diagonal<value_type>>
121 GKO_FACTORY_PARAMETER_SCALAR(col_scaling, nullptr);
122 };
125
126protected:
130 explicit ScaledReordered(std::shared_ptr<const Executor> exec)
131 : EnableLinOp<ScaledReordered>(std::move(exec)),
132 permutation_array_{exec}
133 {}
134
135 explicit ScaledReordered(const Factory* factory,
136 std::shared_ptr<const LinOp> system_matrix)
138 system_matrix->get_size()),
139 parameters_{factory->get_parameters()},
140 permutation_array_{factory->get_executor()}
141 {
142 // For now only support square matrices.
143 GKO_ASSERT_IS_SQUARE_MATRIX(system_matrix);
144
145 auto exec = this->get_executor();
146
147 system_matrix_ = gko::clone(exec, system_matrix);
148
149 // Scale the system matrix if scaling coefficients are provided
150 if (parameters_.row_scaling) {
151 GKO_ASSERT_EQUAL_DIMENSIONS(parameters_.row_scaling,
152 system_matrix_);
153 row_scaling_ = parameters_.row_scaling;
154 row_scaling_->apply(system_matrix_, system_matrix_);
155 }
156 if (parameters_.col_scaling) {
157 GKO_ASSERT_EQUAL_DIMENSIONS(parameters_.col_scaling,
158 system_matrix_);
159 col_scaling_ = parameters_.col_scaling;
160 col_scaling_->rapply(system_matrix_, system_matrix_);
161 }
162
163 // If a reordering factory is provided, generate the reordering and
164 // permute the system matrix accordingly.
165 if (parameters_.reordering) {
166 auto reordering = parameters_.reordering->generate(system_matrix_);
167 permutation_array_ = reordering->get_permutation_array();
168 system_matrix_ = as<Permutable<index_type>>(system_matrix_)
169 ->permute(&permutation_array_);
170 }
171
172 // Generate the inner operator with the scaled and reordered system
173 // matrix. If none is provided, use the Identity.
174 if (parameters_.inner_operator) {
175 inner_operator_ =
176 parameters_.inner_operator->generate(system_matrix_);
177 } else {
179 exec, this->get_size());
180 }
181 }
182
183 void apply_impl(const LinOp* b, LinOp* x) const override;
184
185 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
186 LinOp* x) const override;
187
199 void set_cache_to(const LinOp* b, const LinOp* x) const
200 {
201 if (cache_.inner_b == nullptr ||
202 cache_.inner_b->get_size() != b->get_size()) {
203 const auto size = b->get_size();
204 cache_.inner_b =
205 matrix::Dense<value_type>::create(this->get_executor(), size);
206 cache_.inner_x =
207 matrix::Dense<value_type>::create(this->get_executor(), size);
208 cache_.intermediate =
209 matrix::Dense<value_type>::create(this->get_executor(), size);
210 }
211 cache_.inner_b->copy_from(b);
212 if (inner_operator_->apply_uses_initial_guess()) {
213 cache_.inner_x->copy_from(x);
214 }
215 }
216
217private:
218 std::shared_ptr<LinOp> system_matrix_{};
219 std::shared_ptr<const LinOp> inner_operator_{};
220 std::shared_ptr<const matrix::Diagonal<value_type>> row_scaling_{};
221 std::shared_ptr<const matrix::Diagonal<value_type>> col_scaling_{};
222 array<index_type> permutation_array_{};
223
234 mutable struct cache_struct {
235 cache_struct() = default;
236
237 ~cache_struct() = default;
238
239 cache_struct(const cache_struct&) {}
240
241 cache_struct(cache_struct&&) {}
242
243 cache_struct& operator=(const cache_struct&) { return *this; }
244
245 cache_struct& operator=(cache_struct&&) { return *this; }
246
247 std::unique_ptr<matrix::Dense<value_type>> inner_b{};
248 std::unique_ptr<matrix::Dense<value_type>> inner_x{};
249 std::unique_ptr<matrix::Dense<value_type>> intermediate{};
250 } cache_;
251};
252
253
254} // namespace reorder
255} // namespace experimental
256} // namespace gko
257
258
259#endif // GKO_PUBLIC_CORE_REORDER_SCALED_REORDERED_HPP_
The AbstractFactory is a generic interface template that enables easy implementation of the abstract ...
Definition abstract_factory.hpp:75
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition lin_op.hpp:908
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:691
Definition lin_op.hpp:146
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition polymorphic_object.hpp:263
Definition scaled_reordered.hpp:123
Provides an interface to wrap reorderings like Rcm and diagonal scaling like equilibration around a L...
Definition scaled_reordered.hpp:73
This class is a utility which efficiently implements the identity matrix (a linear operator which map...
Definition identity.hpp:65
#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:308
#define GKO_FACTORY_PARAMETER_SCALAR(_name, _default)
Creates a scalar factory parameter in the factory parameters structure.
Definition abstract_factory.hpp:473
#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:422
#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:1046
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
detail::cloned_type< Pointer > clone(const Pointer &p)
Creates a unique clone of the object pointed to by p.
Definition utils_helper.hpp:203
std::shared_ptr< const ReorderingBaseFactory > reordering
The reordering that is to be applied to the system matrix.
Definition scaled_reordered.hpp:109
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:101
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:115
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:121
This struct is used to pass parameters to the EnableDefaultReorderingBaseFactory::generate() method.
Definition reordering_base.hpp:94