forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSpline1DSpec.h
More file actions
583 lines (479 loc) · 19.4 KB
/
Spline1DSpec.h
File metadata and controls
583 lines (479 loc) · 19.4 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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// 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 o2
{
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() int32_t getU() const { return (int32_t)(u + 0.1f); }
};
/// _____________ Version control __________________________
/// Version control
GPUd() static constexpr int32_t getVersion() { return 1; }
/// _____________ C++ constructors / destructors __________________________
/// Default constructor, required by the Root IO
Spline1DContainer() = default;
/// Disable all other constructors
Spline1DContainer(const Spline1DContainer&) = delete;
/// Destructor
~Spline1DContainer() = 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,
int32_t nAuxiliaryDataPoints = 4);
#endif
/// _______________ IO ________________________
#if !defined(GPUCA_GPUCODE) && !defined(GPUCA_STANDALONE)
/// write a class object to the file
int32_t 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() int32_t getUmax() const { return mUmax; }
/// Get number of Y dimensions
GPUd() int32_t 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() int32_t 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() int32_t 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(int32_t 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() int32_t 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 int32_t* 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 Xmax
GPUd() DataT getXmax() const { return mXmin + mUmax / mXtoUscale; }
/// 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() int32_t calcNumberOfParameters(int32_t nYdim) const { return (2 * nYdim) * getNumberOfKnots(); }
///_______________ Test tools _______________
#if !defined(GPUCA_GPUCODE) && !defined(GPUCA_STANDALONE) // code invisible on GPU and in the standalone compilation
/// Test the class functionality
static int32_t 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
int32_t* getUtoKnotMap() { return mUtoKnotMap; }
#if !defined(GPUCA_GPUCODE)
/// Constructor for a regular spline
void recreate(int32_t nYdim, int32_t numberOfKnots);
/// Constructor for an irregular spline
void recreate(int32_t nYdim, int32_t numberOfKnots, const int32_t knotU[]);
#endif
/// _____________ Data members ____________
int32_t mYdim = 0; ///< dimentionality of F
int32_t mNumberOfKnots = 0; ///< n knots on the grid
int32_t 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
int32_t* 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
ClassDefNV(Spline1DContainer, 1);
};
template <typename DataT>
template <typename Spline1DContainer<DataT>::SafetyLevel SafeT>
GPUdi() int32_t 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
int32_t iu = u < 0 ? 0 : (u > (float)mUmax ? mUmax : (int32_t)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, int32_t YdimT, int32_t 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, int32_t 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
{
interpolateAtU<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 interpolateAtU(int32_t inpYdim, GPUgeneric() const DataT Parameters[],
DataT u, GPUgeneric() DataT S[/*nYdim*/]) const
{
const auto nYdimTmp = SplineUtil::getNdim<YdimT>(inpYdim);
const auto nYdim = nYdimTmp.get();
int32_t iknot = TBase::template getLeftKnotIndexForU<SafeT>(u);
const DataT* d = Parameters + (2 * nYdim) * iknot;
interpolateAtU(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 interpolateAtU(int32_t 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();
auto val = getSderivativesOverParsAtU<T>(knotL, u);
const auto& dSdSl = val[0];
const auto& dSdDl = val[1];
const auto& dSdSr = val[2];
const auto& dSdDr = val[3];
for (int32_t dim = 0; dim < nYdim; ++dim) {
S[dim] = dSdSr * Sr[dim] + dSdSl * Sl[dim] + dSdDl * Dl[dim] + dSdDr * Dr[dim];
}
/*
another way to calculate f(u):
if (u < (DataT)0) {
u = (DataT)0;
}
if (u > (DataT)TBase::getUmax()) {
u = (DataT)TBase::getUmax();
}
T uu = T(u - knotL.u);
T li = T(knotL.Li);
T v = uu * li; // scaled u
for (int32_t 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];
}
*/
}
template <typename T>
GPUd() std::array<T, 4> getSderivativesOverParsAtU(const Knot& knotL, DataT u) const
{
/// Get derivatives of the interpolated value {S(u): 1D -> nYdim} at the segment [knotL, next knotR]
/// over the spline parameters Sl(eft), Sr(ight) and the slopes Dl, Dr
if (u < (DataT)0) {
u = (DataT)0;
}
if (u > (DataT)TBase::getUmax()) {
u = (DataT)TBase::getUmax();
}
u = u - knotL.u;
T v = u * T(knotL.Li); // scaled u
T vm1 = v - T(1.);
T a = u * vm1;
T v2 = v * v;
T dSdSr = v2 * (T(3.) - v - v);
T dSdSl = T(1.) - dSdSr;
T dSdDl = vm1 * a;
T dSdDr = v * a;
// S(u) = dSdSl * Sl + dSdSr * Sr + dSdDl * Dl + dSdDr * Dr;
return {dSdSl, dSdDl, dSdSr, dSdDr};
}
template <typename T>
GPUd() std::array<T, 8> getSDderivativesOverParsAtU(const Knot& knotL, DataT u) const
{
/// Get derivatives of the interpolated value {S(u): 1D -> nYdim} at the segment [knotL, next knotR]
/// over the spline values Sl, Sr and the slopes Dl, Dr
if (u < (DataT)0) {
u = (DataT)0;
}
if (u > (DataT)TBase::getUmax()) {
u = (DataT)TBase::getUmax();
}
u = u - knotL.u;
T v = u * T(knotL.Li); // scaled u
T vm1 = v - T(1.);
T a = u * vm1;
T v2 = v * v;
T dSdSr = v2 * (T(3.) - v - v);
T dSdSl = T(1.) - dSdSr;
T dSdDl = vm1 * a;
T dSdDr = v * a;
T dv = T(knotL.Li);
T dDdSr = 6. * v * (T(1.) - v) * dv;
T dDdSl = -dDdSr;
T dDdDl = vm1 * (v + v + vm1);
T dDdDr = v * (v + vm1 + vm1);
// S(u) = dSdSl * Sl + dSdSr * Sr + dSdDl * Dl + dSdDr * Dr;
// D(u) = dS(u)/du = dDdSl * Sl + dDdSr * Sr + dDdDl * Dl + dDdDr * Dr;
return {dSdSl, dSdDl, dSdSr, dSdDr, dDdSl, dDdDl, dDdSr, dDdDr};
}
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
ClassDefNV(Spline1DSpec, 0);
};
/// ==================================================================================================
/// Specialization 1: YdimT>0 where the number of Y dimensions is taken from template parameters
/// at the compile time
///
template <typename DataT, int32_t 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(int32_t numberOfKnots) : TBase()
{
recreate(numberOfKnots);
}
/// Constructor for an irregular spline
Spline1DSpec(int32_t numberOfKnots, const int32_t knotU[])
: TBase()
{
recreate(numberOfKnots, knotU);
}
/// Copy constructor
Spline1DSpec(const Spline1DSpec& v) : TBase()
{
TBase::cloneFromObject(v, nullptr);
}
/// Constructor for a regular spline
void recreate(int32_t numberOfKnots) { TBase::recreate(YdimT, numberOfKnots); }
/// Constructor for an irregular spline
void recreate(int32_t numberOfKnots, const int32_t knotU[])
{
TBase::recreate(YdimT, numberOfKnots, knotU);
}
#endif
/// Get number of Y dimensions
GPUd() constexpr int32_t 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() int32_t 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 interpolateAtU(GPUgeneric() const DataT Parameters[],
DataT u, GPUgeneric() DataT S[/*nYdim*/]) const
{
TBase::template interpolateAtU<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 interpolateAtU(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::interpolateAtU(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::interpolateAtU;
};
/// ==================================================================================================
/// Specialization 2 (YdimT<=0) where the numbaer of Y dimensions
/// must be set in the runtime via a constructor parameter
///
template <typename DataT, int32_t 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(int32_t nYdim, int32_t numberOfKnots) : TBase()
{
TBase::recreate(nYdim, numberOfKnots);
}
/// Constructor for an irregular spline
Spline1DSpec(int32_t nYdim, int32_t numberOfKnots, const int32_t 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(int32_t nYdim, int32_t numberOfKnots) { TBase::recreate(nYdim, numberOfKnots); }
/// Constructor for an irregular spline
void recreate(int32_t nYdim, int32_t numberOfKnots, const int32_t knotU[])
{
TBase::recreate(nYdim, numberOfKnots, knotU);
}
#endif
/// _______ Expert tools: interpolation with given nYdim and external Parameters _______
using TBase::interpolateAtU;
ClassDefNV(Spline1DSpec, 0);
};
/// ==================================================================================================
/// 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 o2
#endif