5 #ifndef GKO_PUBLIC_CORE_LOG_LOGGER_HPP_
6 #define GKO_PUBLIC_CORE_LOG_LOGGER_HPP_
12 #include <type_traits>
16 #include <ginkgo/core/base/types.hpp>
17 #include <ginkgo/core/base/utils_helper.hpp>
24 template <
typename ValueType>
29 class PolymorphicObject;
31 class stopping_status;
38 class BatchLinOpFactory;
40 template <
typename ValueType>
112 #define GKO_LOGGER_REGISTER_EVENT(_id, _event_name, ...) \
114 virtual void on_##_event_name(__VA_ARGS__) const {} \
117 template <size_type Event, typename... Params> \
118 std::enable_if_t<Event == _id && (_id < event_count_max)> on( \
119 Params&&... params) const \
121 if (enabled_events_ & (mask_type{1} << _id)) { \
122 this->on_##_event_name(std::forward<Params>(params)...); \
125 static constexpr size_type _event_name{_id}; \
126 static constexpr mask_type _event_name##_mask{mask_type{1} << _id};
134 GKO_LOGGER_REGISTER_EVENT(0, allocation_started,
const Executor* exec,
144 GKO_LOGGER_REGISTER_EVENT(1, allocation_completed,
const Executor* exec,
154 GKO_LOGGER_REGISTER_EVENT(2, free_started,
const Executor* exec,
163 GKO_LOGGER_REGISTER_EVENT(3, free_completed,
const Executor* exec,
175 GKO_LOGGER_REGISTER_EVENT(4, copy_started,
const Executor* exec_from,
188 GKO_LOGGER_REGISTER_EVENT(5, copy_completed,
const Executor* exec_from,
198 GKO_LOGGER_REGISTER_EVENT(6, operation_launched,
const Executor* exec,
212 GKO_LOGGER_REGISTER_EVENT(7, operation_completed,
const Executor* exec,
221 GKO_LOGGER_REGISTER_EVENT(8, polymorphic_object_create_started,
231 GKO_LOGGER_REGISTER_EVENT(9, polymorphic_object_create_completed,
243 GKO_LOGGER_REGISTER_EVENT(10, polymorphic_object_copy_started,
255 GKO_LOGGER_REGISTER_EVENT(11, polymorphic_object_copy_completed,
266 GKO_LOGGER_REGISTER_EVENT(12, polymorphic_object_deleted,
276 GKO_LOGGER_REGISTER_EVENT(13, linop_apply_started,
const LinOp* A,
286 GKO_LOGGER_REGISTER_EVENT(14, linop_apply_completed,
const LinOp* A,
298 GKO_LOGGER_REGISTER_EVENT(15, linop_advanced_apply_started,
const LinOp* A,
311 GKO_LOGGER_REGISTER_EVENT(16, linop_advanced_apply_completed,
322 GKO_LOGGER_REGISTER_EVENT(17, linop_factory_generate_started,
333 GKO_LOGGER_REGISTER_EVENT(18, linop_factory_generate_completed,
348 GKO_LOGGER_REGISTER_EVENT(19, criterion_check_started,
352 const uint8& stopping_id,
353 const bool& set_finalized)
375 GKO_LOGGER_REGISTER_EVENT(
378 const uint8& stopping_id,
const bool& set_finalized,
380 const bool& all_converged)
399 virtual void on_criterion_check_completed(
402 const uint8& stopping_id,
const bool& set_finalized,
404 const bool& all_converged)
const
406 this->on_criterion_check_completed(
criterion, it, r, tau, x,
407 stopping_id, set_finalized, status,
408 one_changed, all_converged);
412 static constexpr
size_type iteration_complete{21};
413 static constexpr mask_type iteration_complete_mask{mask_type{1} << 21};
415 template <
size_type Event,
typename... Params>
417 Params&&... params)
const
419 if (enabled_events_ & (mask_type{1} << 21)) {
420 this->on_iteration_complete(std::forward<Params>(params)...);
438 "Please use the version with the additional stopping "
440 virtual
void on_iteration_complete(const LinOp*
solver, const
size_type& it,
441 const LinOp* r, const LinOp* x =
nullptr,
442 const LinOp* tau =
nullptr)
const
459 "Please use the version with the additional stopping "
461 virtual
void on_iteration_complete(const LinOp*
solver, const
size_type& it,
462 const LinOp* r, const LinOp* x,
464 const LinOp* implicit_tau_sq)
const
466 GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS
467 this->on_iteration_complete(
solver, it, r, x, tau);
468 GKO_END_DISABLE_DEPRECATION_WARNINGS
486 virtual void on_iteration_complete(
const LinOp*
solver,
const LinOp* b,
488 const LinOp* r,
const LinOp* tau,
489 const LinOp* implicit_tau_sq,
490 const array<stopping_status>* status,
493 GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS
494 this->on_iteration_complete(
solver, it, r, x, tau, implicit_tau_sq);
495 GKO_END_DISABLE_DEPRECATION_WARNINGS
506 GKO_LOGGER_REGISTER_EVENT(22, polymorphic_object_move_started,
507 const Executor* exec,
508 const PolymorphicObject* input,
509 const PolymorphicObject* output)
518 GKO_LOGGER_REGISTER_EVENT(23, polymorphic_object_move_completed,
519 const Executor* exec,
520 const PolymorphicObject* input,
521 const PolymorphicObject* output)
530 GKO_LOGGER_REGISTER_EVENT(24, batch_linop_factory_generate_started,
531 const batch::BatchLinOpFactory*
factory,
532 const batch::BatchLinOp* input)
542 GKO_LOGGER_REGISTER_EVENT(25, batch_linop_factory_generate_completed,
543 const batch::BatchLinOpFactory*
factory,
544 const batch::BatchLinOp* input,
545 const batch::BatchLinOp* output)
548 static constexpr
size_type batch_solver_completed{26};
549 static constexpr mask_type batch_solver_completed_mask{mask_type{1} << 26};
551 template <
size_type Event,
typename... Params>
553 Params&&... params)
const
555 if (enabled_events_ & batch_solver_completed_mask) {
556 this->on_batch_solver_completed(std::forward<Params>(params)...);
568 virtual void on_batch_solver_completed(
569 const array<int>& iters,
const array<double>& residual_norms)
const
579 virtual void on_batch_solver_completed(
580 const array<int>& iters,
const array<float>& residual_norms)
const
584 #undef GKO_LOGGER_REGISTER_EVENT
590 allocation_started_mask | allocation_completed_mask |
591 free_started_mask | free_completed_mask | copy_started_mask |
598 operation_launched_mask | operation_completed_mask;
604 polymorphic_object_create_started_mask |
605 polymorphic_object_create_completed_mask |
606 polymorphic_object_copy_started_mask |
607 polymorphic_object_copy_completed_mask |
608 polymorphic_object_move_started_mask |
609 polymorphic_object_move_completed_mask |
610 polymorphic_object_deleted_mask;
616 linop_apply_started_mask | linop_apply_completed_mask |
617 linop_advanced_apply_started_mask | linop_advanced_apply_completed_mask;
623 linop_factory_generate_started_mask |
624 linop_factory_generate_completed_mask;
630 batch_linop_factory_generate_started_mask |
631 batch_linop_factory_generate_completed_mask;
637 criterion_check_started_mask | criterion_check_completed_mask;
645 virtual ~
Logger() =
default;
662 GKO_DEPRECATED(
"use single-parameter constructor")
683 : enabled_events_{enabled_events}
687 mask_type enabled_events_;
705 virtual void add_logger(std::shared_ptr<const Logger> logger) = 0;
728 virtual const std::vector<std::shared_ptr<const Logger>>&
get_loggers()
748 template <
typename ConcreteLoggable,
typename PolymorphicBase = Loggable>
751 void add_logger(std::shared_ptr<const Logger> logger)
override
753 loggers_.push_back(logger);
756 void remove_logger(
const Logger* logger)
override
759 find_if(begin(loggers_), end(loggers_),
760 [&logger](
const auto& l) {
return l.get() == logger; });
761 if (idx != end(loggers_)) {
771 remove_logger(logger.
get());
774 const std::vector<std::shared_ptr<const Logger>>& get_loggers()
780 void clear_loggers()
override { loggers_.clear(); }
790 template <
size_type Event,
typename ConcreteLoggableT,
typename =
void>
791 struct propagate_log_helper {
792 template <
typename... Args>
793 static void propagate_log(
const ConcreteLoggableT*, Args&&...)
797 template <
size_type Event,
typename ConcreteLoggableT>
798 struct propagate_log_helper<
799 Event, ConcreteLoggableT,
801 decltype(std::declval<ConcreteLoggableT>().get_executor())>> {
802 template <
typename... Args>
803 static void propagate_log(
const ConcreteLoggableT* loggable,
806 const auto exec = loggable->get_executor();
807 if (exec->should_propagate_log()) {
808 for (
auto& logger : exec->get_loggers()) {
809 if (logger->needs_propagation()) {
810 logger->template on<Event>(std::forward<Args>(args)...);
818 template <
size_type Event,
typename... Params>
819 void log(Params&&... params)
const
821 propagate_log_helper<Event, ConcreteLoggable>::propagate_log(
822 static_cast<const ConcreteLoggable*>(
this),
823 std::forward<Params>(params)...);
824 for (
auto& logger : loggers_) {
825 logger->template on<Event>(std::forward<Params>(params)...);
829 std::vector<std::shared_ptr<const Logger>> loggers_;
837 #endif // GKO_PUBLIC_CORE_LOG_LOGGER_HPP_