]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/tracking-ca/AliHLTTPCCAStandaloneFramework.cxx
Modifications (M.Nicassio)
[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 "AliHLTTPCCADataCompressor.h"
23 #include "AliHLTTPCCAMath.h"
24 #include "AliHLTTPCCAClusterData.h"
25 #include "TStopwatch.h"
26
27 #ifdef HLTCA_STANDALONE
28 #include <omp.h>
29 #include "include.h"
30 #ifdef R__WIN32
31 #include <conio.h>
32 #else
33 #include <pthread.h>
34 #endif
35 #endif
36
37 AliHLTTPCCAStandaloneFramework &AliHLTTPCCAStandaloneFramework::Instance()
38 {
39   // reference to static object
40   static AliHLTTPCCAStandaloneFramework gAliHLTTPCCAStandaloneFramework;
41   return gAliHLTTPCCAStandaloneFramework;
42 }
43
44 AliHLTTPCCAStandaloneFramework::AliHLTTPCCAStandaloneFramework()
45     : fMerger(), fOutputControl(), fTracker(), fStatNEvents( 0 ), fDebugLevel(0), fEventDisplay(0), fRunMerger(1)
46 {
47   //* constructor
48
49   for ( int i = 0; i < 20; i++ ) {
50     fLastTime[i] = 0;
51     fStatTime[i] = 0;
52   }
53   for ( int i = 0;i < fgkNSlices;i++) fSliceOutput[i] = NULL;
54   fTracker.SetOutputControl(&fOutputControl);
55 }
56
57 AliHLTTPCCAStandaloneFramework::AliHLTTPCCAStandaloneFramework( const AliHLTTPCCAStandaloneFramework& )
58     : fMerger(), fOutputControl(), fTracker(), fStatNEvents( 0 ), fDebugLevel(0), fEventDisplay(0), fRunMerger(1)
59 {
60   //* dummy
61   for ( int i = 0; i < 20; i++ ) {
62     fLastTime[i] = 0;
63     fStatTime[i] = 0;
64   }
65   for ( int i = 0;i < fgkNSlices;i++) fSliceOutput[i] = NULL;
66 }
67
68 const AliHLTTPCCAStandaloneFramework &AliHLTTPCCAStandaloneFramework::operator=( const AliHLTTPCCAStandaloneFramework& ) const
69 {
70   //* dummy
71   return *this;
72 }
73
74 AliHLTTPCCAStandaloneFramework::~AliHLTTPCCAStandaloneFramework()
75 {
76 #ifndef HLTCA_STANDALONE
77         for (int i = 0;i < fgkNSlices;i++) if (fSliceOutput[i]) free(fSliceOutput[i]);
78 #endif
79   //* destructor
80 }
81
82
83 void AliHLTTPCCAStandaloneFramework::StartDataReading( int guessForNumberOfClusters )
84 {
85   //prepare for reading of the event
86
87   int sliceGuess = 2 * guessForNumberOfClusters / fgkNSlices;
88
89   for ( int i = 0; i < fgkNSlices; i++ ) {
90     fClusterData[i].StartReading( i, sliceGuess );
91   }
92 }
93
94 void AliHLTTPCCAStandaloneFramework::FinishDataReading()
95 {
96   // finish reading of the event
97
98   /*static int event_number = 0;
99   char filename[256];
100
101   sprintf(filename, "events/event.%d.dump", event_number);
102   printf("Dumping event into file %s\n", filename);
103   std::ofstream outfile(filename, std::ofstream::binary);
104   if (outfile.fail())
105   {
106     printf("Error opening event dump file\n");
107     exit(1);
108   }
109   WriteEvent(outfile);
110   if (outfile.fail())
111   {
112     printf("Error writing event dump file\n");
113     exit(1);
114   }
115   outfile.close();
116
117   sprintf(filename, "events/settings.%d.dump", event_number);
118   outfile.open(filename);
119   WriteSettings(outfile);
120   outfile.close();
121
122   event_number++;*/
123   
124   /*std::ifstream infile(filename, std::ifstream::binary);
125   ReadEvent(infile);
126   infile.close();*/
127
128   for ( int i = 0; i < fgkNSlices; i++ ) {
129     fClusterData[i].FinishReading();
130   }
131 }
132
133
134 int AliHLTTPCCAStandaloneFramework::ProcessEvent(int forceSingleSlice)
135 {
136   // perform the event reconstruction
137
138   fStatNEvents++;
139
140   TStopwatch timer0;
141   TStopwatch timer1;
142
143 #ifdef HLTCA_STANDALONE
144   unsigned long long int startTime, endTime, checkTime;
145   unsigned long long int cpuTimers[16], gpuTimers[16], tmpFreq;
146   StandaloneQueryFreq(&tmpFreq);
147   StandaloneQueryTime(&startTime);
148
149   if (fEventDisplay)
150   {
151         fTracker.SetKeepData(1);
152   }
153 #endif
154
155   if (forceSingleSlice != -1)
156   {
157         if (fTracker.ProcessSlices(forceSingleSlice, 1, &fClusterData[forceSingleSlice], &fSliceOutput[forceSingleSlice])) return (1);
158   }
159   else
160   {
161         for (int iSlice = 0;iSlice < fgkNSlices;iSlice += fTracker.MaxSliceCount())
162         {
163                 if (fTracker.ProcessSlices(iSlice, CAMath::Min(fTracker.MaxSliceCount(), fgkNSlices - iSlice), &fClusterData[iSlice], &fSliceOutput[iSlice])) return (1);
164         }
165   }
166
167 #ifdef HLTCA_STANDALONE
168   StandaloneQueryTime(&endTime);
169   StandaloneQueryTime(&checkTime);
170 #endif
171
172   timer1.Stop();
173   TStopwatch timer2;
174
175   if (fRunMerger)
176   {
177           fMerger.Clear();
178           fMerger.SetSliceParam( fTracker.Param(0) );
179
180           for ( int i = 0; i < fgkNSlices; i++ ) {
181                 //printf("slice %d clusters %d tracks %d\n", i, fClusterData[i].NumberOfClusters(), fSliceOutput[i]->NTracks());
182                 fMerger.SetSliceData( i, fSliceOutput[i] );
183           }
184
185           fMerger.Reconstruct();
186   }
187
188   timer2.Stop();
189   timer0.Stop();
190
191   fLastTime[0] = timer0.CpuTime();
192   fLastTime[1] = timer1.CpuTime();
193   fLastTime[2] = timer2.CpuTime();
194
195 #ifdef HLTCA_STANDALONE
196   if (fEventDisplay)
197   {
198     static int displayActive = 0;
199         if (!displayActive)
200         {
201 #ifdef R__WIN32
202                 semLockDisplay = CreateSemaphore(0, 1, 1, 0);
203                 HANDLE hThread;
204                 if ((hThread = CreateThread(NULL, NULL, &OpenGLMain, NULL, NULL, NULL)) == NULL)
205 #else
206                 static pthread_t hThread;
207                 if (pthread_create(&hThread, NULL, OpenGLMain, NULL))
208 #endif
209                 {
210                         printf("Coult not Create GL Thread...\nExiting...\n");
211                 }
212                 displayActive = 1;
213         }
214         else
215         {
216 #ifdef R__WIN32
217                 ReleaseSemaphore(semLockDisplay, 1, NULL);
218 #else
219                 pthread_mutex_unlock(&semLockDisplay);
220 #endif
221         }
222
223 #ifdef R__WIN32
224         while (kbhit()) getch();
225 #endif
226         printf("Press key for next event!\n");
227
228         int iKey;
229         do
230         {
231 #ifdef R__WIN32
232                 Sleep(10);
233                 iKey = kbhit() ? getch() : 0;
234 #else
235                 iKey = getchar();
236 #endif
237                 if (iKey == 'q') exit(0);
238         } while (iKey != 'n' && buttonPressed == 0);
239         buttonPressed = 0;
240         printf("Loading next event\n");
241
242 #ifdef R__WIN32
243         WaitForSingleObject(semLockDisplay, INFINITE);
244 #else
245         pthread_mutex_lock(&semLockDisplay);
246 #endif
247
248         displayEventNr++;
249   }
250
251   printf("Tracking Time: %lld us\nTime uncertainty: %lld ns\n", (endTime - startTime) * 1000000 / tmpFreq, (checkTime - endTime) * 1000000000 / tmpFreq);
252
253   if (fDebugLevel >= 1)
254   {
255                 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"};
256
257                 for (int i = 0;i < 10;i++)
258                 {
259                         if (i == 6) continue;
260                         cpuTimers[i] = gpuTimers[i] = 0;
261                         for ( int iSlice = 0; iSlice < fgkNSlices;iSlice++)
262                         {
263                                 if (forceSingleSlice != -1) iSlice = forceSingleSlice;
264                                 cpuTimers[i] += *fTracker.PerfTimer(0, iSlice, i + 1) - *fTracker.PerfTimer(0, iSlice, i);
265                                 if (forceSingleSlice != -1 || (fTracker.MaxSliceCount() && (iSlice % fTracker.MaxSliceCount() == 0 || i <= 5)))
266                                         gpuTimers[i] += *fTracker.PerfTimer(1, iSlice, i + 1) - *fTracker.PerfTimer(1, iSlice, i);
267                                 if (forceSingleSlice != -1) break;
268                         }
269                         if (forceSingleSlice == -1)
270                         {
271                                 cpuTimers[i] /= fgkNSlices;
272                                 gpuTimers[i] /= fgkNSlices;
273                         }
274                         cpuTimers[i] *= 1000000;
275                         gpuTimers[i] *= 1000000;
276                         cpuTimers[i] /= tmpFreq;
277                         gpuTimers[i] /= tmpFreq;
278                         cpuTimers[i] /= omp_get_max_threads();
279
280                         printf("Execution Time: Task: %20s ", tmpNames[i]);
281                                 printf("CPU: %15lld\t\t", cpuTimers[i]);
282                                 printf("GPU: %15lld\t\t", gpuTimers[i]);
283                         if (fDebugLevel >=6 && gpuTimers[i])
284                                 printf("Speedup: %4lld%%", cpuTimers[i] * 100 / gpuTimers[i]);
285                         printf("\n");
286                 }
287                 printf("Execution Time: Task: %20s CPU: %15lld\n", "Merger", (long long int) (timer2.CpuTime() * 1000000));
288   }
289 #endif
290
291   for ( int i = 0; i < 3; i++ ) fStatTime[i] += fLastTime[i];
292
293   return(0);
294 }
295
296
297 void AliHLTTPCCAStandaloneFramework::WriteSettings( std::ostream &out ) const
298 {
299   //* write settings to the file
300   out << NSlices() << std::endl;
301   for ( int iSlice = 0; iSlice < NSlices(); iSlice++ ) {
302     fTracker.Param(iSlice).WriteSettings( out );
303   }
304 }
305
306 void AliHLTTPCCAStandaloneFramework::ReadSettings( std::istream &in )
307 {
308   //* Read settings from the file
309   int nSlices = 0;
310   in >> nSlices;
311   if( nSlices>0 && nSlices<100 ){
312     for ( int iSlice = 0; iSlice < nSlices; iSlice++ ) {
313       AliHLTTPCCAParam param;
314       param.ReadSettings ( in );
315       fTracker.InitializeSliceParam(iSlice, param);
316     }
317   }
318 }
319
320 void AliHLTTPCCAStandaloneFramework::WriteEvent( std::ostream &out ) const
321 {
322   // write event to the file
323   for ( int iSlice = 0; iSlice < fgkNSlices; iSlice++ ) {
324     fClusterData[iSlice].WriteEvent( out );
325   }
326 }
327
328 void AliHLTTPCCAStandaloneFramework::ReadEvent( std::istream &in )
329 {
330   //* Read event from file
331   int nClusters = 0;
332   for ( int iSlice = 0; iSlice < fgkNSlices; iSlice++ ) {
333     fClusterData[iSlice].ReadEvent( in );
334         nClusters += fClusterData[iSlice].NumberOfClusters();
335   }
336 #ifdef HLTCA_STANDALONE
337   printf("Read %d Clusters\n", nClusters);
338 #endif
339 }
340
341 void AliHLTTPCCAStandaloneFramework::WriteTracks( std::ostream &out ) const
342 {
343   //* Write tracks to file
344
345   for ( int i = 0; i < 20; i++ ) out << fLastTime[i] << std::endl;
346   //fMerger.Output()->Write( out );
347 }
348
349 void AliHLTTPCCAStandaloneFramework::ReadTracks( std::istream &in )
350 {
351   //* Read tracks  from file
352
353   for ( int i = 0; i < 20; i++ ) {
354     in >> fLastTime[i];
355     fStatTime[i] += fLastTime[i];
356   }
357   //fMerger.Output()->Read( in );
358 }