Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Teuchos_Time.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 //
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 // Kris
00043 // 07.08.03 -- Move into Teuchos package/namespace
00044 
00045 #include "Teuchos_Time.hpp"
00046 
00047 #if defined(__INTEL_COMPILER) && defined(_WIN32)
00048 
00049 #define WIN32_LEAN_AND_MEAN
00050 #include <windows.h>
00051 #include <cassert>
00052 
00053 namespace {
00054 
00055 bool seconds_initialized = false;
00056 LARGE_INTEGER start_count, count_freq;  // counts per sec.
00057 
00058 inline void seconds_initialize() {
00059   if( seconds_initialized ) return;
00060   std::cout << "\nCalling Win32 version of Teuchos::seconds_initialize()!\n";
00061   // Figure out how often the performance counter increments
00062   ::QueryPerformanceFrequency( &count_freq );
00063   // Set this thread's priority as high as reasonably possible to prevent
00064   // timeslice interruptions
00065   ::SetThreadPriority( ::GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL );
00066   // Get the first count.
00067   assert( QueryPerformanceCounter( &start_count ) );
00068   seconds_initialized = true;
00069 }
00070 
00071 }       // end namespace
00072 
00073 #endif // defined(__INTEL_COMPILER) && defined(_WIN32)
00074 
00075 namespace Teuchos {
00076 
00077 //=============================================================================
00078 Time::Time(const std::string& name_in, bool start_in)
00079   : startTime_(0), totalTime_(0), isRunning_(false), enabled_ (true), name_(name_in), numCalls_(0)
00080 {
00081   if(start_in) this->start();
00082 }
00083 
00084 void Time::start(bool reset_in)
00085 {
00086   if (enabled_) {
00087     isRunning_ = true;
00088     if (reset_in) totalTime_ = 0;
00089     startTime_ = wallTime();
00090   }
00091 }
00092 
00093 double Time::stop()
00094 {
00095   if (enabled_) {
00096     if (isRunning_) {
00097       totalTime_ += ( wallTime() - startTime_ );
00098       isRunning_ = false;
00099       startTime_ = 0;
00100     }
00101   }
00102   return totalTime_;
00103 }
00104 
00105 double Time::totalElapsedTime(bool readCurrentTime) const
00106 {
00107   if(readCurrentTime)
00108     return wallTime() - startTime_ + totalTime_;
00109   return totalTime_;
00110 }
00111 
00112 void Time::reset () {
00113   totalTime_ = 0;
00114   numCalls_ = 0;
00115 }
00116 
00117 void Time::disable () {
00118   enabled_ = false;
00119 }
00120 
00121 void Time::enable () {
00122   enabled_ = true;
00123 }
00124 
00125 void Time::incrementNumCalls() {
00126   if (enabled_) {
00127     ++numCalls_;
00128   }
00129 }
00130 
00131 double Time::wallTime()
00132 {
00133   /* KL: warning: this code is probably not portable! */
00134   /* HT: have added some preprocessing to address problem compilers */
00135         /* RAB: I modifed so that timer will work if MPI support is compiled in but not initialized */
00136 
00137 #ifdef HAVE_MPI
00138 
00139         int mpiInitialized;
00140         MPI_Initialized(&mpiInitialized);
00141 
00142         if( mpiInitialized ) {
00143 
00144                 return(MPI_Wtime());
00145 
00146         }
00147         else {
00148 
00149                 clock_t start;
00150 
00151                 start = clock();
00152                 return( (double)( start ) / CLOCKS_PER_SEC );
00153 
00154         }
00155 
00156 #elif defined(__INTEL_COMPILER) && defined(_WIN32)
00157 
00158   seconds_initialize();
00159   LARGE_INTEGER count;
00160   QueryPerformanceCounter( &count );
00161   // "QuadPart" is a 64 bit integer (__int64).  VC++ supports them!
00162   const double
00163     sec = (double)( count.QuadPart - start_count.QuadPart ) / count_freq.QuadPart;
00164   //std::cout << "ticks = " << ticks << ", sec = " << sec << std::endl;
00165   return sec;
00166 
00167 #elif ICL || defined(_WIN32)
00168 
00169   clock_t start;
00170 
00171   start = clock();
00172   return( (double)( start ) / CLOCKS_PER_SEC );
00173 
00174 #else
00175 
00176 #  ifndef MINGW
00177   struct timeval tp;
00178   static long start = 0, startu;
00179   if (!start)
00180   {
00181     gettimeofday(&tp, NULL);
00182     start = tp.tv_sec;
00183     startu = tp.tv_usec;
00184     return(0.0);
00185   }
00186   gettimeofday(&tp, NULL);
00187   return( ((double) (tp.tv_sec - start)) + (tp.tv_usec-startu)/1000000.0 );
00188 #  else // MINGW
00189   return( (double) clock() / CLOCKS_PER_SEC );
00190 #  endif // MINGW
00191 
00192 #endif
00193 
00194 }
00195 
00196 } // namespace Teuchos
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines