Ginkgo  Generated from pipelines/1330831941 branch based on master. Ginkgo version 1.8.0
A numerical linear algebra library targeting many-core architectures
convergence.hpp
1 // SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_LOG_CONVERGENCE_HPP_
6 #define GKO_PUBLIC_CORE_LOG_CONVERGENCE_HPP_
7 
8 
9 #include <memory>
10 
11 
12 #include <ginkgo/core/log/logger.hpp>
13 #include <ginkgo/core/matrix/dense.hpp>
14 #include <ginkgo/core/stop/criterion.hpp>
15 
16 
17 namespace gko {
23 namespace log {
24 
25 
37 template <typename ValueType = default_precision>
38 class Convergence : public Logger {
39 public:
41  const stop::Criterion* criterion, const size_type& num_iterations,
42  const LinOp* residual, const LinOp* residual_norm,
43  const LinOp* solution, const uint8& stopping_id,
44  const bool& set_finalized, const array<stopping_status>* status,
45  const bool& one_changed, const bool& all_stopped) const override;
46 
48  const stop::Criterion* criterion, const size_type& num_iterations,
49  const LinOp* residual, const LinOp* residual_norm,
50  const LinOp* implicit_sq_resnorm, const LinOp* solution,
51  const uint8& stopping_id, const bool& set_finalized,
52  const array<stopping_status>* status, const bool& one_changed,
53  const bool& all_stopped) const override;
54 
55  void on_iteration_complete(const LinOp* solver, const LinOp* b,
56  const LinOp* x, const size_type& num_iterations,
57  const LinOp* residual,
58  const LinOp* residual_norm,
59  const LinOp* implicit_resnorm_sq,
60  const array<stopping_status>* status,
61  bool stopped) const override;
62 
73  GKO_DEPRECATED("use single-parameter create")
74  static std::unique_ptr<Convergence> create(
75  std::shared_ptr<const Executor>,
76  const mask_type& enabled_events = Logger::criterion_events_mask |
77  Logger::iteration_complete_mask)
78  {
79  return std::unique_ptr<Convergence>(new Convergence(enabled_events));
80  }
81 
91  static std::unique_ptr<Convergence> create(
92  const mask_type& enabled_events = Logger::criterion_events_mask |
93  Logger::iteration_complete_mask)
94  {
95  return std::unique_ptr<Convergence>(new Convergence(enabled_events));
96  }
97 
103  bool has_converged() const noexcept { return convergence_status_; }
104 
108  void reset_convergence_status() { this->convergence_status_ = false; }
109 
115  const size_type& get_num_iterations() const noexcept
116  {
117  return num_iterations_;
118  }
119 
125  const LinOp* get_residual() const noexcept { return residual_.get(); }
126 
132  const LinOp* get_residual_norm() const noexcept
133  {
134  return residual_norm_.get();
135  }
136 
142  const LinOp* get_implicit_sq_resnorm() const noexcept
143  {
144  return implicit_sq_resnorm_.get();
145  }
146 
147 protected:
155  GKO_DEPRECATED("use single-parameter constructor")
156  explicit Convergence(
157  std::shared_ptr<const gko::Executor>,
158  const mask_type& enabled_events = Logger::criterion_events_mask |
159  Logger::iteration_complete_mask)
160  : Logger(enabled_events)
161  {}
162 
169  explicit Convergence(
170  const mask_type& enabled_events = Logger::criterion_events_mask |
171  Logger::iteration_complete_mask)
172  : Logger(enabled_events)
173  {}
174 
175 private:
176  mutable bool convergence_status_{false};
177  mutable size_type num_iterations_{};
178  mutable std::unique_ptr<LinOp> residual_{};
179  mutable std::unique_ptr<LinOp> residual_norm_{};
180  mutable std::unique_ptr<LinOp> implicit_sq_resnorm_{};
181 };
182 
183 
184 } // namespace log
185 } // namespace gko
186 
187 
188 #endif // GKO_PUBLIC_CORE_LOG_CONVERGENCE_HPP_
gko::uint8
std::uint8_t uint8
8-bit unsigned integral type.
Definition: types.hpp:137
gko::log::profile_event_category::solver
Solver events.
gko::log::Convergence::get_residual
const LinOp * get_residual() const noexcept
Returns the residual.
Definition: convergence.hpp:125
gko::log::Convergence::create
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:74
gko::LinOp
Definition: lin_op.hpp:118
gko::log::Convergence::get_num_iterations
const size_type & get_num_iterations() const noexcept
Returns the number of iterations.
Definition: convergence.hpp:115
gko::log::Convergence::reset_convergence_status
void reset_convergence_status()
Resets the convergence status to false.
Definition: convergence.hpp:108
gko::log::Convergence::get_implicit_sq_resnorm
const LinOp * get_implicit_sq_resnorm() const noexcept
Returns the implicit squared residual norm.
Definition: convergence.hpp:142
gko::log::profile_event_category::criterion
Stopping criterion events.
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:108
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::array
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition: array.hpp:27
gko::log::Logger::criterion_events_mask
static constexpr mask_type criterion_events_mask
Bitset Mask which activates all criterion events.
Definition: logger.hpp:636
gko::log::Convergence
Convergence is a Logger which logs data strictly from the criterion_check_completed event.
Definition: convergence.hpp:38
gko::log::Convergence::create
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:91
gko::log::Convergence::has_converged
bool has_converged() const noexcept
Returns true if the solver has converged.
Definition: convergence.hpp:103
gko::log::Logger
Definition: logger.hpp:76
gko::log::Convergence::on_criterion_check_completed
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.
gko::stop::Criterion
The Criterion class is a base class for all stopping criteria.
Definition: criterion.hpp:36
gko::Executor
The first step in using the Ginkgo library consists of creating an executor.
Definition: executor.hpp:616
gko::log::Convergence::get_residual_norm
const LinOp * get_residual_norm() const noexcept
Returns the residual norm.
Definition: convergence.hpp:132
gko::log::Convergence::on_iteration_complete
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.