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 1301 : std::ostream& operator<<(std::ostream&os,const Stopwatch&sw) { 36 1301 : return sw.log(os); 37 : } 38 : 39 806722 : Stopwatch::~Stopwatch() { 40 806722 : 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 9669 : for(auto & w : watches) { 45 8371 : if(w.second.state==Watch::State::paused) w.second.start().stop(); 46 : } 47 1298 : *mylog << *this; 48 : } 49 806722 : } 50 : 51 1301 : std::ostream& Stopwatch::log(std::ostream&os)const { 52 : const std::size_t bufferlen=1000; 53 : char buffer[bufferlen]; 54 1301 : buffer[0]=0; 55 53341 : for(unsigned i=0; i<40; i++) os<<" "; 56 1301 : os<<" Cycles Total Average Minimum Maximum\n"; 57 : 58 : std::vector<std::string> names; 59 9678 : for(const auto & it : watches) names.emplace_back(it.first); 60 1301 : std::sort(names.begin(),names.end()); 61 : 62 : const double frac=1.0/1000000000.0; 63 : 64 9678 : for(const auto & name : names) { 65 8377 : const Watch&t(watches.find(name)->second); 66 : os<<name; 67 207374 : for(unsigned i=name.length(); i<40; i++) os<<" "; 68 8377 : std::snprintf(buffer,bufferlen,"%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); 69 8377 : os<<buffer; 70 : } 71 1301 : return os; 72 1301 : } 73 : 74 : } 75 : 76 : 77 : 78 :