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