Teuchos - Trilinos Tools Package  Version of the Day
Teuchos_Time.cpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 // Kris
43 // 07.08.03 -- Move into Teuchos package/namespace
44 
45 #include "Teuchos_Time.hpp"
46 
47 #if defined(__INTEL_COMPILER) && defined(_WIN32)
48 
49 #define WIN32_LEAN_AND_MEAN
50 #include <windows.h>
51 #include <cassert>
52 
53 namespace {
54 
55 bool seconds_initialized = false;
56 LARGE_INTEGER start_count, count_freq; // counts per sec.
57 
58 inline void seconds_initialize() {
59  if( seconds_initialized ) return;
60  std::cout << "\nCalling Win32 version of Teuchos::seconds_initialize()!\n";
61  // Figure out how often the performance counter increments
62  ::QueryPerformanceFrequency( &count_freq );
63  // Set this thread's priority as high as reasonably possible to prevent
64  // timeslice interruptions
65  ::SetThreadPriority( ::GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL );
66  // Get the first count.
67  assert( QueryPerformanceCounter( &start_count ) );
68  seconds_initialized = true;
69 }
70 
71 } // end namespace
72 
73 #endif // defined(__INTEL_COMPILER) && defined(_WIN32)
74 
75 namespace Teuchos {
76 
77 //=============================================================================
78 Time::Time(const std::string& name_in, bool start_in)
79  : startTime_(0), totalTime_(0), isRunning_(false), enabled_ (true), name_(name_in), numCalls_(0)
80 {
81  if(start_in) this->start();
82 }
83 
84 void Time::start(bool reset_in)
85 {
86  if (enabled_) {
87  isRunning_ = true;
88  if (reset_in) totalTime_ = 0;
89  startTime_ = wallTime();
90  }
91 }
92 
93 double Time::stop()
94 {
95  if (enabled_) {
96  if (isRunning_) {
97  totalTime_ += ( wallTime() - startTime_ );
98  isRunning_ = false;
99  startTime_ = 0;
100  }
101  }
102  return totalTime_;
103 }
104 
105 double Time::totalElapsedTime(bool readCurrentTime) const
106 {
107  if(readCurrentTime)
108  return wallTime() - startTime_ + totalTime_;
109  return totalTime_;
110 }
111 
112 void Time::reset () {
113  totalTime_ = 0;
114  numCalls_ = 0;
115 }
116 
117 void Time::disable () {
118  enabled_ = false;
119 }
120 
121 void Time::enable () {
122  enabled_ = true;
123 }
124 
126  if (enabled_) {
127  ++numCalls_;
128  }
129 }
130 
132 {
133  /* KL: warning: this code is probably not portable! */
134  /* HT: have added some preprocessing to address problem compilers */
135  /* RAB: I modifed so that timer will work if MPI support is compiled in but not initialized */
136 
137 #ifdef HAVE_MPI
138 
139  int mpiInitialized;
140  MPI_Initialized(&mpiInitialized);
141 
142  if( mpiInitialized ) {
143 
144  return(MPI_Wtime());
145 
146  }
147  else {
148 
149  clock_t start;
150 
151  start = clock();
152  return( (double)( start ) / CLOCKS_PER_SEC );
153 
154  }
155 
156 #elif defined(__INTEL_COMPILER) && defined(_WIN32)
157 
158  seconds_initialize();
159  LARGE_INTEGER count;
160  QueryPerformanceCounter( &count );
161  // "QuadPart" is a 64 bit integer (__int64). VC++ supports them!
162  const double
163  sec = (double)( count.QuadPart - start_count.QuadPart ) / count_freq.QuadPart;
164  //std::cout << "ticks = " << ticks << ", sec = " << sec << std::endl;
165  return sec;
166 
167 #elif ICL || defined(_WIN32)
168 
169  clock_t start;
170 
171  start = clock();
172  return( (double)( start ) / CLOCKS_PER_SEC );
173 
174 #else
175 
176 # ifndef MINGW
177  struct timeval tp;
178  static long start = 0, startu;
179  if (!start)
180  {
181  gettimeofday(&tp, NULL);
182  start = tp.tv_sec;
183  startu = tp.tv_usec;
184  return(0.0);
185  }
186  gettimeofday(&tp, NULL);
187  return( ((double) (tp.tv_sec - start)) + (tp.tv_usec-startu)/1000000.0 );
188 # else // MINGW
189  return( (double) clock() / CLOCKS_PER_SEC );
190 # endif // MINGW
191 
192 #endif
193 
194 }
195 
196 } // namespace Teuchos
void disable()
"Disable" this timer, so that it ignores calls to start() and stop().
Basic wall-clock timer class.
void reset()
Reset the cummulative time and call count.
void start(bool reset=false)
Start the timer, if the timer is enabled (see disable()).
double stop()
Stop the timer, if the timer is enabled (see disable()).
Time(const std::string &name, bool start=false)
Constructor.
void enable()
"Enable" this timer, so that it (again) respects calls to start() and stop().
double totalElapsedTime(bool readCurrentTime=false) const
The total time in seconds accumulated by this timer.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
static double wallTime()
Current wall-clock time in seconds.
void incrementNumCalls()
Increment the number of times this timer has been called, if the timer is enabled (see disable())...