]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/tracking-ca/AliHLTTPCCAStandaloneFramework.cxx
bug fix: reconstruction crash when the output buffer size exceed
[u/mrichter/AliRoot.git] / HLT / TPCLib / tracking-ca / AliHLTTPCCAStandaloneFramework.cxx
1 // **************************************************************************
2 // This file is property of and copyright by the ALICE HLT Project          *
3 // ALICE Experiment at CERN, All rights reserved.                           *
4 //                                                                          *
5 // Primary Authors: Sergey Gorbunov <sergey.gorbunov@kip.uni-heidelberg.de> *
6 //                  Ivan Kisel <kisel@kip.uni-heidelberg.de>                *
7 //                  for The ALICE HLT Project.                              *
8 //                                                                          *
9 // Permission to use, copy, modify and distribute this software and its     *
10 // documentation strictly for non-commercial purposes is hereby granted     *
11 // without fee, provided that the above copyright notice appears in all     *
12 // copies and that both the copyright notice and this permission notice     *
13 // appear in the supporting documentation. The authors make no claims       *
14 // about the suitability of this software for any purpose. It is            *
15 // provided "as is" without express or implied warranty.                    *
16 //                                                                          *
17 //***************************************************************************
18
19
20 #include "AliHLTTPCCAStandaloneFramework.h"
21 #include "AliHLTTPCCATrackParam.h"
22 #include "AliHLTTPCCAMerger.h"
23 #include "AliHLTTPCCAMergerOutput.h"
24 #include "AliHLTTPCCADataCompressor.h"
25 #include "AliHLTTPCCAMath.h"
26 #include "AliHLTTPCCAClusterData.h"
27 #include "TStopwatch.h"
28
29 #ifdef HLTCA_STANDALONE
30 #include <omp.h>
31 #endif
32
33 AliHLTTPCCAStandaloneFramework &AliHLTTPCCAStandaloneFramework::Instance()
34 {
35   // reference to static object
36   static AliHLTTPCCAStandaloneFramework gAliHLTTPCCAStandaloneFramework;
37   return gAliHLTTPCCAStandaloneFramework;
38 }
39
40 AliHLTTPCCAStandaloneFramework::AliHLTTPCCAStandaloneFramework()
41     : fMerger(), fOutputControl(), fTracker(), fStatNEvents( 0 ), fDebugLevel(0)
42 {
43   //* constructor
44
45   for ( int i = 0; i < 20; i++ ) {
46     fLastTime[i] = 0;
47     fStatTime[i] = 0;
48   }
49   for ( int i = 0;i < fgkNSlices;i++) fSliceOutput[i] = NULL;
50   fTracker.SetOutputControl(&fOutputControl);
51 }
52
53 AliHLTTPCCAStandaloneFramework::AliHLTTPCCAStandaloneFramework( const AliHLTTPCCAStandaloneFramework& )
54     : fMerger(), fOutputControl(), fTracker(), fStatNEvents( 0 ), fDebugLevel(0)
55 {
56   //* dummy
57 }
58
59 const AliHLTTPCCAStandaloneFramework &AliHLTTPCCAStandaloneFramework::operator=( const AliHLTTPCCAStandaloneFramework& ) const
60 {
61   //* dummy
62   return *this;
63 }
64
65 AliHLTTPCCAStandaloneFramework::~AliHLTTPCCAStandaloneFramework()
66 {
67         for (int i = 0;i < fgkNSlices;i++) if (fSliceOutput[i]) free(fSliceOutput[i]);
68   //* destructor
69 }
70
71
72 void AliHLTTPCCAStandaloneFramework::StartDataReading( int guessForNumberOfClusters )
73 {
74   //prepare for reading of the event
75
76   int sliceGuess = 2 * guessForNumberOfClusters / fgkNSlices;
77
78   for ( int i = 0; i < fgkNSlices; i++ ) {
79     fClusterData[i].StartReading( i, sliceGuess );
80   }
81 }
82
83 void AliHLTTPCCAStandaloneFramework::FinishDataReading()
84 {
85   // finish reading of the event
86
87   /*static int event_number = 0;
88   char filename[256];
89
90   sprintf(filename, "events/event.%d.dump", event_number);
91   printf("Dumping event into file %s\n", filename);
92   std::ofstream outfile(filename, std::ofstream::binary);
93   if (outfile.fail())
94   {
95     printf("Error opening event dump file\n");
96     exit(1);
97   }
98   WriteEvent(outfile);
99   if (outfile.fail())
100   {
101     printf("Error writing event dump file\n");
102     exit(1);
103   }
104   outfile.close();
105
106   sprintf(filename, "events/settings.%d.dump", event_number);
107   outfile.open(filename);
108   WriteSettings(outfile);
109   outfile.close();
110
111   event_number++;*/
112   
113   /*std::ifstream infile(filename, std::ifstream::binary);
114   ReadEvent(infile);
115   infile.close();*/
116
117   for ( int i = 0; i < fgkNSlices; i++ ) {
118     fClusterData[i].FinishReading();
119   }
120 }
121
122
123 //int
124 void AliHLTTPCCAStandaloneFramework::ProcessEvent(int forceSingleSlice)
125 {
126   // perform the event reconstruction
127
128   fStatNEvents++;
129
130   TStopwatch timer0;
131   TStopwatch timer1;
132
133 #ifdef HLTCA_STANDALONE
134   unsigned long long int startTime, endTime, checkTime;
135   unsigned long long int cpuTimers[16], gpuTimers[16], tmpFreq;
136   StandaloneQueryFreq(&tmpFreq);
137   StandaloneQueryTime(&startTime);
138
139   fOutputControl.fObsoleteOutput = 0;
140 #endif
141
142   if (forceSingleSlice != -1)
143   {
144         if (fTracker.ProcessSlices(forceSingleSlice, 1, &fClusterData[forceSingleSlice], &fSliceOutput[forceSingleSlice])) return;
145   }
146   else
147   {
148         for (int iSlice = 0;iSlice < fgkNSlices;iSlice += fTracker.MaxSliceCount())
149         {
150                 if (fTracker.ProcessSlices(iSlice, fTracker.MaxSliceCount(), &fClusterData[iSlice], &fSliceOutput[iSlice])) return;
151         }
152   }
153
154 #ifdef HLTCA_STANDALONE
155   StandaloneQueryTime(&endTime);
156   StandaloneQueryTime(&checkTime);
157 #endif
158
159   timer1.Stop();
160   TStopwatch timer2;
161
162   fMerger.Clear();
163   fMerger.SetSliceParam( fTracker.Param(0) );
164
165   for ( int i = 0; i < fgkNSlices; i++ ) {
166         //printf("slice %d clusters %d tracks %d\n", i, fClusterData[i].NumberOfClusters(), fSliceOutput[i]->NTracks());
167     fMerger.SetSliceData( i, fSliceOutput[i] );
168   }
169
170   fMerger.Reconstruct();
171
172   timer2.Stop();
173   timer0.Stop();
174
175   fLastTime[0] = timer0.CpuTime();
176   fLastTime[1] = timer1.CpuTime();
177   fLastTime[2] = timer2.CpuTime();
178
179 #ifdef HLTCA_STANDALONE
180   printf("Tracking Time: %lld us\nTime uncertainty: %lld ns\n", (endTime - startTime) * 1000000 / tmpFreq, (checkTime - endTime) * 1000000000 / tmpFreq);
181
182   if (fDebugLevel >= 1)
183   {
184                 const char* tmpNames[16] = {"Initialisation", "Neighbours Finder", "Neighbours Cleaner", "Starts Hits Finder", "Start Hits Sorter", "Weight Cleaner", "Reserved", "Tracklet Constructor", "Tracklet Selector", "Write Output", "Unused", "Unused", "Unused", "Unused", "Unused", "Unused"};
185
186                 for (int i = 0;i < 10;i++)
187                 {
188                         if (i == 6) continue;
189                         cpuTimers[i] = gpuTimers[i] = 0;
190                         for ( int iSlice = 0; iSlice < fgkNSlices;iSlice++)
191                         {
192                                 if (forceSingleSlice != -1) iSlice = forceSingleSlice;
193                                 cpuTimers[i] += *fTracker.PerfTimer(0, iSlice, i + 1) - *fTracker.PerfTimer(0, iSlice, i);
194                                 if (forceSingleSlice != -1 || (fTracker.MaxSliceCount() && (iSlice % fTracker.MaxSliceCount() == 0 || i <= 5)))
195                                         gpuTimers[i] += *fTracker.PerfTimer(1, iSlice, i + 1) - *fTracker.PerfTimer(1, iSlice, i);
196                                 if (forceSingleSlice != -1) break;
197                         }
198                         if (forceSingleSlice == -1)
199                         {
200                                 cpuTimers[i] /= fgkNSlices;
201                                 gpuTimers[i] /= fgkNSlices;
202                         }
203                         cpuTimers[i] *= 1000000;
204                         gpuTimers[i] *= 1000000;
205                         cpuTimers[i] /= tmpFreq;
206                         gpuTimers[i] /= tmpFreq;
207                         cpuTimers[i] /= omp_get_max_threads();
208
209                         printf("Execution Time: Task: %20s ", tmpNames[i]);
210                                 printf("CPU: %15lld\t\t", cpuTimers[i]);
211                                 printf("GPU: %15lld\t\t", gpuTimers[i]);
212                         if (fDebugLevel >=6 && gpuTimers[i])
213                                 printf("Speedup: %4lld%%", cpuTimers[i] * 100 / gpuTimers[i]);
214                         printf("\n");
215                 }
216                 printf("Execution Time: Task: %20s CPU: %15lld\n", "Merger", (long long int) (timer2.CpuTime() * 1000000));
217   }
218 #endif
219
220   for ( int i = 0; i < 3; i++ ) fStatTime[i] += fLastTime[i];
221
222   //return(0);
223 }
224
225
226 void AliHLTTPCCAStandaloneFramework::WriteSettings( std::ostream &out ) const
227 {
228   //* write settings to the file
229   out << NSlices() << std::endl;
230   for ( int iSlice = 0; iSlice < NSlices(); iSlice++ ) {
231     fTracker.Param(iSlice).WriteSettings( out );
232   }
233 }
234
235 void AliHLTTPCCAStandaloneFramework::ReadSettings( std::istream &in )
236 {
237   //* Read settings from the file
238   int nSlices = 0;
239   in >> nSlices;
240   for ( int iSlice = 0; iSlice < nSlices; iSlice++ ) {
241     AliHLTTPCCAParam param;
242     param.ReadSettings ( in );
243         fTracker.InitializeSliceParam(iSlice, param);
244   }
245 }
246
247 void AliHLTTPCCAStandaloneFramework::WriteEvent( std::ostream &out ) const
248 {
249   // write event to the file
250   for ( int iSlice = 0; iSlice < fgkNSlices; iSlice++ ) {
251     fClusterData[iSlice].WriteEvent( out );
252   }
253 }
254
255 void AliHLTTPCCAStandaloneFramework::ReadEvent( std::istream &in )
256 {
257   //* Read event from file
258   for ( int iSlice = 0; iSlice < fgkNSlices; iSlice++ ) {
259     fClusterData[iSlice].ReadEvent( in );
260   }
261 }
262
263 void AliHLTTPCCAStandaloneFramework::WriteTracks( std::ostream &out ) const
264 {
265   //* Write tracks to file
266
267   for ( int i = 0; i < 20; i++ ) out << fLastTime[i] << std::endl;
268   //fMerger.Output()->Write( out );
269 }
270
271 void AliHLTTPCCAStandaloneFramework::ReadTracks( std::istream &in )
272 {
273   //* Read tracks  from file
274
275   for ( int i = 0; i < 20; i++ ) {
276     in >> fLastTime[i];
277     fStatTime[i] += fLastTime[i];
278   }
279   //fMerger.Output()->Read( in );
280 }