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
combined.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_COMBINED_HPP_
34 #define GKO_CORE_STOP_COMBINED_HPP_
35 
36 
37 #include <ginkgo/core/stop/criterion.hpp>
38 
39 
40 #include <vector>
41 
42 
43 namespace gko {
44 namespace stop {
45 
46 
55 class Combined : public EnablePolymorphicObject<Combined, Criterion> {
56  friend class EnablePolymorphicObject<Combined, Criterion>;
57 
58 public:
60  {
70  std::vector<std::shared_ptr<const CriterionFactory>>
71  GKO_FACTORY_PARAMETER(criteria, nullptr);
72  };
73  GKO_ENABLE_CRITERION_FACTORY(Combined, parameters, Factory);
75 
76 protected:
77  bool check_impl(uint8 stoppingId, bool setFinalized,
78  Array<stopping_status> *stop_status, bool *one_changed,
79  const Updater &) override;
80 
81  explicit Combined(std::shared_ptr<const gko::Executor> exec)
82  : EnablePolymorphicObject<Combined, Criterion>(std::move(exec))
83  {}
84 
85  explicit Combined(const Factory *factory, const CriterionArgs &args)
86  : EnablePolymorphicObject<Combined, Criterion>(factory->get_executor()),
87  parameters_{factory->get_parameters()}
88  {
89  for (const auto &f : parameters_.criteria) {
90  criteria_.push_back(f->generate(args));
91  }
92  }
93 
94 private:
95  std::vector<std::unique_ptr<Criterion>> criteria_{};
96 };
97 
98 
115 template <typename FactoryContainer>
116 std::shared_ptr<const CriterionFactory> combine(FactoryContainer &&factories)
117 {
118  switch (factories.size()) {
119  case 0:
120  GKO_NOT_SUPPORTED(nullptr);
121  return nullptr;
122  case 1:
123  return factories[0];
124  default:
125  auto exec = factories[0]->get_executor();
126  return Combined::build()
127  .with_criteria(std::forward<FactoryContainer>(factories))
128  .on(exec);
129  }
130 }
131 
132 
133 } // namespace stop
134 } // namespace gko
135 
136 
137 #endif // GKO_CORE_STOP_COMBINED_HPP_
std::vector< std::shared_ptr< const CriterionFactory > > criteria
Criterion factories to combine.
Definition: combined.hpp:71
const parameters_type & get_parameters() const noexcept
Returns the parameters of the factory.
Definition: abstract_factory.hpp:175
The Combined class is used to combine multiple criterions together through an OR operation.
Definition: combined.hpp:55
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition: polymorphic_object.hpp:505
#define GKO_FACTORY_PARAMETER(_name,...)
Creates a factory parameter in the factory parameters structure.
Definition: lin_op.hpp:751
STL namespace.
The Ginkgo namespace.
Definition: abstract_factory.hpp:45
#define GKO_ENABLE_CRITERION_FACTORY(_criterion, _parameters_name, _factory_name)
This macro will generate a default implementation of a CriterionFactory for the Criterion subclass it...
Definition: criterion.hpp:280
std::uint8_t uint8
8-bit unsigned integral type.
Definition: types.hpp:123
An Array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the Arr...
Definition: array.hpp:63
#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: lin_op.hpp:611
Definition: combined.hpp:73
std::shared_ptr< const CriterionFactory > combine(FactoryContainer &&factories)
Combines multiple criterion factories into a single combined criterion factory.
Definition: combined.hpp:116
#define GKO_ENABLE_BUILD_METHOD(_factory_name)
Defines a build method for the factory, simplifying its construction by removing the repetitive typin...
Definition: lin_op.hpp:730
This struct is used to pass parameters to the EnableDefaultCriterionFactoryCriterionFactory::generate...
Definition: criterion.hpp:216