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 #ifndef Stopwatch_H
00033 #define Stopwatch_H
00034
00035 #include <iostream>
00036 #include <fstream>
00037 #include <string>
00038 #include <iomanip>
00039
00040 using namespace std;
00041
00042
00043
00044
00045
00046 typedef long long longType;
00047
00048
00049 typedef double microType;
00050
00051 #if defined(_WIN32) || defined(__CYGWIN__)
00052
00053 #include <sys/timeb.h>
00054 #include <sys/types.h>
00055
00056 #include <windows.h>
00057 #define USE_HIGH_PRECISION
00058
00059 struct timezone {
00060 int tz_minuteswest;
00061 int tz_dsttime;
00062 };
00063
00064
00065
00066 #if defined(__CYGWIN__)
00067 struct timeval {
00068 long tv_sec;
00069 microType tv_usec;
00070 };
00071 #endif //defined(__CYGWIN__)
00072
00073 static void gettimeofday(struct timeval* t,void* timezone){
00074 #ifdef USE_HIGH_PRECISION
00075 LARGE_INTEGER timeLI, freqLI;
00076 QueryPerformanceCounter(&timeLI);
00077 QueryPerformanceFrequency(&freqLI);
00078 t->tv_sec=0;
00079 double temp = 1.0e6*(double)(timeLI.QuadPart)/freqLI.QuadPart;
00080 t->tv_usec = temp;
00081 #else
00082 struct _timeb timebuffer;
00083 _ftime( &timebuffer );
00084 t->tv_sec=(long)timebuffer.time;
00085 t->tv_usec=1000*timebuffer.millitm;
00086 #endif
00087 }
00088
00089 #else
00090 #include <sys/time.h>
00091 #endif//defined(_WIN32) || defined(__CYGWIN__)
00092
00093
00094 #include <string>
00095 #include <sstream>
00096 #include <iostream>
00097
00098 #ifdef PARALLEL
00099 #include <mpi.h>
00100 #endif
00101
00106 class Stopwatch
00107 {
00108 private:
00109 longType stime1, stime2, result_us, total_us;
00110 microType micro1, micro2;
00111 bool running;
00112
00113 longType average_us;
00114 int numSamples;
00115
00116 struct timeval tp;
00117 struct timezone tz;
00118
00119 public:
00124 Stopwatch();
00125
00126
00131 void reset();
00132
00133
00138 void start();
00139
00140
00145 void stop();
00146
00147 void lap();
00148 void print(string title);
00149
00154 longType timeMS();
00155
00160 longType timeUS();
00161
00166 bool isRunning();
00167
00168
00173 string toString();
00174
00179 void operator = ( const Stopwatch &rhs);
00180
00185 Stopwatch operator+(Stopwatch & rhs);
00186
00190 friend ostream& operator <<(ostream& strm, Stopwatch &watch);
00191
00192
00193 #ifdef PARALLEL
00194
00195 private:
00200 static bool mpiTypeCreated;
00201
00202
00207 static void buildMpiType();
00208
00213 static void buildMpiReduce();
00214
00215
00220 static void Reduce_Function(Stopwatch *in, Stopwatch *inout,
00221 int *len, MPI_Datatype *dptr);
00222
00223 public:
00224
00228 static MPI_Datatype MPI_TYPE;
00229
00230
00235 static MPI_Op MPI_REDUCE;
00236
00237 #endif
00238
00239 };
00240
00241 #endif