-
Notifications
You must be signed in to change notification settings - Fork 499
Expand file tree
/
Copy pathSpline1DSpec.h
More file actions
517 lines (425 loc) · 17.2 KB
/
Spline1DSpec.h
File metadata and controls
517 lines (425 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
// Copyright CERN and copyright holders of ALICE O2. This software is
// distributed under the terms of the GNU General Public License v3 (GPL
// Version 3), copied verbatim in the file "COPYING".
//
// See http://alice-o2.web.cern.ch/license for full licensing information.
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// \file Spline1DSpec.h
/// \brief Definition of Spline1DSpec class
///
/// \author Sergey Gorbunov <sergey.gorbunov@cern.ch>
#ifndef ALICEO2_GPUCOMMON_TPCFASTTRANSFORMATION_SPLINE1DSPEC_H
#define ALICEO2_GPUCOMMON_TPCFASTTRANSFORMATION_SPLINE1DSPEC_H
#include "GPUCommonDef.h"
#include "FlatObject.h"
#include "SplineUtil.h"
#if !defined(GPUCA_GPUCODE)
#include <functional>
#endif
class TFile;
namespace GPUCA_NAMESPACE
{
namespace gpu
{
/// ==================================================================================================
/// The class Spline1DContainer is a base class of Spline1D.
/// It contains all the class members and those methods which only depends on the DataT data type.
/// It also contains all non-inlined methods with the implementation in Spline1DSpec.cxx file.
///
/// DataT is a data type, which is supposed to be either double or float.
/// For other possible data types one has to add the corresponding instantiation line
/// at the end of the Spline1DSpec.cxx file
///
template <typename DataT>
class Spline1DContainer : public FlatObject
{
public:
/// Named enumeration for the safety level used by some methods
enum SafetyLevel { kNotSafe,
kSafe };
/// The struct Knot represents the i-th knot and the segment [knot_i, knot_i+1]
///
struct Knot {
DataT u; ///< u coordinate of the knot i (an integer number in float format)
DataT Li; ///< inverse length of the [knot_i, knot_{i+1}] segment ( == 1./ a (small) integer )
/// Get u as an integer
GPUd() int getU() const { return (int)(u + 0.1); }
};
/// _____________ Version control __________________________
/// Version control
GPUd() static constexpr int getVersion() { return 1; }
/// _____________ C++ constructors / destructors __________________________
/// Default constructor, required by the Root IO
Spline1DContainer() CON_DEFAULT;
/// Disable all other constructors
Spline1DContainer(const Spline1DContainer&) CON_DELETE;
/// Destructor
~Spline1DContainer() CON_DEFAULT;
/// _______________ Construction interface ________________________
#if !defined(GPUCA_GPUCODE) && !defined(GPUCA_STANDALONE)
/// approximate a function F with this spline
void approximateFunction(double xMin, double xMax,
std::function<void(double x, double f[/*mYdim*/])> F,
int nAuxiliaryDataPoints = 4);
#endif
/// _______________ IO ________________________
#if !defined(GPUCA_GPUCODE) && !defined(GPUCA_STANDALONE)
/// write a class object to the file
int writeToFile(TFile& outf, const char* name);
/// read a class object from the file
static Spline1DContainer* readFromFile(TFile& inpf, const char* name);
#endif
/// _______________ Getters ________________________
/// Get U coordinate of the last knot
GPUd() int getUmax() const { return mUmax; }
/// Get number of Y dimensions
GPUd() int getYdimensions() const { return mYdim; }
/// Get minimal required alignment for the spline parameters
GPUd() size_t getParameterAlignmentBytes() const
{
size_t s = 2 * sizeof(DataT) * mYdim;
return (s < 16) ? s : 16;
}
/// Number of parameters
GPUd() int getNumberOfParameters() const { return calcNumberOfParameters(mYdim); }
/// Size of the parameter array in bytes
GPUd() size_t getSizeOfParameters() const { return sizeof(DataT) * getNumberOfParameters(); }
/// Get a number of knots
GPUd() int getNumberOfKnots() const { return mNumberOfKnots; }
/// Get the array of knots
GPUd() const Knot* getKnots() const { return reinterpret_cast<const Knot*>(mFlatBufferPtr); }
/// Get i-th knot
template <SafetyLevel SafeT = SafetyLevel::kSafe>
GPUd() const Knot& getKnot(int i) const
{
if (SafeT == SafetyLevel::kSafe) {
i = (i < 0) ? 0 : (i >= mNumberOfKnots ? mNumberOfKnots - 1 : i);
}
return getKnots()[i];
}
/// Get index of an associated knot for a given U coordinate. Performs a boundary check.
template <SafetyLevel SafeT = SafetyLevel::kSafe>
GPUd() int getLeftKnotIndexForU(DataT u) const;
/// Get spline parameters
GPUd() DataT* getParameters() { return mParameters; }
/// Get spline parameters const
GPUd() const DataT* getParameters() const { return mParameters; }
/// _______________ Technical stuff ________________________
/// Get a map (integer U -> corresponding knot index)
GPUd() const int* getUtoKnotMap() const { return mUtoKnotMap; }
/// Convert X coordinate to U
GPUd() DataT convXtoU(DataT x) const { return (x - mXmin) * mXtoUscale; }
/// Convert U coordinate to X
GPUd() DataT convUtoX(DataT u) const { return mXmin + u / mXtoUscale; }
/// Get Xmin
GPUd() DataT getXmin() const { return mXmin; }
/// Get XtoUscale
GPUd() DataT getXtoUscale() const { return mXtoUscale; }
/// Set X range
GPUd() void setXrange(DataT xMin, DataT xMax);
/// Print method
void print() const;
/// _______________ Expert tools _______________
/// Number of parameters for given Y dimensions
GPUd() int calcNumberOfParameters(int nYdim) const { return (2 * nYdim) * getNumberOfKnots(); }
///_______________ Test tools _______________
#if !defined(GPUCA_GPUCODE) && !defined(GPUCA_STANDALONE) && !defined(GPUCA_ALIROOT_LIB) // code invisible on GPU and in the standalone compilation
/// Test the class functionality
static int test(const bool draw = 0, const bool drawDataPoints = 1);
#endif
/// _____________ FlatObject functionality, see FlatObject class for description ____________
using FlatObject::getBufferAlignmentBytes;
using FlatObject::getClassAlignmentBytes;
#if !defined(GPUCA_GPUCODE)
void cloneFromObject(const Spline1DContainer& obj, char* newFlatBufferPtr);
void moveBufferTo(char* newBufferPtr);
#endif
using FlatObject::releaseInternalBuffer;
void destroy();
void setActualBufferAddress(char* actualFlatBufferPtr);
void setFutureBufferAddress(char* futureFlatBufferPtr);
protected:
/// Non-const accessor to the knots array
Knot* getKnots() { return reinterpret_cast<Knot*>(mFlatBufferPtr); }
/// Non-const accessor to U->knots map
int* getUtoKnotMap() { return mUtoKnotMap; }
#if !defined(GPUCA_GPUCODE)
/// Constructor for a regular spline
void recreate(int nYdim, int numberOfKnots);
/// Constructor for an irregular spline
void recreate(int nYdim, int numberOfKnots, const int knotU[]);
#endif
/// _____________ Data members ____________
int mYdim = 0; ///< dimentionality of F
int mNumberOfKnots = 0; ///< n knots on the grid
int mUmax = 0; ///< U of the last knot
DataT mXmin = 0; ///< X of the first knot
DataT mXtoUscale = 0; ///< a scaling factor to convert X to U
int* mUtoKnotMap = nullptr; //! (transient!!) pointer to (integer U -> knot index) map inside the mFlatBufferPtr array
DataT* mParameters = nullptr; //! (transient!!) pointer to F-dependent parameters inside the mFlatBufferPtr array
#ifndef GPUCA_ALIROOT_LIB
ClassDefNV(Spline1DContainer, 1);
#endif
};
template <typename DataT>
template <typename Spline1DContainer<DataT>::SafetyLevel SafeT>
GPUdi() int Spline1DContainer<DataT>::getLeftKnotIndexForU(DataT u) const
{
/// Get i: u is in [knot_i, knot_{i+1}) segment
/// when u is otside of [0, mUmax], return a corresponding edge segment
int iu = (int)u;
if (SafeT == SafetyLevel::kSafe) {
iu = (iu < 0) ? 0 : (iu > mUmax ? mUmax : iu);
}
return getUtoKnotMap()[iu];
}
template <typename DataT>
GPUdi() void Spline1DContainer<DataT>::setXrange(DataT xMin, DataT xMax)
{
mXmin = xMin;
double l = ((double)xMax) - xMin;
if (l < 1.e-8) {
l = 1.e-8;
}
mXtoUscale = mUmax / l;
}
/// ==================================================================================================
///
/// Spline1DSpec class declares different specializations of the Spline1D class.
///
/// The specializations depend on the value of Spline1D's template parameter YdimT.
/// specializations have different constructors and slightly different declarations of methods.
///
/// The meaning of the template parameters:
///
/// \param DataT data type: float or double
/// \param YdimT
/// YdimT > 0 : the number of Y dimensions is known at the compile time and is equal to YdimT
/// YdimT = 0 : the number of Y dimensions will be set in the runtime
/// YdimT < 0 : the number of Y dimensions will be set in the runtime, and it will not exceed abs(XdimT)
/// \param SpecT specialisation number:
/// 0 - a parent class for all other specializations
/// 1 - nYdim>0: nYdim is set at the compile time
/// 2 - nYdim<0: nYdim must be set during runtime
/// 3 - specialization where nYdim==1 (a small add-on on top of the other specs)
///
template <typename DataT, int YdimT, int SpecT>
class Spline1DSpec;
/// ==================================================================================================
/// Specialization 0 declares common methods for all other Spline2D specializations.
/// Implementations of the methods may depend on the YdimT value.
///
template <typename DataT, int YdimT>
class Spline1DSpec<DataT, YdimT, 0> : public Spline1DContainer<DataT>
{
typedef Spline1DContainer<DataT> TBase;
public:
typedef typename TBase::SafetyLevel SafetyLevel;
typedef typename TBase::Knot Knot;
/// _______________ Interpolation math ________________________
/// Get interpolated value S(x)
GPUd() void interpolate(DataT x, GPUgeneric() DataT S[/*mYdim*/]) const
{
interpolateU<SafetyLevel::kSafe>(mYdim, mParameters, convXtoU(x), S);
}
/// Get interpolated value for an nYdim-dimensional S(u) using spline parameters Parameters.
template <SafetyLevel SafeT = SafetyLevel::kSafe>
GPUd() void interpolateU(int inpYdim, GPUgeneric() const DataT Parameters[],
DataT u, GPUgeneric() DataT S[/*nYdim*/]) const
{
const auto nYdimTmp = SplineUtil::getNdim<YdimT>(inpYdim);
const auto nYdim = nYdimTmp.get();
int iknot = TBase::template getLeftKnotIndexForU<SafeT>(u);
const DataT* d = Parameters + (2 * nYdim) * iknot;
interpolateU(nYdim, getKnots()[iknot], &(d[0]), &(d[nYdim]), &(d[2 * nYdim]), &(d[3 * nYdim]), u, S);
}
/// The main mathematical utility.
/// Get interpolated value {S(u): 1D -> nYdim} at the segment [knotL, next knotR]
/// using the spline values Sl, Sr and the slopes Dl, Dr
template <typename T>
GPUd() void interpolateU(int inpYdim, const Knot& knotL,
GPUgeneric() const T Sl[/*mYdim*/], GPUgeneric() const T Dl[/*mYdim*/],
GPUgeneric() const T Sr[/*mYdim*/], GPUgeneric() const T Dr[/*mYdim*/],
DataT u, GPUgeneric() T S[/*mYdim*/]) const
{
const auto nYdimTmp = SplineUtil::getNdim<YdimT>(inpYdim);
const auto nYdim = nYdimTmp.get();
T uu = T(u - knotL.u);
T li = T(knotL.Li);
T v = uu * li; // scaled u
for (int dim = 0; dim < nYdim; ++dim) {
T df = (Sr[dim] - Sl[dim]) * li;
T a = Dl[dim] + Dr[dim] - df - df;
T b = df - Dl[dim] - a;
S[dim] = ((a * v + b) * v + Dl[dim]) * uu + Sl[dim];
}
/*
another way to calculate f(u):
T uu = T(u - knotL.u);
T v = uu * T(knotL.Li); // scaled u
T vm1 = v-1;
T v2 = v * v;
float cSr = v2*(3-2*v);
float cSl = 1-cSr;
float cDl = v*vm1*vm1*knotL.L;
float cDr = v2*vm1*knotL.L;
return cSl*Sl + cSr*Sr + cDl*Dl + cDr*Dr;
*/
}
using TBase::convXtoU;
using TBase::getKnot;
using TBase::getKnots;
using TBase::getNumberOfKnots;
protected:
using TBase::mParameters;
using TBase::mYdim;
using TBase::TBase; // inherit constructors and hide them
#ifndef GPUCA_ALIROOT_LIB
ClassDefNV(Spline1DSpec, 0);
#endif
};
/// ==================================================================================================
/// Specialization 1: YdimT>0 where the number of Y dimensions is taken from template parameters
/// at the compile time
///
template <typename DataT, int YdimT>
class Spline1DSpec<DataT, YdimT, 1>
: public Spline1DSpec<DataT, YdimT, 0>
{
typedef Spline1DContainer<DataT> TVeryBase;
typedef Spline1DSpec<DataT, YdimT, 0> TBase;
public:
typedef typename TVeryBase::SafetyLevel SafetyLevel;
#if !defined(GPUCA_GPUCODE)
/// Default constructor
Spline1DSpec() : Spline1DSpec(2) {}
/// Constructor for a regular spline
Spline1DSpec(int numberOfKnots) : TBase()
{
recreate(numberOfKnots);
}
/// Constructor for an irregular spline
Spline1DSpec(int numberOfKnots, const int knotU[])
: TBase()
{
recreate(numberOfKnots, knotU);
}
/// Copy constructor
Spline1DSpec(const Spline1DSpec& v) : TBase()
{
TBase::cloneFromObject(v, nullptr);
}
/// Constructor for a regular spline
void recreate(int numberOfKnots) { TBase::recreate(YdimT, numberOfKnots); }
/// Constructor for an irregular spline
void recreate(int numberOfKnots, const int knotU[])
{
TBase::recreate(YdimT, numberOfKnots, knotU);
}
#endif
/// Get number of Y dimensions
GPUd() constexpr int getYdimensions() const { return YdimT; }
/// Get minimal required alignment for the spline parameters
GPUd() constexpr size_t getParameterAlignmentBytes() const
{
size_t s = 2 * sizeof(DataT) * YdimT;
return (s < 16) ? s : 16;
}
/// Number of parameters
GPUd() int getNumberOfParameters() const { return (2 * YdimT) * getNumberOfKnots(); }
/// Size of the parameter array in bytes
GPUd() size_t getSizeOfParameters() const { return (sizeof(DataT) * 2 * YdimT) * getNumberOfKnots(); }
/// _______ Expert tools: interpolation with given nYdim and external Parameters _______
/// Get interpolated value for an YdimT-dimensional S(u) using spline parameters Parameters.
template <SafetyLevel SafeT = SafetyLevel::kSafe>
GPUd() void interpolateU(GPUgeneric() const DataT Parameters[],
DataT u, GPUgeneric() DataT S[/*nYdim*/]) const
{
TBase::template interpolateU<SafeT>(YdimT, Parameters, u, S);
}
/// Get interpolated value for an YdimT-dimensional S(u) at the segment [knotL, next knotR]
/// using the spline values Sl, Sr and the slopes Dl, Dr
template <typename T>
GPUd() void interpolateU(const typename TBase::Knot& knotL,
GPUgeneric() const T Sl[/*mYdim*/], GPUgeneric() const T Dl[/*mYdim*/],
GPUgeneric() const T Sr[/*mYdim*/], GPUgeneric() const T Dr[/*mYdim*/],
DataT u, GPUgeneric() T S[/*mYdim*/]) const
{
TBase::template interpolateU(YdimT, knotL, Sl, Dl, Sr, Dr, u, S);
}
using TBase::getNumberOfKnots;
/// _______________ Suppress some parent class methods ________________________
private:
#if !defined(GPUCA_GPUCODE)
using TBase::recreate;
#endif
using TBase::interpolateU;
};
/// ==================================================================================================
/// Specialization 2 (YdimT<=0) where the numbaer of Y dimensions
/// must be set in the runtime via a constructor parameter
///
template <typename DataT, int YdimT>
class Spline1DSpec<DataT, YdimT, 2>
: public Spline1DSpec<DataT, YdimT, 0>
{
typedef Spline1DContainer<DataT> TVeryBase;
typedef Spline1DSpec<DataT, YdimT, 0> TBase;
public:
typedef typename TVeryBase::SafetyLevel SafetyLevel;
#if !defined(GPUCA_GPUCODE)
/// Default constructor
Spline1DSpec() : Spline1DSpec(0, 2) {}
/// Constructor for a regular spline
Spline1DSpec(int nYdim, int numberOfKnots) : TBase()
{
TBase::recreate(nYdim, numberOfKnots);
}
/// Constructor for an irregular spline
Spline1DSpec(int nYdim, int numberOfKnots, const int knotU[]) : TBase()
{
TBase::recreate(nYdim, numberOfKnots, knotU);
}
/// Copy constructor
Spline1DSpec(const Spline1DSpec& v) : TBase()
{
TVeryBase::cloneFromObject(v, nullptr);
}
/// Constructor for a regular spline
void recreate(int nYdim, int numberOfKnots) { TBase::recreate(nYdim, numberOfKnots); }
/// Constructor for an irregular spline
void recreate(int nYdim, int numberOfKnots, const int knotU[])
{
TBase::recreate(nYdim, numberOfKnots, knotU);
}
#endif
/// _______ Expert tools: interpolation with given nYdim and external Parameters _______
using TBase::interpolateU;
#ifndef GPUCA_ALIROOT_LIB
ClassDefNV(Spline1DSpec, 0);
#endif
};
/// ==================================================================================================
/// Specialization 3, where the number of Y dimensions is 1.
///
template <typename DataT>
class Spline1DSpec<DataT, 1, 3>
: public Spline1DSpec<DataT, 1, SplineUtil::getSpec(999)>
{
typedef Spline1DSpec<DataT, 1, SplineUtil::getSpec(999)> TBase;
public:
using TBase::TBase; // inherit constructors
/// Simplified interface for 1D: return the interpolated value
GPUd() DataT interpolate(DataT x) const
{
DataT S = 0.;
TBase::interpolate(x, &S);
return S;
}
};
} // namespace gpu
} // namespace GPUCA_NAMESPACE
#endif