Teuchos_Time.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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;
00044
00045 inline void seconds_initialize() {
00046 if( seconds_initialized ) return;
00047 std::cout << "\nCalling Win32 version of Teuchos::seconds_initialize()!\n";
00048
00049 ::QueryPerformanceFrequency( &count_freq );
00050
00051
00052 ::SetThreadPriority( ::GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL );
00053
00054 assert( QueryPerformanceCounter( &start_count ) );
00055 seconds_initialized = true;
00056 }
00057
00058 }
00059
00060 #endif // defined(__INTEL_COMPILER) && defined(_WIN32)
00061
00062 namespace Teuchos {
00063
00064
00065 Time::Time(const std::string& name_in, bool start_in)
00066 : startTime_(0), totalTime_(0), isRunning_(false), name_(name_in), numCalls_(0)
00067 {
00068 if(start_in) this->start();
00069 }
00070
00071 void Time::start(bool reset_in)
00072 {
00073 isRunning_ = true;
00074 if (reset_in) 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
00099
00100
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
00127 const double
00128 sec = (double)( count.QuadPart - start_count.QuadPart ) / count_freq.QuadPart;
00129
00130 return sec;
00131
00132 #elif ICL || defined(_WIN32)
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 }