Project
Loading...
Searching...
No Matches
MultivariatePolynomial.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
14
15#ifndef ALICEO2_TPC_MULTIVARIATEPOLYNOMIAL
16#define ALICEO2_TPC_MULTIVARIATEPOLYNOMIAL
17
18#include "GPUCommonDef.h"
19#include "GPUCommonLogger.h"
20#include "FlatObject.h"
22
23#if !defined(GPUCA_GPUCODE)
24#include <algorithm>
25#include <type_traits>
26#if !defined(GPUCA_STANDALONE)
27#include <TFile.h>
28#endif
29#endif
30
31namespace o2::gpu
32{
33
43template <uint32_t Dim, uint32_t Degree, bool InteractionOnly = false>
44class MultivariatePolynomial : public FlatObject, public MultivariatePolynomialHelper<Dim, Degree, InteractionOnly>
45{
46 public:
47#if !defined(GPUCA_GPUCODE)
51 template <bool IsEnabled = true, typename std::enable_if<(IsEnabled && (Dim == 0 && Degree == 0)), int32_t>::type = 0>
52 MultivariatePolynomial(const uint32_t nDim, const uint32_t degree, const bool interactionOnly = false) : MultivariatePolynomialHelper<Dim, Degree, false>{nDim, degree, interactionOnly}, mNParams{this->getNParameters(degree, nDim, interactionOnly)}
53 {
54 construct();
55 }
56
58 template <bool IsEnabled = true, typename std::enable_if<(IsEnabled && (Dim != 0 && Degree != 0)), int32_t>::type = 0>
59 MultivariatePolynomial() : mNParams{this->getNParameters(Degree, Dim, InteractionOnly)}
60 {
61 construct();
62 }
63#else
65 MultivariatePolynomial() = default;
66#endif
67
70
73
75#if !defined(GPUCA_GPUCODE)
77 void cloneFromObject(const MultivariatePolynomial& obj, char* newFlatBufferPtr);
78
81 void moveBufferTo(char* newBufferPtr);
82#endif
83
85 void destroy();
86
88 void setActualBufferAddress(char* actualFlatBufferPtr);
89
91 void setFutureBufferAddress(char* futureFlatBufferPtr);
93
96 GPUd() float eval(const float x[/*Dim*/]) const { return this->evalPol(mParams, x); }
97
98#if !defined(GPUCA_GPUCODE)
100 uint32_t getNParams() const { return mNParams; }
101
104 void setParams(const float params[/*mNParams*/]) { std::copy(params, params + mNParams, mParams); }
105
108 void setParam(const uint32_t param, const float val) { mParams[param] = val; };
109
111 const float* getParams() const { return mParams; }
112
113#ifndef GPUCA_STANDALONE
117 void loadFromFile(TFile& inpf, const char* name);
118
122 void writeToFile(TFile& outf, const char* name) const;
123
129 template <bool IsEnabled = true, typename std::enable_if<(IsEnabled && (Dim == 0 && Degree == 0)), int32_t>::type = 0>
130 void fit(std::vector<double>& x, std::vector<double>& y, std::vector<double>& error, const bool clearPoints)
131 {
133 if (vec.empty()) {
134 return;
135 }
136 setParams(vec.data());
137 }
138#endif
139
141 MultivariatePolynomialContainer getContainer() const { return MultivariatePolynomialContainer{this->getDim(), this->getDegree(), mNParams, mParams, this->isInteractionOnly()}; }
142
146#endif
147
148 private:
149 using DataTParams = float;
150 uint32_t mNParams{};
151 DataTParams* mParams{nullptr};
152
153#if !defined(GPUCA_GPUCODE)
155 std::size_t sizeOfParameters() const { return mNParams * sizeof(DataTParams); }
156
157 // construct the object (flatbuffer)
158 void construct();
159#endif
160
161 ClassDefNV(MultivariatePolynomial, 1);
162};
163
164//=================================================================================
165//============================ inline implementations =============================
166//=================================================================================
167
168#if !defined(GPUCA_GPUCODE) && !defined(GPUCA_STANDALONE)
169template <uint32_t Dim, uint32_t Degree, bool InteractionOnly>
171{
172 MultivariatePolynomialContainer* polTmp = nullptr;
173 inpf.GetObject(name, polTmp);
174 if (polTmp) {
175 setFromContainer(*polTmp);
176 delete polTmp;
177 } else {
178 LOGP(info, "couldnt load object {} from input file", name);
179 }
180}
181
182template <uint32_t Dim, uint32_t Degree, bool InteractionOnly>
184{
185 if constexpr (Dim > 0 && Degree > 0) {
186 if (this->getDim() != container.mDim) {
187 LOGP(info, "wrong number of dimensions! this {} container {}", this->getDim(), container.mDim);
188 return;
189 }
190 if (this->getDegree() != container.mDegree) {
191 LOGP(info, "wrong number of degrees! this {} container {}", this->getDegree(), container.mDegree);
192 return;
193 }
194 if (this->isInteractionOnly() != container.mInteractionOnly) {
195 LOGP(info, "InteractionOnly is set for this object to {}, but stored as {} in the container", this->isInteractionOnly(), container.mInteractionOnly);
196 return;
197 }
198 setParams(container.mParams.data());
199 } else {
200 MultivariatePolynomial polTmp(container.mDim, container.mDegree);
201 polTmp.setParams(container.mParams.data());
202 this->cloneFromObject(polTmp, nullptr);
203 }
204}
205
206template <uint32_t Dim, uint32_t Degree, bool InteractionOnly>
208{
209 const MultivariatePolynomialContainer cont = getContainer();
210 outf.WriteObject(&cont, name);
211}
212#endif
213
214#ifndef GPUCA_GPUCODE
215template <uint32_t Dim, uint32_t Degree, bool InteractionOnly>
217{
218 const char* oldFlatBufferPtr = obj.mFlatBufferPtr;
219 FlatObject::cloneFromObject(obj, newFlatBufferPtr);
220 mNParams = obj.mNParams;
221 if constexpr (Dim == 0 && Degree == 0) {
222 this->mDim = obj.mDim;
223 this->mDegree = obj.mDegree;
224 this->mInteractionOnly = obj.mInteractionOnly;
225 }
226 if (obj.mParams) {
227 mParams = FlatObject::relocatePointer(oldFlatBufferPtr, mFlatBufferPtr, obj.mParams);
228 }
229}
230
231template <uint32_t Dim, uint32_t Degree, bool InteractionOnly>
233{
234 char* oldFlatBufferPtr = mFlatBufferPtr;
235 FlatObject::moveBufferTo(newFlatBufferPtr);
236 char* currFlatBufferPtr = mFlatBufferPtr;
237 mFlatBufferPtr = oldFlatBufferPtr;
238 setActualBufferAddress(currFlatBufferPtr);
239}
240
241template <uint32_t Dim, uint32_t Degree, bool InteractionOnly>
243{
245 const std::size_t flatbufferSize = sizeOfParameters();
246 FlatObject::finishConstruction(flatbufferSize);
247 mParams = reinterpret_cast<float*>(mFlatBufferPtr);
248}
249#endif
250
251template <uint32_t Dim, uint32_t Degree, bool InteractionOnly>
257
258template <uint32_t Dim, uint32_t Degree, bool InteractionOnly>
260{
261 FlatObject::setActualBufferAddress(actualFlatBufferPtr);
262 mParams = reinterpret_cast<float*>(mFlatBufferPtr);
263}
264
265template <uint32_t Dim, uint32_t Degree, bool InteractionOnly>
267{
268 mParams = FlatObject::relocatePointer(mFlatBufferPtr, futureFlatBufferPtr, mParams);
269 FlatObject::setFutureBufferAddress(futureFlatBufferPtr);
270}
271
272} // namespace o2::gpu
273
274#endif
Definition of FlatObject class.
GPUCA_GPUCODE.
Definition FlatObject.h:176
void setFutureBufferAddress(char *futureFlatBufferPtr)
Definition FlatObject.h:557
void destroy()
_______________ Utilities _______________________________________________
Definition FlatObject.h:349
static T * relocatePointer(const char *oldBase, char *newBase, const T *ptr)
Relocates a pointer inside a buffer to the new buffer address.
Definition FlatObject.h:285
void setActualBufferAddress(char *actualFlatBufferPtr)
_____________ Methods for moving the class with its external buffer to another location _____________...
Definition FlatObject.h:547
void startConstruction()
_____________ Construction _________
Definition FlatObject.h:342
void moveBufferTo(char *newBufferPtr)
Definition FlatObject.h:396
void finishConstruction(int32_t flatBufferSize)
Definition FlatObject.h:358
void cloneFromObject(const FlatObject &obj, char *newFlatBufferPtr)
Definition FlatObject.h:373
~MultivariatePolynomial()=default
default destructor
void setFromContainer(const MultivariatePolynomialContainer &container)
MultivariatePolynomialContainer getContainer() const
converts the parameters to a container which can be written to a root file
MultivariatePolynomial(const uint32_t nDim, const uint32_t degree, const bool interactionOnly=false)
void loadFromFile(TFile &inpf, const char *name)
void writeToFile(TFile &outf, const char *name) const
MultivariatePolynomial()
constructor for compile time evaluation of polynomial formula
GPUd() float eval(const float x[]) const
================================================================================================
void setParams(const float params[])
void setActualBufferAddress(char *actualFlatBufferPtr)
set location of external flat buffer
void setFutureBufferAddress(char *futureFlatBufferPtr)
set future location of the flat buffer
void destroy()
destroy the object (release internal flat buffer)
void cloneFromObject(const MultivariatePolynomial &obj, char *newFlatBufferPtr)
========== FlatObject functionality, see FlatObject class for description =================
MultivariatePolynomial(const MultivariatePolynomial &obj)
Copy constructor.
void setParam(const uint32_t param, const float val)
void fit(std::vector< double > &x, std::vector< double > &y, std::vector< double > &error, const bool clearPoints)
GLint GLenum GLint x
Definition glcorearb.h:403
GLuint const GLchar * name
Definition glcorearb.h:781
GLenum const GLfloat * params
Definition glcorearb.h:272
GLuint GLfloat * val
Definition glcorearb.h:1582
GLenum GLfloat param
Definition glcorearb.h:271
simple struct to enable writing the MultivariatePolynomial to file
const uint32_t mDim
number of dimensions of the polynomial
const uint32_t mDegree
degree of the polynomials
const bool mInteractionOnly
consider only interaction terms
const std::vector< float > mParams
parameters of the polynomial
std::vector< o2::ctf::BufferType > vec