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 "Stopwatch.h"
00033
00034 Stopwatch::Stopwatch()
00035 {
00036 tz.tz_minuteswest = 0;
00037 tz.tz_dsttime = 0;
00038
00039 reset();
00040
00041 #ifdef PARALLEL
00042 if( !mpiTypeCreated )
00043 {
00044 mpiTypeCreated = true;
00045 buildMpiType();
00046 buildMpiReduce();
00047 }
00048 #endif
00049 }
00050
00051 void Stopwatch::reset()
00052 {
00053 stime1 = 0;
00054 micro1 = 0;
00055 total_us = 0;
00056 running = false;
00057
00058 average_us = 0;
00059 numSamples = -5;
00060 }
00061
00062 void Stopwatch::start()
00063 {
00064 if(running)
00065 {
00066 #ifdef QMC_DEBUG
00067
00068
00069 #endif
00070 }
00071 else
00072 {
00073 gettimeofday(&tp,&tz);
00074 stime1 = tp.tv_sec;
00075 micro1 = tp.tv_usec;
00076 running=true;
00077 }
00078 }
00079
00080 void Stopwatch::stop()
00081 {
00082 if(running)
00083 {
00084 gettimeofday(&tp,&tz);
00085 stime2 = tp.tv_sec;
00086 micro2 = tp.tv_usec;
00087 result_us = (longType)((stime2-stime1)*1e6 + micro2 - micro1);
00088 total_us += result_us;
00089 running = false;
00090 }
00091 else
00092 {
00093
00094 cerr << "ERROR: You just stopped a stopwatch that was not running"
00095 << endl;
00096 cerr << "check to make sure this is not a bug" << endl;
00097 cerr << "exit in void Stopwatch::stop()" << endl;
00098 exit(1);
00099 }
00100 }
00101
00102 void Stopwatch::lap()
00103 {
00104 if(running)
00105 {
00106 gettimeofday(&tp,&tz);
00107 stime2 = tp.tv_sec;
00108 micro2 = tp.tv_usec;
00109 result_us = (longType)((stime2-stime1)*1e6 + micro2 - micro1);
00110 total_us = result_us;
00111 running = false;
00112 }
00113 else
00114 {
00115
00116 cerr << "ERROR: You just stopped a stopwatch that was not running"
00117 << endl;
00118 cerr << "check to make sure this is not a bug" << endl;
00119 cerr << "exit in void Stopwatch::stop()" << endl;
00120 exit(1);
00121 }
00122
00123 if(numSamples >= 0)
00124 average_us += total_us;
00125 numSamples++;
00126 }
00127
00128 void Stopwatch::print(string title)
00129 {
00130 cout << setw(10) << title << ": " << (int)(total_us+0.5);
00131 cout << " ( avg = " << setw(8);
00132 if(numSamples > 0)
00133 cout << (int)(average_us/(numSamples)+0.5) << ", n=" << numSamples << ")\n";
00134 else
00135 cout << 0 << ")\n";
00136 }
00137
00138 longType Stopwatch::timeMS()
00139 {
00140 return total_us/1000;
00141 }
00142
00143 longType Stopwatch::timeUS()
00144 {
00145 return total_us;
00146 }
00147
00148 bool Stopwatch::isRunning()
00149 {
00150 return running;
00151 }
00152
00153 string Stopwatch::toString()
00154 {
00155 ostringstream stream;
00156 longType time = timeMS();
00157 if(time <= 0 && isRunning())
00158 {
00159 gettimeofday(&tp,&tz);
00160 stime2 = tp.tv_sec;
00161 micro2 = tp.tv_usec;
00162 time = (longType)((stime2-stime1)*1e6 + micro2 - micro1);
00163 time /= 1000;
00164 }
00165 double hrs = time/(1000.0*60.0*60.0);
00166 double mins = time/(1000.0*60.0);
00167 double secs = time/(1000.0);
00168 int wdth = 20;
00169 int prec = 5;
00170 stream.width(wdth); stream.precision(prec);
00171 stream << time << " ms " << " = ";
00172 stream.width(wdth); stream.precision(prec);
00173 stream << secs << " s " << " = ";
00174 stream.width(wdth); stream.precision(prec);
00175 stream << mins << " mins" << " = ";
00176 stream.width(wdth); stream.precision(prec);
00177 stream << hrs << " hrs ";
00178 return stream.str();
00179 }
00180
00181 void Stopwatch::operator =( const Stopwatch & rhs )
00182 {
00183 total_us = rhs.total_us;
00184
00185
00186
00187 }
00188
00189 Stopwatch Stopwatch::operator+(Stopwatch & rhs)
00190 {
00191 Stopwatch result;
00192 result.total_us = this->total_us + rhs.total_us;
00193 result.running = false;
00194
00195 return result;
00196 }
00197
00198
00199 ostream& operator <<(ostream& strm, Stopwatch &watch)
00200 {
00201 strm << watch.toString();
00202 return strm;
00203 }
00204
00205
00206
00207 #ifdef PARALLEL
00208
00209 bool Stopwatch::mpiTypeCreated = false;
00210
00211 void Stopwatch::buildMpiReduce()
00212 {
00213 MPI_Op_create((MPI_User_function*)Reduce_Function,
00214 true,&MPI_REDUCE);
00215 }
00216
00217 void Stopwatch::buildMpiType()
00218 {
00219 Stopwatch indata;
00220
00221 int block_lengths[1];
00222 MPI_Aint displacements[1];
00223 MPI_Aint addresses[2];
00224 MPI_Datatype typelist[1];
00225
00226
00227 typelist[0] = MPI_LONG_LONG_INT;
00228
00229 block_lengths[0] = 1;
00230
00231 MPI_Address(&indata, &addresses[0]);
00232 MPI_Address(&(indata.total_us), &addresses[1]);
00233
00234 displacements[0] = addresses[1] - addresses[0];
00235
00236 MPI_Type_struct(1, block_lengths, displacements, typelist,
00237 &MPI_TYPE);
00238 MPI_Type_commit(&MPI_TYPE);
00239 }
00240
00241
00242 MPI_Datatype Stopwatch::MPI_TYPE;
00243
00244 MPI_Op Stopwatch::MPI_REDUCE;
00245
00246 void Stopwatch::Reduce_Function(Stopwatch *in, Stopwatch *inout,
00247 int *len, MPI_Datatype *dptr)
00248 {
00249 *inout = *inout + *in;
00250 }
00251
00252
00253 #endif
00254
00255
00256
00257
00258
00259
00260
00261
00262