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 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 // Kris
00030 // 07.08.03 -- Move into Teuchos package/namespace
00031 
00032 #include "Teuchos_Time.hpp"
00033 
00034 #if defined(__INTEL_COMPILER) && defined(_WIN32)
00035 
00036 #define WIN32_LEAN_AND_MEAN
00037 #include <windows.h>
00038 #include <cassert>
00039 
00040 namespace {
00041 
00042 bool seconds_initialized = false;
00043 LARGE_INTEGER start_count, count_freq;  // counts per sec.
00044 
00045 inline void seconds_initialize() {
00046   if( seconds_initialized ) return;
00047   std::cout << "\nCalling Win32 version of Teuchos::seconds_initialize()!\n";
00048   // Figure out how often the performance counter increments
00049   ::QueryPerformanceFrequency( &count_freq );
00050   // Set this thread's priority as high as reasonably possible to prevent
00051   // timeslice interruptions
00052   ::SetThreadPriority( ::GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL );
00053   // Get the first count.
00054   assert( QueryPerformanceCounter( &start_count ) );
00055   seconds_initialized = true;
00056 }
00057 
00058 } // end namespace
00059 
00060 #endif // defined(__INTEL_COMPILER) && defined(_WIN32)
00061 
00062 namespace Teuchos {
00063 
00064 //=============================================================================
00065 Time::Time(const std::string& name, bool start) 
00066   : startTime_(0), totalTime_(0), isRunning_(false), name_(name), numCalls_(0)
00067 {
00068   if(start) this->start();
00069 }
00070 
00071 void Time::start(bool reset)
00072 {
00073   isRunning_ = true;
00074   if (reset) totalTime_ = 0;
00075   startTime_ = wallTime();
00076 }
00077 
00078 double Time::stop()
00079 {
00080   if (isRunning_) {
00081     totalTime_ += ( wallTime() - startTime_ );
00082     isRunning_ = false;
00083     startTime_ = 0;
00084   }
00085   return totalTime_;
00086 }
00087 
00088 double Time::totalElapsedTime(bool readCurrentTime) const
00089 {
00090   if(readCurrentTime)
00091     return wallTime() - startTime_ + totalTime_;
00092   return totalTime_;
00093 }
00094 
00095 //=============================================================================
00096 double Time::wallTime() 
00097 {
00098   /* KL: warning: this code is probably not portable! */
00099   /* HT: have added some preprocessing to address problem compilers */
00100   /* RAB: I modifed so that timer will work if MPI support is compiled in but not initialized */
00101 
00102 #ifdef HAVE_MPI
00103 
00104   int mpiInitialized;
00105   MPI_Initialized(&mpiInitialized);
00106 
00107   if( mpiInitialized ) {
00108 
00109     return(MPI_Wtime());
00110 
00111   }
00112   else {
00113 
00114     clock_t start;
00115 
00116     start = clock();
00117     return( (double)( start ) / CLOCKS_PER_SEC );
00118 
00119   }
00120 
00121 #elif defined(__INTEL_COMPILER) && defined(_WIN32)
00122 
00123   seconds_initialize();
00124   LARGE_INTEGER count;
00125   QueryPerformanceCounter( &count );
00126   // "QuadPart" is a 64 bit integer (__int64).  VC++ supports them!
00127   const double
00128     sec = (double)( count.QuadPart - start_count.QuadPart ) / count_freq.QuadPart;
00129   //std::cout << "ticks = " << ticks << ", sec = " << sec << std::endl;
00130   return sec;
00131 
00132 #elif ICL
00133 
00134   clock_t start;
00135 
00136   start = clock();
00137   return( (double)( start ) / CLOCKS_PER_SEC );
00138 
00139 #else
00140 
00141 #  ifndef MINGW
00142   struct timeval tp;
00143   static long start = 0, startu;
00144   if (!start)
00145   {
00146     gettimeofday(&tp, NULL);
00147     start = tp.tv_sec;
00148     startu = tp.tv_usec;
00149     return(0.0);
00150   }
00151   gettimeofday(&tp, NULL);
00152   return( ((double) (tp.tv_sec - start)) + (tp.tv_usec-startu)/1000000.0 );
00153 #  else // MINGW
00154   return( (double) clock() / CLOCKS_PER_SEC );
00155 #  endif // MINGW
00156 
00157 #endif
00158 
00159 }
00160 
00161 } // namespace Teuchos

Generated on Tue Oct 20 12:45:26 2009 for Teuchos - Trilinos Tools Package by doxygen 1.4.7