LIBINT  2.1.0-stable
timer.h
1 /*
2  * This file is a part of Libint.
3  * Copyright (C) 2004-2014 Edward F. Valeev
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Library General Public License, version 2,
7  * as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this program. If not, see http://www.gnu.org/licenses/.
16  *
17  */
18 
19 #ifndef _libint2_src_lib_libint_timer_h_
20 #define _libint2_src_lib_libint_timer_h_
21 
22 #include <libint2/cxxstd.h>
23 
24 #if LIBINT2_CPLUSPLUS_STD >= 2011
25 
26 #include <chrono>
27 
28 namespace libint2 {
29 
33  template <size_t N>
34  class Timers {
35  public:
36  typedef std::chrono::duration<double> dur_t;
37  typedef std::chrono::high_resolution_clock clock_t;
38  typedef std::chrono::time_point<clock_t> time_point_t;
39 
40  Timers() {
41  clear();
43  }
44 
46  static time_point_t now() {
47  return clock_t::now();
48  }
49 
53  void set_now_overhead(size_t ns) {
54  overhead_ = std::chrono::nanoseconds(ns);
55  }
56 
58  void start(size_t t) {
59  tstart_[t] = now();
60  }
63  dur_t stop(size_t t) {
64  const auto tstop = now();
65  const dur_t result = (tstop - tstart_[t]) - overhead_;
66  timers_[t] += result;
67  return result;
68  }
70  double read(size_t t) const {
71  return timers_[t].count();
72  }
74  void clear() {
75  for(auto t=0; t!=ntimers; ++t) {
76  timers_[t] = dur_t::zero();
77  tstart_[t] = time_point_t();
78  }
79  }
80 
81  private:
82  constexpr static size_t ntimers = N;
83  dur_t timers_[ntimers];
84  time_point_t tstart_[ntimers];
85  dur_t overhead_; // the duration of now() call ... use this to automatically adjust reported timings is you need fine-grained timing
86  };
87 
88 } // namespace libint2
89 
90 #endif // C++11 or later
91 
92 #endif // header guard
93 
94 
void clear()
resets timers to zero
Definition: timer.h:74
Defaults definitions for various parameters assumed by Libint.
Definition: algebra.cc:23
dur_t stop(size_t t)
stops timer t
Definition: timer.h:63
double read(size_t t) const
reads value (in seconds) of timer t , converted to double
Definition: timer.h:70
static time_point_t now()
returns the current time point
Definition: timer.h:46
void start(size_t t)
starts timer t
Definition: timer.h:58
void set_now_overhead(size_t ns)
use this to report the overhead of now() call; if set, the reported timings will be adjusted for this...
Definition: timer.h:53
Timers aggregates N C++11 "timers"; used to high-resolution profile stages of integral computation...
Definition: timer.h:34