]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/src/AliL3Benchmark.cxx
Changes for independant library of standalone l3 code. Most of them are by having...
[u/mrichter/AliRoot.git] / HLT / src / AliL3Benchmark.cxx
1 // Author: Uli Frankenfeld <mailto:franken@fi.uib.no>
2 //*-- Copyright &copy Uli
3
4 #include <time.h>
5 #ifndef no_root
6 #include <TFile.h>
7 #include <TGraphAsymmErrors.h>
8 #include <TString.h>
9 #include <TStopwatch.h>
10 #include <TMath.h>
11 #endif
12
13 #include "AliL3Benchmark.h"
14 #include "AliL3Logging.h"
15
16
17 /**
18 // \class AliL3Benchmark
19 //_____________________________________________________________
20 //
21 // AliL3Benchmark
22 //
23 //   Benchmark class for level3 code
24 //  
25 //</pre>
26 */
27
28 ClassImp(AliL3Benchmark)
29 AliL3Benchmark::AliL3Benchmark()
30 {
31   //Constructor
32
33   fNbench = 0;
34   fNmax   = 20;
35   fNames  = 0;
36   fTimer  = 0;
37   fSum    = 0;
38   fMin    = 0;
39   fMax    = 0;
40   fCount  = 0;
41   //fStopwatch = 0;
42 }
43
44 AliL3Benchmark::~AliL3Benchmark()
45 {
46    fNbench   = 0;
47    if (fNames)  {delete [] fNames; fNames  = 0;}
48    if (fTimer)  {delete [] fTimer; fTimer  = 0;}
49    if (fSum)    {delete [] fSum;   fSum   = 0;}
50    if (fMin)    {delete [] fMin;   fMin   = 0;}
51    if (fMax)    {delete [] fMax;   fMax   = 0;}
52    if (fCount)  {delete [] fCount; fCount =0;}
53    //if(fStopwatch) {delete fStopwatch; fStopwatch =0;}
54 }
55
56 Int_t AliL3Benchmark::GetBench(const char *name)
57 {
58    for (Int_t i=0;i<fNbench;i++) {
59       if (!strcmp(name,(const char*)fNames[i])) return i;
60    }
61    return -1;
62 }
63
64
65 void AliL3Benchmark::Start(const char *name)
66 {
67    if (!fNbench) {
68 #ifdef no_root
69      fNames=new Char_t*[fNmax];
70      fTimer = new AliL3Stopwatch[fNmax];
71 #else
72      fNames = new TString[fNmax];
73      fTimer = new TStopwatch[fNmax];
74 #endif
75
76      fSum   = new Float_t[fNmax];
77      fMin   = new Float_t[fNmax];
78      fMax   = new Float_t[fNmax];
79      fCount = new Int_t[fNmax];
80      for(Int_t i =0;i<fNmax;i++){
81        fSum[i]=0;
82        fMin[i]=0;
83        fMax[i]=0;
84        fCount[i]=0;
85      }
86    }
87    Int_t bench = GetBench(name);
88    if (bench < 0 && fNbench < fNmax ) {
89       // define a new benchmark to Start
90 #ifdef no_root
91      fNames[fNbench]=new Char_t[strlen(name)+1];
92      strcpy(fNames[fNbench],name);
93 #else
94       fNames[fNbench] = name;
95 #endif
96       bench = fNbench;
97       fNbench++;
98       fTimer[bench].Reset();
99       fTimer[bench].Start();
100       //if(fStopwatch) {delete fStopwatch; fStopwatch =0;}
101       //fStopwatch = new TStopwatch();
102       //fStopwatch->Reset();
103       //fStopwatch->Start();
104    } else if (bench >=0) {
105    // Resume the existent benchmark
106       fTimer[bench].Reset();
107       fTimer[bench].Start();
108       //if(fStopwatch) {delete fStopwatch; fStopwatch =0;}
109       //fStopwatch = new TStopwatch();
110       //fStopwatch->Reset();
111       //fStopwatch->Start();
112    }
113    else
114      LOG(AliL3Log::kWarning,"AliL3Benchmark::Start","Start")
115      <<"too many benches"<<ENDLOG;
116 }
117
118 void AliL3Benchmark::Stop(const char *name)
119 {
120    Int_t bench = GetBench(name);
121    if (bench < 0) return;
122
123    fTimer[bench].Stop();
124    Float_t val = fTimer[bench].CpuTime();
125    //fStopwatch->Stop();
126    //Float_t val = fStopwatch->CpuTime();
127    
128    fSum[bench] += val; 
129    fCount[bench]++;
130    if(fCount[bench]==1){
131      fMin[bench] = val;
132      fMax[bench] = val;
133    }
134    else{
135      if(val<fMin[bench])fMin[bench]=val;
136      if(val>fMax[bench])fMax[bench]=val;
137    }
138 }
139
140 void AliL3Benchmark::Analyze(const char* name){
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 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,"%4.0f ms\n",av*1000);
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 AliL3Benchmark::GetCpuTime()
192 {
193   {return (Double_t)(clock()) / CLOCKS_PER_SEC;}
194 }