1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
/////////////////////////////////////////////////////////////
// //
// Copyright (c) 2003-2011 by The University of Queensland //
// Earth Systems Science Computational Centre (ESSCC) //
// http://www.uq.edu.au/esscc //
// //
// Primary Business: Brisbane, Queensland, Australia //
// Licensed under the Open Software License version 3.0 //
// http://www.opensource.org/licenses/osl-3.0.php //
// //
/////////////////////////////////////////////////////////////
#ifndef __MPI_TIMER_H_
#define __MPI_TIMER_H_
//--- MPI includes ---
#include <mpi.h>
//--- STL includes ---
#include <string>
#include <map>
#include <boost/shared_ptr.hpp>
/*!
* Simple timer class for storing a start and stop time.
*/
class MpiWTimer
{
public:
MpiWTimer();
MpiWTimer(const std::string &name);
void setStart(const double &wTime);
void pause(const double &wTime);
void resume(const double &wTime);
void setStop(const double &wTime, bool elapseIsStopMinusStart=false);
double getTiming() const;
const std::string &getName() const;
bool isPaused() const;
void zeroise();
protected:
void isPaused(bool paused);
private:
std::string m_name;
double m_startTime;
double m_stopTime;
bool m_isPaused;
double m_pauseTime;
double m_resumeTime;
double m_elapsedTime;
};
class MpiWTimers;
/**
* Helper for appending timing data to file, caches an ofstream for a file.
*/
class TimingDataWriter
{
public:
TimingDataWriter(const std::string &fileName, MpiWTimers &timers);
std::ostream &getOStream();
const std::string &getFileName() const;
void writeHeader();
void appendData();
private:
std::string m_fileName;
MpiWTimers *m_pTimers;
bool m_haveWrittenHeader;
typedef boost::shared_ptr<std::ofstream> OFStreamPtr;
OFStreamPtr m_oFStreamPtr;
};
/*!
* Helper class for recording various pieces of timing info.
*/
class MpiWTimers
{
public:
MpiWTimers();
void start(const std::string &name);
void stop(const std::string &name, bool elapseIsStopMinusStart=false);
void pause(const std::string &name);
void resume(const std::string &name);
void zeroise(const std::string &name);
void zeroise();
bool timerExists(const std::string &name) const;
double getTiming(const std::string &name) const;
void writeHeader(std::ostream &oStream);
void appendData(std::ostream &oStream);
void appendData(const std::string &fileName);
void clear();
protected:
void createTimer(const std::string &timerName);
MpiWTimer *findTimer(const std::string &timerName);
const MpiWTimer *findTimer(const std::string &timerName) const;
MpiWTimer &findOrCreateTimer(const std::string &timerName);
TimingDataWriter &getWriter(const std::string &fileName);
private:
typedef std::map<std::string, MpiWTimer> NameMpiWTimerMap;
NameMpiWTimerMap m_timerMap;
typedef std::map<std::string, TimingDataWriter> FileNameWriterMap;
FileNameWriterMap m_fileNameWriterMap;
};
#endif //__MPI_TIMER_H_
|