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
combined.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_STOP_COMBINED_HPP_
34#define GKO_PUBLIC_CORE_STOP_COMBINED_HPP_
35
36
37#include <vector>
38
39
40#include <ginkgo/core/stop/criterion.hpp>
41
42
43namespace gko {
44namespace stop {
45
46
55class Combined : public EnablePolymorphicObject<Combined, Criterion> {
57
58public:
59 class Factory;
60
62 : public ::gko::enable_parameters_type<parameters_type, Factory> {
72 std::vector<std::shared_ptr<const CriterionFactory>>
73 GKO_DEFERRED_FACTORY_VECTOR_PARAMETER(criteria);
74 };
75
76 class Factory
77 : public ::gko::stop::EnableDefaultCriterionFactory<Factory, Combined,
78 parameters_type> {
79 friend class ::gko::EnablePolymorphicObject<
81 friend class ::gko::enable_parameters_type<parameters_type, Factory>;
82
83 using Base =
85 parameters_type>;
86
87 public:
88 explicit Factory(std::shared_ptr<const ::gko::Executor> exec);
89 explicit Factory(std::shared_ptr<const ::gko::Executor> exec,
90 const parameters_type& parameters);
91
92 Factory(const Factory& other) = default;
93 Factory(Factory&& other) = default;
94
95 Factory& operator=(const Factory& other);
96 };
97
98 static parameters_type build() { return {}; }
99
100 const parameters_type& get_parameters() const { return parameters_; }
101
102protected:
103 bool check_impl(uint8 stoppingId, bool setFinalized,
105 const Updater&) override;
106
107 explicit Combined(std::shared_ptr<const gko::Executor> exec);
108
109 explicit Combined(const Factory* factory, const CriterionArgs& args);
110
111private:
112 friend ::gko::stop::EnableDefaultCriterionFactory<Factory, Combined,
113 parameters_type>;
114
115 parameters_type parameters_;
116
117 std::vector<std::unique_ptr<Criterion>> criteria_{};
118};
119
120
137template <typename FactoryContainer>
138std::shared_ptr<const CriterionFactory> combine(FactoryContainer&& factories)
139{
140 switch (factories.size()) {
141 case 0:
142 GKO_NOT_SUPPORTED(nullptr);
143 case 1:
144 if (factories[0] == nullptr) {
145 GKO_NOT_SUPPORTED(nullptr);
146 }
147 return factories[0];
148 default:
149 if (factories[0] == nullptr) {
150 // first factory must be valid to capture executor
151 GKO_NOT_SUPPORTED(nullptr);
152 } else {
153 auto exec = factories[0]->get_executor();
154 return Combined::build()
155 .with_criteria(std::forward<FactoryContainer>(factories))
156 .on(exec);
157 }
158 }
159}
160
161
162} // namespace stop
163} // namespace gko
164
165
166#endif // GKO_PUBLIC_CORE_STOP_COMBINED_HPP_
The AbstractFactory is a generic interface template that enables easy implementation of the abstract ...
Definition abstract_factory.hpp:75
This mixin provides a default implementation of a concrete factory.
Definition abstract_factory.hpp:154
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:691
The enable_parameters_type mixin is used to create a base implementation of the factory parameters st...
Definition abstract_factory.hpp:239
Definition combined.hpp:78
The Combined class is used to combine multiple criterions together through an OR operation.
Definition combined.hpp:55
The Criterion class is a base class for all stopping criteria.
Definition criterion.hpp:64
std::shared_ptr< const CriterionFactory > combine(FactoryContainer &&factories)
Combines multiple criterion factories into a single combined criterion factory.
Definition combined.hpp:138
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
Definition combined.hpp:62
std::vector< std::shared_ptr< const CriterionFactory > > criteria
Criterion factories to combine.
Definition combined.hpp:73