00001 #ifndef FROMMER_TIMER_H
00002 #define FROMMER_TIMER_H
00003
00004
00005
00006 #include <vector>
00007 #include "timer.h"
00008
00009 namespace nCenterFocus
00010 {
00011
00012
00013 #ifdef FROMMERSTAGE_TIMER
00014
00015 class StageTimeData
00016 {
00017 vector<double> stage_m;
00018 vector<double> cstep_m;
00019 vector<double> astep_m;
00020 vector<uint64_t> stage_calls_m;
00021
00022 public:
00023 void clear()
00024 {
00025 stage_m.clear();
00026 cstep_m.clear();
00027 astep_m.clear();
00028 stage_calls_m.clear();
00029 }
00030
00031 int size()
00032 {
00033 return stage_m.size();
00034 }
00035
00036 void resize(int size, double initialValue)
00037 {
00038 stage_calls_m.resize(size,initialValue);
00039 stage_m.resize(size,initialValue);
00040 cstep_m.resize(size,initialValue);
00041 astep_m.resize(size, initialValue);
00042 }
00043
00044 StageTimeData()
00045 {
00046
00047 stage_calls_m.clear();
00048 stage_m.clear();
00049 cstep_m.clear();
00050 astep_m.clear();
00051 }
00052
00053 void checkBounds(unsigned int stage, vector<double> &timings)
00054 {
00055
00056 assert( stage<timings.size() );
00057
00058 }
00059
00060 inline void accumulateStepTime(unsigned int stage, double time)
00061 {
00062 checkBounds(stage, stage_m);
00063 stage_calls_m[stage]++;
00064 stage_m[stage] +=time;
00065 }
00066 inline void accumulateCStepTime(unsigned int stage, double time)
00067 {
00068 checkBounds(stage, cstep_m);
00069 cstep_m[stage] +=time;
00070 }
00071 inline void accumulateAStepTime(unsigned int stage, double time)
00072 {
00073 checkBounds(stage, astep_m);
00074 astep_m[stage] +=time;
00075 }
00076
00077 void printSingleStageTimings(std::ostream & os, const vector<double> &timings) const
00078 {
00079 for (size_t i=1;i<timings.size(); i++)
00080 {
00081 os << "--\t\t stage " << setw(3) << i << " costs " << setw(9) << setprecision(4) << timings[i] << " sec." << endl;
00082 }
00083 }
00084
00085 void printSingleStageCalls(std::ostream & os) const
00086 {
00087 for (size_t i=1;i<stage_calls_m.size(); i++)
00088 {
00089 os << "--\t\t stage " << setw(3) << i << " called " << setw(20) << stage_calls_m[i] << " times!" << endl;
00090 }
00091 }
00092
00093 void printStageTimings(std::ostream & os) const
00094 {
00095 os << "--\t stage calls: " << std::endl;
00096 printSingleStageCalls(os);
00097 os << "--\t stage timings: " << std::endl;
00098 printSingleStageTimings(os, stage_m);
00099 }
00100
00101 void printAStepTimings(std::ostream & os) const
00102 {
00103 os << "--\t A-Step timings: " << std::endl;
00104 printSingleStageTimings(os, astep_m);
00105 }
00106
00107 void printCStepTimings(std::ostream & os) const
00108 {
00109 os << "-- \t C-Step timings: " << std::endl;
00110 printSingleStageTimings(os, cstep_m);
00111 }
00112
00113 void printAccumulatedAStepTime(std::ostream & os) const
00114 {
00115 double sum=0;
00116 for (size_t i=1;i<astep_m.size(); i++)
00117 {
00118 sum += astep_m[i];
00119 }
00120 os << "--\t Accumulated A-Step time: " << sum << std::endl;
00121 }
00122
00123 void printAccumulatedCStepTime(std::ostream & os) const
00124 {
00125 double sum=0;
00126 for (size_t i=1;i<cstep_m.size(); i++)
00127 {
00128 sum += cstep_m[i];
00129 }
00130 os << "--\t Accumulated C-Step time: " << sum << std::endl;
00131 }
00132
00133 void printAccumulatedStageTime(std::ostream & os) const
00134 {
00135 double sum=0;
00136 for (size_t i=1;i<stage_m.size(); i++)
00137 {
00138 sum += stage_m[i];
00139 }
00140 os << "--\t Accumulated stage time: " << sum << std::endl;
00141 }
00142
00143 double getAccumulatedStageTime() const
00144 {
00145 double sum=0;
00146 for (size_t i=1;i<stage_m.size(); i++)
00147 {
00148 sum += stage_m[i];
00149 }
00150 return sum;
00151 }
00152
00153 };
00154
00156 class FrommerTimer
00157 {
00158 private:
00159 StageTimeData stageTimes_m;
00160 StageTimeData stageTimes_doit1_m;
00161 StageTimeData stageTimes_doit2_m;
00162 StageTimeData stageTimes_doit3_m;
00163 StageTimeData stageTimes_jacobian_m;
00164 StageTimeData accumulatedStageTimes_m;
00165
00166 public:
00167 FrommerTimer(int maxFocalValuesToCompute)
00168 {
00169 stageTimes_m.clear();
00170 stageTimes_doit1_m.clear();
00171 stageTimes_doit2_m.clear();
00172 stageTimes_doit3_m.clear();
00173 accumulatedStageTimes_m.clear();
00174 stageTimes_m.resize(maxFocalValuesToCompute*2+1, 0);
00175 stageTimes_jacobian_m.resize(maxFocalValuesToCompute*2+1, 0);
00176 stageTimes_doit1_m.resize(3, 0);
00177 stageTimes_doit2_m.resize(5, 0);
00178 stageTimes_doit3_m.resize(7, 0);
00179 accumulatedStageTimes_m.resize(maxFocalValuesToCompute*2+1, 0);
00180 };
00181
00182 void logStepTime(int focalValuesToCompute, int stage, const Timer & tim)
00183 {
00184 double time = getTime(tim);
00185 assert(stage<accumulatedStageTimes_m.size() );
00186
00187 accumulatedStageTimes_m.accumulateStepTime(stage, time);
00188 switch (focalValuesToCompute)
00189 {
00190 case -2:
00191 assert(stage<stageTimes_jacobian_m.size() );
00192 stageTimes_jacobian_m.accumulateStepTime(stage, time);
00193 break;
00194 case 1:
00195 assert(stage<stageTimes_doit1_m.size() );
00196 stageTimes_doit1_m.accumulateStepTime(stage, time);
00197 break;
00198 case 2:
00199 assert(stage<stageTimes_doit2_m.size() );
00200 stageTimes_doit2_m.accumulateStepTime(stage, time);
00201 break;
00202 case 3:
00203 assert(stage<stageTimes_doit3_m.size() );
00204 stageTimes_doit3_m.accumulateStepTime(stage, time);
00205 break;
00206 default:
00207 assert(stage<stageTimes_m.size() );
00208 stageTimes_m.accumulateStepTime(stage, time);
00209 }
00210 };
00211
00212 void logCStepTime(int focalValuesToCompute, int stage,const Timer & tim)
00213 {
00214 double time = getTime(tim);
00215 assert( stage<accumulatedStageTimes_m.size() );
00216
00217 accumulatedStageTimes_m.accumulateCStepTime(stage, time);
00218 switch (focalValuesToCompute)
00219 {
00220 case -2:
00221 assert(stage<stageTimes_jacobian_m.size() );
00222 stageTimes_jacobian_m.accumulateCStepTime(stage, time);
00223 break;
00224 case 1:
00225 assert( stage<stageTimes_doit1_m.size() );
00226 stageTimes_doit1_m.accumulateCStepTime(stage, time);
00227 break;
00228 case 2:
00229 assert(stage<stageTimes_doit2_m.size());
00230 stageTimes_doit2_m.accumulateCStepTime(stage, time);
00231 break;
00232 case 3:
00233 assert(stage<stageTimes_doit3_m.size());
00234 stageTimes_doit3_m.accumulateCStepTime(stage, time);
00235 break;
00236 default:
00237 assert(stage<stageTimes_m.size() );
00238 stageTimes_m.accumulateCStepTime(stage, time);
00239 }
00240 }
00241
00242 inline double getTime(const Timer & tim) const
00243 {
00244 return tim.usertime();
00245 }
00246
00247 void logAStepTime(int focalValuesToCompute, int stage,const Timer & tim)
00248 {
00249 double time = getTime(tim);
00250 assert( stage<accumulatedStageTimes_m.size() );
00251
00252 accumulatedStageTimes_m.accumulateAStepTime(stage, time);
00253 switch (focalValuesToCompute)
00254 {
00255 case -2:
00256 assert(stage<stageTimes_jacobian_m.size() );
00257 stageTimes_jacobian_m.accumulateAStepTime(stage, time);
00258 break;
00259
00260 case 1:
00261 assert(stage <stageTimes_doit1_m.size() );
00262 stageTimes_doit1_m.accumulateAStepTime(stage, time);
00263 break;
00264 case 2:
00265 assert(stage<stageTimes_doit2_m.size() );
00266 stageTimes_doit2_m.accumulateAStepTime(stage, time);
00267 break;
00268 case 3:
00269 assert(stage<stageTimes_doit3_m.size() );
00270 stageTimes_doit3_m.accumulateAStepTime(stage, time);
00271 break;
00272 default:
00273 assert(stage<stageTimes_m.size() );
00274 stageTimes_m.accumulateAStepTime(stage, time);
00275 }
00276 }
00277
00278 void printSingleStageTimings(std::ostream & os, const StageTimeData & timings, string titlestr) const
00279 {
00280
00281 if (timings.getAccumulatedStageTime()!=0)
00282 {
00283 os << "-- " << titlestr << endl;
00284 timings.printAccumulatedStageTime(os);
00285 timings.printAccumulatedAStepTime(os);
00286 timings.printAccumulatedCStepTime(os);
00287 timings.printStageTimings(os);
00288 timings.printAStepTimings(os);
00289 timings.printCStepTimings(os);
00290
00291 }
00292 }
00293
00294
00295 void print(std::ostream & os) const
00296 {
00297 printSingleStageTimings(os, stageTimes_doit1_m," doit1 timings: ");
00298
00299 printSingleStageTimings(os, stageTimes_doit2_m," doit2 timings: ");
00300
00301 printSingleStageTimings(os, stageTimes_doit3_m," doit3 timings: ");
00302
00303 printSingleStageTimings(os,stageTimes_m," doit timings: ");
00304
00305 printSingleStageTimings(os, stageTimes_jacobian_m," jacobian timings: ");
00306
00307 printSingleStageTimings(os,accumulatedStageTimes_m," accumulated timings: ");
00308 }
00309 };
00310 #endif
00311 };
00312
00313 #endif //FROMMER_TIMER_H