MPQC  3.0.0-alpha
regtime.h
1 //
2 // regtime.h
3 //
4 // Copyright (C) 1996 Limit Point Systems, Inc.
5 //
6 // Author: Curtis Janssen <cljanss@limitpt.com>
7 // Maintainer: LPS
8 //
9 // This file is part of the SC Toolkit.
10 //
11 // The SC Toolkit is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU Library General Public License as published by
13 // the Free Software Foundation; either version 2, or (at your option)
14 // any later version.
15 //
16 // The SC Toolkit is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU Library General Public License for more details.
20 //
21 // You should have received a copy of the GNU Library General Public License
22 // along with the SC Toolkit; see the file COPYING.LIB. If not, write to
23 // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 //
25 // The U.S. Government is granted a limited license as per AL 91-7.
26 //
27 
28 #ifndef _util_misc_regtime_h
29 #define _util_misc_regtime_h
30 
31 #include <iostream>
32 #include <string>
33 #include <list>
34 #ifdef MPQC_NEW_FEATURES
35 #include <chrono>
36 #endif
37 #include <mpqc_config.h>
38 #include <util/class/class.h>
39 
40 namespace sc {
41 
42 #if MPQC_NEW_FEATURES
43 class MultiThreadTimer;
44 #endif
45 
47 class TimedRegion {
48  private:
49  char *name_;
50  TimedRegion *up_;
51  TimedRegion *subregions_;
52  TimedRegion *next_;
53  TimedRegion *prev_;
54  double cpu_time_;
55  double wall_time_;
56  double cpu_enter_;
57  double wall_enter_;
58  double flops_;
59  double flops_enter_;
60 
61  TimedRegion *insert_after(const char *name);
62  TimedRegion *insert_before(const char *name);
63  public:
64  TimedRegion(const char *name);
65  ~TimedRegion();
66  const char *name() const { return name_; }
67  TimedRegion *findinsubregion(const char *);
68  void acquire_subregion(TimedRegion*);
69  void cpu_enter(double);
70  void wall_enter(double);
71  void flops_enter(double);
72  void cpu_exit(double);
73  void wall_exit(double);
74  void flops_exit(double);
75  void cpu_add(double t) { cpu_time_ += t; }
76  void wall_add(double t) { wall_time_ += t; }
77  void flops_add(double t) { flops_ += t; }
78  TimedRegion *up() const { return up_; }
79  TimedRegion *subregions() const { return subregions_; }
80  TimedRegion *next() const { return next_; }
81  TimedRegion *prev() const { return prev_; }
82 
85  double cpu_time() const { return cpu_time_; }
86  double wall_time() const { return wall_time_; }
87  double flops() const { return flops_; }
89 
91  void merge(const TimedRegion* r);
92 
93  int nregion();
94  void get_region_names(const char *names[]);
95  void get_wall_times(double *);
96  void get_cpu_times(double *);
97  void get_flops(double *);
98  void get_depth(int *, int depth = 0);
99 
100 #if MPQC_NEW_FEATURES
101  friend class MultiThreadTimer;
102 #endif
103  friend class RegionTimer;
104 };
105 
111  protected:
112  int wall_time_;
113  int cpu_time_;
114  int flops_;
115 
116  TimedRegion *top_;
117  TimedRegion *current_;
118  TimedRegion *default_;
119  std::list<TimedRegion*> defaults_;
120 
121  public:
122  RegionTimer(const char *topname = "total",
123  int cpu_time = 0, int wall_time = 1);
124  RegionTimer(const Ref<KeyVal> &);
125  ~RegionTimer();
126  void enter(const char * = 0);
127  void change(const char *newname, const char * oldname = 0);
128  void exit(const char * = 0, bool do_not_throw = false);
129  double cpu_time(const char * name = 0) const;
130  double wall_time(const char * name = 0) const;
131  double flops(const char * name = 0) const;
132  void set_default(const char *);
133  void unset_default();
134  void enter_default();
135  void exit_default();
136  virtual void print(std::ostream& = ExEnv::out0()) const;
137  void reset();
138 
140  void merge(const Ref<RegionTimer> &r);
141 
142  void update_top() const;
143 
144  int nregion() const;
145  void get_region_names(const char *names[]) const;
146  void get_wall_times(double *) const;
147  void get_cpu_times(double *) const;
148  void get_flops(double *) const;
149  void get_depth(int *) const;
150 
153  static double get_wall_time();
155  static double get_cpu_time();
156  static double get_flops();
157 
158 #ifdef MPQC_NEW_FEATURES
159  static std::chrono::time_point<std::chrono::high_resolution_clock>
161  get_time_point();
162 #endif
163 
164  void add_wall_time(const char *, double);
165  void add_cpu_time(const char *, double);
166  void add_flops(const char *, double);
167 
170 
171  static RegionTimer *default_regiontimer();
172  static void set_default_regiontimer(const Ref<RegionTimer> &);
173 };
174 
181 class Timer {
182  Ref<RegionTimer> timer_;
183  int depth_;
184  int default_depth_;
185  bool default_entered_;
186  public:
190  Timer(const char *name);
192  Timer(const std::string &name);
195  Timer(const Ref<RegionTimer> &, const char *name);
197  Timer(const Ref<RegionTimer> &, const std::string &name);
200  Timer(const Ref<RegionTimer> &);
203  Timer();
205  ~Timer();
207  void print(std::ostream& = ExEnv::out0()) const;
212  DEPRECATED void reset(const char * = 0);
213 
219  void enter(const char *region);
220  void enter(const std::string &region) { enter(region.c_str()); }
222 
225  void change(const char *region);
226  void change(const std::string &region) { change(region.c_str()); }
228 
231  void exit(const char *region = 0);
232  void exit(const std::string &region) { this->exit(region.c_str()); }
234 
243  void set_default(const char *region);
244  void set_default(const std::string &r) { set_default(r.c_str()); }
245  void unset_default();
246  void enter_default();
247  void exit_default();
249 
251  double cpu_time(const char *region) const;
252  double cpu_time(const std::string &region) const { return cpu_time(region.c_str()); }
253  double wall_time(const char *region) const;
254  double wall_time(const std::string &region) const { return wall_time(region.c_str()); }
255  double flops(const char *region) const;
256  double flops(const std::string &region) const { return flops(region.c_str()); }
258 
259 #if MPQC_NEW_FEATURES
260  void insert(const MultiThreadTimer& timer);
261 #endif
262 };
263 
264 }
265 
266 #endif
267 
268 // Local Variables:
269 // mode: c++
270 // c-file-style: "CLJ"
271 // End:
sc::TimedRegion::merge
void merge(const TimedRegion *r)
Include the regions in r in this object's regions.
sc::Timer
The Timer class uses RegionTimer to time intervals in an exception safe manner.
Definition: regtime.h:181
sc::Ref
A template class that maintains references counts.
Definition: ref.h:361
sc::RegionTimer::acquire_timed_region
void acquire_timed_region(TimedRegion *r)
Add r as a subregion of current_. After invocation, r is owned by this RegionTimer.
sc::RegionTimer::print
virtual void print(std::ostream &=ExEnv::out0()) const
Print the object.
sc::Timer::reset
DEPRECATED void reset(const char *=0)
Stop timing the current region, if active.
sc::Timer::enter
void enter(const char *region)
Begin timing, using the given timing region name.
sc::RegionTimer::get_wall_time
static double get_wall_time()
sc::MultiThreadTimer
Definition: thread_timer.h:438
sc::TimedRegion::cpu_time
double cpu_time() const
reports the current values of cpu_time, wall_time, and flops
Definition: regtime.h:85
sc::Timer::exit
void exit(const char *region=0)
Exit the current timing region.
sc::TimedRegion
TimedRegion is a helper class for RegionTimer.
Definition: regtime.h:47
sc::Timer::Timer
Timer()
Construct a Timer object using the default RegionTimer and do not begin timing a region.
sc::Timer::print
void print(std::ostream &=ExEnv::out0()) const
Print the timings held by this object's RegionTimer.
sc::RegionTimer::get_cpu_time
static double get_cpu_time()
sc::DescribedClass
Classes which need runtime information about themselves and their relationship to other classes can v...
Definition: class.h:233
sc::Timer::~Timer
~Timer()
Stop timing a region, if active.
sc::ExEnv::out0
static std::ostream & out0()
Return an ostream that writes from node 0.
sc::RegionTimer::merge
void merge(const Ref< RegionTimer > &r)
Include the regions in r in this object's regions.
sc::Timer::change
void change(const char *region)
Change the current timing region to the one specified by the given name.
sc::RegionTimer
The RegionTimer class is used to record the time spent in a section of code.
Definition: regtime.h:110
sc::Timer::set_default
void set_default(const char *region)
Default timing regions are provided as a performance optimization.
sc
Contains all MPQC code up to version 3.
Definition: mpqcin.h:14
sc::Timer::cpu_time
double cpu_time(const char *region) const
Query the timers and flop counter for the region.

Generated at Sun Jan 26 2020 23:24:02 for MPQC 3.0.0-alpha using the documentation package Doxygen 1.8.16.