]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/tracking-ca/AliHLTTPCCASliceData.h
14914cd116409c25e1d59ccbb189c64e70d371d1
[u/mrichter/AliRoot.git] / HLT / TPCLib / tracking-ca / AliHLTTPCCASliceData.h
1 // **************************************************************************
2 // * This file is property of and copyright by the ALICE HLT Project        *
3 // * All rights reserved.                                                   *
4 // *                                                                        *
5 // * Primary Authors:                                                       *
6 // *     Copyright 2009       Matthias Kretz <kretz@kde.org>                *
7 // *                                                                        *
8 // * Permission to use, copy, modify and distribute this software and its   *
9 // * documentation strictly for non-commercial purposes is hereby granted   *
10 // * without fee, provided that the above copyright notice appears in all   *
11 // * copies and that both the copyright notice and this permission notice   *
12 // * appear in the supporting documentation. The authors make no claims     *
13 // * about the suitability of this software for any purpose. It is          *
14 // * provided "as is" without express or implied warranty.                  *
15 // **************************************************************************
16
17 #ifndef ALIHLTTPCCASLICEDATA_H
18 #define ALIHLTTPCCASLICEDATA_H
19
20 #include "AliHLTTPCCADef.h"
21 #include "AliHLTTPCCARow.h"
22 #include "AliHLTTPCCAMath.h"
23 #include "AliHLTArray.h"
24 #include "AliHLTTPCCAGPUConfig.h"
25
26 typedef int int_v;
27 typedef unsigned int uint_v;
28 typedef short short_v;
29 typedef unsigned short ushort_v;
30 typedef float float_v;
31
32 class AliHLTTPCCAClusterData;
33 #if !defined(__OPENCL__) || defined(HLTCA_HOSTCODE)
34 template<typename T, int Dim> class AliHLTArray;
35 #endif
36 class AliHLTTPCCAHit;
37 MEM_CLASS_PRE() class AliHLTTPCCAParam;
38
39 /**
40  * Data abstraction class for the Slice Tracker.
41  *
42  * Different architectures implement this for the most efficient loads and stores. All access to the
43  * data happens through inline functions so that access to the data has no extra costs.
44  */
45 MEM_CLASS_PRE() class AliHLTTPCCASliceData
46 {
47   public:
48     AliHLTTPCCASliceData()
49       : 
50       fIsGpuSliceData(0), fGPUSharedDataReq(0), fFirstRow( 0 ), fLastRow( HLTCA_ROW_COUNT - 1), fNumberOfHits( 0 ), fNumberOfHitsPlusAlign( 0 ), fMemorySize( 0 ), fGpuMemorySize( 0 ), fMemory( 0 ), fGPUTextureBase( 0 )
51       ,fRows( NULL ), fLinkUpData( 0 ), fLinkDownData( 0 ), fHitData( 0 ), fClusterDataIndex( 0 )
52       , fFirstHitInBin( 0 ), fHitWeights( 0 )
53     {
54     }
55
56 #ifndef HLTCA_GPUCODE
57     ~AliHLTTPCCASliceData();
58 #endif //!HLTCA_GPUCODE
59
60     MEM_CLASS_PRE2() void InitializeRows( const MEM_LG2(AliHLTTPCCAParam) &parameters );
61
62     /**
63      * (Re)Create the data that is tuned for optimal performance of the algorithm from the cluster
64      * data.
65      */
66
67     void SetGPUSliceDataMemory(void* const pSliceMemory, void* const pRowMemory);
68     size_t SetPointers(const AliHLTTPCCAClusterData *data, bool allocate = false);
69     void InitFromClusterData( const AliHLTTPCCAClusterData &data );
70
71     /**
72      * Clear the slice data (e.g. for an empty slice)
73      */
74     void Clear();
75
76     /**
77      * Return the number of hits in this slice.
78      */
79     GPUhd() int NumberOfHits() const { return fNumberOfHits; }
80     GPUhd() int NumberOfHitsPlusAlign() const { return fNumberOfHitsPlusAlign; }
81
82     /**
83      * Access to the hit links.
84      *
85      * The links values give the hit index in the row above/below. Or -1 if there is no link.
86      */
87     MEM_TEMPLATE() GPUd() short_v HitLinkUpData  ( const MEM_TYPE(AliHLTTPCCARow) &row, const short_v &hitIndex ) const;
88     MEM_TEMPLATE() GPUd() short_v HitLinkDownData( const MEM_TYPE(AliHLTTPCCARow) &row, const short_v &hitIndex ) const;
89
90     MEM_TEMPLATE() GPUhd() GPUglobalref() const ushort2 *HitData( const MEM_TYPE(AliHLTTPCCARow) &row ) const {return &fHitData[row.fHitNumberOffset];}
91     GPUhd() GPUglobalref() const ushort2* HitData() const { return(fHitData); }
92         MEM_TEMPLATE() GPUd() GPUglobalref() const short_v *HitLinkUpData  ( const MEM_TYPE(AliHLTTPCCARow) &row ) const {return &fLinkUpData[row.fHitNumberOffset];}
93         MEM_TEMPLATE() GPUd() GPUglobalref() const short_v *HitLinkDownData( const MEM_TYPE(AliHLTTPCCARow) &row ) const {return &fLinkDownData[row.fHitNumberOffset];}
94         MEM_TEMPLATE() GPUd() GPUglobalref() const ushort_v *FirstHitInBin( const MEM_TYPE( AliHLTTPCCARow) &row ) const {return &fFirstHitInBin[row.fFirstHitInBinOffset];}
95
96     MEM_TEMPLATE() GPUd() void SetHitLinkUpData  ( const MEM_TYPE(AliHLTTPCCARow) &row, const short_v &hitIndex,
97                              const short_v &value );
98     MEM_TEMPLATE() GPUd() void SetHitLinkDownData( const MEM_TYPE(AliHLTTPCCARow) &row, const short_v &hitIndex,
99                              const short_v &value );
100
101     /**
102      * Reset all links to -1.
103      */
104     void ClearLinks();
105
106     /**
107      * Return the y and z coordinate(s) of the given hit(s).
108      */
109     // TODO return float_v
110     MEM_TEMPLATE() GPUd() ushort_v HitDataY( const MEM_TYPE( AliHLTTPCCARow) &row, const uint_v &hitIndex ) const;
111     MEM_TEMPLATE() GPUd() ushort_v HitDataZ( const MEM_TYPE( AliHLTTPCCARow) &row, const uint_v &hitIndex ) const;
112     MEM_TEMPLATE() GPUd() ushort2 HitData( const MEM_TYPE( AliHLTTPCCARow) &row, const uint_v &hitIndex ) const;
113
114     /**
115      * For a given bin index, content tells how many hits there are in the preceding bins. This maps
116      * directly to the hit index in the given row.
117      *
118      * \param binIndexes in the range 0 to row.Grid.N + row.Grid.Ny + 3.
119      */
120     MEM_TEMPLATE() GPUd() ushort_v FirstHitInBin( const MEM_TYPE( AliHLTTPCCARow)&row, ushort_v binIndexes ) const;
121
122     /**
123      * If the given weight is higher than what is currently stored replace with the new weight.
124      */
125     MEM_TEMPLATE() GPUd() void MaximizeHitWeight( const MEM_TYPE( AliHLTTPCCARow)&row, uint_v hitIndex, int_v weight );
126         MEM_TEMPLATE() GPUd() void SetHitWeight( const MEM_TYPE( AliHLTTPCCARow)&row, uint_v hitIndex, int_v weight );
127
128     /**
129      * Return the maximal weight the given hit got from one tracklet
130      */
131     MEM_TEMPLATE() GPUd() int_v HitWeight( const MEM_TYPE( AliHLTTPCCARow)&row, uint_v hitIndex ) const;
132
133     /**
134      * Reset all hit weights to 0.
135      */
136     void ClearHitWeights();
137
138     /**
139      * Returns the index in the original AliHLTTPCCAClusterData object of the given hit
140      */
141     MEM_TEMPLATE() GPUhd() int_v ClusterDataIndex( const MEM_TYPE( AliHLTTPCCARow)&row, uint_v hitIndex ) const;
142
143     /**
144      * Return the row object for the given row index.
145      */
146         GPUhd() GPUglobalref() const MEM_GLOBAL(AliHLTTPCCARow)& Row( int rowIndex ) const {return fRows[rowIndex];}
147     GPUhd() GPUglobalref() MEM_GLOBAL(AliHLTTPCCARow)* Rows() const {return fRows;}
148
149     GPUhd() GPUglobalref() int* HitWeights() const {return(fHitWeights); }
150
151     GPUhd() void SetGPUTextureBase(char* const val) {fGPUTextureBase = val;}
152     GPUhd() char* GPUTextureBase() const { return(fGPUTextureBase); }
153     GPUhd() char* GPUTextureBaseConst() const { return(fGPUTextureBase); }
154
155 #if !defined(__OPENCL__) || defined(HLTCA_HOSTCODE)
156         GPUh() char* Memory() const {return(fMemory); }
157     GPUh() size_t MemorySize() const {return(fMemorySize); }
158     GPUh() size_t GpuMemorySize() const {return(fGpuMemorySize); }
159     GPUh() int GPUSharedDataReq() const { return fGPUSharedDataReq; }
160 #endif
161
162     void SetGpuSliceData() { fIsGpuSliceData = 1; }
163
164   private:
165     AliHLTTPCCASliceData( const AliHLTTPCCASliceData & )
166       : 
167       fIsGpuSliceData(0), fGPUSharedDataReq(0), fFirstRow(0), fLastRow(HLTCA_ROW_COUNT - 1), fNumberOfHits( 0 ), fNumberOfHitsPlusAlign( 0 ), fMemorySize( 0 ), fGpuMemorySize( 0 ), fMemory( 0 ), fGPUTextureBase( 0 )
168       ,fRows( NULL ), fLinkUpData( 0 ), fLinkDownData( 0 ), fHitData( 0 ), fClusterDataIndex( 0 )
169       , fFirstHitInBin( 0 ), fHitWeights( 0 )
170     {
171     }
172     AliHLTTPCCASliceData& operator=( const AliHLTTPCCASliceData & ) {
173       return *this;
174     }
175
176 #if !defined(__OPENCL__) || defined(HLTCA_HOSTCODE)
177     void CreateGrid( AliHLTTPCCARow *row, const float2* data, int ClusterDataHitNumberOffset );
178     void PackHitData( AliHLTTPCCARow *row, const AliHLTArray<AliHLTTPCCAHit, 1> &binSortedHits );
179 #endif
180
181     int fIsGpuSliceData;       //Slice Data for GPU Tracker?
182     int fGPUSharedDataReq;     //Size of shared memory required for GPU Reconstruction
183
184     int fFirstRow;             //First non-empty row
185     int fLastRow;              //Last non-empty row
186
187     int fNumberOfHits;         // the number of hits in this slice
188     int fNumberOfHitsPlusAlign;
189
190     int fMemorySize;           // size of the allocated memory in bytes
191     int fGpuMemorySize;        // size of Memory needed to be transfered to GPU
192     GPUglobalref() char *fMemory;             // pointer to the allocated memory where all the following arrays reside in
193     GPUglobalref() char *fGPUTextureBase;     // pointer to start of GPU texture
194
195     GPUglobalref() MEM_GLOBAL(AliHLTTPCCARow) *fRows;     // The row objects needed for most accessor functions
196
197     GPUglobalref() short *fLinkUpData;        // hit index in the row above which is linked to the given (global) hit index
198     GPUglobalref() short *fLinkDownData;      // hit index in the row below which is linked to the given (global) hit index
199
200     GPUglobalref() ushort2 *fHitData;         // packed y,z coordinate of the given (global) hit index
201
202     GPUglobalref() int *fClusterDataIndex;    // see ClusterDataIndex()
203
204     /*
205      * The size of the array is row.Grid.N + row.Grid.Ny + 3. The row.Grid.Ny + 3 is an optimization
206      * to remove the need for bounds checking. The last values are the same as the entry at [N - 1].
207      */
208     GPUglobalref() unsigned short *fFirstHitInBin;         // see FirstHitInBin
209
210     GPUglobalref() int *fHitWeights;          // the weight of the longest tracklet crossed the cluster
211
212 };
213
214 MEM_CLASS_PRE() MEM_TEMPLATE() GPUd() inline short_v MEM_LG(AliHLTTPCCASliceData)::HitLinkUpData  ( const MEM_TYPE( AliHLTTPCCARow)&row, const short_v &hitIndex ) const
215 {
216   return fLinkUpData[row.fHitNumberOffset + hitIndex];
217 }
218
219 MEM_CLASS_PRE() MEM_TEMPLATE() GPUd() inline short_v MEM_LG(AliHLTTPCCASliceData)::HitLinkDownData( const MEM_TYPE( AliHLTTPCCARow)&row, const short_v &hitIndex ) const
220 {
221   return fLinkDownData[row.fHitNumberOffset + hitIndex];
222 }
223
224 MEM_CLASS_PRE() MEM_TEMPLATE() GPUd() inline void MEM_LG(AliHLTTPCCASliceData)::SetHitLinkUpData  ( const MEM_TYPE( AliHLTTPCCARow)&row, const short_v &hitIndex, const short_v &value )
225 {
226   fLinkUpData[row.fHitNumberOffset + hitIndex] = value;
227 }
228
229 MEM_CLASS_PRE() MEM_TEMPLATE() GPUd() inline void MEM_LG(AliHLTTPCCASliceData)::SetHitLinkDownData( const MEM_TYPE( AliHLTTPCCARow)&row, const short_v &hitIndex, const short_v &value )
230 {
231   fLinkDownData[row.fHitNumberOffset + hitIndex] = value;
232 }
233
234 MEM_CLASS_PRE() MEM_TEMPLATE() GPUd() inline ushort_v MEM_LG(AliHLTTPCCASliceData)::HitDataY( const MEM_TYPE( AliHLTTPCCARow)&row, const uint_v &hitIndex ) const
235 {
236   return fHitData[row.fHitNumberOffset + hitIndex].x;
237 }
238
239 MEM_CLASS_PRE() MEM_TEMPLATE() GPUd() inline ushort_v MEM_LG(AliHLTTPCCASliceData)::HitDataZ( const MEM_TYPE( AliHLTTPCCARow)&row, const uint_v &hitIndex ) const
240 {
241   return fHitData[row.fHitNumberOffset + hitIndex].y;
242 }
243
244 MEM_CLASS_PRE() MEM_TEMPLATE() GPUd() inline ushort2 MEM_LG(AliHLTTPCCASliceData)::HitData( const MEM_TYPE( AliHLTTPCCARow)&row, const uint_v &hitIndex ) const
245 {
246   return fHitData[row.fHitNumberOffset + hitIndex];
247 }
248
249 MEM_CLASS_PRE() MEM_TEMPLATE() GPUd() inline ushort_v MEM_LG(AliHLTTPCCASliceData)::FirstHitInBin( const MEM_TYPE( AliHLTTPCCARow)&row, ushort_v binIndexes ) const
250 {
251   return fFirstHitInBin[row.fFirstHitInBinOffset + binIndexes];
252 }
253
254 MEM_CLASS_PRE() MEM_TEMPLATE() GPUhd() inline int_v MEM_LG(AliHLTTPCCASliceData)::ClusterDataIndex( const MEM_TYPE( AliHLTTPCCARow)&row, uint_v hitIndex ) const
255 {
256   return fClusterDataIndex[row.fHitNumberOffset + hitIndex];
257 }
258
259 MEM_CLASS_PRE() MEM_TEMPLATE() GPUd() inline void MEM_LG(AliHLTTPCCASliceData)::MaximizeHitWeight( const MEM_TYPE( AliHLTTPCCARow)&row, uint_v hitIndex, int_v weight )
260 {
261   CAMath::AtomicMax( &fHitWeights[row.fHitNumberOffset + hitIndex], weight );
262 }
263
264 MEM_CLASS_PRE() MEM_TEMPLATE() GPUd() inline void MEM_LG(AliHLTTPCCASliceData)::SetHitWeight( const MEM_TYPE( AliHLTTPCCARow)&row, uint_v hitIndex, int_v weight )
265 {
266   fHitWeights[row.fHitNumberOffset + hitIndex] = weight;
267 }
268
269 MEM_CLASS_PRE() MEM_TEMPLATE() GPUd() inline int_v MEM_LG(AliHLTTPCCASliceData)::HitWeight( const MEM_TYPE( AliHLTTPCCARow)&row, uint_v hitIndex ) const
270 {
271   return fHitWeights[row.fHitNumberOffset + hitIndex];
272 }
273
274 //typedef AliHLTTPCCASliceData SliceData;
275
276 #endif // ALIHLTTPCCASLICEDATA_H