ESyS-Particle  4.0.1
Timer.h
00001 
00002 //                                                         //
00003 // Copyright (c) 2003-2011 by The University of Queensland //
00004 // Earth Systems Science Computational Centre (ESSCC)      //
00005 // http://www.uq.edu.au/esscc                              //
00006 //                                                         //
00007 // Primary Business: Brisbane, Queensland, Australia       //
00008 // Licensed under the Open Software License version 3.0    //
00009 // http://www.opensource.org/licenses/osl-3.0.php          //
00010 //                                                         //
00012 
00013 #ifndef __MPI_TIMER_H_
00014 #define __MPI_TIMER_H_
00015 
00016 //--- MPI includes ---
00017 #include <mpi.h>
00018 
00019 //--- STL includes ---
00020 #include <string>
00021 #include <map>
00022 
00023 #include <boost/shared_ptr.hpp>
00024 
00028 class MpiWTimer
00029 {
00030 public:
00031   MpiWTimer();
00032 
00033   MpiWTimer(const std::string &name);
00034   void setStart(const double &wTime);
00035   void pause(const double &wTime);
00036   void resume(const double &wTime);
00037   void setStop(const double &wTime, bool elapseIsStopMinusStart=false);
00038   double getTiming() const;
00039   const std::string &getName() const;
00040 
00041   bool isPaused() const;
00042 
00043   void zeroise();
00044 protected:
00045   void isPaused(bool paused);
00046   
00047 private:
00048   std::string m_name;
00049   double      m_startTime;
00050   double      m_stopTime;
00051   bool        m_isPaused;
00052   double      m_pauseTime;
00053   double      m_resumeTime;
00054   double      m_elapsedTime;
00055 };
00056 
00057 class MpiWTimers;
00058 
00062 class TimingDataWriter
00063 {
00064 public:
00065   TimingDataWriter(const std::string &fileName, MpiWTimers &timers);
00066 
00067   std::ostream &getOStream();
00068   
00069   const std::string &getFileName() const;
00070 
00071   void writeHeader();
00072 
00073   void appendData();
00074 
00075 private:
00076   std::string      m_fileName;
00077   MpiWTimers       *m_pTimers;
00078   bool             m_haveWrittenHeader;
00079   typedef boost::shared_ptr<std::ofstream> OFStreamPtr;
00080   OFStreamPtr      m_oFStreamPtr;
00081 };
00082 
00086 class MpiWTimers
00087 {
00088 public:
00089   MpiWTimers();
00090   
00091   void start(const std::string &name);
00092   void stop(const std::string &name, bool elapseIsStopMinusStart=false);
00093   void pause(const std::string &name);
00094   void resume(const std::string &name);
00095   void zeroise(const std::string &name);
00096   void zeroise();
00097   bool timerExists(const std::string &name) const;
00098   double getTiming(const std::string &name) const;
00099 
00100   void writeHeader(std::ostream &oStream);
00101   void appendData(std::ostream &oStream);
00102   void appendData(const std::string &fileName);
00103   void clear();
00104 
00105 protected:
00106   void createTimer(const std::string &timerName);
00107 
00108   MpiWTimer *findTimer(const std::string &timerName);
00109   const MpiWTimer *findTimer(const std::string &timerName) const;
00110 
00111   MpiWTimer &findOrCreateTimer(const std::string &timerName);
00112 
00113   TimingDataWriter &getWriter(const std::string &fileName);
00114 
00115 private:
00116   typedef std::map<std::string, MpiWTimer> NameMpiWTimerMap;
00117   NameMpiWTimerMap m_timerMap;
00118 
00119   typedef std::map<std::string, TimingDataWriter> FileNameWriterMap;
00120   FileNameWriterMap m_fileNameWriterMap;
00121 };
00122 
00123 #endif //__MPI_TIMER_H_