]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/tracking-ca/AliHLTTPCCATracker.h
Update of the GPU tracker
[u/mrichter/AliRoot.git] / HLT / TPCLib / tracking-ca / AliHLTTPCCATracker.h
1 //-*- Mode: C++ -*-
2 // @(#) $Id$
3 // ************************************************************************
4 // This file is property of and copyright by the ALICE HLT Project        *
5 // ALICE Experiment at CERN, All rights reserved.                         *
6 // See cxx source for full Copyright notice                               *
7 //                                                                        *
8 //*************************************************************************
9
10 #ifndef ALIHLTTPCCATRACKER_H
11 #define ALIHLTTPCCATRACKER_H
12
13
14 #include "AliHLTTPCCADef.h"
15 #include "AliHLTTPCCAGPUConfig.h"
16 #include "AliHLTTPCCAParam.h"
17 #include <iostream>
18 #include "AliHLTTPCCAHitId.h"
19 #include "AliHLTTPCCASliceData.h"
20 #include "AliHLTTPCCASliceOutput.h"
21 #include "AliHLTTPCCATrackletConstructor.h"
22 #include "AliHLTTPCCATracklet.h"
23
24 class AliHLTTPCCATrack;
25 class AliHLTTPCCATrackParam;
26 class AliHLTTPCCAClusterData;
27 class AliHLTTPCCARow;
28
29 /**
30  * @class AliHLTTPCCATracker
31  *
32  * Slice tracker for ALICE HLT.
33  * The class reconstructs tracks in one slice of TPC.
34  * The reconstruction algorithm is based on the Cellular Automaton method
35  *
36  * The CA tracker is designed stand-alone.
37  * It is integrated to the HLT framework via AliHLTTPCCATrackerComponent interface.
38  * The class is under construction.
39  *
40  */
41
42 class AliHLTTPCCAClusterData;
43
44 class AliHLTTPCCATracker
45 {
46   public:
47
48   AliHLTTPCCATracker()
49     :
50 #ifdef HLTCA_STANDALONE
51       fStageAtSync( NULL ),
52           fLinkTmpMemory( NULL ),
53 #endif
54           fParam(),
55       fOutputControl(),
56       fClusterData( 0 ),
57       fData(),
58       fIsGPUTracker( false ),
59       fGPUDebugLevel( 0 ),
60       fGPUDebugOut( 0 ),
61       fRowStartHitCountOffset( NULL ),
62       fTrackletTmpStartHits( NULL ),
63       fGPUTrackletTemp( NULL ),
64       fRowBlockTracklets( NULL ),
65       fRowBlockPos( NULL ),
66       fBlockStartingTracklet( NULL ),
67       fGPUParametersConst(),
68       fCommonMem( 0 ),
69       fHitMemory( 0 ),
70       fHitMemorySize( 0 ),
71       fTrackletMemory( 0 ),
72       fTrackletMemorySize( 0 ),
73       fTrackMemory( 0 ),
74       fTrackMemorySize( 0 ),
75       fTrackletStartHits( 0 ),
76       fTracklets( 0 ),
77       fTrackletRowHits( NULL ),
78       fTracks( 0 ),
79       fTrackHits( 0 ),
80       fOutput( 0 )
81   {
82     // constructor
83   }
84   ~AliHLTTPCCATracker();
85   
86   struct StructGPUParameters
87   {
88     StructGPUParameters() : fScheduleFirstDynamicTracklet( 0 ), fGPUError( 0 ) {}
89     int fScheduleFirstDynamicTracklet;          //Last Tracklet with fixed position in sheduling
90     int fGPUError;                                                      //Signalizes error on GPU during GPU Reconstruction, kind of return value
91   };
92   
93   struct StructGPUParametersConst
94   {
95     StructGPUParametersConst() : fGPUFixedBlockCount( 0 ), fGPUiSlice( 0 ), fGPUnSlices( 0 ) {}
96     int fGPUFixedBlockCount;                            //Count of blocks that is used for this tracker in fixed schedule situations
97     int fGPUiSlice;                                                     // slice number processed by running GPU MP
98     int fGPUnSlices;                                            // n of slices to be processed in parallel
99   };
100   
101   struct commonMemoryStruct
102   {
103     commonMemoryStruct() : fNTracklets( 0 ), fNTracks( 0 ), fNTrackHits( 0 ), fGPUParameters() {}
104     int fNTracklets;     // number of tracklets
105     int fNTracks;            // number of reconstructed tracks
106     int fNTrackHits;           // number of track hits
107     StructGPUParameters fGPUParameters; // GPU parameters
108   };
109   
110   void Initialize( const AliHLTTPCCAParam &param );
111   
112   void StartEvent();
113   
114   int CheckEmptySlice() const;
115   void WriteOutput();
116   
117 #if !defined(HLTCA_GPUCODE)
118   void Reconstruct();
119 #endif //!HLTCA_GPUCODE
120   
121   //Make Reconstruction steps directly callable (Used for GPU debugging)
122   void RunNeighboursFinder();
123   void RunNeighboursCleaner();
124   void RunStartHitsFinder();
125   void RunTrackletConstructor();
126   void RunTrackletSelector();
127   
128   //GPU Tracker Interface
129   void SetGPUTracker();
130   void SetGPUDebugLevel(int Level, std::ostream *NewDebugOut = NULL) {fGPUDebugLevel = Level;if (NewDebugOut) fGPUDebugOut = NewDebugOut;}
131   
132   char* SetGPUTrackerCommonMemory(char* const pGPUMemory);
133   char* SetGPUTrackerHitsMemory(char* pGPUMemory, int MaxNHits);
134   char* SetGPUTrackerTrackletsMemory(char* pGPUMemory, int MaxNTracklets);
135   char* SetGPUTrackerTracksMemory(char* pGPUMemory, int MaxNTracks, int MaxNHits );
136   
137   //Debugging Stuff
138   void DumpSliceData(std::ostream &out);        //Dump Input Slice Data
139   void DumpLinks(std::ostream &out);            //Dump all links to file (for comparison after NeighboursFinder/Cleaner)
140   void DumpStartHits(std::ostream &out);        //Same for Start Hits
141   void DumpHitWeights(std::ostream &out); //....
142   void DumpTrackHits(std::ostream &out);        //Same for Track Hits
143   void DumpTrackletHits(std::ostream &out);     //Same for Track Hits
144   
145   GPUd() void GetErrors2( int iRow,  const AliHLTTPCCATrackParam &t, float &Err2Y, float &Err2Z ) const;
146   GPUd() void GetErrors2( int iRow, float z, float sinPhi, float cosPhi, float DzDs, float &Err2Y, float &Err2Z ) const;
147   
148   void FitTrack( const AliHLTTPCCATrack &track, float *t0 = 0 ) const;
149   void FitTrackFull( const AliHLTTPCCATrack &track, float *t0 = 0 ) const;
150   
151   void SetupCommonMemory();
152   void SetPointersHits( int MaxNHits );
153   void SetPointersTracklets ( int MaxNTracklets );
154   void SetPointersTracks( int MaxNTracks, int MaxNHits );
155   size_t SetPointersSliceData(const AliHLTTPCCAClusterData *data, bool allocate = false) { return(fData.SetPointers(data, allocate)); }
156   
157   void SetOutput( AliHLTTPCCASliceOutput** out ) { fOutput = out; }
158   
159   void ReadEvent( AliHLTTPCCAClusterData *clusterData );
160   
161 #if !defined(HLTCA_GPUCODE)
162   GPUh() void WriteEvent( std::ostream &out );
163   GPUh() void WriteTracks( std::ostream &out ) ;
164   GPUh() void ReadTracks( std::istream &in );
165 #endif //!HLTCA_GPUCODE
166   
167   GPUhd() const AliHLTTPCCAParam &Param() const { return fParam; }
168   GPUhd() void SetParam( const AliHLTTPCCAParam &v ) { fParam = v; }
169   
170   GPUhd() const AliHLTTPCCASliceOutput::outputControlStruct* OutputControl() const { return fOutputControl; }  
171   GPUh() void SetOutputControl( AliHLTTPCCASliceOutput::outputControlStruct* const val) { fOutputControl = val; }
172
173   GPUhd() AliHLTTPCCAClusterData *ClusterData() const { return fClusterData; }
174   GPUhd() const AliHLTTPCCASliceData &Data() const { return fData; }
175   
176   GPUh() void ClearSliceDataHitWeights() {fData.ClearHitWeights();}
177   
178   GPUhd() const AliHLTTPCCARow &Row( int rowIndex ) const { return fData.Row( rowIndex ); }
179   GPUh() const AliHLTTPCCARow &Row( const AliHLTTPCCAHitId &HitId ) const { return fData.Row( HitId.RowIndex() ); }
180   
181   GPUhd() double Timer( int i ) const { return fTimers[i]; }
182   GPUhd() void SetTimer( int i, double v ) { fTimers[i] = v; }
183   
184   GPUhd() int NHitsTotal() const { return fData.NumberOfHits(); }
185   
186   GPUd() void SetHitLinkUpData( const AliHLTTPCCARow &row, int hitIndex, short v ) { fData.SetHitLinkUpData( row, hitIndex, v ); }
187   GPUd() void SetHitLinkDownData( const AliHLTTPCCARow &row, int hitIndex, short v ) { fData.SetHitLinkDownData( row, hitIndex, v ); }
188   GPUd() short HitLinkUpData( const AliHLTTPCCARow &row, int hitIndex ) const { return fData.HitLinkUpData( row, hitIndex ); }
189   GPUd() short HitLinkDownData( const AliHLTTPCCARow &row, int hitIndex ) const { return fData.HitLinkDownData( row, hitIndex ); }
190   
191   GPUd() const ushort2 *HitData( const AliHLTTPCCARow &row ) const { return fData.HitData(row); }
192   GPUd() const short_v *HitLinkUpData  ( const AliHLTTPCCARow &row ) const { return fData.HitLinkUpData(row); }
193   GPUd() const short_v *HitLinkDownData( const AliHLTTPCCARow &row ) const { return fData.HitLinkDownData(row); }
194   GPUd() const ushort_v *FirstHitInBin( const AliHLTTPCCARow &row ) const { return fData.FirstHitInBin(row); }
195   
196   GPUd() int FirstHitInBin( const AliHLTTPCCARow &row, int binIndex ) const { return fData.FirstHitInBin( row, binIndex ); }
197   
198   GPUd() unsigned short HitDataY( const AliHLTTPCCARow &row, int hitIndex ) const {
199     return fData.HitDataY( row, hitIndex );
200   }
201   GPUd() unsigned short HitDataZ( const AliHLTTPCCARow &row, int hitIndex ) const {
202     return fData.HitDataZ( row, hitIndex );
203   }
204   GPUd() ushort2 HitData( const AliHLTTPCCARow &row, int hitIndex ) const {
205     return fData.HitData( row, hitIndex );
206   }
207   
208   GPUhd() int HitInputID( const AliHLTTPCCARow &row, int hitIndex ) const { return fData.ClusterDataIndex( row, hitIndex ); }
209   
210   /**
211    * The hit weight is used to determine whether a hit belongs to a certain tracklet or another one
212    * competing for the same hit. The tracklet that has a higher weight wins. Comparison is done
213    * using the the number of hits in the tracklet (the more hits it has the more it keeps). If
214    * tracklets have the same number of hits then it doesn't matter who gets it, but it should be
215    * only one. So a unique number (row index is good) is added in the least significant part of
216    * the weight
217    */
218   static int CalculateHitWeight( int NHits, int unique ) {
219     return ( NHits << 16 ) + unique;
220   }
221   GPUd() void MaximizeHitWeight( const AliHLTTPCCARow &row, int hitIndex, int weight ) {
222     fData.MaximizeHitWeight( row, hitIndex, weight );
223   }
224   GPUd() int HitWeight( const AliHLTTPCCARow &row, int hitIndex ) const {
225     return fData.HitWeight( row, hitIndex );
226   }
227   
228   GPUhd() int *NTracklets() const { return &fCommonMem->fNTracklets; }
229   
230   GPUhd() const AliHLTTPCCAHitId &TrackletStartHit( int i ) const { return fTrackletStartHits[i]; }
231   GPUhd() AliHLTTPCCAHitId *TrackletStartHits() const { return fTrackletStartHits; }
232   GPUhd() AliHLTTPCCAHitId *TrackletTmpStartHits() const { return fTrackletTmpStartHits; }
233   GPUhd() const AliHLTTPCCATracklet &Tracklet( int i ) const { return fTracklets[i]; }
234   GPUhd() AliHLTTPCCATracklet  *Tracklets() const { return fTracklets;}
235   GPUhd() int* TrackletRowHits() const { return fTrackletRowHits; }
236
237   GPUhd() int *NTracks()  const { return &fCommonMem->fNTracks; }
238   GPUhd() AliHLTTPCCATrack *Tracks() const { return  fTracks; }
239   GPUhd() int *NTrackHits()  const { return &fCommonMem->fNTrackHits; }
240   GPUhd() AliHLTTPCCAHitId *TrackHits() const { return fTrackHits; }
241   
242   GPUhd() AliHLTTPCCASliceOutput** Output() const { return fOutput; }
243   
244   GPUh() commonMemoryStruct *CommonMemory() const {return(fCommonMem); }
245   GPUh() static  size_t CommonMemorySize() { return(sizeof(AliHLTTPCCATracker::commonMemoryStruct)); }
246   GPUh() char* HitMemory() const {return(fHitMemory); }
247   GPUh() size_t HitMemorySize() const {return(fHitMemorySize); }
248   GPUh() char* TrackletMemory() {return(fTrackletMemory); }
249   GPUh() size_t TrackletMemorySize() const {return(fTrackletMemorySize); }
250   GPUh() char* TrackMemory() {return(fTrackMemory); }
251   GPUh() size_t TrackMemorySize() const {return(fTrackMemorySize); }
252   GPUhd() AliHLTTPCCARow* SliceDataRows() const {return(fData.Rows()); }
253   
254   GPUhd() uint3* RowStartHitCountOffset() const {return(fRowStartHitCountOffset);}
255   GPUhd() AliHLTTPCCATrackletConstructor::AliHLTTPCCAGPUTempMemory* GPUTrackletTemp() const {return(fGPUTrackletTemp);}
256   GPUhd() int* RowBlockTracklets(int reverse, int iRowBlock) const {return(&fRowBlockTracklets[(reverse * ((fParam.NRows() / HLTCA_GPU_SCHED_ROW_STEP) + 1) + iRowBlock) * fCommonMem->fNTracklets]);}
257   GPUhd() int* RowBlockTracklets() const {return(fRowBlockTracklets);}
258   GPUhd() int4* RowBlockPos(int reverse, int iRowBlock) const {return(&fRowBlockPos[reverse * ((fParam.NRows() / HLTCA_GPU_SCHED_ROW_STEP) + 1) + iRowBlock]);}
259   GPUhd() int4* RowBlockPos() const {return(fRowBlockPos);}
260   GPUhd() uint2* BlockStartingTracklet() const {return(fBlockStartingTracklet);}
261   GPUhd() StructGPUParameters* GPUParameters() const {return(&fCommonMem->fGPUParameters);}
262   GPUhd() StructGPUParametersConst* GPUParametersConst() {return(&fGPUParametersConst);}
263   GPUhd() void SetGPUTextureBase(char* val) { fData.SetGPUTextureBase(val); }
264   GPUh() void SetGPUSliceDataMemory(void* const pSliceMemory, void* const pRowMemory) { fData.SetGPUSliceDataMemory(pSliceMemory, pRowMemory); }
265
266   GPUh() unsigned long long int* PerfTimer(unsigned int i) {return &fPerfTimers[i]; }
267
268 #ifdef HLTCA_STANDALONE
269   GPUhd() char* StageAtSync() {return(fStageAtSync);}
270   GPUh() const char* LinkTmpMemory() const {return(fLinkTmpMemory);}
271 #endif
272
273 private:
274         //Temporary Variables for Standalone measurements
275 #ifdef HLTCA_STANDALONE
276   char* fStageAtSync;                           //Pointer to array storing current stage for every thread at every sync point
277   char *fLinkTmpMemory; //tmp memory for hits after neighbours finder
278 #endif
279   
280   AliHLTTPCCAParam fParam; // parameters
281   double fTimers[10]; // timers
282   unsigned long long int fPerfTimers[16]; // running CPU time for different parts of the algorithm
283   void StandalonePerfTime(int i);
284   
285   AliHLTTPCCASliceOutput::outputControlStruct* fOutputControl; // output control
286   
287   /** A pointer to the ClusterData object that the SliceData was created from. This can be used to
288    * merge clusters from inside the SliceTracker code and recreate the SliceData. */
289   AliHLTTPCCAClusterData *fClusterData; // ^
290   AliHLTTPCCASliceData fData; // The SliceData object. It is used to encapsulate the storage in memory from the access
291   
292   bool fIsGPUTracker; // is it GPU tracker object
293   int fGPUDebugLevel; // debug level
294   std::ostream *fGPUDebugOut; // debug stream
295   
296   //GPU Temp Arrays
297   uint3* fRowStartHitCountOffset;                               //Offset, length and new offset of start hits in row
298   AliHLTTPCCAHitId *fTrackletTmpStartHits;      //Unsorted start hits
299   AliHLTTPCCATrackletConstructor::AliHLTTPCCAGPUTempMemory *fGPUTrackletTemp;   //Temp Memory for GPU Tracklet Constructor
300   int* fRowBlockTracklets;                                      //Reference which tracklet is processed in which rowblock next
301   int4* fRowBlockPos;                                                   //x is last tracklet to be processed, y is last tracklet already processed, z is last tracklet to be processed in next iteration, w is initial x value to check if tracklet must be initialized  
302   uint2* fBlockStartingTracklet;                        // First Tracklet that is to be processed by current GPU MP
303
304   StructGPUParametersConst fGPUParametersConst; // Parameters for GPU if this is a GPU tracker
305
306   // event
307   
308   commonMemoryStruct *fCommonMem; // common event memory
309   
310   char *fHitMemory; // event memory for hits
311   size_t   fHitMemorySize; // size of the event memory for hits [bytes]
312
313   char *fTrackletMemory;        //event memory for tracklets
314   size_t fTrackletMemorySize; //size of the event memory for tracklets
315
316   char *fTrackMemory; // event memory for tracks
317   size_t   fTrackMemorySize; // size of the event memory for tracks [bytes]
318
319   AliHLTTPCCAHitId *fTrackletStartHits;   // start hits for the tracklets
320   AliHLTTPCCATracklet *fTracklets; // tracklets
321   int *fTrackletRowHits;                        //Hits for each Tracklet in each row
322
323   //
324   AliHLTTPCCATrack *fTracks;  // reconstructed tracks
325   AliHLTTPCCAHitId *fTrackHits;          // array of track hit numbers
326   
327   // output
328   
329   AliHLTTPCCASliceOutput **fOutput;             //address of pointer pointing to SliceOutput Object
330   
331   // disable copy
332   AliHLTTPCCATracker( const AliHLTTPCCATracker& );
333   AliHLTTPCCATracker &operator=( const AliHLTTPCCATracker& );
334   
335   static int StarthitSortComparison(const void*a, const void* b);
336 };
337
338
339 #endif //ALIHLTTPCCATRACKER_H