Teuchos - Trilinos Tools Package Version of the Day
Teuchos_Time.cpp
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), name_(name_in), numCalls_(0)
00080 {
00081   if(start_in) this->start();
00082 }
00083 
00084 void Time::start(bool reset_in)
00085 {
00086   isRunning_ = true;
00087   if (reset_in) totalTime_ = 0;
00088   startTime_ = wallTime();
00089 }
00090 
00091 double Time::stop()
00092 {
00093   if (isRunning_) {
00094     totalTime_ += ( wallTime() - startTime_ );
00095     isRunning_ = false;
00096     startTime_ = 0;
00097   }
00098   return totalTime_;
00099 }
00100 
00101 double Time::totalElapsedTime(bool readCurrentTime) const
00102 {
00103   if(readCurrentTime)
00104     return wallTime() - startTime_ + totalTime_;
00105   return totalTime_;
00106 }
00107 
00108 //=============================================================================
00109 double Time::wallTime() 
00110 {
00111   /* KL: warning: this code is probably not portable! */
00112   /* HT: have added some preprocessing to address problem compilers */
00113   /* RAB: I modifed so that timer will work if MPI support is compiled in but not initialized */
00114 
00115 #ifdef HAVE_MPI
00116 
00117   int mpiInitialized;
00118   MPI_Initialized(&mpiInitialized);
00119 
00120   if( mpiInitialized ) {
00121 
00122     return(MPI_Wtime());
00123 
00124   }
00125   else {
00126 
00127     clock_t start;
00128 
00129     start = clock();
00130     return( (double)( start ) / CLOCKS_PER_SEC );
00131 
00132   }
00133 
00134 #elif defined(__INTEL_COMPILER) && defined(_WIN32)
00135 
00136   seconds_initialize();
00137   LARGE_INTEGER count;
00138   QueryPerformanceCounter( &count );
00139   // "QuadPart" is a 64 bit integer (__int64).  VC++ supports them!
00140   const double
00141     sec = (double)( count.QuadPart - start_count.QuadPart ) / count_freq.QuadPart;
00142   //std::cout << "ticks = " << ticks << ", sec = " << sec << std::endl;
00143   return sec;
00144 
00145 #elif ICL || defined(_WIN32)
00146 
00147   clock_t start;
00148 
00149   start = clock();
00150   return( (double)( start ) / CLOCKS_PER_SEC );
00151 
00152 #else
00153 
00154 #  ifndef MINGW
00155   struct timeval tp;
00156   static long start = 0, startu;
00157   if (!start)
00158   {
00159     gettimeofday(&tp, NULL);
00160     start = tp.tv_sec;
00161     startu = tp.tv_usec;
00162     return(0.0);
00163   }
00164   gettimeofday(&tp, NULL);
00165   return( ((double) (tp.tv_sec - start)) + (tp.tv_usec-startu)/1000000.0 );
00166 #  else // MINGW
00167   return( (double) clock() / CLOCKS_PER_SEC );
00168 #  endif // MINGW
00169 
00170 #endif
00171 
00172 }
00173 
00174 } // namespace Teuchos
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines