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
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_LOGGER_HPP_
34#define GKO_PUBLIC_CORE_LOG_LOGGER_HPP_
35
36
37#include <algorithm>
38#include <memory>
39#include <string>
40#include <type_traits>
41#include <vector>
42
43
44#include <ginkgo/core/base/types.hpp>
45#include <ginkgo/core/base/utils_helper.hpp>
46
47
48namespace gko {
49
50
51/* Eliminate circular dependencies the hard way */
52template <typename ValueType>
53class array;
54class Executor;
55class LinOp;
56class LinOpFactory;
57class PolymorphicObject;
58class Operation;
59class stopping_status;
60
61
62namespace batch {
63
64
65class BatchLinOp;
66class BatchLinOpFactory;
67
68template <typename ValueType>
69class MultiVector;
70
71
72} // namespace batch
73
74
80namespace stop {
81class Criterion;
82} // namespace stop
83
84
85namespace log {
86
87
104class Logger {
105public:
107 using mask_type = gko::uint64;
108
112 static constexpr size_type event_count_max = sizeof(mask_type) * byte_size;
113
117 static constexpr mask_type all_events_mask = ~mask_type{0};
118
140#define GKO_LOGGER_REGISTER_EVENT(_id, _event_name, ...) \
141protected: \
142 virtual void on_##_event_name(__VA_ARGS__) const {} \
143 \
144public: \
145 template <size_type Event, typename... Params> \
146 std::enable_if_t<Event == _id && (_id < event_count_max)> on( \
147 Params&&... params) const \
148 { \
149 if (enabled_events_ & (mask_type{1} << _id)) { \
150 this->on_##_event_name(std::forward<Params>(params)...); \
151 } \
152 } \
153 static constexpr size_type _event_name{_id}; \
154 static constexpr mask_type _event_name##_mask{mask_type{1} << _id};
155
162 GKO_LOGGER_REGISTER_EVENT(0, allocation_started, const Executor* exec,
163 const size_type& num_bytes)
164
165
172 GKO_LOGGER_REGISTER_EVENT(1, allocation_completed, const Executor* exec,
173 const size_type& num_bytes,
174 const uintptr& location)
175
176
182 GKO_LOGGER_REGISTER_EVENT(2, free_started, const Executor* exec,
183 const uintptr& location)
184
185
191 GKO_LOGGER_REGISTER_EVENT(3, free_completed, const Executor* exec,
192 const uintptr& location)
193
194
203 GKO_LOGGER_REGISTER_EVENT(4, copy_started, const Executor* exec_from,
204 const Executor* exec_to, const uintptr& loc_from,
205 const uintptr& loc_to, const size_type& num_bytes)
206
207
216 GKO_LOGGER_REGISTER_EVENT(5, copy_completed, const Executor* exec_from,
217 const Executor* exec_to, const uintptr& loc_from,
218 const uintptr& loc_to, const size_type& num_bytes)
219
220
226 GKO_LOGGER_REGISTER_EVENT(6, operation_launched, const Executor* exec,
227 const Operation* op)
228
229
240 GKO_LOGGER_REGISTER_EVENT(7, operation_completed, const Executor* exec,
241 const Operation* op)
242
243
249 GKO_LOGGER_REGISTER_EVENT(8, polymorphic_object_create_started,
250 const Executor* exec, const PolymorphicObject* po)
251
252
259 GKO_LOGGER_REGISTER_EVENT(9, polymorphic_object_create_completed,
260 const Executor* exec,
261 const PolymorphicObject* input,
262 const PolymorphicObject* output)
263
264
271 GKO_LOGGER_REGISTER_EVENT(10, polymorphic_object_copy_started,
272 const Executor* exec,
273 const PolymorphicObject* input,
274 const PolymorphicObject* output)
275
276
283 GKO_LOGGER_REGISTER_EVENT(11, polymorphic_object_copy_completed,
284 const Executor* exec,
285 const PolymorphicObject* input,
286 const PolymorphicObject* output)
287
288
294 GKO_LOGGER_REGISTER_EVENT(12, polymorphic_object_deleted,
295 const Executor* exec, const PolymorphicObject* po)
296
297
304 GKO_LOGGER_REGISTER_EVENT(13, linop_apply_started, const LinOp* A,
305 const LinOp* b, const LinOp* x)
306
307
314 GKO_LOGGER_REGISTER_EVENT(14, linop_apply_completed, const LinOp* A,
315 const LinOp* b, const LinOp* x)
316
317
326 GKO_LOGGER_REGISTER_EVENT(15, linop_advanced_apply_started, const LinOp* A,
327 const LinOp* alpha, const LinOp* b,
328 const LinOp* beta, const LinOp* x)
329
330
339 GKO_LOGGER_REGISTER_EVENT(16, linop_advanced_apply_completed,
340 const LinOp* A, const LinOp* alpha,
341 const LinOp* b, const LinOp* beta, const LinOp* x)
342
343
350 GKO_LOGGER_REGISTER_EVENT(17, linop_factory_generate_started,
351 const LinOpFactory* factory, const LinOp* input)
352
353
361 GKO_LOGGER_REGISTER_EVENT(18, linop_factory_generate_completed,
362 const LinOpFactory* factory, const LinOp* input,
363 const LinOp* output)
364
365
376 GKO_LOGGER_REGISTER_EVENT(19, criterion_check_started,
378 const size_type& it, const LinOp* r,
379 const LinOp* tau, const LinOp* x,
380 const uint8& stopping_id,
381 const bool& set_finalized)
382
383
403 GKO_LOGGER_REGISTER_EVENT(
404 20, criterion_check_completed, const stop::Criterion* criterion,
405 const size_type& it, const LinOp* r, const LinOp* tau, const LinOp* x,
406 const uint8& stopping_id, const bool& set_finalized,
407 const array<stopping_status>* status, const bool& one_changed,
408 const bool& all_converged)
409protected:
427 virtual void on_criterion_check_completed(
428 const stop::Criterion* criterion, const size_type& it, const LinOp* r,
429 const LinOp* tau, const LinOp* implicit_tau_sq, const LinOp* x,
430 const uint8& stopping_id, const bool& set_finalized,
431 const array<stopping_status>* status, const bool& one_changed,
432 const bool& all_converged) const
433 {
434 this->on_criterion_check_completed(criterion, it, r, tau, x,
435 stopping_id, set_finalized, status,
437 }
438
439public:
440 static constexpr size_type iteration_complete{21};
441 static constexpr mask_type iteration_complete_mask{mask_type{1} << 21};
442
443 template <size_type Event, typename... Params>
444 std::enable_if_t<Event == 21 && (21 < event_count_max)> on(
445 Params&&... params) const
446 {
447 if (enabled_events_ & (mask_type{1} << 21)) {
448 this->on_iteration_complete(std::forward<Params>(params)...);
449 }
450 }
451
452protected:
465 GKO_DEPRECATED(
466 "Please use the version with the additional stopping "
467 "information.")
468 virtual void on_iteration_complete(const LinOp* solver, const size_type& it,
469 const LinOp* r, const LinOp* x = nullptr,
470 const LinOp* tau = nullptr) const
471 {}
472
486 GKO_DEPRECATED(
487 "Please use the version with the additional stopping "
488 "information.")
489 virtual void on_iteration_complete(const LinOp* solver, const size_type& it,
490 const LinOp* r, const LinOp* x,
491 const LinOp* tau,
492 const LinOp* implicit_tau_sq) const
493 {
494 GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS
495 this->on_iteration_complete(solver, it, r, x, tau);
496 GKO_END_DISABLE_DEPRECATION_WARNINGS
497 }
498
514 virtual void on_iteration_complete(const LinOp* solver, const LinOp* b,
515 const LinOp* x, const size_type& it,
516 const LinOp* r, const LinOp* tau,
517 const LinOp* implicit_tau_sq,
518 const array<stopping_status>* status,
519 bool stopped) const
520 {
521 GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS
522 this->on_iteration_complete(solver, it, r, x, tau, implicit_tau_sq);
523 GKO_END_DISABLE_DEPRECATION_WARNINGS
524 }
525
526public:
534 GKO_LOGGER_REGISTER_EVENT(22, polymorphic_object_move_started,
535 const Executor* exec,
536 const PolymorphicObject* input,
537 const PolymorphicObject* output)
538
539
546 GKO_LOGGER_REGISTER_EVENT(23, polymorphic_object_move_completed,
547 const Executor* exec,
548 const PolymorphicObject* input,
549 const PolymorphicObject* output)
550
551
558 GKO_LOGGER_REGISTER_EVENT(24, batch_linop_factory_generate_started,
559 const batch::BatchLinOpFactory* factory,
560 const batch::BatchLinOp* input)
561
562
570 GKO_LOGGER_REGISTER_EVENT(25, batch_linop_factory_generate_completed,
571 const batch::BatchLinOpFactory* factory,
572 const batch::BatchLinOp* input,
573 const batch::BatchLinOp* output)
574
575public:
576 static constexpr size_type batch_solver_completed{26};
577 static constexpr mask_type batch_solver_completed_mask{mask_type{1} << 26};
578
579 template <size_type Event, typename... Params>
580 std::enable_if_t<Event == 26 && (26 < event_count_max)> on(
581 Params&&... params) const
582 {
583 if (enabled_events_ & batch_solver_completed_mask) {
584 this->on_batch_solver_completed(std::forward<Params>(params)...);
585 }
586 }
587
588protected:
596 virtual void on_batch_solver_completed(
597 const array<int>& iters, const array<double>& residual_norms) const
598 {}
599
607 virtual void on_batch_solver_completed(
608 const array<int>& iters, const array<float>& residual_norms) const
609 {}
610
611public:
612#undef GKO_LOGGER_REGISTER_EVENT
613
617 static constexpr mask_type executor_events_mask =
618 allocation_started_mask | allocation_completed_mask |
619 free_started_mask | free_completed_mask | copy_started_mask |
620 copy_completed_mask;
621
625 static constexpr mask_type operation_events_mask =
626 operation_launched_mask | operation_completed_mask;
627
631 static constexpr mask_type polymorphic_object_events_mask =
632 polymorphic_object_create_started_mask |
633 polymorphic_object_create_completed_mask |
634 polymorphic_object_copy_started_mask |
635 polymorphic_object_copy_completed_mask |
636 polymorphic_object_move_started_mask |
637 polymorphic_object_move_completed_mask |
638 polymorphic_object_deleted_mask;
639
643 static constexpr mask_type linop_events_mask =
644 linop_apply_started_mask | linop_apply_completed_mask |
645 linop_advanced_apply_started_mask | linop_advanced_apply_completed_mask;
646
650 static constexpr mask_type linop_factory_events_mask =
651 linop_factory_generate_started_mask |
652 linop_factory_generate_completed_mask;
653
657 static constexpr mask_type batch_linop_factory_events_mask =
658 batch_linop_factory_generate_started_mask |
659 batch_linop_factory_generate_completed_mask;
660
664 static constexpr mask_type criterion_events_mask =
665 criterion_check_started_mask | criterion_check_completed_mask;
666
671 virtual bool needs_propagation() const { return false; }
672
673 virtual ~Logger() = default;
674
675protected:
690 GKO_DEPRECATED("use single-parameter constructor")
691 explicit Logger(std::shared_ptr<const gko::Executor> exec,
694 {}
695
710 explicit Logger(const mask_type& enabled_events = all_events_mask)
711 : enabled_events_{enabled_events}
712 {}
713
714private:
715 mask_type enabled_events_;
716};
717
718
724class Loggable {
725public:
726 virtual ~Loggable() = default;
727
733 virtual void add_logger(std::shared_ptr<const Logger> logger) = 0;
734
744 virtual void remove_logger(const Logger* logger) = 0;
745
747 {
748 remove_logger(logger.get());
749 }
750
756 virtual const std::vector<std::shared_ptr<const Logger>>& get_loggers()
757 const = 0;
758
760 virtual void clear_loggers() = 0;
761};
762
763
776template <typename ConcreteLoggable, typename PolymorphicBase = Loggable>
777class EnableLogging : public PolymorphicBase {
778public:
779 void add_logger(std::shared_ptr<const Logger> logger) override
780 {
781 loggers_.push_back(logger);
782 }
783
784 void remove_logger(const Logger* logger) override
785 {
786 auto idx =
787 find_if(begin(loggers_), end(loggers_),
788 [&logger](const auto& l) { return l.get() == logger; });
789 if (idx != end(loggers_)) {
790 loggers_.erase(idx);
791 } else {
792 throw OutOfBoundsError(__FILE__, __LINE__, loggers_.size(),
793 loggers_.size());
794 }
795 }
796
798 {
799 remove_logger(logger.get());
800 }
801
802 const std::vector<std::shared_ptr<const Logger>>& get_loggers()
803 const override
804 {
805 return loggers_;
806 }
807
808 void clear_loggers() override { loggers_.clear(); }
809
810private:
818 template <size_type Event, typename ConcreteLoggableT, typename = void>
819 struct propagate_log_helper {
820 template <typename... Args>
821 static void propagate_log(const ConcreteLoggableT*, Args&&...)
822 {}
823 };
824
825 template <size_type Event, typename ConcreteLoggableT>
826 struct propagate_log_helper<
828 xstd::void_t<
829 decltype(std::declval<ConcreteLoggableT>().get_executor())>> {
830 template <typename... Args>
831 static void propagate_log(const ConcreteLoggableT* loggable,
832 Args&&... args)
833 {
834 const auto exec = loggable->get_executor();
835 if (exec->should_propagate_log()) {
836 for (auto& logger : exec->get_loggers()) {
837 if (logger->needs_propagation()) {
838 logger->template on<Event>(std::forward<Args>(args)...);
839 }
840 }
841 }
842 }
843 };
844
845protected:
846 template <size_type Event, typename... Params>
847 void log(Params&&... params) const
848 {
849 propagate_log_helper<Event, ConcreteLoggable>::propagate_log(
850 static_cast<const ConcreteLoggable*>(this),
851 std::forward<Params>(params)...);
852 for (auto& logger : loggers_) {
853 logger->template on<Event>(std::forward<Params>(params)...);
854 }
855 }
856
857 std::vector<std::shared_ptr<const Logger>> loggers_;
858};
859
860
861} // namespace log
862} // namespace gko
863
864
865#endif // GKO_PUBLIC_CORE_LOG_LOGGER_HPP_
The first step in using the Ginkgo library consists of creating an executor.
Definition executor.hpp:644
A LinOpFactory represents a higher order mapping which transforms one linear operator into another.
Definition lin_op.hpp:414
Definition lin_op.hpp:146
Operations can be used to define functionalities whose implementations differ among devices.
Definition executor.hpp:287
OutOfBoundsError is thrown if a memory access is detected to be out-of-bounds.
Definition exception.hpp:587
A PolymorphicObject is the abstract base for all "heavy" objects in Ginkgo that behave polymorphicall...
Definition polymorphic_object.hpp:72
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:187
EnableLogging is a mixin which should be inherited by any class which wants to enable logging.
Definition logger.hpp:777
void clear_loggers() override
Remove all loggers registered at this object.
Definition logger.hpp:808
const std::vector< std::shared_ptr< const Logger > > & get_loggers() const override
Returns the vector containing all loggers registered at this object.
Definition logger.hpp:802
void add_logger(std::shared_ptr< const Logger > logger) override
Adds a new logger to the list of subscribed loggers.
Definition logger.hpp:779
void remove_logger(const Logger *logger) override
Removes a logger from the list of subscribed loggers.
Definition logger.hpp:784
Loggable class is an interface which should be implemented by classes wanting to support logging.
Definition logger.hpp:724
virtual const std::vector< std::shared_ptr< const Logger > > & get_loggers() const =0
Returns the vector containing all loggers registered at this object.
virtual void clear_loggers()=0
Remove all loggers registered at this object.
virtual void remove_logger(const Logger *logger)=0
Removes a logger from the list of subscribed loggers.
virtual void add_logger(std::shared_ptr< const Logger > logger)=0
Adds a new logger to the list of subscribed loggers.
Definition logger.hpp:104
static constexpr mask_type all_events_mask
Bitset Mask which activates all events.
Definition logger.hpp:117
static constexpr mask_type operation_events_mask
Bitset Mask which activates all operation events.
Definition logger.hpp:625
static constexpr mask_type batch_linop_factory_events_mask
Bitset Mask which activates all batch linop factory events.
Definition logger.hpp:657
static constexpr mask_type linop_events_mask
Bitset Mask which activates all linop events.
Definition logger.hpp:643
static constexpr mask_type polymorphic_object_events_mask
Bitset Mask which activates all polymorphic object events.
Definition logger.hpp:631
virtual bool needs_propagation() const
Returns true if this logger, when attached to an Executor, needs to be forwarded all events from obje...
Definition logger.hpp:671
static constexpr mask_type criterion_events_mask
Bitset Mask which activates all criterion events.
Definition logger.hpp:664
static constexpr size_type event_count_max
Maximum amount of events (bits) with the current implementation.
Definition logger.hpp:112
static constexpr mask_type linop_factory_events_mask
Bitset Mask which activates all linop factory events.
Definition logger.hpp:650
static constexpr mask_type executor_events_mask
Bitset Mask which activates all executor events.
Definition logger.hpp:617
This class is used for function parameters in the place of raw pointers.
Definition utils_helper.hpp:71
The Criterion class is a base class for all stopping criteria.
Definition criterion.hpp:64
@ criterion
Stopping criterion events.
@ factory
LinOpFactory events.
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
std::uint8_t uint8
8-bit unsigned integral type.
Definition types.hpp:149
std::uint64_t uint64
64-bit unsigned integral type.
Definition types.hpp:166
std::uintptr_t uintptr
Unsigned integer type capable of holding a pointer to void.
Definition types.hpp:172
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:120
constexpr size_type byte_size
Number of bits in a byte.
Definition types.hpp:211
@ array
The matrix should be written as dense matrix in column-major order.