]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/AliHLTTPCBenchmark.cxx
code documentation
[u/mrichter/AliRoot.git] / HLT / TPCLib / AliHLTTPCBenchmark.cxx
1 // @(#) $Id$
2
3 // Author: Uli Frankenfeld <mailto:franken@fi.uib.no>
4 //*-- Copyright &copy ALICE HLT Group
5
6 /** \class AliHLTTPCBenchmark
7 <pre>
8 //_____________________________________________________________
9 //
10 // AliHLTTPCBenchmark
11 //
12 //   Benchmark class for level3 code
13 //  
14 //
15 </pre>
16 */
17
18 #ifndef no_root
19 #include <TFile.h>
20 #include <TGraphAsymmErrors.h>
21 #include <TString.h>
22 #include <TStopwatch.h>
23 #include <TMath.h>
24 #endif
25 #include "AliHLTTPCRootTypes.h"
26 #include "AliHLTTPCLogging.h"
27 #include "AliHLTTPCBenchmark.h"
28
29 #if __GNUC__ >= 3
30 using namespace std;
31 #endif
32
33 ClassImp(AliHLTTPCBenchmark)
34
35 AliHLTTPCBenchmark::AliHLTTPCBenchmark()
36   :
37   fNbench(0),
38   fNmax(20),
39   fNames(NULL),
40   fTimer(NULL),
41   fSum(NULL),
42   fMin(NULL),
43   fMax(NULL),
44   fCount(NULL)
45 {
46   //Constructor
47 }
48
49 AliHLTTPCBenchmark::~AliHLTTPCBenchmark()
50 {
51   //deconstructor
52    fNbench   = 0;
53    if (fNames)  {delete [] fNames; fNames  = 0;}
54    if (fTimer)  {delete [] fTimer; fTimer  = 0;}
55    if (fSum)    {delete [] fSum;   fSum   = 0;}
56    if (fMin)    {delete [] fMin;   fMin   = 0;}
57    if (fMax)    {delete [] fMax;   fMax   = 0;}
58    if (fCount)  {delete [] fCount; fCount =0;}
59 }
60
61 Int_t AliHLTTPCBenchmark::GetBench(const Char_t *name)
62 {
63   //get bench with name
64    for (Int_t i=0;i<fNbench;i++) {
65       if (!strcmp(name,(const Char_t*)fNames[i])) return i;
66    }
67    return -1;
68 }
69
70
71 void AliHLTTPCBenchmark::Start(const Char_t *name)
72 {
73   //start the benchmark with name
74    if (!fNbench) {
75 #ifdef no_root
76      fNames=new Char_t*[fNmax];
77      fTimer = new AliHLTTPCStopwatch[fNmax];
78 #else
79      fNames = new TString[fNmax];
80      fTimer = new TStopwatch[fNmax];
81 #endif
82
83      fSum   = new Float_t[fNmax];
84      fMin   = new Float_t[fNmax];
85      fMax   = new Float_t[fNmax];
86      fCount = new Int_t[fNmax];
87      for(Int_t i =0;i<fNmax;i++){
88        fSum[i]=0;
89        fMin[i]=0;
90        fMax[i]=0;
91        fCount[i]=0;
92      }
93    }
94    Int_t bench = GetBench(name);
95    if (bench < 0 && fNbench < fNmax ) {
96       // define a new benchmark to Start
97 #ifdef no_root
98      fNames[fNbench]=new Char_t[strlen(name)+1];
99      strcpy(fNames[fNbench],name);
100 #else
101       fNames[fNbench] = name;
102 #endif
103       bench = fNbench;
104       fNbench++;
105       fTimer[bench].Reset();
106       fTimer[bench].Start();
107    } else if (bench >=0) {
108    // Resume the existent benchmark
109       fTimer[bench].Reset();
110       fTimer[bench].Start();
111    }
112    else
113      LOG(AliHLTTPCLog::kWarning,"AliHLTTPCBenchmark::Start","Start")
114      <<"too many benches"<<ENDLOG;
115 }
116
117 void AliHLTTPCBenchmark::Stop(const char *name)
118 {
119   //stop the benchmark with name
120    Int_t bench = GetBench(name);
121    if (bench < 0) return;
122
123    fTimer[bench].Stop();
124    Float_t val = fTimer[bench].CpuTime();
125    
126    fSum[bench] += val; 
127    fCount[bench]++;
128    if(fCount[bench]==1){
129      fMin[bench] = val;
130      fMax[bench] = val;
131    }
132    else{
133      if(val<fMin[bench])fMin[bench]=val;
134      if(val>fMax[bench])fMax[bench]=val;
135    }
136 }
137
138 void AliHLTTPCBenchmark::Analyze(const Char_t* name)
139 {
140   //get results of benchmark
141   Float_t *x = new Float_t[fNbench]; 
142   Float_t *y = new Float_t[fNbench];
143   Float_t *eyl = new Float_t[fNbench]; 
144   Float_t *eyh = new Float_t[fNbench];
145   Char_t filename[256];
146   sprintf(filename,"%s.dat",name);
147   FILE *f= fopen(filename,"w");
148   for (Int_t i=0;i<fNbench;i++) {
149     Float_t av =0;
150     if(fCount[i]) av = fSum[i]/fCount[i]; 
151     x[i]=i+1;
152     y[i]=av*1000;
153     eyl[i]=(av-fMin[i])*1000;
154     eyh[i]=(fMax[i]-av)*1000;
155 #ifdef no_root
156     fprintf(f,"%2d. %s: ",i+1,fNames[i]);
157 #else
158     fprintf(f,"%2d. %s: ",i+1,fNames[i].Data());
159 #endif
160     fprintf(f,"total %4.0f patch %4.0f -%4.0f +%4.0f ms\n",fSum[i],av*1000,eyl[i],eyh[i]);
161   }
162   fclose(f);
163   sprintf(filename,"%s.tmp",name);
164 /* only a workaround!!
165   FILE *f2= fopen(filename,"w");
166   for (Int_t i=0;i<fNbench;i++) fprintf(f2,"%f ",x[i]); fprintf(f2,"\n");
167   for (Int_t i=0;i<fNbench;i++) fprintf(f2,"%f ",y[i]); fprintf(f2,"\n");
168   for (Int_t i=0;i<fNbench;i++) fprintf(f2,"%f ",eyl[i]); fprintf(f2,"\n");
169   for (Int_t i=0;i<fNbench;i++) fprintf(f2,"%f ",eyh[i]); fprintf(f2,"\n");
170   fclose(f2);
171 */
172 #ifndef no_root
173   sprintf(filename,"%s.root",name);
174   TFile *file = new TFile(filename,"RECREATE");
175   TGraphAsymmErrors *gr = new TGraphAsymmErrors(fNbench,x,y,0,0,eyl,eyh);
176   gr->SetTitle("benchmark");
177   gr->SetMarkerStyle(8);
178   gr->SetMinimum(0);
179   //gr->Draw("ALP");
180   gr->Write();
181   file->Close();
182   delete file; 
183   file=0;
184 #endif
185   delete[] x;
186   delete[] y;
187   delete[] eyl;
188   delete[] eyh;
189 }
190
191 Double_t AliHLTTPCBenchmark::GetCpuTime()
192 {
193   //get cpu time
194   {return (Double_t)(clock()) / CLOCKS_PER_SEC;}
195 }