Ginkgo  Generated from tags/v1.0.0^0 branch based on master. Ginkgo version 1.0.0
A numerical linear algebra library targeting many-core architectures
logger.hpp
1 /*******************************<GINKGO LICENSE>******************************
2 Copyright (c) 2017-2019, the Ginkgo authors
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 ******************************<GINKGO LICENSE>*******************************/
32 
33 #ifndef GKO_CORE_LOGGER_HPP_
34 #define GKO_CORE_LOGGER_HPP_
35 
36 
37 #include <algorithm>
38 #include <memory>
39 #include <string>
40 #include <vector>
41 
42 
43 #include <ginkgo/core/base/std_extensions.hpp>
44 #include <ginkgo/core/base/types.hpp>
45 #include <ginkgo/core/base/utils.hpp>
46 
47 
48 namespace gko {
49 
50 
51 /* Eliminate circular dependencies the hard way */
52 template <typename ValueType>
53 class Array;
54 class Executor;
55 class LinOp;
56 class LinOpFactory;
57 class PolymorphicObject;
58 class Operation;
59 class stopping_status;
60 
66 namespace stop {
67 class Criterion;
68 } // namespace stop
69 
70 
71 namespace log {
72 
73 
90 class Logger {
91 public:
93  using mask_type = gko::uint64;
94 
98  static constexpr size_type event_count_max = sizeof(mask_type) * byte_size;
99 
103  static constexpr mask_type all_events_mask = ~mask_type{0};
104 
126 #define GKO_LOGGER_REGISTER_EVENT(_id, _event_name, ...) \
127 protected: \
128  virtual void on_##_event_name(__VA_ARGS__) const {} \
129  \
130 public: \
131  template <size_type Event, typename... Params> \
132  xstd::enable_if_t<Event == _id && (_id < event_count_max)> on( \
133  Params &&... params) const \
134  { \
135  if (enabled_events_ & (mask_type{1} << _id)) { \
136  this->on_##_event_name(std::forward<Params>(params)...); \
137  } \
138  } \
139  static constexpr size_type _event_name{_id}; \
140  static constexpr mask_type _event_name##_mask{mask_type{1} << _id};
141 
148  GKO_LOGGER_REGISTER_EVENT(0, allocation_started, const Executor *exec,
149  const size_type &num_bytes)
150 
151 
158  GKO_LOGGER_REGISTER_EVENT(1, allocation_completed, const Executor *exec,
159  const size_type &num_bytes,
160  const uintptr &location)
161 
162 
168  GKO_LOGGER_REGISTER_EVENT(2, free_started, const Executor *exec,
169  const uintptr &location)
170 
171 
177  GKO_LOGGER_REGISTER_EVENT(3, free_completed, const Executor *exec,
178  const uintptr &location)
179 
180 
189  GKO_LOGGER_REGISTER_EVENT(4, copy_started, const Executor *exec_from,
190  const Executor *exec_to, const uintptr &loc_from,
191  const uintptr &loc_to, const size_type &num_bytes)
192 
193 
202  GKO_LOGGER_REGISTER_EVENT(5, copy_completed, const Executor *exec_from,
203  const Executor *exec_to, const uintptr &loc_from,
204  const uintptr &loc_to, const size_type &num_bytes)
205 
206 
212  GKO_LOGGER_REGISTER_EVENT(6, operation_launched, const Executor *exec,
213  const Operation *op)
214 
215 
226  GKO_LOGGER_REGISTER_EVENT(7, operation_completed, const Executor *exec,
227  const Operation *op)
228 
229 
235  GKO_LOGGER_REGISTER_EVENT(8, polymorphic_object_create_started,
236  const Executor *exec, const PolymorphicObject *po)
237 
238 
245  GKO_LOGGER_REGISTER_EVENT(9, polymorphic_object_create_completed,
246  const Executor *exec,
247  const PolymorphicObject *input,
248  const PolymorphicObject *output)
249 
250 
257  GKO_LOGGER_REGISTER_EVENT(10, polymorphic_object_copy_started,
258  const Executor *exec,
259  const PolymorphicObject *input,
260  const PolymorphicObject *output)
261 
262 
269  GKO_LOGGER_REGISTER_EVENT(11, polymorphic_object_copy_completed,
270  const Executor *exec,
271  const PolymorphicObject *input,
272  const PolymorphicObject *output)
273 
274 
280  GKO_LOGGER_REGISTER_EVENT(12, polymorphic_object_deleted,
281  const Executor *exec, const PolymorphicObject *po)
282 
283 
290  GKO_LOGGER_REGISTER_EVENT(13, linop_apply_started, const LinOp *A,
291  const LinOp *b, const LinOp *x)
292 
293 
300  GKO_LOGGER_REGISTER_EVENT(14, linop_apply_completed, const LinOp *A,
301  const LinOp *b, const LinOp *x)
302 
303 
312  GKO_LOGGER_REGISTER_EVENT(15, linop_advanced_apply_started, const LinOp *A,
313  const LinOp *alpha, const LinOp *b,
314  const LinOp *beta, const LinOp *x)
315 
316 
325  GKO_LOGGER_REGISTER_EVENT(16, linop_advanced_apply_completed,
326  const LinOp *A, const LinOp *alpha,
327  const LinOp *b, const LinOp *beta, const LinOp *x)
328 
329 
336  GKO_LOGGER_REGISTER_EVENT(17, linop_factory_generate_started,
337  const LinOpFactory *factory, const LinOp *input)
338 
339 
347  GKO_LOGGER_REGISTER_EVENT(18, linop_factory_generate_completed,
348  const LinOpFactory *factory, const LinOp *input,
349  const LinOp *output)
350 
351 
362  GKO_LOGGER_REGISTER_EVENT(19, criterion_check_started,
363  const stop::Criterion *criterion,
364  const size_type &it, const LinOp *r,
365  const LinOp *tau, const LinOp *x,
366  const uint8 &stopping_id,
367  const bool &set_finalized)
368 
369 
385  GKO_LOGGER_REGISTER_EVENT(
386  20, criterion_check_completed, const stop::Criterion *criterion,
387  const size_type &it, const LinOp *r, const LinOp *tau, const LinOp *x,
388  const uint8 &stopping_id, const bool &set_finalized,
389  const Array<stopping_status> *status, const bool &one_changed,
390  const bool &all_converged)
391 
392 
401  GKO_LOGGER_REGISTER_EVENT(21, iteration_complete, const LinOp *solver,
402  const size_type &it, const LinOp *r,
403  const LinOp *x = nullptr,
404  const LinOp *tau = nullptr)
405 
406 
407 #undef GKO_LOGGER_REGISTER_EVENT
408 
412  static constexpr mask_type executor_events_mask =
413  allocation_started_mask | allocation_completed_mask |
414  free_started_mask | free_completed_mask | copy_started_mask |
415  copy_completed_mask;
416 
420  static constexpr mask_type operation_events_mask =
421  operation_launched_mask | operation_completed_mask;
422 
426  static constexpr mask_type polymorphic_object_events_mask =
427  polymorphic_object_create_started_mask |
428  polymorphic_object_create_completed_mask |
429  polymorphic_object_copy_started_mask |
430  polymorphic_object_copy_completed_mask |
431  polymorphic_object_deleted_mask;
432 
436  static constexpr mask_type linop_events_mask =
437  linop_apply_started_mask | linop_apply_completed_mask |
438  linop_advanced_apply_started_mask | linop_advanced_apply_completed_mask;
439 
443  static constexpr mask_type linop_factory_events_mask =
444  linop_factory_generate_started_mask |
445  linop_factory_generate_completed_mask;
446 
450  static constexpr mask_type criterion_events_mask =
451  criterion_check_started_mask | criterion_check_completed_mask;
452 
453 protected:
468  explicit Logger(std::shared_ptr<const gko::Executor> exec,
469  const mask_type &enabled_events = all_events_mask)
470  : exec_{exec}, enabled_events_{enabled_events}
471  {}
472 
473 private:
474  std::shared_ptr<const Executor> exec_;
475  mask_type enabled_events_;
476 };
477 
478 
484 class Loggable {
485 public:
486  virtual ~Loggable() = default;
487 
493  virtual void add_logger(std::shared_ptr<const Logger> logger) = 0;
494 
504  virtual void remove_logger(const Logger *logger) = 0;
505 };
506 
507 
520 template <typename ConcreteLoggable, typename PolymorphicBase = Loggable>
521 class EnableLogging : public Loggable {
522 public:
523  void add_logger(std::shared_ptr<const Logger> logger) override
524  {
525  loggers_.push_back(logger);
526  }
527 
528  void remove_logger(const Logger *logger) override
529  {
530  auto idx = find_if(begin(loggers_), end(loggers_),
531  [&logger](std::shared_ptr<const Logger> l) {
532  return lend(l) == logger;
533  });
534  if (idx != end(loggers_)) {
535  loggers_.erase(idx);
536  } else {
537  throw OutOfBoundsError(__FILE__, __LINE__, loggers_.size(),
538  loggers_.size());
539  }
540  }
541 
542 protected:
543  template <size_type Event, typename... Params>
544  void log(Params &&... params) const
545  {
546  for (auto &logger : loggers_) {
547  logger->template on<Event>(std::forward<Params>(params)...);
548  }
549  }
550 
551  std::vector<std::shared_ptr<const Logger>> loggers_;
552 };
553 
554 
555 } // namespace log
556 } // namespace gko
557 
558 
559 #endif // GKO_CORE_LOGGER_HPP_
Definition: logger.hpp:90
constexpr size_type byte_size
Number of bits in a byte.
Definition: types.hpp:185
void remove_logger(const Logger *logger) override
Removes a logger from the list of subscribed loggers.
Definition: logger.hpp:528
A LinOpFactory represents a higher order mapping which transforms one linear operator into another...
Definition: lin_op.hpp:356
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:94
The Ginkgo namespace.
Definition: abstract_factory.hpp:45
std::uint64_t uint64
64-bit unsigned integral type.
Definition: types.hpp:140
A PolymorphicObject is the abstract base for all "heavy" objects in Ginkgo that behave polymorphicall...
Definition: polymorphic_object.hpp:67
EnableLogging is a mixin which should be inherited by any class which wants to enable logging...
Definition: logger.hpp:521
void add_logger(std::shared_ptr< const Logger > logger) override
Adds a new logger to the list of subscribed loggers.
Definition: logger.hpp:523
OutOfBoundsError is thrown if a memory access is detected to be out-of-bounds.
Definition: exception.hpp:297
Definition: lin_op.hpp:134
Loggable class is an interface which should be implemented by classes wanting to support logging...
Definition: logger.hpp:484
std::uint8_t uint8
8-bit unsigned integral type.
Definition: types.hpp:123
Operations can be used to define functionalities whose implementations differ among devices...
Definition: executor.hpp:173
An Array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the Arr...
Definition: array.hpp:63
std::enable_if< detail::have_ownership< Pointer >), detail::pointee< Pointer > * >::type lend(const Pointer &p)
Returns a non-owning (plain) pointer to the object pointed to by p.
Definition: utils.hpp:249
The Criterion class is a base class for all stopping criteria.
Definition: criterion.hpp:63
The first step in using the Ginkgo library consists of creating an executor.
Definition: executor.hpp:410