00001 #ifndef Impala_Util_TimePlot_h
00002 #define Impala_Util_TimePlot_h
00003
00004 #include <iostream>
00005 #include <string>
00006 #include "Basis/Timer.h"
00007
00008 namespace Impala
00009 {
00010 namespace Util
00011 {
00012
00013
00014 class TimePlot {
00015 public:
00016
00017 TimePlot(int nrFunctions, int nrXCoords, int* xCoords, int nrSplitTimes,
00018 int nrRuns) : mTimer0(0), mTimer1(1)
00019 {
00020 mNrFunctions = nrFunctions;
00021 mNrXCoords = nrXCoords;
00022 mXCoords = new int[nrXCoords];
00023 for (int x=0 ; x<nrXCoords ; x++)
00024 mXCoords[x] = xCoords[x];
00025 mNrSplitTimes = nrSplitTimes;
00026 mNrRuns = nrRuns;
00027 mNrTimers = 2;
00028 mRunDataSize = mNrSplitTimes + 1;
00029 mXCoordDataSize = mRunDataSize * mNrRuns;
00030 mFuncDataSize = mNrXCoords * mXCoordDataSize;
00031 mTimerDataSize = mNrFunctions * mFuncDataSize;
00032 mData = new double[mNrTimers * mTimerDataSize];
00033 }
00034
00035 ~TimePlot()
00036 {
00037 delete mXCoords;
00038 delete mData;
00039 }
00040
00041 void
00042 Start(int function, int xCoord, int run)
00043 {
00044 mCurFunc = function;
00045 mCurXCoord = xCoord;
00046 mCurDataPtr0 = GetPtr(0, mCurFunc, mCurXCoord, 0, run);
00047 mCurDataPtr1 = GetPtr(1, mCurFunc, mCurXCoord, 0, run);
00048 mTimer0.Start();
00049 mTimer1.Start();
00050 }
00051
00052 void
00053 SplitTime()
00054 {
00055 *mCurDataPtr0++ = mTimer0.SplitTime();
00056 *mCurDataPtr1++ = mTimer1.SplitTime();
00057 }
00058
00059 void
00060 Stop()
00061 {
00062 *mCurDataPtr0 = mTimer0.Stop();
00063 *mCurDataPtr1 = mTimer1.Stop();
00064 }
00065
00066 void
00067 PrintAverageFinal(FILE* fp, int timer)
00068 {
00069 PrintAverageSplit(fp, timer, mNrSplitTimes);
00070 }
00071
00072 void
00073 PrintAverageSplit(FILE* fp, int timer, int split)
00074 {
00075 fprintf(fp, "\n\n# Average of split %d using timer mode %d\n",
00076 split, timer);
00077 if (split == mNrSplitTimes)
00078 fprintf(fp, "# This split is actually the end time\n");
00079 for (int x=0 ; x<mNrXCoords ; x++)
00080 {
00081 fprintf(fp, "%d", mXCoords[x]);
00082 for (int f=0 ; f<mNrFunctions ; f++)
00083 {
00084 double avg = 0;
00085 for (int r=0 ; r<mNrRuns ; r++)
00086 avg += *GetPtr(timer, f, x, split, r);
00087 avg /= mNrRuns;
00088 fprintf(fp, "\t%f", avg);
00089 }
00090 fprintf(fp, "\n");
00091 }
00092 }
00093
00094 void
00095 PrintRunsSplit(FILE* fp, int timer, int function, int split)
00096 {
00097 fprintf(fp, "\n\n# All of split %d of func %d using timer mode %d\n",
00098 split, function, timer);
00099 if (split == mNrSplitTimes)
00100 fprintf(fp, "# This split is actually the end time\n");
00101 for (int x=0 ; x<mNrXCoords ; x++)
00102 {
00103 fprintf(fp, "%d", mXCoords[x]);
00104 for (int r=0 ; r<mNrRuns ; r++)
00105 fprintf(fp, "\t%f", *GetPtr(timer, function, x, split, r));
00106 fprintf(fp, "\n");
00107 }
00108 }
00109
00110 void
00111 SaveData(std::string machineName, std::string experiment,
00112 std::string imageName, std::string* funcName)
00113 {
00114 std::string baseName = machineName + "/" + machineName + "_" + experiment +
00115 "_" + imageName;
00116 printf("Saving data %s\n", baseName.c_str());
00117
00118
00119
00120 std::string fileName = baseName + "_proctime.dat";
00121 FILE* fp;
00122 if (! (fp = DoOpenFile(fileName)))
00123 return;
00124 DoSaveData(fp, machineName, experiment, imageName, 0, funcName);
00125 fclose(fp);
00126
00127
00128
00129 fileName = baseName + "_realtime.dat";
00130 if (! (fp = DoOpenFile(fileName)))
00131 return;
00132 DoSaveData(fp, machineName, experiment, imageName, 1, funcName);
00133 fclose(fp);
00134
00135
00136
00137 fileName = baseName + "_proctime.gp";
00138 if (! (fp = DoOpenFile(fileName)))
00139 return;
00140 DoWriteGpBase(fp, machineName, experiment, imageName,
00141 std::string("proctime"));
00142 fclose(fp);
00143
00144 fileName = baseName + "_realtime.gp";
00145 if (! (fp = DoOpenFile(fileName)))
00146 return;
00147 DoWriteGpBase(fp, machineName, experiment, imageName,
00148 std::string("realtime"));
00149 fclose(fp);
00150
00151 fileName = baseName + "_split_average.gp";
00152 if (! (fp = DoOpenFile(fileName)))
00153 return;
00154 DoWriteGpSplitAverage(fp, machineName, experiment, imageName, funcName);
00155 fclose(fp);
00156
00157 fileName = baseName + "_func_decomp.gp";
00158 if (! (fp = DoOpenFile(fileName)))
00159 return;
00160 DoWriteGpFuncDecomp(fp, machineName, experiment, imageName);
00161 fclose(fp);
00162
00163 fileName = baseName + "_func_runs.gp";
00164 if (! (fp = DoOpenFile(fileName)))
00165 return;
00166 DoWriteGpFuncRuns(fp, machineName, experiment, imageName);
00167 fclose(fp);
00168 }
00169
00170 private:
00171
00172 FILE*
00173 DoOpenFile(std::string fileName)
00174 {
00175 FILE* fp = fopen(fileName.c_str(), "w") ;
00176 if (!fp)
00177 {
00178 std::cout << "TimePlot: Error opening file " << fileName << std::endl;
00179 return 0;
00180 }
00181 return fp;
00182 }
00183
00184 void
00185 DoSaveData(FILE* fp, std::string machineName, std::string experiment,
00186 std::string imageName, int timer, std::string* funcName)
00187 {
00188 fprintf(fp, "# machine %s, experiment %s, image %s, timer %d\n",
00189 machineName.c_str(), experiment.c_str(), imageName.c_str(), timer);
00190 fprintf(fp, "# %d runs with %d splitTimes on %d functions : \n", mNrRuns,
00191 mNrSplitTimes, mNrFunctions);
00192 for (int i=0 ; i<mNrFunctions ; i++)
00193 fprintf(fp, "# func %d = %s\n", i, funcName[i].c_str());
00194 PrintAverageFinal(fp, timer);
00195 for (int split=0 ; split<mNrSplitTimes ; split++)
00196 PrintAverageSplit(fp, timer, split);
00197 for (int function=0 ; function<mNrFunctions ; function++)
00198 PrintRunsSplit(fp, timer, function, mNrSplitTimes);
00199 }
00200
00201 void
00202 DoWriteGpBase(FILE* fp, std::string machineName, std::string experiment,
00203 std::string imageName, std::string timer)
00204 {
00205 int i;
00206 for (i=0 ; i<=mNrSplitTimes ; i++)
00207 fprintf(fp, "call '%s_%s_%s_split_average.gp' %s %d\n",
00208 machineName.c_str(), experiment.c_str(), imageName.c_str(),
00209 timer.c_str(), i);
00210 fprintf(fp, "\n");
00211 for (i=0 ; i<mNrFunctions ; i++)
00212 fprintf(fp, "call '%s_%s_%s_func_decomp.gp' %s %d\n",
00213 machineName.c_str(), experiment.c_str(), imageName.c_str(),
00214 timer.c_str(), i);
00215 fprintf(fp, "\n");
00216 for (i=0 ; i<mNrFunctions ; i++)
00217 fprintf(fp, "call '%s_%s_%s_func_runs.gp' %s %d\n",
00218 machineName.c_str(), experiment.c_str(), imageName.c_str(),
00219 timer.c_str(), i);
00220 }
00221
00222 void
00223 DoWriteGpSplitAverage(FILE* fp, std::string machineName, std::string experiment,
00224 std::string imageName, std::string* funcName)
00225 {
00226 fprintf(fp, "print \"%s_%s_%s_split_average timer=[$0] split=[$1]\"\n\n",
00227 machineName.c_str(), experiment.c_str(), imageName.c_str());
00228 fprintf(fp, "set xlabel 'Kernel size'\n");
00229 fprintf(fp, "set ylabel 'Time'\n");
00230 fprintf(fp, "set key left top\n");
00231 fprintf(fp, "set data style linespoints\n\n\n");
00232 fprintf(fp, "set title '%s . %s . %s . average of split $1 . $0'\n",
00233 machineName.c_str(), experiment.c_str(), imageName.c_str());
00234 fprintf(fp, "plot ");
00235 for (int i=0 ; i<mNrFunctions ; i++)
00236 {
00237 if (i>0)
00238 fprintf(fp, ", \\\n ");
00239 fprintf(fp, "'%s_%s_%s_$0.dat' index $1 using 1:%d title '%s'",
00240 machineName.c_str(), experiment.c_str(), imageName.c_str(),
00241 2+i, funcName[i].c_str());
00242 }
00243 fprintf(fp, "\n\npause -1 \"Hit return\"\n");
00244 }
00245
00246 void
00247 DoWriteGpFuncDecomp(FILE* fp, std::string machineName, std::string experiment,
00248 std::string imageName)
00249 {
00250 fprintf(fp, "print \"%s_%s_%s_func_decomp timer=[$0] func=[$1]\"\n\n",
00251 machineName.c_str(), experiment.c_str(), imageName.c_str());
00252 fprintf(fp, "set xlabel 'Kernel size'\n");
00253 fprintf(fp, "set ylabel 'Time'\n");
00254 fprintf(fp, "set key left top\n");
00255 fprintf(fp, "set data style linespoints\n\n\n");
00256 fprintf(fp, "set title '%s . %s . %s . decomposition function $1 . $0'\n",
00257 machineName.c_str(), experiment.c_str(), imageName.c_str());
00258 fprintf(fp, "plot ");
00259 for (int i=0 ; i<=mNrSplitTimes ; i++)
00260 {
00261 if (i>0)
00262 fprintf(fp, ", \\\n ");
00263 fprintf(fp, "'%s_%s_%s_$0.dat' index %d using 1:2+$1 ",
00264 machineName.c_str(), experiment.c_str(), imageName.c_str(), i);
00265 if (i == 0)
00266 fprintf(fp, "title 'final'");
00267 else
00268 fprintf(fp, "title 'split %d'", i);
00269 }
00270 fprintf(fp, "\n\npause -1 \"Hit return\"\n");
00271 }
00272
00273 void
00274 DoWriteGpFuncRuns(FILE* fp, std::string machineName, std::string experiment,
00275 std::string imageName)
00276 {
00277 fprintf(fp, "print \"%s_%s_%s_func_runs timer=[$0] func=[$1]\"\n\n",
00278 machineName.c_str(), experiment.c_str(), imageName.c_str());
00279 fprintf(fp, "set xlabel 'Kernel size'\n");
00280 fprintf(fp, "set ylabel 'Time'\n");
00281 fprintf(fp, "set key left top\n");
00282 fprintf(fp, "set data style linespoints\n\n\n");
00283 fprintf(fp, "set title '%s . %s . %s . all runs of function $1 . $0'\n",
00284 machineName.c_str(), experiment.c_str(), imageName.c_str());
00285 fprintf(fp, "plot '%s_%s_%s_$0.dat' index 0 using 1:2+$1 title 'average'",
00286 machineName.c_str(), experiment.c_str(), imageName.c_str());
00287 for (int i=0 ; i<mNrRuns ; i++)
00288 {
00289 fprintf(fp, ", \\\n ");
00290 fprintf(fp, "'%s_%s_%s_$0.dat' index %d+$1 using 1:%d title 'run %d'",
00291 machineName.c_str(), experiment.c_str(), imageName.c_str(),
00292 mNrSplitTimes+1, 2+i, 1+i);
00293 }
00294 fprintf(fp, "\n\npause -1 \"Hit return\"\n");
00295 }
00296
00297 double*
00298 GetPtr(int timer, int function, int xCoord, int split, int run)
00299 {
00300 return mData + timer * mTimerDataSize + function * mFuncDataSize
00301 + xCoord * mXCoordDataSize + run * mRunDataSize + split;
00302 }
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314 int mNrFunctions;
00315 int mNrXCoords;
00316 int* mXCoords;
00317 int mNrSplitTimes;
00318 int mNrRuns;
00319 int mNrTimers;
00320 Timer mTimer0;
00321 Timer mTimer1;
00322 double* mData;
00323 int mTimerDataSize;
00324 int mFuncDataSize;
00325 int mXCoordDataSize;
00326 int mRunDataSize;
00327 int mCurFunc;
00328 int mCurXCoord;
00329 int mCurSplit;
00330 int mCurRun;
00331 double* mCurDataPtr0;
00332 double* mCurDataPtr1;
00333
00334 };
00335
00336 }
00337 }
00338
00339 #endif