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