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
batch_logger.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_BATCH_LOGGER_HPP_
34#define GKO_PUBLIC_CORE_LOG_BATCH_LOGGER_HPP_
35
36
37#include <memory>
38
39
40#include <ginkgo/core/base/batch_multi_vector.hpp>
41#include <ginkgo/core/base/types.hpp>
42#include <ginkgo/core/log/logger.hpp>
43
44
45namespace gko {
46namespace batch {
52namespace log {
53namespace detail {
54
55
61template <typename ValueType>
62struct log_data final {
63 using real_type = remove_complex<ValueType>;
64
65 log_data(std::shared_ptr<const Executor> exec, size_type num_batch_items)
66 : res_norms(exec), iter_counts(exec)
67 {
69 num_batch_items * (sizeof(real_type) + sizeof(int));
70 if (num_batch_items > 0) {
71 iter_counts.resize_and_reset(num_batch_items);
72 res_norms.resize_and_reset(num_batch_items);
73 } else {
74 GKO_INVALID_STATE("Invalid num batch items passed in");
75 }
76 }
77
78 log_data(std::shared_ptr<const Executor> exec, size_type num_batch_items,
79 array<unsigned char>& workspace)
80 : res_norms(exec), iter_counts(exec)
81 {
83 num_batch_items * (sizeof(real_type) + sizeof(int));
84 if (num_batch_items > 0 && !workspace.is_owning() &&
85 workspace.get_num_elems() >= workspace_size) {
86 iter_counts =
88 reinterpret_cast<int*>(workspace.get_data()));
89 res_norms = array<real_type>::view(
90 exec, num_batch_items,
91 reinterpret_cast<real_type*>(workspace.get_data() +
92 (sizeof(int) * num_batch_items)));
93 } else {
94 GKO_INVALID_STATE("invalid workspace or num batch items passed in");
95 }
96 }
97
101 array<real_type> res_norms;
102
106 array<int> iter_counts;
107};
108
109
110} // namespace detail
111
112
125template <typename ValueType = default_precision>
127public:
128 using real_type = remove_complex<ValueType>;
129 using mask_type = gko::log::Logger::mask_type;
130
131 void on_batch_solver_completed(
133 const array<real_type>& residual_norm) const override;
134
147 static std::unique_ptr<BatchConvergence> create(
148 const mask_type& enabled_events =
149 gko::log::Logger::batch_solver_completed_mask)
150 {
151 return std::unique_ptr<BatchConvergence>(
153 }
154
159 {
160 return iteration_count_;
161 }
162
167 {
168 return residual_norm_;
169 }
170
171protected:
172 explicit BatchConvergence(const mask_type& enabled_events =
173 gko::log::Logger::batch_solver_completed_mask)
174 : gko::log::Logger(enabled_events)
175 {}
176
177private:
178 mutable array<int> iteration_count_{};
179 mutable array<real_type> residual_norm_{};
180};
181
182
183} // namespace log
184} // namespace batch
185} // namespace gko
186
187
188#endif // GKO_PUBLIC_CORE_LOG_BATCH_LOGGER_HPP_
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:187
value_type * get_data() noexcept
Returns a pointer to the block of memory used to store the elements of the array.
Definition array.hpp:646
bool is_owning()
Tells whether this array owns its data or not.
Definition array.hpp:696
size_type get_num_elems() const noexcept
Returns the number of elements in the array.
Definition array.hpp:637
static array view(std::shared_ptr< const Executor > exec, size_type num_elems, value_type *data)
Creates an array from existing memory.
Definition array.hpp:388
Logs the final residuals and iteration counts for a batch solver.
Definition batch_logger.hpp:126
const array< real_type > & get_residual_norm() const noexcept
Definition batch_logger.hpp:166
const array< int > & get_num_iterations() const noexcept
Definition batch_logger.hpp:158
static std::unique_ptr< BatchConvergence > create(const mask_type &enabled_events=gko::log::Logger::batch_solver_completed_mask)
Creates a convergence logger.
Definition batch_logger.hpp:147
Definition logger.hpp:104
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
typename detail::remove_complex_s< T >::type remove_complex
Obtain the type which removed the complex of complex/scalar type or the template parameter of class b...
Definition math.hpp:354
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:120