Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2012-2020 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 37 749 : std::ostream& operator<<(std::ostream&os,const Stopwatch&sw) { 38 749 : return sw.log(os); 39 : } 40 : 41 6308 : Stopwatch::~Stopwatch() { 42 3154 : 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 5711 : for(auto & w : watches) { 47 5704 : if(w.second.state==Watch::State::paused) w.second.start().stop(); 48 : } 49 746 : *mylog << *this; 50 : } 51 3154 : } 52 : 53 749 : std::ostream& Stopwatch::log(std::ostream&os)const { 54 : char buffer[1000]; 55 749 : buffer[0]=0; 56 30709 : for(unsigned i=0; i<40; i++) os<<" "; 57 749 : os<<" Cycles Total Average Minumum Maximum\n"; 58 : 59 749 : std::vector<std::string> names; 60 5720 : 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 5720 : for(const auto & name : names) { 66 : const Watch&t(watches.find(name)->second); 67 : os<<name; 68 4971 : for(unsigned i=name.length(); i<40; i++) os<<" "; 69 4971 : 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 4971 : os<<buffer; 71 : } 72 749 : return os; 73 : } 74 : 75 5517 : } 76 : 77 : 78 : 79 :