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