Ginkgo  Generated from pipelines/1330831941 branch based on master. Ginkgo version 1.8.0
A numerical linear algebra library targeting many-core architectures
record.hpp
1 // SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_LOG_RECORD_HPP_
6 #define GKO_PUBLIC_CORE_LOG_RECORD_HPP_
7 
8 
9 #include <deque>
10 #include <memory>
11 
12 
13 #include <ginkgo/core/log/logger.hpp>
14 #include <ginkgo/core/matrix/dense.hpp>
15 #include <ginkgo/core/stop/criterion.hpp>
16 
17 
18 namespace gko {
19 
25 namespace log {
26 
27 
32  std::unique_ptr<const LinOp> solver;
33  std::unique_ptr<const LinOp> right_hand_side;
34  std::unique_ptr<const LinOp> solution;
35  const size_type num_iterations;
36  std::unique_ptr<const LinOp> residual;
37  std::unique_ptr<const LinOp> residual_norm;
38  std::unique_ptr<const LinOp> implicit_sq_residual_norm;
40  bool all_stopped;
41 
42  iteration_complete_data(const LinOp* solver, const LinOp* right_hand_side,
43  const LinOp* solution,
44  const size_type num_iterations,
45  const LinOp* residual = nullptr,
46  const LinOp* residual_norm = nullptr,
47  const LinOp* implicit_sq_residual_norm = nullptr,
48  const gko::array<stopping_status>* status = nullptr,
49  bool all_stopped = false)
50  : num_iterations{num_iterations}, all_stopped(all_stopped)
51  {
52  this->solver = solver->clone();
53  this->solution = solution->clone();
54  if (right_hand_side != nullptr) {
55  this->right_hand_side = right_hand_side->clone();
56  }
57  if (residual != nullptr) {
58  this->residual = residual->clone();
59  }
60  if (residual_norm != nullptr) {
61  this->residual_norm = residual_norm->clone();
62  }
63  if (implicit_sq_residual_norm != nullptr) {
64  this->implicit_sq_residual_norm =
65  implicit_sq_residual_norm->clone();
66  }
67  if (status != nullptr) {
68  this->status = *status;
69  }
70  }
71 };
72 
73 
77 struct executor_data {
78  const Executor* exec;
79  const size_type num_bytes;
80  const uintptr location;
81 };
82 
83 
88  const Executor* exec;
89  const Operation* operation;
90 };
91 
92 
97  const Executor* exec;
98  std::unique_ptr<const PolymorphicObject> input;
99  std::unique_ptr<const PolymorphicObject> output; // optional
100 
101  polymorphic_object_data(const Executor* exec,
102  const PolymorphicObject* input,
103  const PolymorphicObject* output = nullptr)
104  : exec{exec}
105  {
106  this->input = input->clone();
107  if (output != nullptr) {
108  this->output = output->clone();
109  }
110  }
111 };
112 
113 
117 struct linop_data {
118  std::unique_ptr<const LinOp> A;
119  std::unique_ptr<const LinOp> alpha;
120  std::unique_ptr<const LinOp> b;
121  std::unique_ptr<const LinOp> beta;
122  std::unique_ptr<const LinOp> x;
123 
124  linop_data(const LinOp* A, const LinOp* alpha, const LinOp* b,
125  const LinOp* beta, const LinOp* x)
126  {
127  this->A = A->clone();
128  if (alpha != nullptr) {
129  this->alpha = alpha->clone();
130  }
131  this->b = b->clone();
132  if (beta != nullptr) {
133  this->beta = beta->clone();
134  }
135  this->x = x->clone();
136  }
137 };
138 
139 
144  const LinOpFactory* factory;
145  std::unique_ptr<const LinOp> input;
146  std::unique_ptr<const LinOp> output;
147 
148  linop_factory_data(const LinOpFactory* factory, const LinOp* input,
149  const LinOp* output)
150  : factory{factory}
151  {
152  this->input = input->clone();
153  if (output != nullptr) {
154  this->output = output->clone();
155  }
156  }
157 };
158 
159 
164  const stop::Criterion* criterion;
165  const size_type num_iterations;
166  std::unique_ptr<const LinOp> residual;
167  std::unique_ptr<const LinOp> residual_norm;
168  std::unique_ptr<const LinOp> solution;
169  const uint8 stopping_id;
170  const bool set_finalized;
171  const array<stopping_status>* status;
172  const bool oneChanged;
173  const bool converged;
174 
176  const size_type& num_iterations, const LinOp* residual,
177  const LinOp* residual_norm, const LinOp* solution,
178  const uint8 stopping_id, const bool set_finalized,
179  const array<stopping_status>* status = nullptr,
180  const bool oneChanged = false, const bool converged = false)
181  : criterion{criterion},
182  num_iterations{num_iterations},
183  residual{nullptr},
184  residual_norm{nullptr},
185  solution{nullptr},
186  stopping_id{stopping_id},
187  set_finalized{set_finalized},
188  status{status},
189  oneChanged{oneChanged},
190  converged{converged}
191  {
192  if (residual != nullptr) {
193  this->residual = std::unique_ptr<const LinOp>(residual->clone());
194  }
195  if (residual_norm != nullptr) {
196  this->residual_norm =
197  std::unique_ptr<const LinOp>(residual_norm->clone());
198  }
199  if (solution != nullptr) {
200  this->solution = std::unique_ptr<const LinOp>(solution->clone());
201  }
202  }
203 };
204 
205 
216 class Record : public Logger {
217 public:
221  struct logged_data {
222  std::deque<std::unique_ptr<executor_data>> allocation_started;
223  std::deque<std::unique_ptr<executor_data>> allocation_completed;
224  std::deque<std::unique_ptr<executor_data>> free_started;
225  std::deque<std::unique_ptr<executor_data>> free_completed;
226  std::deque<std::unique_ptr<std::tuple<executor_data, executor_data>>>
227  copy_started;
228  std::deque<std::unique_ptr<std::tuple<executor_data, executor_data>>>
229  copy_completed;
230 
231  std::deque<std::unique_ptr<operation_data>> operation_launched;
232  std::deque<std::unique_ptr<operation_data>> operation_completed;
233 
234  std::deque<std::unique_ptr<polymorphic_object_data>>
235  polymorphic_object_create_started;
236  std::deque<std::unique_ptr<polymorphic_object_data>>
237  polymorphic_object_create_completed;
238  std::deque<std::unique_ptr<polymorphic_object_data>>
239  polymorphic_object_copy_started;
240  std::deque<std::unique_ptr<polymorphic_object_data>>
241  polymorphic_object_copy_completed;
242  std::deque<std::unique_ptr<polymorphic_object_data>>
243  polymorphic_object_move_started;
244  std::deque<std::unique_ptr<polymorphic_object_data>>
245  polymorphic_object_move_completed;
246  std::deque<std::unique_ptr<polymorphic_object_data>>
247  polymorphic_object_deleted;
248 
249  std::deque<std::unique_ptr<linop_data>> linop_apply_started;
250  std::deque<std::unique_ptr<linop_data>> linop_apply_completed;
251  std::deque<std::unique_ptr<linop_data>> linop_advanced_apply_started;
252  std::deque<std::unique_ptr<linop_data>> linop_advanced_apply_completed;
253  std::deque<std::unique_ptr<linop_factory_data>>
254  linop_factory_generate_started;
255  std::deque<std::unique_ptr<linop_factory_data>>
256  linop_factory_generate_completed;
257 
258  std::deque<std::unique_ptr<criterion_data>> criterion_check_started;
259  std::deque<std::unique_ptr<criterion_data>> criterion_check_completed;
260 
261  std::deque<std::unique_ptr<iteration_complete_data>>
262  iteration_completed;
263  };
264 
265  /* Executor events */
266  void on_allocation_started(const Executor* exec,
267  const size_type& num_bytes) const override;
268 
269  void on_allocation_completed(const Executor* exec,
270  const size_type& num_bytes,
271  const uintptr& location) const override;
272 
273  void on_free_started(const Executor* exec,
274  const uintptr& location) const override;
275 
276  void on_free_completed(const Executor* exec,
277  const uintptr& location) const override;
278 
279  void on_copy_started(const Executor* from, const Executor* to,
280  const uintptr& location_from,
281  const uintptr& location_to,
282  const size_type& num_bytes) const override;
283 
284  void on_copy_completed(const Executor* from, const Executor* to,
285  const uintptr& location_from,
286  const uintptr& location_to,
287  const size_type& num_bytes) const override;
288 
289  /* Operation events */
290  void on_operation_launched(const Executor* exec,
291  const Operation* operation) const override;
292 
293  void on_operation_completed(const Executor* exec,
294  const Operation* operation) const override;
295 
296  /* PolymorphicObject events */
298  const Executor* exec, const PolymorphicObject* po) const override;
299 
301  const Executor* exec, const PolymorphicObject* input,
302  const PolymorphicObject* output) const override;
303 
305  const Executor* exec, const PolymorphicObject* from,
306  const PolymorphicObject* to) const override;
307 
309  const Executor* exec, const PolymorphicObject* from,
310  const PolymorphicObject* to) const override;
311 
313  const Executor* exec, const PolymorphicObject* from,
314  const PolymorphicObject* to) const override;
315 
317  const Executor* exec, const PolymorphicObject* from,
318  const PolymorphicObject* to) const override;
319 
321  const Executor* exec, const PolymorphicObject* po) const override;
322 
323  /* LinOp events */
324  void on_linop_apply_started(const LinOp* A, const LinOp* b,
325  const LinOp* x) const override;
326 
327  void on_linop_apply_completed(const LinOp* A, const LinOp* b,
328  const LinOp* x) const override;
329 
330  void on_linop_advanced_apply_started(const LinOp* A, const LinOp* alpha,
331  const LinOp* b, const LinOp* beta,
332  const LinOp* x) const override;
333 
334  void on_linop_advanced_apply_completed(const LinOp* A, const LinOp* alpha,
335  const LinOp* b, const LinOp* beta,
336  const LinOp* x) const override;
337 
338  /* LinOpFactory events */
340  const LinOp* input) const override;
341 
343  const LinOpFactory* factory, const LinOp* input,
344  const LinOp* output) const override;
345 
346  /* Criterion events */
348  const size_type& num_iterations,
349  const LinOp* residual,
350  const LinOp* residual_norm,
351  const LinOp* solution,
352  const uint8& stopping_id,
353  const bool& set_finalized) const override;
354 
356  const stop::Criterion* criterion, const size_type& num_iterations,
357  const LinOp* residual, const LinOp* residual_norm,
358  const LinOp* implicit_residual_norm_sq, const LinOp* solution,
359  const uint8& stopping_id, const bool& set_finalized,
360  const array<stopping_status>* status, const bool& one_changed,
361  const bool& all_converged) const override;
362 
364  const stop::Criterion* criterion, const size_type& num_iterations,
365  const LinOp* residual, const LinOp* residual_norm,
366  const LinOp* solution, const uint8& stopping_id,
367  const bool& set_finalized, const array<stopping_status>* status,
368  const bool& one_changed, const bool& all_converged) const override;
369 
370  /* Internal solver events */
372  const LinOp* solver, const LinOp* right_hand_side, const LinOp* x,
373  const size_type& num_iterations, const LinOp* residual,
374  const LinOp* residual_norm, const LinOp* implicit_resnorm_sq,
375  const array<stopping_status>* status, bool stopped) const override;
376 
377  GKO_DEPRECATED(
378  "Please use the version with the additional stopping "
379  "information.")
380  void on_iteration_complete(const LinOp* solver,
381  const size_type& num_iterations,
382  const LinOp* residual, const LinOp* solution,
383  const LinOp* residual_norm) const override;
384 
385  GKO_DEPRECATED(
386  "Please use the version with the additional stopping "
387  "information.")
389  const LinOp* solver, const size_type& num_iterations,
390  const LinOp* residual, const LinOp* solution,
391  const LinOp* residual_norm,
392  const LinOp* implicit_sq_residual_norm) const override;
393 
408  GKO_DEPRECATED("use two-parameter create")
409  static std::unique_ptr<Record> create(
410  std::shared_ptr<const Executor> exec,
411  const mask_type& enabled_events = Logger::all_events_mask,
412  size_type max_storage = 1)
413  {
414  return std::unique_ptr<Record>(new Record(enabled_events, max_storage));
415  }
416 
431  static std::unique_ptr<Record> create(
432  const mask_type& enabled_events = Logger::all_events_mask,
433  size_type max_storage = 1)
434  {
435  return std::unique_ptr<Record>(new Record(enabled_events, max_storage));
436  }
437 
443  const logged_data& get() const noexcept { return data_; }
444 
448  logged_data& get() noexcept { return data_; }
449 
450 protected:
462  GKO_DEPRECATED("use two-parameter constructor")
463  explicit Record(std::shared_ptr<const gko::Executor> exec,
464  const mask_type& enabled_events = Logger::all_events_mask,
465  size_type max_storage = 0)
466  : Record(enabled_events, max_storage)
467  {}
468 
479  explicit Record(const mask_type& enabled_events = Logger::all_events_mask,
480  size_type max_storage = 0)
481  : Logger(enabled_events), max_storage_{max_storage}
482  {}
483 
492  template <typename deque_type>
493  void append_deque(std::deque<deque_type>& deque, deque_type object) const
494  {
495  if (this->max_storage_ && deque.size() == this->max_storage_) {
496  deque.pop_front();
497  }
498  deque.push_back(std::move(object));
499  }
500 
501 private:
502  mutable logged_data data_{};
503  size_type max_storage_{};
504 };
505 
506 
507 } // namespace log
508 } // namespace gko
509 
510 
511 #endif // GKO_PUBLIC_CORE_LOG_RECORD_HPP_
gko::uint8
std::uint8_t uint8
8-bit unsigned integral type.
Definition: types.hpp:137
gko::log::profile_event_category::solver
Solver events.
gko::log::Record::on_free_completed
void on_free_completed(const Executor *exec, const uintptr &location) const override
Executor's free completed event.
gko::log::Record::create
static std::unique_ptr< Record > create(const mask_type &enabled_events=Logger::all_events_mask, size_type max_storage=1)
Creates a Record logger.
Definition: record.hpp:431
gko::LinOp
Definition: lin_op.hpp:118
gko::log::Record::on_iteration_complete
void on_iteration_complete(const LinOp *solver, const LinOp *right_hand_side, const LinOp *x, const size_type &num_iterations, const LinOp *residual, const LinOp *residual_norm, const LinOp *implicit_resnorm_sq, const array< stopping_status > *status, bool stopped) const override
Register the iteration_complete event which logs every completed iterations.
gko::log::linop_data
Struct representing LinOp related data.
Definition: record.hpp:117
gko::log::polymorphic_object_data
Struct representing PolymorphicObject related data.
Definition: record.hpp:96
gko::log::Record::create
static std::unique_ptr< Record > create(std::shared_ptr< const Executor > exec, const mask_type &enabled_events=Logger::all_events_mask, size_type max_storage=1)
Creates a Record logger.
Definition: record.hpp:409
gko::PolymorphicObject
A PolymorphicObject is the abstract base for all "heavy" objects in Ginkgo that behave polymorphicall...
Definition: polymorphic_object.hpp:44
gko::log::profile_event_category::factory
LinOpFactory events.
gko::log::Record::on_polymorphic_object_deleted
void on_polymorphic_object_deleted(const Executor *exec, const PolymorphicObject *po) const override
PolymorphicObject's deleted event.
gko::log::profile_event_category::criterion
Stopping criterion events.
gko::uintptr
std::uintptr_t uintptr
Unsigned integer type capable of holding a pointer to void.
Definition: types.hpp:160
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:108
gko::log::Record::get
logged_data & get() noexcept
Definition: record.hpp:448
gko::log::operation_data
Struct representing Operator related data.
Definition: record.hpp:87
gko::log::Record::on_copy_started
void on_copy_started(const Executor *from, const Executor *to, const uintptr &location_from, const uintptr &location_to, const size_type &num_bytes) const override
Executor's copy started event.
gko::log::Record::on_copy_completed
void on_copy_completed(const Executor *from, const Executor *to, const uintptr &location_from, const uintptr &location_to, const size_type &num_bytes) const override
Executor's copy completed event.
gko::log::Record::on_polymorphic_object_create_started
void on_polymorphic_object_create_started(const Executor *exec, const PolymorphicObject *po) const override
PolymorphicObject's create started event.
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::log::Record::on_operation_launched
void on_operation_launched(const Executor *exec, const Operation *operation) const override
Executor's operation launched event (method run).
gko::PolymorphicObject::clone
std::unique_ptr< PolymorphicObject > clone(std::shared_ptr< const Executor > exec) const
Creates a clone of the object.
Definition: polymorphic_object.hpp:99
gko::log::Record::on_criterion_check_completed
void on_criterion_check_completed(const stop::Criterion *criterion, const size_type &num_iterations, const LinOp *residual, const LinOp *residual_norm, const LinOp *implicit_residual_norm_sq, const LinOp *solution, const uint8 &stopping_id, const bool &set_finalized, const array< stopping_status > *status, const bool &one_changed, const bool &all_converged) const override
stop::Criterion's check completed event.
gko::log::criterion_data
Struct representing Criterion related data.
Definition: record.hpp:163
gko::array
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition: array.hpp:27
gko::version
This structure is used to represent versions of various Ginkgo modules.
Definition: version.hpp:26
gko::log::Logger::all_events_mask
static constexpr mask_type all_events_mask
Bitset Mask which activates all events.
Definition: logger.hpp:89
gko::log::Record
Record is a Logger which logs every event to an object.
Definition: record.hpp:216
gko::log::iteration_complete_data
Struct representing iteration complete related data.
Definition: record.hpp:31
gko::log::Record::logged_data
Struct storing the actually logged data.
Definition: record.hpp:221
gko::log::Record::on_linop_advanced_apply_completed
void on_linop_advanced_apply_completed(const LinOp *A, const LinOp *alpha, const LinOp *b, const LinOp *beta, const LinOp *x) const override
LinOp's advanced apply completed event.
gko::log::Record::on_linop_factory_generate_completed
void on_linop_factory_generate_completed(const LinOpFactory *factory, const LinOp *input, const LinOp *output) const override
LinOp Factory's generate completed event.
gko::log::Logger
Definition: logger.hpp:76
gko::log::Record::on_polymorphic_object_copy_completed
void on_polymorphic_object_copy_completed(const Executor *exec, const PolymorphicObject *from, const PolymorphicObject *to) const override
PolymorphicObject's copy completed event.
gko::log::Record::on_linop_advanced_apply_started
void on_linop_advanced_apply_started(const LinOp *A, const LinOp *alpha, const LinOp *b, const LinOp *beta, const LinOp *x) const override
LinOp's advanced apply started event.
gko::log::executor_data
Struct representing Executor related data.
Definition: record.hpp:77
gko::stop::Criterion
The Criterion class is a base class for all stopping criteria.
Definition: criterion.hpp:36
gko::log::Record::on_linop_apply_started
void on_linop_apply_started(const LinOp *A, const LinOp *b, const LinOp *x) const override
LinOp's apply started event.
gko::log::Record::on_allocation_completed
void on_allocation_completed(const Executor *exec, const size_type &num_bytes, const uintptr &location) const override
Executor's allocation completed event.
gko::log::Record::on_free_started
void on_free_started(const Executor *exec, const uintptr &location) const override
Executor's free started event.
gko::log::Record::on_polymorphic_object_create_completed
void on_polymorphic_object_create_completed(const Executor *exec, const PolymorphicObject *input, const PolymorphicObject *output) const override
PolymorphicObject's create completed event.
gko::log::Record::on_polymorphic_object_move_completed
void on_polymorphic_object_move_completed(const Executor *exec, const PolymorphicObject *from, const PolymorphicObject *to) const override
PolymorphicObject's move completed event.
gko::log::profile_event_category::operation
Kernel execution and data movement.
gko::Executor
The first step in using the Ginkgo library consists of creating an executor.
Definition: executor.hpp:616
gko::log::Record::on_operation_completed
void on_operation_completed(const Executor *exec, const Operation *operation) const override
Executor's operation completed event (method run).
gko::log::Record::on_criterion_check_started
void on_criterion_check_started(const stop::Criterion *criterion, const size_type &num_iterations, const LinOp *residual, const LinOp *residual_norm, const LinOp *solution, const uint8 &stopping_id, const bool &set_finalized) const override
stop::Criterion's check started event.
gko::log::linop_factory_data
Struct representing LinOp factory related data.
Definition: record.hpp:143
gko::log::Record::on_linop_factory_generate_started
void on_linop_factory_generate_started(const LinOpFactory *factory, const LinOp *input) const override
LinOp Factory's generate started event.
gko::log::Record::on_polymorphic_object_copy_started
void on_polymorphic_object_copy_started(const Executor *exec, const PolymorphicObject *from, const PolymorphicObject *to) const override
PolymorphicObject's copy started event.
gko::LinOpFactory
A LinOpFactory represents a higher order mapping which transforms one linear operator into another.
Definition: lin_op.hpp:385
gko::log::Record::on_linop_apply_completed
void on_linop_apply_completed(const LinOp *A, const LinOp *b, const LinOp *x) const override
LinOp's apply completed event.
gko::Operation
Operations can be used to define functionalities whose implementations differ among devices.
Definition: executor.hpp:259
gko::log::Record::on_polymorphic_object_move_started
void on_polymorphic_object_move_started(const Executor *exec, const PolymorphicObject *from, const PolymorphicObject *to) const override
PolymorphicObject's move started event.
gko::log::Record::on_allocation_started
void on_allocation_started(const Executor *exec, const size_type &num_bytes) const override
Executor's allocation started event.
gko::log::Record::get
const logged_data & get() const noexcept
Returns the logged data.
Definition: record.hpp:443