KASKADE 7 development version
timing.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2016-2022 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13#ifndef TIMING_HH
14#define TIMING_HH
15
16#include <chrono>
17#include <memory>
18#include <mutex>
19#include <ostream>
20#include <stack>
21#include <vector>
22
23
24namespace Kaskade
25{
27 class Timings;
28
29 namespace Timings_Details
30 {
31 struct SectionBeacon
32 {
33 std::string name;
34 Timings* timer;
35 };
36 }
38
39
63 class Timings
64 {
65 struct Times;
66
67 public:
71 static Timings& instance();
72
77
82 std::ostream& report(std::ostream& out, int maxlevel=1000) const;
83
103 Timings_Details::SectionBeacon startSection(std::string const& name);
104
122 struct Times const* start(std::string const& name);
123
128 void stop(std::string const& name);
129
137 void stop(struct Times const* ticket = nullptr);
138
139
143 void clear();
144
145 // needed for use of unique_ptr with incomplete type
147
148 private:
149 std::unique_ptr<Times> all;
150 std::stack<Times*> stack;
151 mutable std::mutex mutex;
152 };
153
159 std::ostream& operator<<(std::ostream& out, Timings const& timings);
160
161 // -----------------------------------------------------------------------------------------------
162 // -----------------------------------------------------------------------------------------------
163
181 {
182 public:
187
189
196 ScopedTimingSection(Timings_Details::SectionBeacon const& sec);
197
207 ScopedTimingSection(std::string const& name, bool gather=true);
208
216 ScopedTimingSection(std::string const& name, Timings& timer, bool gather=true);
217
219
226 void restart(std::string const& name);
227
233 ScopedTimingSection& operator=(Timings_Details::SectionBeacon const& sec);
234
241 void start();
242
248 void stop();
249
253 bool isRunning() const;
254
259
260 private:
261 std::string name;
262 Timings* mytimer;
263 bool running;
264 bool gather; // if false, do not call mytimer at all
265 };
266
267 // ----------------------------------------------------------------------------------------------
268
291 {
292 public:
298 TaskTiming(int nTasks, int m=1);
299
305 void start(int task);
306
312 void stop(int task);
313
314 private:
315 using TimePoint = std::chrono::time_point<std::chrono::high_resolution_clock>;
316 TimePoint zero;
317 std::vector<std::array<TimePoint,2>> times;
318 std::vector<int> segment;
319 int nSegments;
320
321 friend std::ostream& operator <<(std::ostream& out, TaskTiming const& tt);
322 };
323
329 std::ostream& operator <<(std::ostream& out, TaskTiming const& tt);
330}
331
332
333
334#endif
A scope guard object that automatically closes a timing section on destruction.
Definition: timing.hh:181
Timings & timer()
Returns the used timer.
ScopedTimingSection(ScopedTimingSection const &)=delete
void restart(std::string const &name)
Start new section.
ScopedTimingSection()
Default constructor creating a not-running unnamed section.
bool isRunning() const
Reports whether the section is currently timed.
ScopedTimingSection & operator=(Timings_Details::SectionBeacon const &sec)
Assignment from a beacon.
ScopedTimingSection(std::string const &name, Timings &timer, bool gather=true)
Constructor using provided timer.
ScopedTimingSection(Timings_Details::SectionBeacon const &sec)
Move constructor.
void stop()
Ensures the section is not timed.
void start()
Ensures the section is timed.
ScopedTimingSection(std::string const &name, bool gather=true)
Constructor using the default timer provided by Timings::instance().
A class that gathers data on task timing and provides gnuplot visualization.
Definition: timing.hh:291
void start(int task)
defines the start of given task.
TaskTiming(int nTasks, int m=1)
Constructor.
void stop(int task)
defines the start of given task.
friend std::ostream & operator<<(std::ostream &out, TaskTiming const &tt)
Supports gathering and reporting execution times information for nested program parts.
Definition: timing.hh:64
void clear()
Resets the timer to an empty state.
void stop(struct Times const *ticket=nullptr)
Stops the timing of given section.
static Timings & instance()
Returns a reference to a single default instance.
Timings()
Constructor.
std::ostream & report(std::ostream &out, int maxlevel=1000) const
Prints a timing report to the given stream.
void stop(std::string const &name)
Stops the timing of given section.
std::ostream & operator<<(std::ostream &out, Timings const &timings)
write a timing report to a stream
Timings_Details::SectionBeacon startSection(std::string const &name)
Provides a beacon to handing over to ScopedTimingSection for starting a section.
struct Times const * start(std::string const &name)
Starts or continues the timing of given section.
std::ostream & operator<<(std::ostream &s, std::vector< Scalar > const &vec)
Definition: dune_bridge.hh:47