Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Improve vectorized implementation.
- Remove almost all dependencies on Vc (Alignement directive remaining)
  by using vecCore directives.
- Allow code to switch ROOT::Double_v to other VecCore backends in the
  future.
- Refactor redundant code.
  • Loading branch information
xvallspl committed Jun 14, 2017
commit aac3008dfa44af6245dc66186d28e838ee7a0f5c
30 changes: 27 additions & 3 deletions math/mathcore/inc/Fit/FitConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,34 @@ class FitConfig {
set the parameter settings from a model function.
Create always new parameter setting list from a given model function
*/
void CreateParamsSettings(const ROOT::Math::IParamMultiFunction & func);
#ifdef R__HAS_VECCORE
void CreateParamsSettings(const ROOT::Math::IParamMultiFunctionTempl<ROOT::Double_v> & func);
template<class T>
void CreateParamsSettings(const ROOT::Math::IParamMultiFunctionTempl<T> & func) {
// initialize from model function
// set the parameters values from the function
unsigned int npar = func.NPar();
const double * begin = func.Parameters();
if (begin == 0) {
fSettings = std::vector<ParameterSettings>(npar);
return;
}

fSettings.clear();
fSettings.reserve(npar);
const double * end = begin+npar;
unsigned int i = 0;
for (const double * ipar = begin; ipar != end; ++ipar) {
double val = *ipar;
double step = 0.3 * std::fabs(val); // step size is 30% of par value
//double step = 2.0*std::fabs(val); // step size is 30% of par value
if (val == 0) step = 0.3;

fSettings.push_back( ParameterSettings(func.ParameterName(i), val, step ) );
#ifdef DEBUG
std::cout << "FitConfig: add parameter " << func.ParameterName(i) << " val = " << val << std::endl;
#endif
i++;
}
}

/**
set the parameter settings from number of parameters and a vector of values and optionally step values. If there are not existing or number of parameters does not match existing one, create a new parameter setting list.
Expand Down
19 changes: 16 additions & 3 deletions math/mathcore/inc/Fit/FitUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ namespace FitUtil {
vecCore::Load<ROOT::Double_v>(xx, x);
const double *p0 = p;
auto res = (*f)( &xx, (const double *)p0);
return res[0];
return vecCore::Get<ROOT::Double_v>(res, 0);
}
#endif

Expand Down Expand Up @@ -274,8 +274,10 @@ namespace FitUtil {
return also nPoints as the effective number of used points in the LogL evaluation
*/
void EvaluateLogLGradient(const IModelFunction & func, const UnBinData & data, const double * x, double * grad, unsigned int & nPoints);

#ifdef R__HAS_VECCORE
void EvaluateLogLGradient(const IModelFunctionTempl<ROOT::Double_v> &, const UnBinData &, const double *, double *, unsigned int & ) ;
template<class NotCompileIfScalarBackend = std::enable_if<!(std::is_same<double, ROOT::Double_v>::value)>>
void EvaluateLogLGradient(const IModelFunctionTempl<ROOT::Double_v> &, const UnBinData &, const double *, double *, unsigned int & ) {}
#endif

/**
Expand Down Expand Up @@ -308,9 +310,20 @@ namespace FitUtil {
is used
*/
double EvaluatePdf(const IModelFunction & func, const UnBinData & data, const double * x, unsigned int ipoint, double * g = 0);

#ifdef R__HAS_VECCORE
double EvaluatePdf(const IModelFunctionTempl<ROOT::Double_v> & func, const UnBinData & data, const double * p, unsigned int i, double *);
template<class NotCompileIfScalarBackend = std::enable_if<!(std::is_same<double, ROOT::Double_v>::value)>>
double EvaluatePdf(const IModelFunctionTempl<ROOT::Double_v> & func, const UnBinData & data, const double * p, unsigned int i, double *){
// evaluate the pdf contribution to the generic logl function in case of bin data
// return actually the log of the pdf and its derivatives
//func.SetParameters(p);
const auto x = vecCore::FromPtr<ROOT::Double_v>(data.GetCoordComponent(i,0));
auto fval = func (&x, p);
auto logPdf = ROOT::Math::Util::EvalLog(fval);
return vecCore::Get<ROOT::Double_v>(logPdf, 0);
}
#endif

/**
evaluate the pdf contribution to the Poisson LogL given a model function and the BinPoint data.
If the pointer g is not null evaluate also the gradient of the Poisson pdf.
Expand Down
18 changes: 17 additions & 1 deletion math/mathcore/inc/Fit/Fitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Classes used for fitting (regression analysis) and estimation of parameter value
#include "Fit/FitConfig.h"
#include "Fit/FitExecutionPolicy.h"
#include "Fit/FitResult.h"
#include "Math/IParamFunctionfwd.h"
#include "Math/IParamFunction.h"
#include <memory>

namespace ROOT {
Expand Down Expand Up @@ -324,6 +324,7 @@ class Fitter {
Set the fitted function (model function) from a vectorized parametric function interface
*/
#ifdef R__HAS_VECCORE
template<class NotCompileIfScalarBackend = std::enable_if<!(std::is_same<double, ROOT::Double_v>::value)>>
void SetFunction(const IModelFunction_v & func);
#endif
/**
Expand Down Expand Up @@ -516,6 +517,21 @@ bool Fitter::GetDataFromFCN() {
}
}

#ifdef R__HAS_VECCORE
template<class NotCompileIfScalarBackend>
void Fitter::SetFunction(const IModelFunction_v & func)
{
// set the fit model function (clone the given one and keep a copy )
//std::cout << "set a non-grad function" << std::endl;
fUseGradient = false;
fFunc_v = std::shared_ptr<IModelFunction_v>(dynamic_cast<IModelFunction_v *>(func.Clone() ) );
assert(fFunc_v);

// creates the parameter settings
fConfig.CreateParamsSettings(*fFunc_v);
fFunc.reset();
}
#endif

} // end namespace Fit

Expand Down
61 changes: 0 additions & 61 deletions math/mathcore/src/FitConfig.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -171,67 +171,6 @@ void FitConfig::SetParamsSettings(unsigned int npar, const double *params, const
}
}

void FitConfig::CreateParamsSettings(const ROOT::Math::IParamMultiFunction & func) {
// initialize from model function
// set the parameters values from the function
unsigned int npar = func.NPar();
const double * begin = func.Parameters();
if (begin == 0) {
fSettings = std::vector<ParameterSettings>(npar);
return;
}

fSettings.clear();
fSettings.reserve(npar);
const double * end = begin+npar;
unsigned int i = 0;
for (const double * ipar = begin; ipar != end; ++ipar) {
double val = *ipar;
double step = 0.3*std::fabs(val); // step size is 30% of par value
//double step = 2.0*std::fabs(val); // step size is 30% of par value
if (val == 0) step = 0.3;

fSettings.push_back( ParameterSettings(func.ParameterName(i), val, step ) );
#ifdef DEBUG
std::cout << "FitConfig: add parameter " << func.ParameterName(i) << " val = " << val << std::endl;
#endif
i++;
}

}

#ifdef R__HAS_VECCORE
void FitConfig::CreateParamsSettings(const ROOT::Math::IParamMultiFunctionTempl<ROOT::Double_v> & func){

// initialize from model function
// set the parameters values from the function
unsigned int npar = func.NPar();
const double * begin = func.Parameters();
if (begin == 0) {
fSettings = std::vector<ParameterSettings>(npar);
return;
}

fSettings.clear();
fSettings.reserve(npar);
const double * end = begin+npar;
unsigned int i = 0;
for (const double * ipar = begin; ipar != end; ++ipar) {
double val = *ipar;
double step = 0.3*std::fabs(val); // step size is 30% of par value
//double step = 2.0*std::fabs(val); // step size is 30% of par value
if (val == 0) step = 0.3;

fSettings.push_back( ParameterSettings(func.ParameterName(i), val, step ) );
#ifdef DEBUG
std::cout << "FitConfig: add parameter " << func.ParameterName(i) << " val = " << val << std::endl;
#endif
i++;
}

}
#endif

ROOT::Math::Minimizer * FitConfig::CreateMinimizer() {
// create minimizer according to the chosen configuration using the
// plug-in manager
Expand Down
20 changes: 0 additions & 20 deletions math/mathcore/src/FitUtil.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -754,21 +754,6 @@ void FitUtil::EvaluateChi2Gradient(const IModelFunction & f, const BinData & dat
// utility function used by the likelihoods

// for LogLikelihood functions
#ifdef R__HAS_VECCORE
double FitUtil::EvaluatePdf(const IModelFunctionTempl<ROOT::Double_v> & func, const UnBinData & data, const double * p, unsigned int i, double *) {
// evaluate the pdf contribution to the generic logl function in case of bin data
// return actually the log of the pdf and its derivatives


//func.SetParameters(p);


const auto x = vecCore::FromPtr<ROOT::Double_v>(data.GetCoordComponent(i,0));
auto fval = func (&x, p);
auto logPdf = ROOT::Math::Util::EvalLog(fval);
return logPdf[0];
}
#endif

double FitUtil::EvaluatePdf(const IModelFunction & func, const UnBinData & data, const double * p, unsigned int i, double * g) {
// evaluate the pdf contribution to the generic logl function in case of bin data
Expand Down Expand Up @@ -1000,11 +985,6 @@ nPoints = 0;
return -logl;
}

#ifdef R__HAS_VECCORE
void FitUtil::EvaluateLogLGradient(const IModelFunctionTempl<ROOT::Double_v> &, const UnBinData &, const double * , double *, unsigned int & ) {
}
#endif

void FitUtil::EvaluateLogLGradient(const IModelFunction & f, const UnBinData & data, const double * p, double * grad, unsigned int & ) {
// evaluate the gradient of the log likelihood function

Expand Down
15 changes: 0 additions & 15 deletions math/mathcore/src/Fitter.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,6 @@ void Fitter::SetFunction(const IModelFunction & func, bool useGradient)
fFunc_v.reset();
}

#ifdef R__HAS_VECCORE
void Fitter::SetFunction(const IModelFunction_v & func)
{
// set the fit model function (clone the given one and keep a copy )
//std::cout << "set a non-grad function" << std::endl;
fUseGradient = false;
fFunc_v = std::shared_ptr<IModelFunction_v>(dynamic_cast<IModelFunction_v *>(func.Clone() ) );
assert(fFunc_v);

// creates the parameter settings
fConfig.CreateParamsSettings(*fFunc_v);
fFunc.reset();
}
#endif

void Fitter::SetFunction(const IModel1DFunction & func, bool useGradient)
{
fUseGradient = useGradient;
Expand Down