1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2    Copyright (c) 2012-2021 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 #include "Log.h"
26 
27 #include <cstdio>
28 #include <iostream>
29 #include <vector>
30 #include <algorithm>
31 
32 using namespace std;
33 
34 namespace PLMD {
35 
36 // this is needed for friend operators
operator <<(std::ostream & os,const Stopwatch & sw)37 std::ostream& operator<<(std::ostream&os,const Stopwatch&sw) {
38   return sw.log(os);
39 }
40 
~Stopwatch()41 Stopwatch::~Stopwatch() {
42   if(mylog && mylog->isOpen()) {
43 // Make sure paused watches are stopped.
44 // this is necessary e.g. to make sure the main watch present in PlumedMain
45 // is stopped correctly.
46     for(auto & w : watches) {
47       if(w.second.state==Watch::State::paused) w.second.start().stop();
48     }
49     *mylog << *this;
50   }
51 }
52 
log(std::ostream & os) const53 std::ostream& Stopwatch::log(std::ostream&os)const {
54   char buffer[1000];
55   buffer[0]=0;
56   for(unsigned i=0; i<40; i++) os<<" ";
57   os<<"      Cycles        Total      Average      Minimum      Maximum\n";
58 
59   std::vector<std::string> names;
60   for(const auto & it : watches) names.push_back(it.first);
61   std::sort(names.begin(),names.end());
62 
63   const double frac=1.0/1000000000.0;
64 
65   for(const auto & name : names) {
66     const Watch&t(watches.find(name)->second);
67     os<<name;
68     for(unsigned i=name.length(); i<40; i++) os<<" ";
69     std::sprintf(buffer,"%12u %12.6f %12.6f %12.6f %12.6f\n", t.cycles, frac*t.total, frac*t.total/t.cycles, frac*t.min,frac*t.max);
70     os<<buffer;
71   }
72   return os;
73 }
74 
75 }
76 
77 
78 
79 
80