Ginkgo Generated from branch based on master. Ginkgo version 1.8.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
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
18namespace gko {
19
25namespace 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
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
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
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)
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)
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
216class Record : public Logger {
217public:
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 */
267 const size_type& num_bytes) const override;
268
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 */
291 const Operation* operation) const override;
292
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.")
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
450protected:
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
501private:
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_
The first step in using the Ginkgo library consists of creating an executor.
Definition executor.hpp:616
A LinOpFactory represents a higher order mapping which transforms one linear operator into another.
Definition lin_op.hpp:386
Definition lin_op.hpp:118
Operations can be used to define functionalities whose implementations differ among devices.
Definition executor.hpp:259
A PolymorphicObject is the abstract base for all "heavy" objects in Ginkgo that behave polymorphicall...
Definition polymorphic_object.hpp:44
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition logger.hpp:25
Definition logger.hpp:76
static constexpr mask_type all_events_mask
Bitset Mask which activates all events.
Definition logger.hpp:89
Record is a Logger which logs every event to an object.
Definition record.hpp:216
void on_polymorphic_object_deleted(const Executor *exec, const PolymorphicObject *po) const override
PolymorphicObject's deleted event.
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.
void on_allocation_completed(const Executor *exec, const size_type &num_bytes, const uintptr &location) const override
Executor's allocation completed event.
void on_polymorphic_object_move_started(const Executor *exec, const PolymorphicObject *from, const PolymorphicObject *to) const override
PolymorphicObject's move started event.
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.
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.
void on_polymorphic_object_create_started(const Executor *exec, const PolymorphicObject *po) const override
PolymorphicObject's create started event.
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.
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.
void on_linop_apply_completed(const LinOp *A, const LinOp *b, const LinOp *x) const override
LinOp's apply completed event.
void on_polymorphic_object_copy_completed(const Executor *exec, const PolymorphicObject *from, const PolymorphicObject *to) const override
PolymorphicObject's copy completed event.
void on_linop_factory_generate_completed(const LinOpFactory *factory, const LinOp *input, const LinOp *output) const override
LinOp Factory's generate completed event.
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
void on_polymorphic_object_copy_started(const Executor *exec, const PolymorphicObject *from, const PolymorphicObject *to) const override
PolymorphicObject's copy started event.
void on_polymorphic_object_move_completed(const Executor *exec, const PolymorphicObject *from, const PolymorphicObject *to) const override
PolymorphicObject's move completed event.
void on_free_started(const Executor *exec, const uintptr &location) const override
Executor's free started event.
logged_data & get() noexcept
Definition record.hpp:448
void on_linop_apply_started(const LinOp *A, const LinOp *b, const LinOp *x) const override
LinOp's apply started event.
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.
const logged_data & get() const noexcept
Returns the logged data.
Definition record.hpp:443
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
void on_polymorphic_object_create_completed(const Executor *exec, const PolymorphicObject *input, const PolymorphicObject *output) const override
PolymorphicObject's create completed event.
void on_criterion_check_completed(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 array< stopping_status > *status, const bool &one_changed, const bool &all_converged) const override
stop::Criterion's check completed event.
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.
void on_free_completed(const Executor *exec, const uintptr &location) const override
Executor's free completed event.
void on_operation_launched(const Executor *exec, const Operation *operation) const override
Executor's operation launched event (method run).
void on_linop_factory_generate_started(const LinOpFactory *factory, const LinOp *input) const override
LinOp Factory's generate started event.
void on_allocation_started(const Executor *exec, const size_type &num_bytes) const override
Executor's allocation started event.
void on_operation_completed(const Executor *exec, const Operation *operation) const override
Executor's operation completed event (method run).
The Criterion class is a base class for all stopping criteria.
Definition criterion.hpp:36
@ criterion
Stopping criterion events.
@ factory
LinOpFactory events.
@ operation
Kernel execution and data movement.
The Ginkgo namespace.
Definition abstract_factory.hpp:20
std::uint8_t uint8
8-bit unsigned integral type.
Definition types.hpp:115
std::uintptr_t uintptr
Unsigned integer type capable of holding a pointer to void.
Definition types.hpp:138
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:86
Struct storing the actually logged data.
Definition record.hpp:221
Struct representing Criterion related data.
Definition record.hpp:163
Struct representing Executor related data.
Definition record.hpp:77
Struct representing iteration complete related data.
Definition record.hpp:31
Struct representing LinOp related data.
Definition record.hpp:117
Struct representing LinOp factory related data.
Definition record.hpp:143
Struct representing Operator related data.
Definition record.hpp:87
Struct representing PolymorphicObject related data.
Definition record.hpp:96
This structure is used to represent versions of various Ginkgo modules.
Definition version.hpp:26