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