LCOV - code coverage report
Current view: top level - tools - Stopwatch.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 41 41 100.0 %
Date: 2020-11-18 11:20:57 Functions: 10 10 100.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2012-2019 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             : #include <vector>
      29             : #include <algorithm>
      30             : 
      31             : using namespace std;
      32             : 
      33             : namespace PLMD {
      34             : 
      35             : // this is needed for friend operators
      36         644 : std::ostream& operator<<(std::ostream&os,const Stopwatch&sw) {
      37         644 :   return sw.log(os);
      38             : }
      39             : 
      40      469009 : void Stopwatch::Watch::start() {
      41      469009 :   running++;
      42      469009 :   lastStart=std::chrono::high_resolution_clock::now();
      43      469009 : }
      44             : 
      45      183362 : void Stopwatch::Watch::stop() {
      46      183362 :   pause();
      47      183362 :   cycles++;
      48      183362 :   total+=lap;
      49      183362 :   if(lap>max)max=lap;
      50      183362 :   if(min>lap || cycles==1)min=lap;
      51      183362 :   lap=0;
      52      183362 : }
      53             : 
      54      468963 : void Stopwatch::Watch::pause() {
      55      468966 :   plumed_assert(running>0);
      56      468962 :   running--;
      57      468962 :   if(running!=0) return;
      58      937830 :   auto t=std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now()-lastStart);
      59      468915 :   lap+=t.count();
      60             : }
      61             : 
      62      469009 : void Stopwatch::start(const std::string & name) {
      63      469009 :   watches[name].start();
      64      469009 : }
      65             : 
      66      183362 : void Stopwatch::stop(const std::string & name) {
      67      183362 :   watches[name].stop();
      68      183362 : }
      69             : 
      70      285601 : void Stopwatch::pause(const std::string & name) {
      71      285601 :   watches[name].pause();
      72      285600 : }
      73             : 
      74             : 
      75         644 : std::ostream& Stopwatch::log(std::ostream&os)const {
      76             :   char buffer[1000];
      77         644 :   buffer[0]=0;
      78       26404 :   for(unsigned i=0; i<40; i++) os<<" ";
      79         644 :   os<<"      Cycles        Total      Average      Minumum      Maximum\n";
      80             : 
      81         644 :   std::vector<std::string> names;
      82        4829 :   for(const auto & it : watches) names.push_back(it.first);
      83             :   std::sort(names.begin(),names.end());
      84             : 
      85             :   const double frac=1.0/1000000000.0;
      86             : 
      87        4829 :   for(const auto & name : names) {
      88             :     const Watch&t(watches.find(name)->second);
      89             :     os<<name;
      90        4185 :     for(unsigned i=name.length(); i<40; i++) os<<" ";
      91        4185 :     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);
      92        4185 :     os<<buffer;
      93             :   }
      94         644 :   return os;
      95             : }
      96             : 
      97        4839 : }
      98             : 
      99             : 
     100             : 
     101             : 

Generated by: LCOV version 1.13