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
convergence.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_LOG_CONVERGENCE_HPP_
34#define GKO_PUBLIC_CORE_LOG_CONVERGENCE_HPP_
35
36
37#include <memory>
38
39
40#include <ginkgo/core/log/logger.hpp>
41#include <ginkgo/core/matrix/dense.hpp>
42#include <ginkgo/core/stop/criterion.hpp>
43
44
45namespace gko {
51namespace log {
52
53
65template <typename ValueType = default_precision>
66class Convergence : public Logger {
67public:
69 const stop::Criterion* criterion, const size_type& num_iterations,
70 const LinOp* residual, const LinOp* residual_norm,
71 const LinOp* solution, const uint8& stopping_id,
72 const bool& set_finalized, const array<stopping_status>* status,
73 const bool& one_changed, const bool& all_stopped) const override;
74
76 const stop::Criterion* criterion, const size_type& num_iterations,
77 const LinOp* residual, const LinOp* residual_norm,
78 const LinOp* implicit_sq_resnorm, const LinOp* solution,
79 const uint8& stopping_id, const bool& set_finalized,
80 const array<stopping_status>* status, const bool& one_changed,
81 const bool& all_stopped) const override;
82
83 void on_iteration_complete(const LinOp* solver, const LinOp* b,
84 const LinOp* x, const size_type& num_iterations,
85 const LinOp* residual,
86 const LinOp* residual_norm,
88 const array<stopping_status>* status,
89 bool stopped) const override;
90
105 GKO_DEPRECATED("use single-parameter create")
106 static std::unique_ptr<Convergence> create(
107 std::shared_ptr<const Executor>,
109 Logger::iteration_complete_mask)
110 {
111 return std::unique_ptr<Convergence>(new Convergence(enabled_events));
112 }
113
127 static std::unique_ptr<Convergence> create(
129 Logger::iteration_complete_mask)
130 {
131 return std::unique_ptr<Convergence>(new Convergence(enabled_events));
132 }
133
139 bool has_converged() const noexcept { return convergence_status_; }
140
144 void reset_convergence_status() { this->convergence_status_ = false; }
145
152 {
153 return num_iterations_;
154 }
155
161 const LinOp* get_residual() const noexcept { return residual_.get(); }
162
169 {
170 return residual_norm_.get();
171 }
172
179 {
180 return implicit_sq_resnorm_.get();
181 }
182
183protected:
191 GKO_DEPRECATED("use single-parameter constructor")
193 std::shared_ptr<const gko::Executor>,
195 Logger::iteration_complete_mask)
197 {}
198
205 explicit Convergence(
207 Logger::iteration_complete_mask)
208 : Logger(enabled_events)
209 {}
210
211private:
212 mutable bool convergence_status_{false};
213 mutable size_type num_iterations_{};
214 mutable std::unique_ptr<LinOp> residual_{};
215 mutable std::unique_ptr<LinOp> residual_norm_{};
216 mutable std::unique_ptr<LinOp> implicit_sq_resnorm_{};
217};
218
219
220} // namespace log
221} // namespace gko
222
223
224#endif // GKO_PUBLIC_CORE_LOG_CONVERGENCE_HPP_
The first step in using the Ginkgo library consists of creating an executor.
Definition executor.hpp:644
Definition lin_op.hpp:146
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:187
Convergence is a Logger which logs data strictly from the criterion_check_completed event.
Definition convergence.hpp:66
bool has_converged() const noexcept
Returns true if the solver has converged.
Definition convergence.hpp:139
static std::unique_ptr< Convergence > create(const mask_type &enabled_events=Logger::criterion_events_mask|Logger::iteration_complete_mask)
Creates a convergence logger.
Definition convergence.hpp:127
const size_type & get_num_iterations() const noexcept
Returns the number of iterations.
Definition convergence.hpp:151
void on_criterion_check_completed(const stop::Criterion *criterion, const size_type &num_iterations, const LinOp *residual, const LinOp *residual_norm, const LinOp *implicit_sq_resnorm, const LinOp *solution, const uint8 &stopping_id, const bool &set_finalized, const array< stopping_status > *status, const bool &one_changed, const bool &all_stopped) const override
stop::Criterion's check completed event.
void on_criterion_check_completed(const stop::Criterion *criterion, const size_type &num_iterations, const LinOp *residual, const LinOp *residual_norm, const LinOp *solution, const uint8 &stopping_id, const bool &set_finalized, const array< stopping_status > *status, const bool &one_changed, const bool &all_stopped) const override
stop::Criterion's check completed event.
const LinOp * get_residual() const noexcept
Returns the residual.
Definition convergence.hpp:161
const LinOp * get_residual_norm() const noexcept
Returns the residual norm.
Definition convergence.hpp:168
void on_iteration_complete(const LinOp *solver, const LinOp *b, const LinOp *x, const size_type &num_iterations, const LinOp *residual, const LinOp *residual_norm, const LinOp *implicit_resnorm_sq, const array< stopping_status > *status, bool stopped) const override
Register the iteration_complete event which logs every completed iterations.
static std::unique_ptr< Convergence > create(std::shared_ptr< const Executor >, const mask_type &enabled_events=Logger::criterion_events_mask|Logger::iteration_complete_mask)
Creates a convergence logger.
Definition convergence.hpp:106
void reset_convergence_status()
Resets the convergence status to false.
Definition convergence.hpp:144
const LinOp * get_implicit_sq_resnorm() const noexcept
Returns the implicit squared residual norm.
Definition convergence.hpp:178
Definition logger.hpp:104
static constexpr mask_type criterion_events_mask
Bitset Mask which activates all criterion events.
Definition logger.hpp:664
The Criterion class is a base class for all stopping criteria.
Definition criterion.hpp:64
@ criterion
Stopping criterion events.
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
std::uint8_t uint8
8-bit unsigned integral type.
Definition types.hpp:149
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:120