5 #ifndef GKO_PUBLIC_CORE_LOG_LOGGER_HPP_
6 #define GKO_PUBLIC_CORE_LOG_LOGGER_HPP_
12 #include <type_traits>
15 #include <ginkgo/core/base/types.hpp>
16 #include <ginkgo/core/base/utils_helper.hpp>
23 template <
typename ValueType>
28 class PolymorphicObject;
30 class stopping_status;
37 class BatchLinOpFactory;
39 template <
typename ValueType>
111 #define GKO_LOGGER_REGISTER_EVENT(_id, _event_name, ...) \
113 virtual void on_##_event_name(__VA_ARGS__) const {} \
116 template <size_type Event, typename... Params> \
117 std::enable_if_t<Event == _id && (_id < event_count_max)> on( \
118 Params&&... params) const \
120 if (enabled_events_ & (mask_type{1} << _id)) { \
121 this->on_##_event_name(std::forward<Params>(params)...); \
124 static constexpr size_type _event_name{_id}; \
125 static constexpr mask_type _event_name##_mask{mask_type{1} << _id};
133 GKO_LOGGER_REGISTER_EVENT(0, allocation_started,
const Executor* exec,
143 GKO_LOGGER_REGISTER_EVENT(1, allocation_completed,
const Executor* exec,
153 GKO_LOGGER_REGISTER_EVENT(2, free_started,
const Executor* exec,
162 GKO_LOGGER_REGISTER_EVENT(3, free_completed,
const Executor* exec,
174 GKO_LOGGER_REGISTER_EVENT(4, copy_started,
const Executor* exec_from,
187 GKO_LOGGER_REGISTER_EVENT(5, copy_completed,
const Executor* exec_from,
197 GKO_LOGGER_REGISTER_EVENT(6, operation_launched,
const Executor* exec,
211 GKO_LOGGER_REGISTER_EVENT(7, operation_completed,
const Executor* exec,
220 GKO_LOGGER_REGISTER_EVENT(8, polymorphic_object_create_started,
230 GKO_LOGGER_REGISTER_EVENT(9, polymorphic_object_create_completed,
242 GKO_LOGGER_REGISTER_EVENT(10, polymorphic_object_copy_started,
254 GKO_LOGGER_REGISTER_EVENT(11, polymorphic_object_copy_completed,
265 GKO_LOGGER_REGISTER_EVENT(12, polymorphic_object_deleted,
275 GKO_LOGGER_REGISTER_EVENT(13, linop_apply_started,
const LinOp* A,
285 GKO_LOGGER_REGISTER_EVENT(14, linop_apply_completed,
const LinOp* A,
297 GKO_LOGGER_REGISTER_EVENT(15, linop_advanced_apply_started,
const LinOp* A,
310 GKO_LOGGER_REGISTER_EVENT(16, linop_advanced_apply_completed,
321 GKO_LOGGER_REGISTER_EVENT(17, linop_factory_generate_started,
332 GKO_LOGGER_REGISTER_EVENT(18, linop_factory_generate_completed,
347 GKO_LOGGER_REGISTER_EVENT(19, criterion_check_started,
351 const uint8& stopping_id,
352 const bool& set_finalized)
374 GKO_LOGGER_REGISTER_EVENT(
377 const uint8& stopping_id,
const bool& set_finalized,
379 const bool& all_converged)
398 virtual void on_criterion_check_completed(
401 const uint8& stopping_id,
const bool& set_finalized,
403 const bool& all_converged)
const
405 this->on_criterion_check_completed(
criterion, it, r, tau, x,
406 stopping_id, set_finalized, status,
407 one_changed, all_converged);
411 static constexpr
size_type iteration_complete{21};
412 static constexpr mask_type iteration_complete_mask{mask_type{1} << 21};
414 template <
size_type Event,
typename... Params>
416 Params&&... params)
const
418 if (enabled_events_ & (mask_type{1} << 21)) {
419 this->on_iteration_complete(std::forward<Params>(params)...);
437 "Please use the version with the additional stopping "
439 virtual
void on_iteration_complete(const LinOp*
solver, const
size_type& it,
440 const LinOp* r, const LinOp* x =
nullptr,
441 const LinOp* tau =
nullptr)
const
458 "Please use the version with the additional stopping "
460 virtual
void on_iteration_complete(const LinOp*
solver, const
size_type& it,
461 const LinOp* r, const LinOp* x,
463 const LinOp* implicit_tau_sq)
const
465 GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS
466 this->on_iteration_complete(
solver, it, r, x, tau);
467 GKO_END_DISABLE_DEPRECATION_WARNINGS
485 virtual void on_iteration_complete(
const LinOp*
solver,
const LinOp* b,
487 const LinOp* r,
const LinOp* tau,
488 const LinOp* implicit_tau_sq,
489 const array<stopping_status>* status,
492 GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS
493 this->on_iteration_complete(
solver, it, r, x, tau, implicit_tau_sq);
494 GKO_END_DISABLE_DEPRECATION_WARNINGS
505 GKO_LOGGER_REGISTER_EVENT(22, polymorphic_object_move_started,
506 const Executor* exec,
507 const PolymorphicObject* input,
508 const PolymorphicObject* output)
517 GKO_LOGGER_REGISTER_EVENT(23, polymorphic_object_move_completed,
518 const Executor* exec,
519 const PolymorphicObject* input,
520 const PolymorphicObject* output)
529 GKO_LOGGER_REGISTER_EVENT(24, batch_linop_factory_generate_started,
530 const batch::BatchLinOpFactory*
factory,
531 const batch::BatchLinOp* input)
541 GKO_LOGGER_REGISTER_EVENT(25, batch_linop_factory_generate_completed,
542 const batch::BatchLinOpFactory*
factory,
543 const batch::BatchLinOp* input,
544 const batch::BatchLinOp* output)
547 static constexpr
size_type batch_solver_completed{26};
548 static constexpr mask_type batch_solver_completed_mask{mask_type{1} << 26};
550 template <
size_type Event,
typename... Params>
552 Params&&... params)
const
554 if (enabled_events_ & batch_solver_completed_mask) {
555 this->on_batch_solver_completed(std::forward<Params>(params)...);
567 virtual void on_batch_solver_completed(
568 const array<int>& iters,
const array<double>& residual_norms)
const
578 virtual void on_batch_solver_completed(
579 const array<int>& iters,
const array<float>& residual_norms)
const
583 #undef GKO_LOGGER_REGISTER_EVENT
589 allocation_started_mask | allocation_completed_mask |
590 free_started_mask | free_completed_mask | copy_started_mask |
597 operation_launched_mask | operation_completed_mask;
603 polymorphic_object_create_started_mask |
604 polymorphic_object_create_completed_mask |
605 polymorphic_object_copy_started_mask |
606 polymorphic_object_copy_completed_mask |
607 polymorphic_object_move_started_mask |
608 polymorphic_object_move_completed_mask |
609 polymorphic_object_deleted_mask;
615 linop_apply_started_mask | linop_apply_completed_mask |
616 linop_advanced_apply_started_mask | linop_advanced_apply_completed_mask;
622 linop_factory_generate_started_mask |
623 linop_factory_generate_completed_mask;
629 batch_linop_factory_generate_started_mask |
630 batch_linop_factory_generate_completed_mask;
636 criterion_check_started_mask | criterion_check_completed_mask;
644 virtual ~
Logger() =
default;
661 GKO_DEPRECATED(
"use single-parameter constructor")
682 : enabled_events_{enabled_events}
686 mask_type enabled_events_;
704 virtual void add_logger(std::shared_ptr<const Logger> logger) = 0;
727 virtual const std::vector<std::shared_ptr<const Logger>>&
get_loggers()
747 template <
typename ConcreteLoggable,
typename PolymorphicBase = Loggable>
750 void add_logger(std::shared_ptr<const Logger> logger)
override
752 loggers_.push_back(logger);
755 void remove_logger(
const Logger* logger)
override
758 find_if(begin(loggers_), end(loggers_),
759 [&logger](
const auto& l) {
return l.get() == logger; });
760 if (idx != end(loggers_)) {
770 remove_logger(logger.
get());
773 const std::vector<std::shared_ptr<const Logger>>& get_loggers()
779 void clear_loggers()
override { loggers_.clear(); }
789 template <
size_type Event,
typename ConcreteLoggableT,
typename =
void>
790 struct propagate_log_helper {
791 template <
typename... Args>
792 static void propagate_log(
const ConcreteLoggableT*, Args&&...)
796 template <
size_type Event,
typename ConcreteLoggableT>
797 struct propagate_log_helper<
798 Event, ConcreteLoggableT,
800 decltype(std::declval<ConcreteLoggableT>().get_executor())>> {
801 template <
typename... Args>
802 static void propagate_log(
const ConcreteLoggableT* loggable,
805 const auto exec = loggable->get_executor();
806 if (exec->should_propagate_log()) {
807 for (
auto& logger : exec->get_loggers()) {
808 if (logger->needs_propagation()) {
809 logger->template on<Event>(std::forward<Args>(args)...);
817 template <
size_type Event,
typename... Params>
818 void log(Params&&... params)
const
820 propagate_log_helper<Event, ConcreteLoggable>::propagate_log(
821 static_cast<const ConcreteLoggable*>(
this),
822 std::forward<Params>(params)...);
823 for (
auto& logger : loggers_) {
824 logger->template on<Event>(std::forward<Params>(params)...);
828 std::vector<std::shared_ptr<const Logger>> loggers_;
836 #endif // GKO_PUBLIC_CORE_LOG_LOGGER_HPP_