]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/TPCLib/tracking-ca/AliHLTTPCCAStandaloneFramework.cxx
Update of the GPU tracker
[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"
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
b22af1bf 29#ifdef HLTCA_STANDALONE
30#include <omp.h>
f0bada7f 31#include "include.h"
32#ifdef R__WIN32
33#include <conio.h>
34#else
35#include <pthread.h>
36#endif
7be9b0d7 37#endif
6de2bc40 38
39AliHLTTPCCAStandaloneFramework &AliHLTTPCCAStandaloneFramework::Instance()
40{
41 // reference to static object
42 static AliHLTTPCCAStandaloneFramework gAliHLTTPCCAStandaloneFramework;
43 return gAliHLTTPCCAStandaloneFramework;
44}
b8139972 45
6de2bc40 46AliHLTTPCCAStandaloneFramework::AliHLTTPCCAStandaloneFramework()
f0bada7f 47 : fMerger(), fOutputControl(), fTracker(), fStatNEvents( 0 ), fDebugLevel(0), fEventDisplay(0), fRunMerger(1)
6de2bc40 48{
49 //* constructor
50
b8139972 51 for ( int i = 0; i < 20; i++ ) {
6de2bc40 52 fLastTime[i] = 0;
53 fStatTime[i] = 0;
54 }
98512261 55 for ( int i = 0;i < fgkNSlices;i++) fSliceOutput[i] = NULL;
d4594e7d 56 fTracker.SetOutputControl(&fOutputControl);
6de2bc40 57}
58
59AliHLTTPCCAStandaloneFramework::AliHLTTPCCAStandaloneFramework( const AliHLTTPCCAStandaloneFramework& )
f0bada7f 60 : fMerger(), fOutputControl(), fTracker(), fStatNEvents( 0 ), fDebugLevel(0), fEventDisplay(0), fRunMerger(1)
6de2bc40 61{
62 //* dummy
63}
64
65const AliHLTTPCCAStandaloneFramework &AliHLTTPCCAStandaloneFramework::operator=( const AliHLTTPCCAStandaloneFramework& ) const
66{
67 //* dummy
68 return *this;
69}
70
71AliHLTTPCCAStandaloneFramework::~AliHLTTPCCAStandaloneFramework()
72{
f0bada7f 73#ifndef HLTCA_STANDALONE
98512261 74 for (int i = 0;i < fgkNSlices;i++) if (fSliceOutput[i]) free(fSliceOutput[i]);
f0bada7f 75#endif
b8139972 76 //* destructor
6de2bc40 77}
78
79
80void AliHLTTPCCAStandaloneFramework::StartDataReading( int guessForNumberOfClusters )
81{
82 //prepare for reading of the event
83
b8139972 84 int sliceGuess = 2 * guessForNumberOfClusters / fgkNSlices;
6de2bc40 85
b8139972 86 for ( int i = 0; i < fgkNSlices; i++ ) {
6de2bc40 87 fClusterData[i].StartReading( i, sliceGuess );
88 }
89}
90
91void AliHLTTPCCAStandaloneFramework::FinishDataReading()
92{
93 // finish reading of the event
94
7be9b0d7 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
b22af1bf 119 event_number++;*/
7be9b0d7 120
b22af1bf 121 /*std::ifstream infile(filename, std::ifstream::binary);
7be9b0d7 122 ReadEvent(infile);
123 infile.close();*/
124
b8139972 125 for ( int i = 0; i < fgkNSlices; i++ ) {
6de2bc40 126 fClusterData[i].FinishReading();
127 }
128}
129
130
f0bada7f 131int AliHLTTPCCAStandaloneFramework::ProcessEvent(int forceSingleSlice)
6de2bc40 132{
133 // perform the event reconstruction
134
135 fStatNEvents++;
136
137 TStopwatch timer0;
138 TStopwatch timer1;
139
b22af1bf 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);
d4594e7d 145
f0bada7f 146 if (fEventDisplay)
147 {
148 fTracker.SetKeepData(1);
149 }
b22af1bf 150#endif
151
152 if (forceSingleSlice != -1)
7be9b0d7 153 {
f0bada7f 154 if (fTracker.ProcessSlices(forceSingleSlice, 1, &fClusterData[forceSingleSlice], &fSliceOutput[forceSingleSlice])) return (1);
7be9b0d7 155 }
b22af1bf 156 else
7be9b0d7 157 {
b22af1bf 158 for (int iSlice = 0;iSlice < fgkNSlices;iSlice += fTracker.MaxSliceCount())
159 {
f0bada7f 160 if (fTracker.ProcessSlices(iSlice, CAMath::Min(fTracker.MaxSliceCount(), fgkNSlices - iSlice), &fClusterData[iSlice], &fSliceOutput[iSlice])) return (1);
b22af1bf 161 }
6de2bc40 162 }
163
b22af1bf 164#ifdef HLTCA_STANDALONE
165 StandaloneQueryTime(&endTime);
166 StandaloneQueryTime(&checkTime);
167#endif
168
6de2bc40 169 timer1.Stop();
170 TStopwatch timer2;
171
f0bada7f 172 if (fRunMerger)
173 {
174 fMerger.Clear();
175 fMerger.SetSliceParam( fTracker.Param(0) );
6de2bc40 176
f0bada7f 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 }
6de2bc40 181
f0bada7f 182 fMerger.Reconstruct();
183 }
6de2bc40 184
185 timer2.Stop();
186 timer0.Stop();
187
188 fLastTime[0] = timer0.CpuTime();
189 fLastTime[1] = timer1.CpuTime();
190 fLastTime[2] = timer2.CpuTime();
191
b22af1bf 192#ifdef HLTCA_STANDALONE
f0bada7f 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
b22af1bf 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
b8139972 288 for ( int i = 0; i < 3; i++ ) fStatTime[i] += fLastTime[i];
7be9b0d7 289
f0bada7f 290 return(0);
6de2bc40 291}
292
293
294void 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++ ) {
b22af1bf 299 fTracker.Param(iSlice).WriteSettings( out );
6de2bc40 300 }
301}
302
303void 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 );
b22af1bf 311 fTracker.InitializeSliceParam(iSlice, param);
6de2bc40 312 }
313}
314
7be9b0d7 315void AliHLTTPCCAStandaloneFramework::WriteEvent( std::ostream &out ) const
6de2bc40 316{
317 // write event to the file
b8139972 318 for ( int iSlice = 0; iSlice < fgkNSlices; iSlice++ ) {
7be9b0d7 319 fClusterData[iSlice].WriteEvent( out );
6de2bc40 320 }
321}
322
7be9b0d7 323void AliHLTTPCCAStandaloneFramework::ReadEvent( std::istream &in )
6de2bc40 324{
325 //* Read event from file
f0bada7f 326 int nClusters = 0;
b8139972 327 for ( int iSlice = 0; iSlice < fgkNSlices; iSlice++ ) {
7be9b0d7 328 fClusterData[iSlice].ReadEvent( in );
f0bada7f 329 nClusters += fClusterData[iSlice].NumberOfClusters();
6de2bc40 330 }
f0bada7f 331#ifdef HLTCA_STANDALONE
332 printf("Read %d Clusters\n", nClusters);
333#endif
6de2bc40 334}
335
336void AliHLTTPCCAStandaloneFramework::WriteTracks( std::ostream &out ) const
337{
338 //* Write tracks to file
339
b8139972 340 for ( int i = 0; i < 20; i++ ) out << fLastTime[i] << std::endl;
6de2bc40 341 //fMerger.Output()->Write( out );
342}
343
344void AliHLTTPCCAStandaloneFramework::ReadTracks( std::istream &in )
345{
346 //* Read tracks from file
347
b8139972 348 for ( int i = 0; i < 20; i++ ) {
349 in >> fLastTime[i];
350 fStatTime[i] += fLastTime[i];
6de2bc40 351 }
352 //fMerger.Output()->Read( in );
a59a784e 353}