Ginkgo Generated from develop branch based on develop. Ginkgo version 1.8.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
batch_logger.hpp
1// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#ifndef GKO_PUBLIC_CORE_LOG_BATCH_LOGGER_HPP_
6#define GKO_PUBLIC_CORE_LOG_BATCH_LOGGER_HPP_
7
8
9#include <memory>
10
11
12#include <ginkgo/core/base/batch_multi_vector.hpp>
13#include <ginkgo/core/base/types.hpp>
14#include <ginkgo/core/log/logger.hpp>
15
16
17namespace gko {
18namespace batch {
24namespace log {
25namespace detail {
26
27
33template <typename ValueType>
34struct log_data final {
35 using real_type = remove_complex<ValueType>;
36
37 log_data(std::shared_ptr<const Executor> exec, size_type num_batch_items)
38 : res_norms(exec), iter_counts(exec)
39 {
41 num_batch_items * (sizeof(real_type) + sizeof(int));
42 if (num_batch_items > 0) {
43 iter_counts.resize_and_reset(num_batch_items);
44 res_norms.resize_and_reset(num_batch_items);
45 } else {
46 GKO_INVALID_STATE("Invalid num batch items passed in");
47 }
48 }
49
50 log_data(std::shared_ptr<const Executor> exec, size_type num_batch_items,
51 array<unsigned char>& workspace)
52 : res_norms(exec), iter_counts(exec)
53 {
55 num_batch_items * (sizeof(real_type) + sizeof(int));
56 if (num_batch_items > 0 && !workspace.is_owning() &&
57 workspace.get_size() >= workspace_size) {
58 iter_counts =
60 reinterpret_cast<int*>(workspace.get_data()));
61 res_norms = array<real_type>::view(
62 exec, num_batch_items,
63 reinterpret_cast<real_type*>(workspace.get_data() +
64 (sizeof(int) * num_batch_items)));
65 } else {
66 GKO_INVALID_STATE("invalid workspace or num batch items passed in");
67 }
68 }
69
73 array<real_type> res_norms;
74
78 array<int> iter_counts;
79};
80
81
82} // namespace detail
83
84
97template <typename ValueType = default_precision>
99public:
100 using real_type = remove_complex<ValueType>;
101 using mask_type = gko::log::Logger::mask_type;
102
103 void on_batch_solver_completed(
105 const array<real_type>& residual_norm) const override;
106
119 static std::unique_ptr<BatchConvergence> create(
120 const mask_type& enabled_events =
121 gko::log::Logger::batch_solver_completed_mask)
122 {
123 return std::unique_ptr<BatchConvergence>(
125 }
126
131 {
132 return iteration_count_;
133 }
134
139 {
140 return residual_norm_;
141 }
142
143protected:
144 explicit BatchConvergence(const mask_type& enabled_events =
145 gko::log::Logger::batch_solver_completed_mask)
146 : gko::log::Logger(enabled_events)
147 {}
148
149private:
150 mutable array<int> iteration_count_{};
151 mutable array<real_type> residual_norm_{};
152};
153
154
155} // namespace log
156} // namespace batch
157} // namespace gko
158
159
160#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:167
value_type * get_data() noexcept
Returns a pointer to the block of memory used to store the elements of the array.
Definition array.hpp:674
bool is_owning()
Tells whether this array owns its data or not.
Definition array.hpp:724
static array view(std::shared_ptr< const Executor > exec, size_type size, value_type *data)
Creates an array from existing memory.
Definition array.hpp:366
size_type get_size() const noexcept
Returns the number of elements in the array.
Definition array.hpp:657
Logs the final residuals and iteration counts for a batch solver.
Definition batch_logger.hpp:98
const array< real_type > & get_residual_norm() const noexcept
Definition batch_logger.hpp:138
const array< int > & get_num_iterations() const noexcept
Definition batch_logger.hpp:130
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:119
Definition logger.hpp:76
The Ginkgo namespace.
Definition abstract_factory.hpp:20
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:775
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:326
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:92