Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2012-2018 The plumed team
3 : (see the PEOPLE file at the root of the distribution for a list of names)
4 :
5 : See http://www.plumed.org for more information.
6 :
7 : This file is part of plumed, version 2.
8 :
9 : plumed is free software: you can redistribute it and/or modify
10 : it under the terms of the GNU Lesser General Public License as published by
11 : the Free Software Foundation, either version 3 of the License, or
12 : (at your option) any later version.
13 :
14 : plumed is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU Lesser General Public License for more details.
18 :
19 : You should have received a copy of the GNU Lesser General Public License
20 : along with plumed. If not, see <http://www.gnu.org/licenses/>.
21 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 :
23 : #include "Stopwatch.h"
24 : #include "Exception.h"
25 :
26 : #include <cstdio>
27 : #include <iostream>
28 :
29 : /*
30 : Different clocks can be used
31 :
32 : gettimeofday (default):
33 : seems portable
34 : clock_gettime (#define __PLUMED_HAS_CLOCK_GETTIME)
35 : requires linking -lrt (on linux)
36 : */
37 :
38 : #ifdef __PLUMED_HAS_CLOCK_GETTIME
39 : #include <time.h>
40 : #elif __PLUMED_HAS_GETTIMEOFDAY
41 : // this is the default
42 : #include <sys/time.h>
43 : #endif
44 :
45 : using namespace std;
46 :
47 : namespace PLMD {
48 :
49 : // this is needed for friend operators
50 340 : std::ostream& operator<<(std::ostream&os,const Stopwatch&sw) {
51 340 : return sw.log(os);
52 : }
53 :
54 503492 : Stopwatch::Time::operator double()const {
55 503492 : return sec+0.000000001*nsec;
56 : }
57 :
58 640759 : Stopwatch::Time Stopwatch::Time::get() {
59 640759 : Time t;
60 : #ifdef __PLUMED_HAS_CLOCK_GETTIME
61 : timespec ts;
62 640759 : clock_gettime(CLOCK_REALTIME,&ts);
63 640759 : t.sec=ts.tv_sec;
64 640759 : t.nsec=ts.tv_nsec;
65 : #elif __PLUMED_HAS_GETTIMEOFDAY
66 : timeval tv;
67 : gettimeofday(&tv,NULL);
68 : t.sec=tv.tv_sec;
69 : t.nsec=1000*tv.tv_usec;
70 : #else
71 : t.sec=0;
72 : t.nsec=0;
73 : #endif
74 640759 : return t;
75 : }
76 :
77 123594 : void Stopwatch::Time::reset() {
78 123594 : sec=0;
79 123594 : nsec=0;
80 123594 : }
81 :
82 657999 : Stopwatch::Time::Time():
83 657999 : sec(0),nsec(0) { }
84 :
85 320372 : Stopwatch::Time Stopwatch::Time::operator-(const Time&t2)const {
86 320372 : Time t(*this);
87 320372 : if(t.nsec<t2.nsec) {
88 1004 : t.sec--;
89 1004 : t.nsec+=1000000000;
90 : }
91 320372 : plumed_assert(t.nsec>=t2.nsec);
92 320372 : t.nsec-=t2.nsec;
93 320372 : t.sec-=t2.sec;
94 320372 : return t;
95 : }
96 :
97 443966 : const Stopwatch::Time & Stopwatch::Time::operator+=(const Time&t2) {
98 443966 : Time &t(*this);
99 443966 : t.nsec+=t2.nsec;
100 443966 : if(t.nsec>1000000000) {
101 702 : t.nsec-=1000000000;
102 702 : t.sec++;
103 : }
104 443966 : t.sec+=t2.sec;
105 443966 : return t;
106 : }
107 :
108 3448 : Stopwatch::Watch::Watch():
109 3448 : cycles(0),running(0) { }
110 :
111 320387 : void Stopwatch::Watch::start() {
112 320387 : running++;
113 320387 : lastStart=Time::get();
114 320387 : }
115 :
116 123594 : void Stopwatch::Watch::stop() {
117 123594 : pause();
118 123594 : cycles++;
119 123594 : total+=lap;
120 123594 : if(lap>max)max=lap;
121 123594 : if(min>lap || cycles==1)min=lap;
122 123594 : lap.reset();
123 123594 : }
124 :
125 320387 : void Stopwatch::Watch::pause() {
126 320387 : plumed_assert(running>0);
127 320387 : running--;
128 640774 : if(running!=0) return;
129 320372 : lap+=Time::get()-lastStart;
130 : }
131 :
132 320387 : void Stopwatch::start(const std::string & name) {
133 320387 : watches[name].start();
134 320387 : }
135 :
136 123594 : void Stopwatch::stop(const std::string & name) {
137 123594 : watches[name].stop();
138 123594 : }
139 :
140 196793 : void Stopwatch::pause(const std::string & name) {
141 196793 : watches[name].pause();
142 196793 : }
143 :
144 :
145 340 : std::ostream& Stopwatch::log(std::ostream&os)const {
146 : char buffer[1000];
147 340 : buffer[0]=0;
148 340 : for(unsigned i=0; i<40; i++) os<<" ";
149 340 : os<<" Cycles Total Average Minumum Maximum\n";
150 2619 : for(map<string,Watch>::const_iterator it=watches.begin(); it!=watches.end(); ++it) {
151 2279 : const Watch&t((*it).second);
152 2279 : std::string name((*it).first);
153 2279 : os<<name;
154 2279 : for(unsigned i=name.length(); i<40; i++) os<<" ";
155 2279 : std::sprintf(buffer,"%12u %12.6f %12.6f %12.6f %12.6f\n", t.cycles, double(t.total), double(t.total/t.cycles), double(t.min),double(t.max));
156 2279 : os<<buffer;
157 2279 : }
158 340 : return os;
159 : }
160 :
161 2523 : }
162 :
163 :
164 :
165 :
|