Ginkgo  Generated from tags/v1.0.0^0 branch based on master. Ginkgo version 1.0.0
A numerical linear algebra library targeting many-core architectures
criterion.hpp
1 /*******************************<GINKGO LICENSE>******************************
2 Copyright (c) 2017-2019, the Ginkgo authors
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 ******************************<GINKGO LICENSE>*******************************/
32 
33 #ifndef GKO_CORE_STOP_CRITERION_HPP_
34 #define GKO_CORE_STOP_CRITERION_HPP_
35 
36 
37 #include <ginkgo/core/base/abstract_factory.hpp>
38 #include <ginkgo/core/base/array.hpp>
39 #include <ginkgo/core/base/lin_op.hpp>
40 #include <ginkgo/core/base/polymorphic_object.hpp>
41 #include <ginkgo/core/base/utils.hpp>
42 #include <ginkgo/core/log/logger.hpp>
43 #include <ginkgo/core/stop/stopping_status.hpp>
44 
45 
46 namespace gko {
52 namespace stop {
53 
54 
63 class Criterion : public EnableAbstractPolymorphicObject<Criterion> {
64 public:
79  class Updater {
80  friend class Criterion;
81 
82  public:
88  Updater(const Updater &) = delete;
89  Updater(Updater &&) = delete;
90  Updater &operator=(const Updater &) = delete;
91  Updater &operator=(Updater &&) = delete;
92 
97  bool check(uint8 stoppingId, bool setFinalized,
98  Array<stopping_status> *stop_status, bool *one_changed) const
99  {
100  auto converged = parent_->check(stoppingId, setFinalized,
101  stop_status, one_changed, *this);
102  return converged;
103  }
104 
108 #define GKO_UPDATER_REGISTER_PARAMETER(_type, _name) \
109  const Updater &_name(_type const &value) const \
110  { \
111  _name##_ = value; \
112  return *this; \
113  } \
114  mutable _type _name##_ {}
115 
116  GKO_UPDATER_REGISTER_PARAMETER(size_type, num_iterations);
117  GKO_UPDATER_REGISTER_PARAMETER(const LinOp *, residual);
118  GKO_UPDATER_REGISTER_PARAMETER(const LinOp *, residual_norm);
119  GKO_UPDATER_REGISTER_PARAMETER(const LinOp *, solution);
120 
121 #undef GKO_UPDATER_REGISTER_PARAMETER
122 
123  private:
124  Updater(Criterion *parent) : parent_{parent} {}
125 
126  Criterion *parent_;
127  };
128 
134  Updater update() { return {this}; }
135 
149  bool check(uint8 stoppingId, bool setFinalized,
150  Array<stopping_status> *stop_status, bool *one_changed,
151  const Updater &updater)
152  {
153  this->template log<log::Logger::criterion_check_started>(
154  this, updater.num_iterations_, updater.residual_,
155  updater.residual_norm_, updater.solution_, stoppingId,
156  setFinalized);
157  auto all_converged = this->check_impl(
158  stoppingId, setFinalized, stop_status, one_changed, updater);
159  this->template log<log::Logger::criterion_check_completed>(
160  this, updater.num_iterations_, updater.residual_,
161  updater.residual_norm_, updater.solution_, stoppingId, setFinalized,
162  stop_status, *one_changed, all_converged);
163  return all_converged;
164  }
165 
166 protected:
183  virtual bool check_impl(uint8 stoppingId, bool setFinalized,
184  Array<stopping_status> *stop_status,
185  bool *one_changed, const Updater &updater) = 0;
186 
197  void set_all_statuses(uint8 stoppingId, bool setFinalized,
198  Array<stopping_status> *stop_status);
199 
200  explicit Criterion(std::shared_ptr<const gko::Executor> exec)
202  {}
203 };
204 
205 
217  std::shared_ptr<const LinOp> system_matrix;
218  std::shared_ptr<const LinOp> b;
219  const LinOp *x;
220  const LinOp *initial_residual;
221 
222 
223  CriterionArgs(std::shared_ptr<const LinOp> system_matrix,
224  std::shared_ptr<const LinOp> b, const LinOp *x,
225  const LinOp *initial_residual = nullptr)
226  : system_matrix{system_matrix},
227  b{b},
228  x{x},
229  initial_residual{initial_residual}
230  {}
231 };
232 
233 
238 
239 
255 template <typename ConcreteFactory, typename ConcreteCriterion,
256  typename ParametersType, typename PolymorphicBase = CriterionFactory>
258  EnableDefaultFactory<ConcreteFactory, ConcreteCriterion, ParametersType,
259  PolymorphicBase>;
260 
261 
280 #define GKO_ENABLE_CRITERION_FACTORY(_criterion, _parameters_name, \
281  _factory_name) \
282 public: \
283  const _parameters_name##_type &get_##_parameters_name() const \
284  { \
285  return _parameters_name##_; \
286  } \
287  \
288  class _factory_name \
289  : public ::gko::stop::EnableDefaultCriterionFactory< \
290  _factory_name, _criterion, _parameters_name##_type> { \
291  friend class ::gko::EnablePolymorphicObject< \
292  _factory_name, ::gko::stop::CriterionFactory>; \
293  friend class ::gko::enable_parameters_type<_parameters_name##_type, \
294  _factory_name>; \
295  using ::gko::stop::EnableDefaultCriterionFactory< \
296  _factory_name, _criterion, \
297  _parameters_name##_type>::EnableDefaultCriterionFactory; \
298  }; \
299  friend ::gko::stop::EnableDefaultCriterionFactory< \
300  _factory_name, _criterion, _parameters_name##_type>; \
301  \
302 private: \
303  _parameters_name##_type _parameters_name##_; \
304  \
305 public: \
306  static_assert(true, \
307  "This assert is used to counter the false positive extra " \
308  "semi-colon warnings")
309 
310 
311 } // namespace stop
312 } // namespace gko
313 
314 
315 #endif // GKO_CORE_STOP_CRITERION_HPP_
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition: polymorphic_object.hpp:285
bool check(uint8 stoppingId, bool setFinalized, Array< stopping_status > *stop_status, bool *one_changed) const
Calls the parent Criterion object&#39;s check method.
Definition: criterion.hpp:97
bool check(uint8 stoppingId, bool setFinalized, Array< stopping_status > *stop_status, bool *one_changed, const Updater &updater)
This checks whether convergence was reached for a certain criterion.
Definition: criterion.hpp:149
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:94
The Ginkgo namespace.
Definition: abstract_factory.hpp:45
Updater(const Updater &)=delete
Prevent copying and moving the object This is to enforce the use of argument passing and calling chec...
This mixin provides a default implementation of a concrete factory.
Definition: abstract_factory.hpp:148
Definition: lin_op.hpp:134
std::uint8_t uint8
8-bit unsigned integral type.
Definition: types.hpp:123
The Updater class serves for convenient argument passing to the Criterion&#39;s check function...
Definition: criterion.hpp:79
The AbstractFactory is a generic interface template that enables easy implementation of the abstract ...
Definition: abstract_factory.hpp:70
An Array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the Arr...
Definition: array.hpp:63
The Criterion class is a base class for all stopping criteria.
Definition: criterion.hpp:63
Updater update()
Returns the updater object.
Definition: criterion.hpp:134
This struct is used to pass parameters to the EnableDefaultCriterionFactoryCriterionFactory::generate...
Definition: criterion.hpp:216