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