list_printer.h Source File

CPP API: list_printer.h Source File
list_printer.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Rene Schmieding
5 *
6 * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 #ifndef MIO_TIMER_LIST_PRINTER_H
21 #define MIO_TIMER_LIST_PRINTER_H
22 
25 
26 #include <ostream>
27 #include <list>
28 #include <map>
29 #include <string>
30 
31 namespace mio
32 {
33 namespace timing
34 {
35 
36 struct ListPrinter : public Printer {
43  void print(const std::list<TimerRegistration>& timer_register, std::ostream& out) override
44  {
45  bool is_multithreaded = false;
46  const auto indent = " ";
47  // Write out all timers
48  out << "All Timers: " << timer_register.size() << "\n";
49  for (const auto& [name, scope, timer, thread, rank] : timer_register) {
50  out << indent << qualified_name(name, scope) << ": " << std::scientific
51  << time_in_seconds(timer.get_elapsed_time()) << " (" << rank << ", " << thread << ")\n";
52  is_multithreaded |= thread > 0 || rank > 0;
53  }
54  // Write out timers accumulated over threads by name
55  if (is_multithreaded) {
56  // dedupe list entries from parallel execution
57  std::map<std::string, DurationType> deduper;
58  for (const auto& [name, scope, timer, thread, rank] : timer_register) {
59  deduper[qualified_name(name, scope)] += timer.get_elapsed_time();
60  }
61  out << "Unique Timers (accumulated): " << deduper.size() << "\n";
62  for (const auto& [q_name, time] : deduper) {
63  out << indent << q_name << ": " << std::scientific << time_in_seconds(time) << "\n";
64  }
65  }
66  }
67 };
68 
69 } // namespace timing
70 } // namespace mio
71 
72 #endif // MIO_TIMER_LIST_PRINTER_H
int rank(Comm comm)
Return the rank of the calling process on the given communicator.
Definition: miompi.cpp:63
std::string qualified_name(const std::string &name, const std::string &scope)
If scope is empty, returns name.
Definition: registration.h:48
double time_in_seconds(DurationType duration)
Convert a duration to a (floating point) number of seconds.
Definition: definitions.cpp:41
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
Definition: list_printer.h:36
void print(const std::list< TimerRegistration > &timer_register, std::ostream &out) override
Print the elapsed time of all registered timers in a list.
Definition: list_printer.h:43
Struct with a virtual print method to allow exchanging how TimerRegistrations are evaluated.
Definition: registration.h:54