Ginkgo  Generated from pipelines/1330831941 branch based on master. Ginkgo version 1.8.0
A numerical linear algebra library targeting many-core architectures
performance_hint.hpp
1 // SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_LOG_PERFORMANCE_HINT_HPP_
6 #define GKO_PUBLIC_CORE_LOG_PERFORMANCE_HINT_HPP_
7 
8 
9 #include <fstream>
10 #include <iostream>
11 #include <unordered_map>
12 
13 
14 #include <ginkgo/core/log/logger.hpp>
15 
16 
17 namespace gko {
18 namespace log {
19 
20 
30 class PerformanceHint : public Logger {
31 public:
32  void on_allocation_completed(const Executor* exec,
33  const size_type& num_bytes,
34  const uintptr& location) const override;
35 
36  void on_free_completed(const Executor* exec,
37  const uintptr& location) const override;
38 
39  void on_copy_completed(const Executor* from, const Executor* to,
40  const uintptr& location_from,
41  const uintptr& location_to,
42  const size_type& num_bytes) const override;
43 
48  void print_status() const;
49 
62  static std::unique_ptr<PerformanceHint> create(
63  std::ostream& os = std::cerr, size_type allocation_size_limit = 16,
64  size_type copy_size_limit = 16, size_type histogram_max_size = 1024)
65  {
66  return std::unique_ptr<PerformanceHint>(new PerformanceHint(
67  os, allocation_size_limit, copy_size_limit, histogram_max_size));
68  }
69 
70 protected:
71  explicit PerformanceHint(std::ostream& os, size_type allocation_size_limit,
72  size_type copy_size_limit,
73  size_type histogram_max_size)
74  : Logger(mask_),
75  os_(&os),
76  allocation_size_limit_{allocation_size_limit},
77  copy_size_limit_{copy_size_limit},
78  histogram_max_size_{histogram_max_size}
79  {}
80 
81 private:
82  // set a breakpoint here if you want to see where the output comes from!
83  std::ostream& log() const;
84 
85  std::ostream* os_;
86  mutable std::unordered_map<uintptr_t, size_type> allocation_sizes_;
87  mutable std::unordered_map<size_type, int> allocation_histogram_;
88  mutable std::unordered_map<uintptr_t, int> copy_src_histogram_;
89  mutable std::unordered_map<uintptr_t, int> copy_dst_histogram_;
90  size_type allocation_size_limit_;
91  size_type copy_size_limit_;
92  size_type histogram_max_size_;
93  static constexpr Logger::mask_type mask_ =
94  Logger::allocation_completed_mask | Logger::free_completed_mask |
95  Logger::copy_completed_mask;
96  static constexpr const char* prefix_ = "[PERFORMANCE] >>> ";
97 };
98 
99 
100 } // namespace log
101 } // namespace gko
102 
103 
104 #endif // GKO_PUBLIC_CORE_LOG_PERFORMANCE_HINT_HPP_
gko::log::PerformanceHint::print_status
void print_status() const
Writes out the cross-executor writes and allocations that have been stored so far.
gko::uintptr
std::uintptr_t uintptr
Unsigned integer type capable of holding a pointer to void.
Definition: types.hpp:160
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:108
gko::log::PerformanceHint::create
static std::unique_ptr< PerformanceHint > create(std::ostream &os=std::cerr, size_type allocation_size_limit=16, size_type copy_size_limit=16, size_type histogram_max_size=1024)
Creates a PerformanceHint logger.
Definition: performance_hint.hpp:62
gko::log::PerformanceHint::on_allocation_completed
void on_allocation_completed(const Executor *exec, const size_type &num_bytes, const uintptr &location) const override
Executor's allocation completed event.
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::log::PerformanceHint
PerformanceHint is a Logger which analyzes the performance of the application and outputs hints for u...
Definition: performance_hint.hpp:30
gko::log::PerformanceHint::on_copy_completed
void on_copy_completed(const Executor *from, const Executor *to, const uintptr &location_from, const uintptr &location_to, const size_type &num_bytes) const override
Executor's copy completed event.
gko::log::Logger
Definition: logger.hpp:76
gko::log::PerformanceHint::on_free_completed
void on_free_completed(const Executor *exec, const uintptr &location) const override
Executor's free completed event.
gko::Executor
The first step in using the Ginkgo library consists of creating an executor.
Definition: executor.hpp:616