Logo ROOT  
Reference Guide
BatchHelpers.h
Go to the documentation of this file.
1 // Author: Stephan Hageboeck, CERN 25 Feb 2019
2 
3 /*****************************************************************************
4  * RooFit
5  * Authors: *
6  * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7  * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8  * *
9  * Copyright (c) 2000-2019, Regents of the University of California *
10  * and Stanford University. All rights reserved. *
11  * *
12  * Redistribution and use in source and binary forms, *
13  * with or without modification, are permitted according to the terms *
14  * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15  *****************************************************************************/
16 
17 #ifndef ROOFIT_ROOFITCORE_INC_BATCHHELPERS_H_
18 #define ROOFIT_ROOFITCORE_INC_BATCHHELPERS_H_
19 
20 #include "RooRealProxy.h"
21 #include "RooSpan.h"
22 
23 #include <vector>
24 
25 class RooArgSet;
26 
27 namespace BatchHelpers {
28 
29 constexpr size_t block = 1024;
30 
31 struct ArrayWrapper {
32  const double * __restrict ptr;
33  bool _batch;
34 
35  constexpr double operator[](std::size_t i) const {
36  return ptr[i];
37  }
38  constexpr bool batch() const {
39  return _batch;
40  }
41 };
42 
43 struct EvaluateInfo {
44  size_t size, nBatches;
45 };
46 
47 size_t findSmallestBatch(const std::vector< RooSpan<const double> >& parameters);
48 
49 EvaluateInfo init(std::vector< RooRealProxy > parameters,
50  std::vector< ArrayWrapper* > wrappers,
51  std::vector< double*> arrays,
52  size_t begin, size_t batchSize );
53 
54 ///Little adapter that gives a bracket operator to types that don't
55 ///have one. It completely ignores the index and returns a constant.
56 template <class T = double>
58  public:
59 
60  constexpr BracketAdapter(T payload) noexcept :
61  _payload{payload} { }
62 
63  constexpr BracketAdapter(RooSpan<const T> payload) noexcept :
64  _payload{payload[0]} { }
65 
66  constexpr double operator[](std::size_t) const {
67  return _payload;
68  }
69 
70  constexpr operator double() const {
71  return _payload;
72  }
73 
74  constexpr bool isBatch() const noexcept {
75  return false;
76  }
77 
78  private:
79  const T _payload;
80 };
81 
82 
84  public:
85  /// Construct adapter from a fallback value and a batch of values.
86  /// - If `batch.size() == 0`, always return `payload`.
87  /// - Else, return `batch[i]`.
88  BracketAdapterWithMask(double payload, const RooSpan<const double>& batch) noexcept :
89  _isBatch(!batch.empty()),
90  _payload(payload),
91  _pointer(batch.empty() ? &_payload : batch.data()),
92  _mask(batch.size() > 1 ? ~static_cast<size_t>(0): 0)
93  {
94  }
95 
96  /// Construct adapter from a batch of values.
97  /// - If `batch.size() == 1`, always return the value at `batch[0]`.
98  /// - Else, return `batch[i]`.
100  _isBatch(batch.size() > 1),
101  _payload(batch[0]),
102  _pointer(batch.data()),
103  _mask(batch.size() > 1 ? ~static_cast<size_t>(0): 0)
104  {
105  assert(batch.size() > 0);
106  }
107 
109  _isBatch(other._isBatch),
110  _payload(other._payload),
111  _pointer(other._isBatch ? other._pointer : &_payload),
112  _mask(other._mask)
113  {
114  }
115 
117 
118  inline double operator[](std::size_t i) const noexcept {
119  return _pointer[ i & _mask];
120  }
121 
122  inline bool isBatch() const noexcept {
123  return _isBatch;
124  }
125 
126  private:
127  const bool _isBatch;
128  const double _payload;
129  const double* __restrict const _pointer;
130  const size_t _mask;
131 };
132 
133 
134 /// Helper class to access a batch-related part of RooAbsReal's interface, which should not leak to the outside world.
136  public:
137  static void checkBatchComputation(const RooAbsReal& theReal, const BatchHelpers::RunContext& evalData, std::size_t evtNo,
138  const RooArgSet* normSet = nullptr, double relAccuracy = 1.E-13) {
139  theReal.checkBatchComputation(evalData, evtNo, normSet, relAccuracy);
140  }
141 };
142 
143 }
144 
145 #endif /* ROOFIT_ROOFITCORE_INC_BATCHHELPERS_H_ */
BatchHelpers::BracketAdapterWithMask::BracketAdapterWithMask
BracketAdapterWithMask(RooSpan< const double > batch)
Construct adapter from a batch of values.
Definition: BatchHelpers.h:99
BatchHelpers::EvaluateInfo::nBatches
size_t nBatches
Definition: BatchHelpers.h:44
BatchHelpers::init
EvaluateInfo init(std::vector< RooRealProxy > parameters, std::vector< ArrayWrapper * > wrappers, std::vector< double * > arrays, size_t begin, size_t batchSize)
BatchHelpers::BracketAdapter
Little adapter that gives a bracket operator to types that don't have one.
Definition: BatchHelpers.h:57
BatchHelpers::EvaluateInfo
Definition: BatchHelpers.h:43
RooAbsReal
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:61
BatchHelpers::BatchInterfaceAccessor::checkBatchComputation
static void checkBatchComputation(const RooAbsReal &theReal, const BatchHelpers::RunContext &evalData, std::size_t evtNo, const RooArgSet *normSet=nullptr, double relAccuracy=1.E-13)
Definition: BatchHelpers.h:137
BatchHelpers::BracketAdapterWithMask::operator=
BracketAdapterWithMask & operator=(const BracketAdapterWithMask &other)=delete
BatchHelpers::block
constexpr size_t block
Definition: BatchHelpers.h:29
RooAbsReal::checkBatchComputation
void checkBatchComputation(const BatchHelpers::RunContext &evalData, std::size_t evtNo, const RooArgSet *normSet=nullptr, double relAccuracy=1.E-13) const
Walk through expression tree headed by the this object, and check a batch computation.
Definition: RooAbsReal.cxx:5033
BatchHelpers::RunContext
Data that has to be passed around when evaluating functions / PDFs.
Definition: RunContext.h:32
RooSpan::size
constexpr std::span< T >::index_type size() const noexcept
Definition: RooSpan.h:121
BatchHelpers::ArrayWrapper::ptr
const double *__restrict ptr
Definition: BatchHelpers.h:32
BatchHelpers
Definition: RooFitComputeInterface.h:9
BatchHelpers::BracketAdapterWithMask::_isBatch
const bool _isBatch
Definition: BatchHelpers.h:127
BatchHelpers::ArrayWrapper::operator[]
constexpr double operator[](std::size_t i) const
Definition: BatchHelpers.h:35
BatchHelpers::BracketAdapterWithMask::BracketAdapterWithMask
BracketAdapterWithMask(double payload, const RooSpan< const double > &batch) noexcept
Construct adapter from a fallback value and a batch of values.
Definition: BatchHelpers.h:88
BatchHelpers::BracketAdapterWithMask::isBatch
bool isBatch() const noexcept
Definition: BatchHelpers.h:122
BatchHelpers::BatchInterfaceAccessor
Helper class to access a batch-related part of RooAbsReal's interface, which should not leak to the o...
Definition: BatchHelpers.h:135
BatchHelpers::ArrayWrapper::_batch
bool _batch
Definition: BatchHelpers.h:33
BatchHelpers::ArrayWrapper::batch
constexpr bool batch() const
Definition: BatchHelpers.h:38
BatchHelpers::BracketAdapterWithMask::_payload
const double _payload
Definition: BatchHelpers.h:128
BatchHelpers::BracketAdapter::BracketAdapter
constexpr BracketAdapter(T payload) noexcept
Definition: BatchHelpers.h:60
RooRealProxy.h
BatchHelpers::findSmallestBatch
size_t findSmallestBatch(const std::vector< RooSpan< const double > > &parameters)
double
double
Definition: Converters.cxx:921
BatchHelpers::BracketAdapter::operator[]
constexpr double operator[](std::size_t) const
Definition: BatchHelpers.h:66
BatchHelpers::BracketAdapterWithMask
Definition: BatchHelpers.h:83
BatchHelpers::BracketAdapter::_payload
const T _payload
Definition: BatchHelpers.h:79
BatchHelpers::ArrayWrapper
Definition: BatchHelpers.h:31
BatchHelpers::EvaluateInfo::size
size_t size
Definition: BatchHelpers.h:44
BatchHelpers::BracketAdapterWithMask::operator[]
double operator[](std::size_t i) const noexcept
Definition: BatchHelpers.h:118
BatchHelpers::BracketAdapterWithMask::_pointer
const double *__restrict const _pointer
Definition: BatchHelpers.h:129
BatchHelpers::BracketAdapterWithMask::_mask
const size_t _mask
Definition: BatchHelpers.h:130
ROOT::Math::Chebyshev::T
double T(double x)
Definition: ChebyshevPol.h:34
BatchHelpers::BracketAdapter::isBatch
constexpr bool isBatch() const noexcept
Definition: BatchHelpers.h:74
BatchHelpers::BracketAdapterWithMask::BracketAdapterWithMask
BracketAdapterWithMask(const BracketAdapterWithMask &other) noexcept
Definition: BatchHelpers.h:108
RooSpan.h
BatchHelpers::BracketAdapter::BracketAdapter
constexpr BracketAdapter(RooSpan< const T > payload) noexcept
Definition: BatchHelpers.h:63
RooSpan
A simple container to hold a batch of data values.
Definition: RooSpan.h:34
RooArgSet
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:29