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