timers.h 3.23 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 *  This file is part of the MR utility library.
 *
 *  This code is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This code is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this code; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/* Copyright (C) 2019-2020 Max-Planck-Society
   Authors: Peter Bell, Martin Reinecke */

Martin Reinecke's avatar
Martin Reinecke committed
22
23
#ifndef DUCC0_TIMERS_H
#define DUCC0_TIMERS_H
Martin Reinecke's avatar
Martin Reinecke committed
24
25
26
27
28

#include <chrono>
#include <string>
#include <map>

Martin Reinecke's avatar
Martin Reinecke committed
29
#include "ducc0/infra/error_handling.h"
Martin Reinecke's avatar
Martin Reinecke committed
30

Martin Reinecke's avatar
Martin Reinecke committed
31
namespace ducc0 {
Martin Reinecke's avatar
Martin Reinecke committed
32

Martin Reinecke's avatar
Martin Reinecke committed
33
34
35
36
37
38
39
40
41
class SimpleTimer
  {
  private:
    using clock = std::chrono::steady_clock;
    clock::time_point starttime;

  public:
    SimpleTimer()
      : starttime(clock::now()) {}
Martin Reinecke's avatar
Martin Reinecke committed
42
43
    void reset()
      { starttime = clock::now(); }
Martin Reinecke's avatar
Martin Reinecke committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
    double operator()() const
      {
      return std::chrono::duration<double>(clock::now() - starttime).count();
      }
  };

class TimerHierarchy
  {
  private:
    using clock = std::chrono::steady_clock;
    class tstack_node
      {
      public:
        tstack_node *parent;
        double accTime;
        std::map<std::string,tstack_node> child;

        tstack_node(tstack_node *parent_)
          : parent(parent_), accTime(0.) {}

        double add_timings(const std::string &prefix,
          std::map<std::string, double> &res) const
          {
          double t_own = accTime;
          for (const auto &nd: child)
            t_own += nd.second.add_timings(prefix+":"+nd.first, res);
          res[prefix] = t_own;
          return t_own;
          }
      };

    clock::time_point last_time;
    tstack_node root;
    tstack_node *curnode;

    void adjust_time()
      {
      auto tnow = clock::now();
      curnode->accTime +=
        std::chrono::duration <double>(tnow - last_time).count();
      last_time = tnow;
      }

    void push_internal(const std::string &name)
      {
      auto it=curnode->child.find(name);
      if (it==curnode->child.end())
        {
Martin Reinecke's avatar
Martin Reinecke committed
92
        MR_assert(name.find(':') == std::string::npos, "reserved character");
Martin Reinecke's avatar
Martin Reinecke committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
        it = curnode->child.insert(make_pair(name,tstack_node(curnode))).first;
        }
      curnode=&(it->second);
      }

  public:
    TimerHierarchy()
      : last_time(clock::now()), root(nullptr), curnode(&root) {}
    void push(const std::string &name)
      {
      adjust_time();
      push_internal(name);
      }
    void pop()
      {
      adjust_time();
      curnode = curnode->parent;
Martin Reinecke's avatar
Martin Reinecke committed
110
      MR_assert(curnode!=nullptr, "tried to pop from empty timer stack");
Martin Reinecke's avatar
Martin Reinecke committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
      }
    void poppush(const std::string &name)
      {
      pop();
      push_internal(name);
      }
    std::map<std::string, double> get_timings()
      {
      adjust_time();
      std::map<std::string, double> res;
      root.add_timings("root", res);
      return res;
      }
  };

126
}
Martin Reinecke's avatar
Martin Reinecke committed
127

Martin Reinecke's avatar
Martin Reinecke committed
128
#endif