Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
EvalContext.h
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * Jonas Rembser, CERN 12/2021
5 *
6 * Copyright (c) 2023, CERN
7 *
8 * Redistribution and use in source and binary forms,
9 * with or without modification, are permitted according to the terms
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11 */
12
13#ifndef RooFit_Detail_EvalContext_h
14#define RooFit_Detail_EvalContext_h
15
16#include <RooAbsArg.h>
17
18#include <ROOT/RSpan.hxx>
19
20#include <TNamed.h>
21#include <TObject.h>
22
23#include <Math/Util.h>
24
25#include <functional>
26#include <map>
27#include <stdexcept>
28#include <sstream>
29
30template <class T>
32
33namespace RooBatchCompute {
34class Config;
35}
36
37/// \class RooFit::DataKey
38/// To use as a key type for RooFit data maps and containers. A RooFit::DataKey
39/// can be constructed with no runtime overhead from a RooAbsArg (or any
40/// templated RooFit proxy for convenience). Compared to using the RooAbsArg
41/// pointer directly, this has the advantage that one can easily change the way
42/// the key is constructed from the object, just by changing the implementation
43/// of the DataKey. For example, it is trivial to move from using the RooAbsArg
44/// pointer to using the unique name pointer retrieved by RooAbsArg::namePtr().
45
46namespace RooFit {
47namespace Detail {
48
49class DataKey {
50public:
51 inline DataKey(RooAbsArg const *arg) : _ptr{arg->namePtr()} {}
52 inline DataKey(TNamed const *arg) : _ptr{arg} {}
53 template <class T>
55 {
56 }
57
58 // Comparison operators that wrap the pointer comparisons.
59 friend inline bool operator==(const DataKey &k1, const DataKey &k2) { return k1._ptr == k2._ptr; }
60 friend inline bool operator!=(const DataKey &k1, const DataKey &k2) { return k1._ptr != k2._ptr; }
61 friend inline bool operator<(const DataKey &k1, const DataKey &k2) { return k1._ptr < k2._ptr; }
62
63 // Implementing pointer-style operators.
64 inline TObject const &operator*() const { return *_ptr; }
65 inline TObject const *operator->() const { return _ptr; }
66
67private:
68 TObject const *_ptr;
69};
70
71} // namespace Detail
72} // namespace RooFit
73
74namespace std {
75
76template <>
77struct hash<RooFit::Detail::DataKey> {
78 std::size_t operator()(const RooFit::Detail::DataKey &k) const { return hash<TObject const *>{}(&*k); }
79};
80
81} // namespace std
82
83namespace RooFit {
84
86public:
88
89 auto size() const { return _ctx.size(); }
90 void resize(std::size_t n);
91
92 inline void set(RooAbsArg const *arg, std::span<const double> const &span)
93 {
94 if (!arg->hasDataToken())
95 return;
96 std::size_t idx = arg->dataToken();
97 _ctx[idx] = span;
98 }
99
100 void setConfig(RooAbsArg const *arg, RooBatchCompute::Config const &config);
101
102 std::span<const double> at(RooAbsArg const *arg, RooAbsArg const *caller = nullptr);
103
104 template <class T>
105 inline std::span<const double> at(RooTemplateProxy<T> const &proxy)
106 {
107 return at(&proxy.arg(), proxy.owner());
108 }
109
110 RooBatchCompute::Config config(RooAbsArg const *arg) const;
111
112 /// A counter that is incremented every time new input data is loaded into
113 /// the evaluation context. Reducer nodes can use it as a cache
114 /// invalidation key for quantities that only depend on the input data,
115 /// like the sum of event weights. The counter values are unique across
116 /// all evaluation contexts in the process, so cached values can not be
117 /// wrongly validated by an unrelated context.
118 std::size_t inputGeneration() const { return _inputGeneration; }
119
122 std::span<double> output() { return _currentOutput; }
123
126
127 /// Register an action to be run after the evaluation of the full
128 /// computation graph, when all potentially asynchronous computations and
129 /// data transfers have completed. Used to defer work that depends on
130 /// results that are read back from the GPU without synchronization, like
131 /// the logging of evaluation error counts.
132 void deferAction(std::function<void()> action) { _deferredActions.emplace_back(std::move(action)); }
133
134private:
135 friend class Evaluator;
136
138 std::size_t _inputGeneration = 1;
139 std::span<double> _currentOutput;
140 std::vector<std::span<const double>> _ctx;
142 std::vector<std::vector<double>> _buffers;
143 std::size_t _bufferIdx = 0;
144 std::vector<RooBatchCompute::Config> _cfgs;
145 std::vector<std::function<void()>> _deferredActions;
146};
147
148} // namespace RooFit
149
150#endif
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
The Kahan summation is a compensated summation algorithm, which significantly reduces numerical error...
Definition Util.h:141
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:76
bool hasDataToken() const
Definition RooAbsArg.h:614
std::size_t dataToken() const
Returns the token for retrieving results in the BatchMode. For internal use only.
Definition RooAbsArg.h:613
Minimal configuration struct to steer the evaluation of a single node with the RooBatchCompute librar...
DataKey(RooTemplateProxy< T > const &proxy)
Definition EvalContext.h:54
friend bool operator!=(const DataKey &k1, const DataKey &k2)
Definition EvalContext.h:60
DataKey(RooAbsArg const *arg)
Definition EvalContext.h:51
DataKey(TNamed const *arg)
Definition EvalContext.h:52
TObject const * _ptr
Definition EvalContext.h:68
TObject const * operator->() const
Definition EvalContext.h:65
friend bool operator<(const DataKey &k1, const DataKey &k2)
Definition EvalContext.h:61
friend bool operator==(const DataKey &k1, const DataKey &k2)
Definition EvalContext.h:59
TObject const & operator*() const
Definition EvalContext.h:64
std::size_t _inputGeneration
auto size() const
Definition EvalContext.h:89
std::vector< std::function< void()> > _deferredActions
std::vector< RooBatchCompute::Config > _cfgs
void set(RooAbsArg const *arg, std::span< const double > const &span)
Definition EvalContext.h:92
std::span< const double > at(RooAbsArg const *arg, RooAbsArg const *caller=nullptr)
std::span< double > output()
void enableVectorBuffers(bool enable)
std::size_t _bufferIdx
OffsetMode _offsetMode
void deferAction(std::function< void()> action)
Register an action to be run after the evaluation of the full computation graph, when all potentially...
std::span< const double > at(RooTemplateProxy< T > const &proxy)
RooBatchCompute::Config config(RooAbsArg const *arg) const
std::size_t inputGeneration() const
A counter that is incremented every time new input data is loaded into the evaluation context.
void setConfig(RooAbsArg const *arg, RooBatchCompute::Config const &config)
std::vector< std::span< const double > > _ctx
void setOutputWithOffset(RooAbsArg const *arg, ROOT::Math::KahanSum< double > val, ROOT::Math::KahanSum< double > const &offset)
Sets the output value with an offset.
std::vector< std::vector< double > > _buffers
std::span< double > _currentOutput
void resize(std::size_t n)
Evaluates a RooAbsReal object in other ways than recursive graph traversal.
Definition Evaluator.h:38
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
Mother of all ROOT objects.
Definition TObject.h:42
const Int_t n
Definition legend1.C:16
Namespace for dispatching RooFit computations to various backends.
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:73