Ginkgo  Generated from pipelines/3133041809 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
profiler_hook.hpp
1 // SPDX-FileCopyrightText: 2017 - 2026 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_LOG_PROFILER_HOOK_HPP_
6 #define GKO_PUBLIC_CORE_LOG_PROFILER_HOOK_HPP_
7 
8 
9 #include <iostream>
10 #include <unordered_map>
11 
12 #include <ginkgo/config.hpp>
13 #include <ginkgo/core/base/timer.hpp>
14 #include <ginkgo/core/log/logger.hpp>
15 
16 
17 namespace gko {
18 namespace log {
19 
20 
24  memory,
26  operation,
28  object,
30  linop,
32  factory,
34  solver,
36  criterion,
38  user,
40  internal,
41 };
42 
43 
44 class profiling_scope_guard;
45 
46 
57 class ProfilerHook : public Logger {
58 public:
59  using hook_function =
60  std::function<void(const char*, profile_event_category)>;
61 
62  void on_allocation_started(const gko::Executor* exec,
63  const gko::size_type&) const override;
64 
65  void on_allocation_completed(const gko::Executor* exec,
66  const gko::size_type&,
67  const gko::uintptr&) const override;
68 
69  void on_free_started(const gko::Executor* exec,
70  const gko::uintptr&) const override;
71 
72  void on_free_completed(const gko::Executor* exec,
73  const gko::uintptr&) const override;
74 
75  void on_copy_started(const gko::Executor* from, const gko::Executor* to,
76  const gko::uintptr&, const gko::uintptr&,
77  const gko::size_type&) const override;
78 
79  void on_copy_completed(const gko::Executor* from, const gko::Executor* to,
80  const gko::uintptr&, const gko::uintptr&,
81  const gko::size_type&) const override;
82 
83  /* Operation events */
84  void on_operation_launched(const Executor* exec,
85  const Operation* operation) const override;
86 
87  void on_operation_completed(const Executor* exec,
88  const Operation* operation) const override;
89 
90  /* PolymorphicObject events */
92  const Executor* exec, const PolymorphicObject* from,
93  const PolymorphicObject* to) const override;
94 
96  const Executor* exec, const PolymorphicObject* from,
97  const PolymorphicObject* to) const override;
98 
100  const Executor* exec, const PolymorphicObject* from,
101  const PolymorphicObject* to) const override;
102 
104  const Executor* exec, const PolymorphicObject* from,
105  const PolymorphicObject* to) const override;
106 
107  /* LinOp events */
108  void on_linop_apply_started(const LinOp* A, const AbstractMultiVector* b,
109  const AbstractMultiVector* x) const override;
110 
111  void on_linop_apply_completed(const LinOp* A, const AbstractMultiVector* b,
112  const AbstractMultiVector* x) const override;
113 
115  const LinOp* A, const AbstractMultiVector* alpha,
116  const AbstractMultiVector* b, const AbstractMultiVector* beta,
117  const AbstractMultiVector* x) const override;
118 
120  const LinOp* A, const AbstractMultiVector* alpha,
121  const AbstractMultiVector* b, const AbstractMultiVector* beta,
122  const AbstractMultiVector* x) const override;
123 
124  /* LinOpFactory events */
126  const LinOp* input) const override;
127 
129  const LinOpFactory* factory, const LinOp* input,
130  const LinOp* output) const override;
131 
132  /* Criterion events */
134  const size_type& num_iterations,
135  const AbstractMultiVector* residual,
136  const AbstractMultiVector* residual_norm,
137  const AbstractMultiVector* solution,
138  const uint8& stopping_id,
139  const bool& set_finalized) const override;
140 
142  const stop::Criterion* criterion, const size_type& num_iterations,
143  const AbstractMultiVector* residual,
144  const AbstractMultiVector* residual_norm,
145  const AbstractMultiVector* solution, const uint8& stopping_id,
146  const bool& set_finalized, const array<stopping_status>* status,
147  const bool& one_changed, const bool& all_stopped) const override;
148 
150  const stop::Criterion* criterion, const size_type& num_iterations,
151  const AbstractMultiVector* residual,
152  const AbstractMultiVector* residual_norm,
153  const AbstractMultiVector* implicit_sq_resnorm,
154  const AbstractMultiVector* solution, const uint8& stopping_id,
155  const bool& set_finalized, const array<stopping_status>* status,
156  const bool& one_changed, const bool& all_stopped) const override;
157 
158  /* Internal solver events */
160  const LinOp* solver, const AbstractMultiVector* right_hand_side,
161  const AbstractMultiVector* solution, const size_type& num_iterations,
162  const AbstractMultiVector* residual,
163  const AbstractMultiVector* residual_norm,
164  const AbstractMultiVector* implicit_sq_residual_norm,
165  const array<stopping_status>* status, bool stopped) const override;
166 
167  GKO_DEPRECATED(
168  "Please use the version with the additional stopping "
169  "information.")
171  const LinOp* solver, const size_type& num_iterations,
172  const AbstractMultiVector* residual,
173  const AbstractMultiVector* solution,
174  const AbstractMultiVector* residual_norm) const override;
175 
176  GKO_DEPRECATED(
177  "Please use the version with the additional stopping "
178  "information.")
180  const LinOp* solver, const size_type& num_iterations,
181  const AbstractMultiVector* residual,
182  const AbstractMultiVector* solution,
183  const AbstractMultiVector* residual_norm,
184  const AbstractMultiVector* implicit_sq_residual_norm) const override;
185 
186  bool needs_propagation() const override;
187 
196  std::string name);
197 
203  void set_synchronization(bool synchronize);
204 
214  profiling_scope_guard user_range(const char* name) const;
215 
217  constexpr static uint32 color_yellow_argb = 0xFFFFCB05U;
218 
227  static std::shared_ptr<ProfilerHook> create_tau(bool initialize = true);
228 
232  static std::shared_ptr<ProfilerHook> create_vtune();
233 
239  static std::shared_ptr<ProfilerHook> create_nvtx(
240  uint32 color_argb = color_yellow_argb);
241 
245  static std::shared_ptr<ProfilerHook> create_roctx();
246 
252  static std::shared_ptr<ProfilerHook> create_for_executor(
253  std::shared_ptr<const Executor> exec);
254 
255  struct summary_entry {
257  std::string name;
259  std::chrono::nanoseconds inclusive{0};
264  std::chrono::nanoseconds exclusive{0};
267  };
268 
271  std::string name;
273  std::chrono::nanoseconds elapsed{0};
277  std::vector<nested_summary_entry> children{};
278  };
279 
282  public:
283  virtual ~SummaryWriter() = default;
284 
291  virtual void write(const std::vector<summary_entry>& entries,
292  std::chrono::nanoseconds overhead) = 0;
293  };
294 
297  public:
298  virtual ~NestedSummaryWriter() = default;
299 
306  virtual void write_nested(const nested_summary_entry& root,
307  std::chrono::nanoseconds overhead) = 0;
308  };
309 
316  public NestedSummaryWriter {
317  public:
324  TableSummaryWriter(std::ostream& output = std::cerr,
325  std::string header = "Runtime summary");
326 
327  void write(const std::vector<summary_entry>& entries,
328  std::chrono::nanoseconds overhead) override;
329 
330  void write_nested(const nested_summary_entry& root,
331  std::chrono::nanoseconds overhead) override;
332 
333  private:
334  std::ostream* output_;
335  std::string header_;
336  };
337 
354  static std::shared_ptr<ProfilerHook> create_summary(
355  std::shared_ptr<Timer> timer = std::make_shared<CpuTimer>(),
356  std::unique_ptr<SummaryWriter> writer =
357  std::make_unique<TableSummaryWriter>(),
358  bool debug_check_nesting = false);
359 
377  static std::shared_ptr<ProfilerHook> create_nested_summary(
378  std::shared_ptr<Timer> timer = std::make_shared<CpuTimer>(),
379  std::unique_ptr<NestedSummaryWriter> writer =
380  std::make_unique<TableSummaryWriter>(),
381  bool debug_check_nesting = false);
382 
387  static std::shared_ptr<ProfilerHook> create_custom(hook_function begin,
388  hook_function end);
389 
390 private:
391  ProfilerHook(hook_function begin, hook_function end);
392 
393  void maybe_synchronize(const Executor* exec) const;
394 
395  std::string stringify_object(const PolymorphicObject* obj) const;
396 
397  std::unordered_map<const PolymorphicObject*, std::string> name_map_;
398  bool synchronize_;
399  hook_function begin_hook_;
400  hook_function end_hook_;
401 };
402 
403 
408 public:
411 
420  profiling_scope_guard(const char* name, profile_event_category category,
421  ProfilerHook::hook_function begin,
422  ProfilerHook::hook_function end);
423 
426 
428 
430 
431  profiling_scope_guard& operator=(const profiling_scope_guard&) = delete;
432 
433  profiling_scope_guard& operator=(profiling_scope_guard&&) = delete;
434 
435 private:
436  bool empty_;
437  const char* name_;
438  profile_event_category category_;
439  ProfilerHook::hook_function end_;
440 };
441 
442 
443 } // namespace log
444 } // namespace gko
445 
446 
447 #endif // GKO_PUBLIC_CORE_LOG_PROFILER_HOOK_HPP_
gko::log::ProfilerHook::on_allocation_started
void on_allocation_started(const gko::Executor *exec, const gko::size_type &) const override
Executor's allocation started event.
gko::log::ProfilerHook::on_iteration_complete
void on_iteration_complete(const LinOp *solver, const AbstractMultiVector *right_hand_side, const AbstractMultiVector *solution, const size_type &num_iterations, const AbstractMultiVector *residual, const AbstractMultiVector *residual_norm, const AbstractMultiVector *implicit_sq_residual_norm, const array< stopping_status > *status, bool stopped) const override
Register the iteration_complete event which logs every completed iterations.
gko::log::ProfilerHook::create_vtune
static std::shared_ptr< ProfilerHook > create_vtune()
Creates a logger annotating Ginkgo events with VTune ITT ranges.
gko::uint8
std::uint8_t uint8
8-bit unsigned integral type.
Definition: types.hpp:131
gko::log::ProfilerHook::on_criterion_check_started
void on_criterion_check_started(const stop::Criterion *criterion, const size_type &num_iterations, const AbstractMultiVector *residual, const AbstractMultiVector *residual_norm, const AbstractMultiVector *solution, const uint8 &stopping_id, const bool &set_finalized) const override
stop::Criterion's check started event.
gko::log::ProfilerHook::set_object_name
void set_object_name(ptr_param< const PolymorphicObject > obj, std::string name)
Sets the name for an object to be profiled.
gko::log::ProfilerHook::create_tau
static std::shared_ptr< ProfilerHook > create_tau(bool initialize=true)
Creates a logger annotating Ginkgo events with TAU ranges via PerfStubs.
gko::log::profile_event_category::solver
Solver events.
gko::log::ProfilerHook::create_roctx
static std::shared_ptr< ProfilerHook > create_roctx()
Creates a logger annotating Ginkgo events with ROCTX ranges for HIP.
gko::log::ProfilerHook::create_nested_summary
static std::shared_ptr< ProfilerHook > create_nested_summary(std::shared_ptr< Timer > timer=std::make_shared< CpuTimer >(), std::unique_ptr< NestedSummaryWriter > writer=std::make_unique< TableSummaryWriter >(), bool debug_check_nesting=false)
Creates a logger measuring the runtime of Ginkgo events in a nested fashion and printing a summary wh...
gko::log::ProfilerHook::nested_summary_entry::elapsed
std::chrono::nanoseconds elapsed
The total runtime of all invocations of the range in nanoseconds.
Definition: profiler_hook.hpp:273
gko::log::ProfilerHook::color_yellow_argb
constexpr static uint32 color_yellow_argb
The Ginkgo yellow background color as packed 32 bit ARGB value.
Definition: profiler_hook.hpp:217
gko::LinOp
Definition: lin_op.hpp:163
gko::log::profiling_scope_guard
Scope guard that annotates its scope with the provided profiler hooks.
Definition: profiler_hook.hpp:407
gko::log::ProfilerHook::on_linop_apply_started
void on_linop_apply_started(const LinOp *A, const AbstractMultiVector *b, const AbstractMultiVector *x) const override
LinOp's apply started event.
gko::log::ProfilerHook::on_operation_launched
void on_operation_launched(const Executor *exec, const Operation *operation) const override
Executor's operation launched event (method run).
gko::PolymorphicObject
A PolymorphicObject is the abstract base for all "heavy" objects in Ginkgo that behave polymorphicall...
Definition: polymorphic_object.hpp:46
gko::log::profile_event_category::factory
LinOpFactory events.
gko::log::ProfilerHook::SummaryWriter
Receives the results from ProfilerHook::create_summary().
Definition: profiler_hook.hpp:281
gko::log::profile_event_category::criterion
Stopping criterion events.
gko::log::ProfilerHook::summary_entry::name
std::string name
The name of the range.
Definition: profiler_hook.hpp:257
gko::uintptr
std::uintptr_t uintptr
Unsigned integer type capable of holding a pointer to void.
Definition: types.hpp:154
gko::log::ProfilerHook::summary_entry::exclusive
std::chrono::nanoseconds exclusive
The total runtime of all invocations of the range in nanoseconds, excluding the runtime of all nested...
Definition: profiler_hook.hpp:264
gko::log::ProfilerHook::on_linop_apply_completed
void on_linop_apply_completed(const LinOp *A, const AbstractMultiVector *b, const AbstractMultiVector *x) const override
LinOp's apply completed event.
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:102
gko::log::ProfilerHook::needs_propagation
bool needs_propagation() const override
Returns true if this logger, when attached to an Executor, needs to be forwarded all events from obje...
gko::log::ProfilerHook::NestedSummaryWriter
Receives the results from ProfilerHook::create_nested_summary().
Definition: profiler_hook.hpp:296
gko::log::ProfilerHook::on_copy_completed
void on_copy_completed(const gko::Executor *from, const gko::Executor *to, const gko::uintptr &, const gko::uintptr &, const gko::size_type &) const override
Executor's copy completed event.
gko::initialize
std::unique_ptr< Matrix > initialize(size_type stride, std::initializer_list< typename Matrix::value_type > vals, std::shared_ptr< const Executor > exec, TArgs &&... create_args)
Creates and initializes a column-vector.
Definition: dense.hpp:465
gko::log::ProfilerHook::nested_summary_entry::count
int64 count
The total number of invocations of the range.
Definition: profiler_hook.hpp:275
gko::log::profile_event_category::memory
Memory allocation.
gko::log::ProfilerHook::nested_summary_entry
Definition: profiler_hook.hpp:269
gko::log::ProfilerHook::summary_entry
Definition: profiler_hook.hpp:255
gko::log::ProfilerHook::on_allocation_completed
void on_allocation_completed(const gko::Executor *exec, const gko::size_type &, const gko::uintptr &) const override
Executor's allocation completed event.
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:19
gko::log::ProfilerHook::on_linop_advanced_apply_completed
void on_linop_advanced_apply_completed(const LinOp *A, const AbstractMultiVector *alpha, const AbstractMultiVector *b, const AbstractMultiVector *beta, const AbstractMultiVector *x) const override
LinOp's advanced apply completed event.
gko::log::ProfilerHook::set_synchronization
void set_synchronization(bool synchronize)
Should the events call executor->synchronize on operations and copy/allocation? This leads to a certa...
gko::array
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition: array.hpp:26
gko::log::ProfilerHook::user_range
profiling_scope_guard user_range(const char *name) const
Creates a scope guard for a user-defined range to be included in the profile.
gko::log::profile_event_category::object
PolymorphicObject events.
gko::log::ProfilerHook::nested_summary_entry::name
std::string name
The name of the range.
Definition: profiler_hook.hpp:271
gko::log::profile_event_category::user
User-defined events.
gko::log::ProfilerHook::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::uint32
std::uint32_t uint32
32-bit unsigned integral type.
Definition: types.hpp:142
gko::log::ProfilerHook
This Logger can be used to annotate the execution of Ginkgo functionality with profiler-specific rang...
Definition: profiler_hook.hpp:57
gko::log::ProfilerHook::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::profile_event_category
profile_event_category
Categorization of logger events.
Definition: profiler_hook.hpp:22
gko::log::ProfilerHook::create_summary
static std::shared_ptr< ProfilerHook > create_summary(std::shared_ptr< Timer > timer=std::make_shared< CpuTimer >(), std::unique_ptr< SummaryWriter > writer=std::make_unique< TableSummaryWriter >(), bool debug_check_nesting=false)
Creates a logger measuring the runtime of Ginkgo events and printing a summary when it is destroyed.
gko::ptr_param
This class is used for function parameters in the place of raw pointers.
Definition: utils_helper.hpp:43
gko::log::ProfilerHook::TableSummaryWriter::TableSummaryWriter
TableSummaryWriter(std::ostream &output=std::cerr, std::string header="Runtime summary")
Constructs a writer on an output stream.
gko::log::Logger
Definition: logger.hpp:77
gko::log::ProfilerHook::on_operation_completed
void on_operation_completed(const Executor *exec, const Operation *operation) const override
Executor's operation completed event (method run).
gko::log::ProfilerHook::on_linop_advanced_apply_started
void on_linop_advanced_apply_started(const LinOp *A, const AbstractMultiVector *alpha, const AbstractMultiVector *b, const AbstractMultiVector *beta, const AbstractMultiVector *x) const override
LinOp's advanced apply started event.
gko::log::ProfilerHook::on_free_started
void on_free_started(const gko::Executor *exec, const gko::uintptr &) const override
Executor's free started event.
gko::stop::Criterion
The Criterion class is a base class for all stopping criteria.
Definition: criterion.hpp:36
gko::log::ProfilerHook::create_nvtx
static std::shared_ptr< ProfilerHook > create_nvtx(uint32 color_argb=color_yellow_argb)
Creates a logger annotating Ginkgo events with NVTX ranges for CUDA.
gko::log::ProfilerHook::create_for_executor
static std::shared_ptr< ProfilerHook > create_for_executor(std::shared_ptr< const Executor > exec)
Creates a logger annotating Ginkgo events with the most suitable backend for the given executor: NVTX...
gko::log::ProfilerHook::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::ProfilerHook::on_free_completed
void on_free_completed(const gko::Executor *exec, const gko::uintptr &) const override
Executor's free completed event.
gko::log::ProfilerHook::on_copy_started
void on_copy_started(const gko::Executor *from, const gko::Executor *to, const gko::uintptr &, const gko::uintptr &, const gko::size_type &) const override
Executor's copy started event.
gko::log::ProfilerHook::TableSummaryWriter
Writes the results from ProfilerHook::create_summary() and ProfilerHook::create_nested_summary() to a...
Definition: profiler_hook.hpp:315
gko::log::profile_event_category::linop
LinOp events.
gko::log::ProfilerHook::create_custom
static std::shared_ptr< ProfilerHook > create_custom(hook_function begin, hook_function end)
Creates a logger annotating Ginkgo events with a custom set of functions for range begin and end.
gko::int64
std::int64_t int64
64-bit signed integral type.
Definition: types.hpp:125
gko::log::ProfilerHook::summary_entry::inclusive
std::chrono::nanoseconds inclusive
The total runtime of all invocations of the range in nanoseconds.
Definition: profiler_hook.hpp:259
gko::log::profile_event_category::operation
Kernel execution and data movement.
gko::log::ProfilerHook::TableSummaryWriter::write
void write(const std::vector< summary_entry > &entries, std::chrono::nanoseconds overhead) override
Callback to write out the summary results.
gko::log::ProfilerHook::NestedSummaryWriter::write_nested
virtual void write_nested(const nested_summary_entry &root, std::chrono::nanoseconds overhead)=0
Callback to write out the summary results.
gko::Executor
The first step in using the Ginkgo library consists of creating an executor.
Definition: executor.hpp:617
gko::log::ProfilerHook::TableSummaryWriter::write_nested
void write_nested(const nested_summary_entry &root, std::chrono::nanoseconds overhead) override
Callback to write out the summary results.
gko::log::profiling_scope_guard::~profiling_scope_guard
~profiling_scope_guard()
Calls the range end function if the scope guard was not moved from.
gko::log::ProfilerHook::SummaryWriter::write
virtual void write(const std::vector< summary_entry > &entries, std::chrono::nanoseconds overhead)=0
Callback to write out the summary results.
gko::LinOpFactory
A LinOpFactory represents a higher order mapping which transforms one linear operator into another.
Definition: lin_op.hpp:447
gko::AbstractMultiVector
Abstract interface class for (multi-)vector types.
Definition: multivector.hpp:81
gko::log::ProfilerHook::on_criterion_check_completed
void on_criterion_check_completed(const stop::Criterion *criterion, const size_type &num_iterations, const AbstractMultiVector *residual, const AbstractMultiVector *residual_norm, const AbstractMultiVector *solution, const uint8 &stopping_id, const bool &set_finalized, const array< stopping_status > *status, const bool &one_changed, const bool &all_stopped) const override
stop::Criterion's check completed event.
gko::log::profiling_scope_guard::profiling_scope_guard
profiling_scope_guard()
Creates an empty (moved-from) scope guard.
gko::log::ProfilerHook::nested_summary_entry::children
std::vector< nested_summary_entry > children
The nested ranges inside this range.
Definition: profiler_hook.hpp:277
gko::log::ProfilerHook::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::ProfilerHook::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::ProfilerHook::summary_entry::count
int64 count
The total number of invocations of the range.
Definition: profiler_hook.hpp:266
gko::log::ProfilerHook::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::Operation
Operations can be used to define functionalities whose implementations differ among devices.
Definition: executor.hpp:257