Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
includeLinks/MsqTimer.hpp
Go to the documentation of this file.
1 /* *****************************************************************
2  MESQUITE -- The Mesh Quality Improvement Toolkit
3 
4  Copyright 2004 Sandia Corporation and Argonne National
5  Laboratory. Under the terms of Contract DE-AC04-94AL85000
6  with Sandia Corporation, the U.S. Government retains certain
7  rights in this software.
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library 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 GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  (lgpl.txt) along with this library; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 
23  diachin2@llnl.gov, djmelan@sandia.gov, mbrewer@sandia.gov,
24  pknupp@sandia.gov, tleurent@mcs.anl.gov, tmunson@mcs.anl.gov
25 
26  ***************************************************************** */
27 #ifndef MESQUITE_TIMER_HPP
28 #define MESQUITE_TIMER_HPP
29 
30 #ifdef WIN32
31 #pragma warning ( 4 : 4786)
32 #endif
33 
34 #include "Mesquite.hpp"
35 #include "MsqDebug.hpp"
36 
37 #ifdef MSQ_USE_OLD_STD_HEADERS
38 # include <vector.h>
39 # include <utility.h>
40 #else
41 # include <vector>
42 # include <utility>
43 #endif
44 #include <string>
45 
46 #ifdef MSQ_USE_OLD_IO_HEADERS
47  class ostream;
48 #else
49 # include <iosfwd>
50 #endif
51 
52 namespace Mesquite
53 {
54  class Timer
55  {
56  public:
57  Timer();
58 
59  void reset();//resets the timer as if it were just created
60 
61  double since_last_check(); //- return time in seconds since last
62  //- call to since_last_check(). Note that
63  //- calling since_birth() doesn't count
64  //- as a check. The first time this function
65  //- is called, it returns the time since birth.
66 
67  double since_birth() const;//- return time in seconds since
68  //- object was created.
69 
70  private:
71  double atBirth; //- Time at birth
72  double atLastCheck; //- Time at last call to since_last_check()
73  };
74 
75 
76  class StopWatch
77  {
78  public:
79  // Creates the stopwatch. The stopwatch is stopped
80  // until start() is called.
82  :isRunning(false), totalTime(0.0), numStarts(0)
83  {}
84 
85  // Starts the stopwatch. If it was already running,
86  // this function does nothing.
87  void start();
88 
89  // Stops the stopwatch. If it was not already running,
90  // this function does nothing
91  void stop();
92 
93  // Stops the stopwatch and resets the total_time() to zero.
94  void reset();
95 
96  // Returns the total accumulated time. If the stopwatch
97  // is currently running, the time between the last start()
98  // and the current time IS included in total_time().
99  double total_time() const;
100 
103  int number_of_starts() const{
104  return numStarts;
105  }
106 
107 
108  private:
109  bool isRunning;
111  double totalTime;
113  };
114 
116  {
117  public:
118  typedef size_t Key;
119 
120  // Create a new collection
122  {}
123 
124  // Add a stopwatch to the collection. Returns a non-zero
125  // StopWatchCollection::Key if it succeeds, zero if it fails.
126  // If a StopWatch with the given name already exists in the
127  // collection, the Key of the existing StopWatch is returned
128  // if 'fail_if_exists' is false, or zero is returned if
129  // 'fail_if_exists' is true.
130  Key add(const msq_std::string &name, bool fail_if_exists = true);
131 
132  // Gets the Key for an existing stopwatch. If a stopwatch
133  // with the given name does not exist, function returns zero.
134  Key get_key(const msq_std::string &name) const;
135 
137  msq_std::string get_string(const Key key){
138  return mEntries[key-1].first;}
140  void get_string(const Key key, msq_std::string &new_string){
141  new_string=mEntries[key-1].first;}
142 
143  // Remove a specific stopwatch.
144  void remove(const Key key);
145  void remove(const msq_std::string &name)
146  { remove(get_key(name)); }
147 
148  // start a specific stopwatch
149  void start(const Key key);
150  void start(const msq_std::string &name)
151  { start(get_key(name)); }
152 
153  // stop a specific stopwatch
154  void stop(const Key key);
155  void stop(const msq_std::string &name)
156  { stop(get_key(name)); }
157 
158  // reset a specific stopwatch
159  void reset(const Key key);
160  void reset(const msq_std::string &name)
161  { reset(get_key(name)); }
162 
163  // Get the total time for a specific stopwatch, zero if
164  // the stopwatch doesn't exist.
165  double total_time(const Key key) const;
166  double total_time(const msq_std::string &name) const
167  { return total_time(get_key(name)); }
168  // Get the number of times a StopWatch was started.
169  int number_of_starts(const Key key) const;
170  int number_of_starts(const msq_std::string &name) const
171  { return number_of_starts(get_key(name));}
172 
173  //Gets the number of stop watches in the collection
175  return mEntries.size();}
176 
177  void get_keys_sorted_by_time(msq_std::vector<Key> &sorted_keys);
178 
179  private:
180  msq_std::vector< msq_std::pair<msq_std::string, StopWatch> > mEntries;
181  };
182 
183  msq_stdio::ostream& operator<<( msq_stdio::ostream&, StopWatchCollection& coll );
184 
185  // A stopWatchCollection available anywhere
187 
188  inline void print_timing_diagnostics( int debugflag )
189  { MSQ_DBGOUT(debugflag) << GlobalStopWatches; }
190 
191  inline void print_timing_diagnostics( msq_stdio::ostream& stream )
192  { stream << GlobalStopWatches; }
193 
194 
196 {
197  public:
199  inline void start() { GlobalStopWatches.start( mKey ); }
201  private:
203  // Don't allow any of this stuff (make them private)
204  void* operator new(size_t size);
205  FunctionTimer( const FunctionTimer& );
207 };
208 
209 #ifdef MSQ_USE_FUNCTION_TIMERS
210  #define MSQ_FUNCTION_TIMER( NAME ) \
211  static Mesquite::StopWatchCollection::Key _mesquite_timer_key = \
212  Mesquite::GlobalStopWatches.add( NAME, false ); \
213  FunctionTimer _mesquite_timer( _mesquite_timer_key ); \
214  _mesquite_timer.start()
215 #else
216  #define MSQ_FUNCTION_TIMER( NAME )
217 #endif
218 
219 } // namespace Mesquite
220 
221 #endif
Mesquite::StopWatchCollection GlobalStopWatches
void start(const msq_std::string &name)
int number_of_starts() const
Returns the number of times this StopWatch has been started.
msq_std::string get_string(const Key key)
Gets the string associated with a key.
double since_last_check()
void print_timing_diagnostics(int debugflag)
Key get_key(const msq_std::string &name) const
double since_birth() const
FunctionTimer(StopWatchCollection::Key key)
double total_time(const msq_std::string &name) const
Key add(const msq_std::string &name, bool fail_if_exists=true)
StopWatchCollection::Key mKey
void get_keys_sorted_by_time(msq_std::vector< Key > &sorted_keys)
FunctionTimer & operator=(const FunctionTimer &)
void reset(const msq_std::string &name)
void stop(const msq_std::string &name)
void get_string(const Key key, msq_std::string &new_string)
Gets the string associated with a key.
msq_std::vector< msq_std::pair< msq_std::string, StopWatch > > mEntries
msq_stdio::ostream & operator<<(msq_stdio::ostream &s, const Matrix3D &A)
#define MSQ_DBGOUT(flag)
Check debug flag and return ostream associated with flag.
double total_time(const Key key) const
static T_Key key
Definition: vinci_lass.c:76
double total_time() const
int number_of_starts(const Key key) const
int number_of_starts(const msq_std::string &name) const