1 // **************************************************************************
2 // This file is property of and copyright by the ALICE HLT Project *
3 // ALICE Experiment at CERN, All rights reserved. *
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. *
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. *
17 //***************************************************************************
20 #include "AliHLTTPCCAStandaloneFramework.h"
21 #include "AliHLTTPCCATrackParam.h"
22 #include "AliHLTTPCCADataCompressor.h"
23 #include "AliHLTTPCCAMath.h"
24 #include "AliHLTTPCCAClusterData.h"
25 #include "TStopwatch.h"
27 #ifdef HLTCA_STANDALONE
37 AliHLTTPCCAStandaloneFramework &AliHLTTPCCAStandaloneFramework::Instance()
39 // reference to static object
40 static AliHLTTPCCAStandaloneFramework gAliHLTTPCCAStandaloneFramework;
41 return gAliHLTTPCCAStandaloneFramework;
44 AliHLTTPCCAStandaloneFramework::AliHLTTPCCAStandaloneFramework()
45 : fMerger(), fOutputControl(), fTracker(), fStatNEvents( 0 ), fDebugLevel(0), fEventDisplay(0), fRunMerger(1)
49 for ( int i = 0; i < 20; i++ ) {
53 for ( int i = 0;i < fgkNSlices;i++) fSliceOutput[i] = NULL;
54 fTracker.SetOutputControl(&fOutputControl);
57 AliHLTTPCCAStandaloneFramework::AliHLTTPCCAStandaloneFramework( const AliHLTTPCCAStandaloneFramework& )
58 : fMerger(), fOutputControl(), fTracker(), fStatNEvents( 0 ), fDebugLevel(0), fEventDisplay(0), fRunMerger(1)
61 for ( int i = 0; i < 20; i++ ) {
65 for ( int i = 0;i < fgkNSlices;i++) fSliceOutput[i] = NULL;
68 const AliHLTTPCCAStandaloneFramework &AliHLTTPCCAStandaloneFramework::operator=( const AliHLTTPCCAStandaloneFramework& ) const
74 AliHLTTPCCAStandaloneFramework::~AliHLTTPCCAStandaloneFramework()
76 #ifndef HLTCA_STANDALONE
77 for (int i = 0;i < fgkNSlices;i++) if (fSliceOutput[i]) free(fSliceOutput[i]);
83 void AliHLTTPCCAStandaloneFramework::StartDataReading( int guessForNumberOfClusters )
85 //prepare for reading of the event
87 int sliceGuess = 2 * guessForNumberOfClusters / fgkNSlices;
89 for ( int i = 0; i < fgkNSlices; i++ ) {
90 fClusterData[i].StartReading( i, sliceGuess );
94 void AliHLTTPCCAStandaloneFramework::FinishDataReading()
96 // finish reading of the event
98 /*static int event_number = 0;
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);
106 printf("Error opening event dump file\n");
112 printf("Error writing event dump file\n");
117 sprintf(filename, "events/settings.%d.dump", event_number);
118 outfile.open(filename);
119 WriteSettings(outfile);
124 /*std::ifstream infile(filename, std::ifstream::binary);
128 for ( int i = 0; i < fgkNSlices; i++ ) {
129 fClusterData[i].FinishReading();
134 int AliHLTTPCCAStandaloneFramework::ProcessEvent(int forceSingleSlice)
136 // perform the event reconstruction
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);
151 fTracker.SetKeepData(1);
155 if (forceSingleSlice != -1)
157 if (fTracker.ProcessSlices(forceSingleSlice, 1, &fClusterData[forceSingleSlice], &fSliceOutput[forceSingleSlice])) return (1);
161 for (int iSlice = 0;iSlice < fgkNSlices;iSlice += fTracker.MaxSliceCount())
163 if (fTracker.ProcessSlices(iSlice, CAMath::Min(fTracker.MaxSliceCount(), fgkNSlices - iSlice), &fClusterData[iSlice], &fSliceOutput[iSlice])) return (1);
167 #ifdef HLTCA_STANDALONE
168 StandaloneQueryTime(&endTime);
169 StandaloneQueryTime(&checkTime);
178 fMerger.SetSliceParam( fTracker.Param(0) );
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] );
185 fMerger.Reconstruct();
191 fLastTime[0] = timer0.CpuTime();
192 fLastTime[1] = timer1.CpuTime();
193 fLastTime[2] = timer2.CpuTime();
195 #ifdef HLTCA_STANDALONE
198 static int displayActive = 0;
202 semLockDisplay = CreateSemaphore(0, 1, 1, 0);
204 if ((hThread = CreateThread(NULL, NULL, &OpenGLMain, NULL, NULL, NULL)) == NULL)
206 static pthread_t hThread;
207 if (pthread_create(&hThread, NULL, OpenGLMain, NULL))
210 printf("Coult not Create GL Thread...\nExiting...\n");
217 ReleaseSemaphore(semLockDisplay, 1, NULL);
219 pthread_mutex_unlock(&semLockDisplay);
224 while (kbhit()) getch();
226 printf("Press key for next event!\n");
233 iKey = kbhit() ? getch() : 0;
237 if (iKey == 'q') exit(0);
238 } while (iKey != 'n' && buttonPressed == 0);
240 printf("Loading next event\n");
243 WaitForSingleObject(semLockDisplay, INFINITE);
245 pthread_mutex_lock(&semLockDisplay);
251 printf("Tracking Time: %lld us\nTime uncertainty: %lld ns\n", (endTime - startTime) * 1000000 / tmpFreq, (checkTime - endTime) * 1000000000 / tmpFreq);
253 if (fDebugLevel >= 1)
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"};
257 for (int i = 0;i < 10;i++)
259 if (i == 6) continue;
260 cpuTimers[i] = gpuTimers[i] = 0;
261 for ( int iSlice = 0; iSlice < fgkNSlices;iSlice++)
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;
269 if (forceSingleSlice == -1)
271 cpuTimers[i] /= fgkNSlices;
272 gpuTimers[i] /= fgkNSlices;
274 cpuTimers[i] *= 1000000;
275 gpuTimers[i] *= 1000000;
276 cpuTimers[i] /= tmpFreq;
277 gpuTimers[i] /= tmpFreq;
278 cpuTimers[i] /= omp_get_max_threads();
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]);
287 printf("Execution Time: Task: %20s CPU: %15lld\n", "Merger", (long long int) (timer2.CpuTime() * 1000000));
291 for ( int i = 0; i < 3; i++ ) fStatTime[i] += fLastTime[i];
297 void AliHLTTPCCAStandaloneFramework::WriteSettings( std::ostream &out ) const
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 );
306 void AliHLTTPCCAStandaloneFramework::ReadSettings( std::istream &in )
308 //* Read settings from the file
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);
320 void AliHLTTPCCAStandaloneFramework::WriteEvent( std::ostream &out ) const
322 // write event to the file
323 for ( int iSlice = 0; iSlice < fgkNSlices; iSlice++ ) {
324 fClusterData[iSlice].WriteEvent( out );
328 void AliHLTTPCCAStandaloneFramework::ReadEvent( std::istream &in )
330 //* Read event from file
332 for ( int iSlice = 0; iSlice < fgkNSlices; iSlice++ ) {
333 fClusterData[iSlice].ReadEvent( in );
334 nClusters += fClusterData[iSlice].NumberOfClusters();
336 #ifdef HLTCA_STANDALONE
337 printf("Read %d Clusters\n", nClusters);
341 void AliHLTTPCCAStandaloneFramework::WriteTracks( std::ostream &out ) const
343 //* Write tracks to file
345 for ( int i = 0; i < 20; i++ ) out << fLastTime[i] << std::endl;
346 //fMerger.Output()->Write( out );
349 void AliHLTTPCCAStandaloneFramework::ReadTracks( std::istream &in )
351 //* Read tracks from file
353 for ( int i = 0; i < 20; i++ ) {
355 fStatTime[i] += fLastTime[i];
357 //fMerger.Output()->Read( in );