Ginkgo: ginkgo/core/log/record.hpp Source File

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