Ginkgo: ginkgo/core/log/papi.hpp Source File

| Ginkgo Generated from branch based on main. Ginkgo version 1.11.0 A numerical linear algebra library targeting many-core architectures

Loading...
Searching...
No Matches
papi.hpp
1// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#ifndef GKO_PUBLIC_CORE_LOG_PAPI_HPP_
6#define GKO_PUBLIC_CORE_LOG_PAPI_HPP_
7
8
9#include <ginkgo/config.hpp>
10
11
12#if GKO_HAVE_PAPI_SDE
13
14
15#include <cstddef>
16#include <iostream>
17#include <map>
18#include <mutex>
19
20#include <sde_lib.h>
21
22#include <ginkgo/core/base/polymorphic_object.hpp>
23#include <ginkgo/core/log/logger.hpp>
24
25
26namespace gko {
27namespace log {
28
29
30static size_type papi_logger_count = 0;
31static std::mutex papi_count_mutex;
32
33
58template <typename ValueType = default_precision>
59 class Papi : public Logger {
60 GKO_ASSERT_SUPPORTED_VALUE_TYPE;
61
62public:
63 /* Executor events */
64 void on_allocation_started(const Executor* exec,
65 const size_type& num_bytes) const override;
66
67 void on_allocation_completed(const Executor* exec,
68 const size_type& num_bytes,
69 const uintptr& location) const override;
70
71 void on_free_started(const Executor* exec,
72 const uintptr& location) const override;
73
74 void on_free_completed(const Executor* exec,
75 const uintptr& location) const override;
76
77 void on_copy_started(const Executor* from, const Executor* to,
78 const uintptr& location_from,
79 const uintptr& location_to,
80 const size_type& num_bytes) const override;
81
82 void on_copy_completed(const Executor* from, const Executor* to,
83 const uintptr& location_from,
84 const uintptr& location_to,
85 const size_type& num_bytes) const override;
86
87 /* Operation events */
88 void on_operation_launched(const Executor* exec,
89 const Operation* operation) const override;
90
91 void on_operation_completed(const Executor* exec,
92 const Operation* operation) const override;
93
94 /* PolymorphicObject events */
95 void on_polymorphic_object_create_started(
96 const Executor*, const PolymorphicObject* po) const override;
97
98 void on_polymorphic_object_create_completed(
99 const Executor* exec, const PolymorphicObject* input,
100 const PolymorphicObject* output) const override;
101
102 void on_polymorphic_object_copy_started(
103 const Executor* exec, const PolymorphicObject* from,
104 const PolymorphicObject* to) const override;
105
106 void on_polymorphic_object_copy_completed(
107 const Executor* exec, const PolymorphicObject* from,
108 const PolymorphicObject* to) const override;
109
110 void on_polymorphic_object_move_started(
111 const Executor* exec, const PolymorphicObject* from,
112 const PolymorphicObject* to) const override;
113
114 void on_polymorphic_object_move_completed(
115 const Executor* exec, const PolymorphicObject* from,
116 const PolymorphicObject* to) const override;
117
118 void on_polymorphic_object_deleted(
119 const Executor* exec, const PolymorphicObject* po) const override;
120
121 /* LinOp events */
122 void on_linop_apply_started(const LinOp* A, const LinOp* b,
123 const LinOp* x) const override;
124
125 void on_linop_apply_completed(const LinOp* A, const LinOp* b,
126 const LinOp* x) const override;
127
128 void on_linop_advanced_apply_started(const LinOp* A, const LinOp* alpha,
129 const LinOp* b, const LinOp* beta,
130 const LinOp* x) const override;
131
132 void on_linop_advanced_apply_completed(const LinOp* A, const LinOp* alpha,
133 const LinOp* b, const LinOp* beta,
134 const LinOp* x) const override;
135
136 /* LinOpFactory events */
137 void on_linop_factory_generate_started(const LinOpFactory* factory,
138 const LinOp* input) const override;
139
140 void on_linop_factory_generate_completed(
141 const LinOpFactory* factory, const LinOp* input,
142 const LinOp* output) const override;
143
144 void on_criterion_check_completed(
145 const stop::Criterion* criterion, const size_type& num_iterations,
146 const LinOp* residual, const LinOp* residual_norm,
147 const LinOp* solution, const uint8& stopping_id,
148 const bool& set_finalized, const array<stopping_status>* status,
149 const bool& one_changed, const bool& all_converged) const override;
150
151 /* Internal solver events */
152 void on_iteration_complete(const LinOp* solver, const LinOp* b,
153 const LinOp* x, const size_type& num_iterations,
154 const LinOp* residual,
155 const LinOp* residual_norm,
156 const LinOp* implicit_resnorm_sq,
157 const array<stopping_status>* status,
158 bool stopped) const override;
159
160 GKO_DEPRECATED(
161 "Please use the version with the additional stopping "
162 "information.")
163 void on_iteration_complete(const LinOp* solver,
164 const size_type& num_iterations,
165 const LinOp* residual, const LinOp* solution,
166 const LinOp* residual_norm) const override;
167
168 GKO_DEPRECATED(
169 "Please use the version with the additional stopping "
170 "information.")
171 void on_iteration_complete(
172 const LinOp* solver, const size_type& num_iterations,
173 const LinOp* residual, const LinOp* solution,
174 const LinOp* residual_norm,
175 const LinOp* implicit_sq_residual_norm) const override;
176
182 GKO_DEPRECATED("use single-parameter create")
183 static std::shared_ptr<Papi> create(
184 std::shared_ptr<const gko::Executor>,
185 const Logger::mask_type& enabled_events = Logger::all_events_mask)
186 {
187 return Papi::create(enabled_events);
188 }
189
195 static std::shared_ptr<Papi> create(
196 const Logger::mask_type& enabled_events = Logger::all_events_mask)
197 {
198 return std::shared_ptr<Papi>(new Papi(enabled_events), [](auto logger) {
199 auto handle = logger->get_handle();
200 delete logger;
201 papi_sde_shutdown(handle);
202 });
203 }
204
211 const std::string get_handle_name() const { return name; }
212
218 const papi_handle_t get_handle() const { return papi_handle; }
219
220protected:
221 GKO_DEPRECATED("use single-parameter constructor")
222 explicit Papi(
223 std::shared_ptr<const gko::Executor> exec,
224 const Logger::mask_type& enabled_events = Logger::all_events_mask)
225 : Papi(enabled_events)
226 {}
227
228 explicit Papi(
229 const Logger::mask_type& enabled_events = Logger::all_events_mask)
230 : Logger(enabled_events)
231 {
232 std::ostringstream os;
233
234 std::lock_guard<std::mutex> guard(papi_count_mutex);
235 os << "ginkgo" << papi_logger_count;
236 name = os.str();
237 papi_handle = papi_sde_init(name.c_str());
238 papi_logger_count++;
239 }
240
241private:
242 template <typename PointerType>
243 class papi_queue {
244 public:
245 papi_queue(papi_handle_t* handle, const char* counter_name)
246 : handle{handle}, counter_name{counter_name}
247 {}
248
249 ~papi_queue()
250 {
251 for (auto e : data) {
252 std::ostringstream oss;
253 oss << counter_name << "::" << e.first;
254 papi_sde_unregister_counter(*handle, oss.str().c_str());
255 }
256 data.clear();
257 }
258
259 size_type& get_counter(const PointerType* ptr)
260 {
261 const auto tmp = reinterpret_cast<uintptr>(ptr);
262 if (data.find(tmp) == data.end()) {
263 data[tmp] = 0;
264 }
265 auto& value = data[tmp];
266 if (!value) {
267 std::ostringstream oss;
268 oss << counter_name << "::" << tmp;
269 papi_sde_register_counter(*handle, oss.str().c_str(),
270 PAPI_SDE_RO | PAPI_SDE_INSTANT,
271 PAPI_SDE_long_long, &value);
272 }
273 return data[tmp];
274 }
275
276 private:
277 papi_handle_t* handle;
278 const char* counter_name;
279 std::map<std::uintptr_t, size_type> data;
280 };
281
282
283 mutable papi_queue<Executor> allocation_started{&papi_handle,
284 "allocation_started"};
285 mutable papi_queue<Executor> allocation_completed{&papi_handle,
286 "allocation_completed"};
287 mutable papi_queue<Executor> free_started{&papi_handle, "free_started"};
288 mutable papi_queue<Executor> free_completed{&papi_handle, "free_completed"};
289 mutable papi_queue<Executor> copy_started_from{&papi_handle,
290 "copy_started_from"};
291 mutable papi_queue<Executor> copy_started_to{&papi_handle,
292 "copy_started_to"};
293 mutable papi_queue<Executor> copy_completed_from{&papi_handle,
294 "copy_completed_from"};
295 mutable papi_queue<Executor> copy_completed_to{&papi_handle,
296 "copy_completed_to"};
297
298 mutable papi_queue<Executor> operation_launched{&papi_handle,
299 "operation_launched"};
300 mutable papi_queue<Executor> operation_completed{&papi_handle,
301 "operation_completed"};
302
303 mutable papi_queue<Executor> polymorphic_object_create_started{
304 &papi_handle, "polymorphic_object_create_started"};
305 mutable papi_queue<Executor> polymorphic_object_create_completed{
306 &papi_handle, "polymorphic_object_create_completed"};
307 mutable papi_queue<Executor> polymorphic_object_copy_started{
308 &papi_handle, "polymorphic_object_copy_started"};
309 mutable papi_queue<Executor> polymorphic_object_copy_completed{
310 &papi_handle, "polymorphic_object_copy_completed"};
311 mutable papi_queue<Executor> polymorphic_object_move_started{
312 &papi_handle, "polymorphic_object_move_started"};
313 mutable papi_queue<Executor> polymorphic_object_move_completed{
314 &papi_handle, "polymorphic_object_move_completed"};
315 mutable papi_queue<Executor> polymorphic_object_deleted{
316 &papi_handle, "polymorphic_object_deleted"};
317
318 mutable papi_queue<LinOpFactory> linop_factory_generate_started{
319 &papi_handle, "linop_factory_generate_started"};
320 mutable papi_queue<LinOpFactory> linop_factory_generate_completed{
321 &papi_handle, "linop_factory_generate_completed"};
322
323 mutable papi_queue<LinOp> linop_apply_started{&papi_handle,
324 "linop_apply_started"};
325 mutable papi_queue<LinOp> linop_apply_completed{&papi_handle,
326 "linop_apply_completed"};
327 mutable papi_queue<LinOp> linop_advanced_apply_started{
328 &papi_handle, "linop_advanced_apply_started"};
329 mutable papi_queue<LinOp> linop_advanced_apply_completed{
330 &papi_handle, "linop_advanced_apply_completed"};
331
332 mutable std::map<std::uintptr_t, void*> criterion_check_completed;
333
334 mutable papi_queue<LinOp> iteration_complete{&papi_handle,
335 "iteration_complete"};
336
337
338 std::string name{"ginkgo"};
339 papi_handle_t papi_handle;
340};
341
342
343} // namespace log
344} // namespace gko
345
346
347#endif // GKO_HAVE_PAPI_SDE
348#endif // GKO_PUBLIC_CORE_LOG_PAPI_HPP_
gko::Executor
The first step in using the Ginkgo library consists of creating an executor.
Definition executor.hpp:615
gko::LinOpFactory
A LinOpFactory represents a higher order mapping which transforms one linear operator into another.
Definition lin_op.hpp:385
gko::LinOp
Definition lin_op.hpp:117
gko::Operation
Operations can be used to define functionalities whose implementations differ among devices.
Definition executor.hpp:258
gko::PolymorphicObject
A PolymorphicObject is the abstract base for all "heavy" objects in Ginkgo that behave polymorphicall...
Definition polymorphic_object.hpp:52
gko::array
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:166
gko::log::Logger
Definition logger.hpp:74
gko::log::Logger::all_events_mask
static constexpr mask_type all_events_mask
Bitset Mask which activates all events.
Definition logger.hpp:87
gko::log::Papi
Papi is a Logger which logs every event to the PAPI software.
Definition papi.hpp:59
gko::log::Papi::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::log::Papi::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::Papi::create
static std::shared_ptr< Papi > create(std::shared_ptr< const gko::Executor >, const Logger::mask_type &enabled_events=Logger::all_events_mask)
Creates a Papi Logger.
Definition papi.hpp:183
gko::log::Papi::on_linop_advanced_apply_completed
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.
gko::log::Papi::on_iteration_complete
void on_iteration_complete(const LinOp *solver, const LinOp *b, const LinOp *x, const size_type &num_iterations, const LinOp *residual, const LinOp *residual_norm, const LinOp *implicit_resnorm_sq, const array< stopping_status > *status, bool stopped) const override
Register the iteration_complete event which logs every completed iterations.
gko::log::Papi::on_linop_advanced_apply_started
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.
gko::log::Papi::get_handle
const papi_handle_t get_handle() const
Returns the corresponding papi_handle_t for this logger.
Definition papi.hpp:218
gko::log::Papi::on_allocation_started
void on_allocation_started(const Executor *exec, const size_type &num_bytes) const override
Executor's allocation started event.
gko::log::Papi::create
static std::shared_ptr< Papi > create(const Logger::mask_type &enabled_events=Logger::all_events_mask)
Creates a Papi Logger.
Definition papi.hpp:195
gko::log::Papi::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::log::Papi::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::log::Papi::on_polymorphic_object_create_completed
void on_polymorphic_object_create_completed(const Executor *exec, const PolymorphicObject *input, const PolymorphicObject *output) const override
PolymorphicObject's create completed event.
gko::log::Papi::on_polymorphic_object_create_started
void on_polymorphic_object_create_started(const Executor *, const PolymorphicObject *po) const override
PolymorphicObject's create started event.
gko::log::Papi::on_copy_started
void on_copy_started(const Executor *from, const Executor *to, const uintptr &location_from, const uintptr &location_to, const size_type &num_bytes) const override
Executor's copy started event.
gko::log::Papi::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::Papi::on_free_completed
void on_free_completed(const Executor *exec, const uintptr &location) const override
Executor's free completed event.
gko::log::Papi::on_operation_completed
void on_operation_completed(const Executor *exec, const Operation *operation) const override
Executor's operation completed event (method run).
gko::log::Papi::on_polymorphic_object_deleted
void on_polymorphic_object_deleted(const Executor *exec, const PolymorphicObject *po) const override
PolymorphicObject's deleted event.
gko::log::Papi::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::Papi::get_handle_name
const std::string get_handle_name() const
Returns the unique name of this logger, which can be used in the PAPI_read() call.
Definition papi.hpp:211
gko::log::Papi::on_criterion_check_completed
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_converged) const override
stop::Criterion's check completed event.
gko::log::Papi::on_linop_apply_started
void on_linop_apply_started(const LinOp *A, const LinOp *b, const LinOp *x) const override
LinOp's apply started event.
gko::log::Papi::on_free_started
void on_free_started(const Executor *exec, const uintptr &location) const override
Executor's free started event.
gko::log::Papi::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::Papi::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::Papi::on_operation_launched
void on_operation_launched(const Executor *exec, const Operation *operation) const override
Executor's operation launched event (method run).
gko::log::Papi::on_linop_apply_completed
void on_linop_apply_completed(const LinOp *A, const LinOp *b, const LinOp *x) const override
LinOp's apply completed event.
gko::stop::Criterion
The Criterion class is a base class for all stopping criteria.
Definition criterion.hpp:36
gko::log
The logger namespace .
Definition convergence.hpp:22
gko::log::profile_event_category::criterion
@ criterion
Stopping criterion events.
Definition profiler_hook.hpp:36
gko::log::profile_event_category::factory
@ factory
LinOpFactory events.
Definition profiler_hook.hpp:32
gko::log::profile_event_category::operation
@ operation
Kernel execution and data movement.
Definition profiler_hook.hpp:26
gko::solver
The ginkgo Solve namespace.
Definition bicg.hpp:28
gko
The Ginkgo namespace.
Definition abstract_factory.hpp:20
gko::uint8
std::uint8_t uint8
8-bit unsigned integral type.
Definition types.hpp:119
gko::uintptr
std::uintptr_t uintptr
Unsigned integer type capable of holding a pointer to void.
Definition types.hpp:142
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:90
std
STL namespace.