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
profiler_hook.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_PROFILER_HOOK_HPP_
34#define GKO_PUBLIC_CORE_LOG_PROFILER_HOOK_HPP_
35
36
37#include <iostream>
38#include <unordered_map>
39
40
41#include <ginkgo/config.hpp>
42#include <ginkgo/core/base/timer.hpp>
43#include <ginkgo/core/log/logger.hpp>
44
45
46namespace gko {
47namespace log {
48
49
53 memory,
57 object,
59 linop,
61 factory,
63 solver,
67 user,
70};
71
72
73class profiling_scope_guard;
74
75
86class ProfilerHook : public Logger {
87public:
88 using hook_function =
89 std::function<void(const char*, profile_event_category)>;
90
92 const gko::size_type&) const override;
93
95 const gko::size_type&,
96 const gko::uintptr&) const override;
97
99 const gko::uintptr&) const override;
100
102 const gko::uintptr&) const override;
103
105 const gko::uintptr&, const gko::uintptr&,
106 const gko::size_type&) const override;
107
109 const gko::uintptr&, const gko::uintptr&,
110 const gko::size_type&) const override;
111
112 /* Operation events */
114 const Operation* operation) const override;
115
117 const Operation* operation) const override;
118
119 /* PolymorphicObject events */
121 const Executor* exec, const PolymorphicObject* from,
122 const PolymorphicObject* to) const override;
123
125 const Executor* exec, const PolymorphicObject* from,
126 const PolymorphicObject* to) const override;
127
129 const Executor* exec, const PolymorphicObject* from,
130 const PolymorphicObject* to) const override;
131
133 const Executor* exec, const PolymorphicObject* from,
134 const PolymorphicObject* to) const override;
135
136 /* LinOp events */
137 void on_linop_apply_started(const LinOp* A, const LinOp* b,
138 const LinOp* x) const override;
139
140 void on_linop_apply_completed(const LinOp* A, const LinOp* b,
141 const LinOp* x) const override;
142
143 void on_linop_advanced_apply_started(const LinOp* A, const LinOp* alpha,
144 const LinOp* b, const LinOp* beta,
145 const LinOp* x) const override;
146
147 void on_linop_advanced_apply_completed(const LinOp* A, const LinOp* alpha,
148 const LinOp* b, const LinOp* beta,
149 const LinOp* x) const override;
150
151 /* LinOpFactory events */
153 const LinOp* input) const override;
154
156 const LinOpFactory* factory, const LinOp* input,
157 const LinOp* output) const override;
158
159 /* Criterion events */
161 const size_type& num_iterations,
162 const LinOp* residual,
163 const LinOp* residual_norm,
164 const LinOp* solution,
165 const uint8& stopping_id,
166 const bool& set_finalized) const override;
167
169 const stop::Criterion* criterion, const size_type& num_iterations,
170 const LinOp* residual, const LinOp* residual_norm,
171 const LinOp* solution, const uint8& stopping_id,
172 const bool& set_finalized, const array<stopping_status>* status,
173 const bool& one_changed, const bool& all_stopped) const override;
174
176 const stop::Criterion* criterion, const size_type& num_iterations,
177 const LinOp* residual, const LinOp* residual_norm,
178 const LinOp* implicit_sq_resnorm, const LinOp* solution,
179 const uint8& stopping_id, const bool& set_finalized,
180 const array<stopping_status>* status, const bool& one_changed,
181 const bool& all_stopped) const override;
182
183 /* Internal solver events */
185 const LinOp* solver, const LinOp* right_hand_side,
186 const LinOp* solution, const size_type& num_iterations,
187 const LinOp* residual, const LinOp* residual_norm,
188 const LinOp* implicit_sq_residual_norm,
189 const array<stopping_status>* status, bool stopped) const override;
190
191 GKO_DEPRECATED(
192 "Please use the version with the additional stopping "
193 "information.")
195 const size_type& num_iterations,
196 const LinOp* residual, const LinOp* solution,
197 const LinOp* residual_norm) const override;
198
199 GKO_DEPRECATED(
201 "information.")
203 const LinOp* solver, const size_type& num_iterations,
204 const LinOp* residual, const LinOp* solution,
205 const LinOp* residual_norm,
206 const LinOp* implicit_sq_residual_norm) const override;
207
209
218 std::string name);
219
225 void set_synchronization(bool synchronize);
226
237
240
249 static std::shared_ptr<ProfilerHook> create_tau(bool initialize = true);
250
254 static std::shared_ptr<ProfilerHook> create_vtune();
255
263
267 static std::shared_ptr<ProfilerHook> create_roctx();
268
275 std::shared_ptr<const Executor> exec);
276
279 std::string name;
281 std::chrono::nanoseconds inclusive{0};
286 std::chrono::nanoseconds exclusive{0};
288 int64 count{};
289 };
290
293 std::string name;
295 std::chrono::nanoseconds elapsed{0};
299 std::vector<nested_summary_entry> children{};
300 };
301
304 public:
305 virtual ~SummaryWriter() = default;
306
313 virtual void write(const std::vector<summary_entry>& entries,
314 std::chrono::nanoseconds overhead) = 0;
315 };
316
319 public:
320 virtual ~NestedSummaryWriter() = default;
321
329 std::chrono::nanoseconds overhead) = 0;
330 };
331
338 public NestedSummaryWriter {
339 public:
346 TableSummaryWriter(std::ostream& output = std::cerr,
347 std::string header = "Runtime summary");
348
349 void write(const std::vector<summary_entry>& entries,
350 std::chrono::nanoseconds overhead) override;
351
353 std::chrono::nanoseconds overhead) override;
354
355 private:
356 std::ostream* output_;
357 std::string header_;
358 };
359
376 static std::shared_ptr<ProfilerHook> create_summary(
377 std::shared_ptr<Timer> timer = std::make_shared<CpuTimer>(),
378 std::unique_ptr<SummaryWriter> writer =
379 std::make_unique<TableSummaryWriter>(),
380 bool debug_check_nesting = false);
381
399 static std::shared_ptr<ProfilerHook> create_nested_summary(
400 std::shared_ptr<Timer> timer = std::make_shared<CpuTimer>(),
401 std::unique_ptr<NestedSummaryWriter> writer =
402 std::make_unique<TableSummaryWriter>(),
403 bool debug_check_nesting = false);
404
409 static std::shared_ptr<ProfilerHook> create_custom(hook_function begin,
410 hook_function end);
411
412private:
413 ProfilerHook(hook_function begin, hook_function end);
414
415 void maybe_synchronize(const Executor* exec) const;
416
417 std::string stringify_object(const PolymorphicObject* obj) const;
418
419 std::unordered_map<const PolymorphicObject*, std::string> name_map_;
420 bool synchronize_;
421 hook_function begin_hook_;
422 hook_function end_hook_;
423};
424
425
430public:
433
443 ProfilerHook::hook_function begin,
444 ProfilerHook::hook_function end);
445
448
450
451 // TODO17: unnecessary with guaranteed RVO
454
455 profiling_scope_guard& operator=(const profiling_scope_guard&) = delete;
456
457 profiling_scope_guard& operator=(profiling_scope_guard&&) = delete;
458
459private:
460 bool empty_;
461 const char* name_;
462 profile_event_category category_;
463 ProfilerHook::hook_function end_;
464};
465
466
467} // namespace log
468} // namespace gko
469
470
471#endif // GKO_PUBLIC_CORE_LOG_PROFILER_HOOK_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
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
Definition logger.hpp:104
Receives the results from ProfilerHook::create_nested_summary().
Definition profiler_hook.hpp:318
virtual void write_nested(const nested_summary_entry &root, std::chrono::nanoseconds overhead)=0
Callback to write out the summary results.
Receives the results from ProfilerHook::create_summary().
Definition profiler_hook.hpp:303
virtual void write(const std::vector< summary_entry > &entries, std::chrono::nanoseconds overhead)=0
Callback to write out the summary results.
Writes the results from ProfilerHook::create_summary() and ProfilerHook::create_nested_summary() to a...
Definition profiler_hook.hpp:338
TableSummaryWriter(std::ostream &output=std::cerr, std::string header="Runtime summary")
Constructs a writer on an output stream.
void write(const std::vector< summary_entry > &entries, std::chrono::nanoseconds overhead) override
Callback to write out the summary results.
void write_nested(const nested_summary_entry &root, std::chrono::nanoseconds overhead) override
Callback to write out the summary results.
This Logger can be used to annotate the execution of Ginkgo functionality with profiler-specific rang...
Definition profiler_hook.hpp:86
static std::shared_ptr< ProfilerHook > create_nvtx(uint32 color_argb=color_yellow_argb)
Creates a logger annotating Ginkgo events with NVTX ranges for CUDA.
void set_object_name(ptr_param< const PolymorphicObject > obj, std::string name)
Sets the name for an object to be profiled.
void on_linop_apply_completed(const LinOp *A, const LinOp *b, const LinOp *x) const override
LinOp's apply completed event.
void on_polymorphic_object_move_completed(const Executor *exec, const PolymorphicObject *from, const PolymorphicObject *to) const override
PolymorphicObject's move completed event.
void on_polymorphic_object_copy_started(const Executor *exec, const PolymorphicObject *from, const PolymorphicObject *to) const override
PolymorphicObject's copy started event.
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.
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.
profiling_scope_guard user_range(const char *name) const
Creates a scope guard for a user-defined range to be included in the profile.
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...
void on_polymorphic_object_copy_completed(const Executor *exec, const PolymorphicObject *from, const PolymorphicObject *to) const override
PolymorphicObject's copy completed event.
void on_criterion_check_completed(const stop::Criterion *criterion, const size_type &num_iterations, const LinOp *residual, const LinOp *residual_norm, const LinOp *implicit_sq_resnorm, const LinOp *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.
static std::shared_ptr< ProfilerHook > create_roctx()
Creates a logger annotating Ginkgo events with ROCTX ranges for HIP.
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.
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...
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.
void on_linop_advanced_apply_completed(const LinOp *A, const LinOp *alpha, const LinOp *b, const LinOp *beta, const LinOp *x) const override
LinOp's advanced apply completed event.
static std::shared_ptr< ProfilerHook > create_vtune()
Creates a logger annotating Ginkgo events with VTune ITT ranges.
void on_allocation_completed(const gko::Executor *exec, const gko::size_type &, const gko::uintptr &) const override
Executor's allocation completed event.
static constexpr uint32 color_yellow_argb
The Ginkgo yellow background color as packed 32 bit ARGB value.
Definition profiler_hook.hpp:239
static std::shared_ptr< ProfilerHook > create_tau(bool initialize=true)
Creates a logger annotating Ginkgo events with TAU ranges via PerfStubs.
void set_synchronization(bool synchronize)
Should the events call executor->synchronize on operations and copy/allocation? This leads to a certa...
void on_free_completed(const gko::Executor *exec, const gko::uintptr &) const override
Executor's free completed event.
void on_criterion_check_started(const stop::Criterion *criterion, const size_type &num_iterations, const LinOp *residual, const LinOp *residual_norm, const LinOp *solution, const uint8 &stopping_id, const bool &set_finalized) const override
stop::Criterion's check started event.
void on_iteration_complete(const LinOp *solver, const LinOp *right_hand_side, const LinOp *solution, const size_type &num_iterations, const LinOp *residual, const LinOp *residual_norm, const LinOp *implicit_sq_residual_norm, const array< stopping_status > *status, bool stopped) const override
Register the iteration_complete event which logs every completed iterations.
void on_linop_factory_generate_started(const LinOpFactory *factory, const LinOp *input) const override
LinOp Factory's generate started event.
void on_linop_factory_generate_completed(const LinOpFactory *factory, const LinOp *input, const LinOp *output) const override
LinOp Factory's generate completed event.
void on_linop_advanced_apply_started(const LinOp *A, const LinOp *alpha, const LinOp *b, const LinOp *beta, const LinOp *x) const override
LinOp's advanced apply started event.
void on_operation_completed(const Executor *exec, const Operation *operation) const override
Executor's operation completed event (method run).
bool needs_propagation() const override
Returns true if this logger, when attached to an Executor, needs to be forwarded all events from obje...
void on_criterion_check_completed(const stop::Criterion *criterion, const size_type &num_iterations, const LinOp *residual, const LinOp *residual_norm, const LinOp *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.
void on_polymorphic_object_move_started(const Executor *exec, const PolymorphicObject *from, const PolymorphicObject *to) const override
PolymorphicObject's move started event.
void on_linop_apply_started(const LinOp *A, const LinOp *b, const LinOp *x) const override
LinOp's apply started event.
void on_allocation_started(const gko::Executor *exec, const gko::size_type &) const override
Executor's allocation started event.
void on_free_started(const gko::Executor *exec, const gko::uintptr &) const override
Executor's free started event.
void on_operation_launched(const Executor *exec, const Operation *operation) const override
Executor's operation launched event (method run).
Scope guard that annotates its scope with the provided profiler hooks.
Definition profiler_hook.hpp:429
profiling_scope_guard(profiling_scope_guard &&other)
Move-constructs from another scope guard, other will be left empty.
profiling_scope_guard()
Creates an empty (moved-from) scope guard.
~profiling_scope_guard()
Calls the range end function if the scope guard was not moved from.
profiling_scope_guard(const char *name, profile_event_category category, ProfilerHook::hook_function begin, ProfilerHook::hook_function end)
Creates the scope guard.
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
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:1573
profile_event_category
Categorization of logger events.
Definition profiler_hook.hpp:51
@ criterion
Stopping criterion events.
@ factory
LinOpFactory events.
@ object
PolymorphicObject events.
@ internal
For development use.
@ user
User-defined events.
@ operation
Kernel execution and data movement.
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::uintptr_t uintptr
Unsigned integer type capable of holding a pointer to void.
Definition types.hpp:172
std::uint32_t uint32
32-bit unsigned integral type.
Definition types.hpp:160
std::int64_t int64
64-bit signed integral type.
Definition types.hpp:143
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:120
Definition profiler_hook.hpp:291
std::vector< nested_summary_entry > children
The nested ranges inside this range.
Definition profiler_hook.hpp:299
std::string name
The name of the range.
Definition profiler_hook.hpp:293
std::chrono::nanoseconds elapsed
The total runtime of all invocations of the range in nanoseconds.
Definition profiler_hook.hpp:295
int64 count
The total number of invocations of the range.
Definition profiler_hook.hpp:297
Definition profiler_hook.hpp:277
std::string name
The name of the range.
Definition profiler_hook.hpp:279
This structure is used to represent versions of various Ginkgo modules.
Definition version.hpp:54