Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2012-2023 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 : namespace PLMD { 33 : 34 : // this is needed for friend operators 35 926 : std::ostream& operator<<(std::ostream&os,const Stopwatch&sw) { 36 926 : return sw.log(os); 37 : } 38 : 39 404988 : Stopwatch::~Stopwatch() { 40 404988 : if(mylog && mylog->isOpen()) { 41 : // Make sure paused watches are stopped. 42 : // this is necessary e.g. to make sure the main watch present in PlumedMain 43 : // is stopped correctly. 44 6935 : for(auto & w : watches) { 45 6012 : if(w.second.state==Watch::State::paused) w.second.start().stop(); 46 : } 47 923 : *mylog << *this; 48 : } 49 404988 : } 50 : 51 926 : std::ostream& Stopwatch::log(std::ostream&os)const { 52 : char buffer[1000]; 53 926 : buffer[0]=0; 54 37966 : for(unsigned i=0; i<40; i++) os<<" "; 55 926 : os<<" Cycles Total Average Minimum Maximum\n"; 56 : 57 : std::vector<std::string> names; 58 6944 : for(const auto & it : watches) names.push_back(it.first); 59 926 : std::sort(names.begin(),names.end()); 60 : 61 : const double frac=1.0/1000000000.0; 62 : 63 6944 : for(const auto & name : names) { 64 : const Watch&t(watches.find(name)->second); 65 : os<<name; 66 148755 : for(unsigned i=name.length(); i<40; i++) os<<" "; 67 6018 : 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); 68 6018 : os<<buffer; 69 : } 70 926 : return os; 71 926 : } 72 : 73 : } 74 : 75 : 76 : 77 :