src/Utility/IpplTimings.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /***************************************************************************
00003  *
00004  * The IPPL Framework
00005  * 
00006  *
00007  * Visit http://people.web.psi.ch/adelmann/ for more details
00008  *
00009  ***************************************************************************/
00010 
00011 #ifndef IPPL_TIMINGS_H
00012 #define IPPL_TIMINGS_H
00013 
00014 /*************************************************************************
00015  * IpplTimings - a simple singleton class which lets the user create and
00016  *   timers that can be printed out at the end of the program.
00017  *
00018  * General usage
00019  *  1) create a timer:
00020  *     IpplTimings::TimerRef val = IpplTimings::getTimer("timer name");
00021  *  This will either create a new one, or return a ref to an existing one
00022  *
00023  *  2) start a timer:
00024  *     IpplTimings::startTimer(val);
00025  *  This will start the referenced timer running.  If it is already running,
00026  *  it will not change anything.
00027  *
00028  *  3) stop a timer:
00029  *     IpplTimings::stopTimer(val);
00030  *  This will stop the timer, assuming it was running, and add in the
00031  *  time to the accumulating time for that timer.
00032  *
00033  *  4) print out the results:
00034  *     IpplTimings::print();
00035  *
00036  *************************************************************************/
00037 
00038 // include files
00039 #include "Utility/Pstring.h"
00040 #include "Utility/Timer.h"
00041 #include "Utility/my_auto_ptr.h"
00042 
00043 #ifdef IPPL_STDSTL
00044 #include <vector>
00045 using std::vector;
00046 #include <map>
00047 using std::map;
00048 #else
00049 #include <vector.h>
00050 #include <map.h>
00051 #endif // IPPL_STDSTL
00052 
00053 // a simple class used to store timer values
00054 class IpplTimerInfo
00055 {
00056 public:
00057   // typedef for reference to a timer
00058   typedef int TimerRef;
00059 
00060   // constructor
00061   IpplTimerInfo() : cpuTime(0.0), wallTime(0.0), indx(-1), name("") {
00062     clear();
00063   }
00064 
00065   // destructor
00066   ~IpplTimerInfo() { }
00067 
00068   // timer operations
00069   void start() {
00070     if (!running) {
00071       running = true;
00072       t.stop();
00073       t.clear();
00074       t.start();
00075     }
00076   }
00077 
00078   void stop() {
00079     if (running) {
00080       t.stop();
00081       running = false;
00082       cpuTime += t.cpu_time();
00083       wallTime += t.clock_time();
00084     }
00085   }
00086 
00087   void clear() {
00088     t.stop();
00089     t.clear();
00090     running = false;
00091   }
00092 
00093   // the IPPL timer that this object manages
00094   Timer t;
00095 
00096   // the name of this timer
00097   string name;
00098 
00099   // the accumulated time
00100   double cpuTime;
00101   double wallTime;
00102 
00103   // is the timer turned on right now?
00104   bool running;
00105 
00106   // an index value for this timer
00107   TimerRef indx;
00108 };
00109 
00110 
00111 
00112 class IpplTimings
00113 {
00114 public:
00115   // typedef for reference to a timer
00116   typedef int TimerRef;
00117 
00118   // a typedef for the timer information object
00119   typedef IpplTimerInfo TimerInfo;
00120 
00121 public:
00122   // Default constructor
00123   IpplTimings();
00124 
00125   // Destructor - clear out the existing timers
00126   ~IpplTimings();
00127 
00128   //
00129   // timer manipulation methods
00130   //
00131 
00132   // create a timer, or get one that already exists
00133   static TimerRef getTimer(const char *);
00134 
00135   // start a timer
00136   static void startTimer(TimerRef);
00137 
00138   // stop a timer, and accumulate it's values
00139   static void stopTimer(TimerRef);
00140 
00141   // clear a timer, by turning it off and throwing away its time
00142   static void clearTimer(TimerRef);
00143 
00144   // return a TimerInfo struct by asking for the name
00145   static TimerInfo *infoTimer(const char *nm) {
00146     return TimerMap[string(nm)];
00147   }
00148 
00149   //
00150   // I/O methods
00151   //
00152 
00153   // print the results to standard out
00154   static void print();
00155 
00156   // print the results to a file
00157   static void print(string fn);
00158 
00159 
00160 private:
00161   // type of storage for list of TimerInfo
00162   typedef vector<my_auto_ptr<TimerInfo> > TimerList_t;
00163   typedef map<string, TimerInfo *> TimerMap_t;
00164 
00165   // a list of timer info structs
00166   static TimerList_t TimerList;
00167 
00168   // a map of timers, keyed by string
00169   static TimerMap_t TimerMap;
00170 };
00171 
00172 #endif
00173 
00174 /***************************************************************************
00175  * $RCSfile: IpplTimings.h,v $   $Author: adelmann $
00176  * $Revision: 1.1.1.1 $   $Date: 2003/01/23 07:40:33 $
00177  ***************************************************************************/
00178 
00179 /***************************************************************************
00180  * $RCSfile: addheaderfooter,v $   $Author: adelmann $
00181  * $Revision: 1.1.1.1 $   $Date: 2003/01/23 07:40:17 $
00182  * IPPL_VERSION_ID: $Id: addheaderfooter,v 1.1.1.1 2003/01/23 07:40:17 adelmann Exp $ 
00183  ***************************************************************************/
00184 

Generated on Mon Jan 16 13:23:59 2006 for IPPL by  doxygen 1.4.6