Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
CodegenContext.cxx
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * Garima Singh, CERN 2023
5 * Jonas Rembser, CERN 2023
6 *
7 * Copyright (c) 2023, CERN
8 *
9 * Redistribution and use in source and binary forms,
10 * with or without modification, are permitted according to the terms
11 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
12 */
13
15#include <RooAbsArg.h>
16
17#include "RooFitImplHelpers.h"
18
19#include <TInterpreter.h>
20
21#include <algorithm>
22#include <cctype>
23#include <fstream>
24#include <type_traits>
25#include <unordered_map>
26
27namespace {
28
29bool startsWith(std::string_view str, std::string_view prefix)
30{
31 return str.size() >= prefix.size() && 0 == str.compare(0, prefix.size(), prefix);
32}
33
34} // namespace
35
36namespace RooFit {
37namespace Experimental {
38
39/// @brief Adds (or overwrites) the string representing the result of a node.
40/// @param key The name of the node to add the result for.
41/// @param value The new name to assign/overwrite.
42void CodegenContext::addResult(const char *key, std::string const &value)
43{
44 const TNamed *namePtr = RooNameReg::known(key);
45 if (namePtr)
46 addResult(namePtr, value);
47}
48
49void CodegenContext::addResult(TNamed const *key, std::string const &value)
50{
51 _nodeNames[key] = value;
52}
53
54/// @brief Gets the result for the given node using the node name. This node also performs the necessary
55/// code generation through recursive calls to 'translate'. A call to this function modifies the already
56/// existing code body.
57/// @param key The node to get the result string for.
58/// @return String representing the result of this node.
59std::string const &CodegenContext::getResult(RooAbsArg const &arg)
60{
61 // If the result has already been recorded, just return the result.
62 // It is usually the responsibility of each translate function to assign
63 // the proper result to its class. Hence, if a result has already been recorded
64 // for a particular node, it means the node has already been 'translate'd and we
65 // dont need to visit it again.
66 auto found = _nodeNames.find(arg.namePtr());
67 if (found != _nodeNames.end())
68 return found->second;
69
70 // The result for vector observables should already be in the map if you
71 // opened the loop scope. This is just to check if we did not request the
72 // result of a vector-valued observable outside of the scope of a loop.
73 auto foundVecObs = _vecObsIndices.find(arg.namePtr());
74 if (foundVecObs != _vecObsIndices.end()) {
75 throw std::runtime_error("You requested the result of a vector observable outside a loop scope for it!");
76 }
77
78 auto RAII(OutputScopeRangeComment(&arg));
79
80 // Now, recursively call translate into the current argument to load the correct result.
81 codegen(const_cast<RooAbsArg &>(arg), *this);
82
83 return _nodeNames.at(arg.namePtr());
84}
85
86/// @brief Adds the given string to the string block that will be emitted at the top of the squashed function. Useful
87/// for variable declarations.
88/// @param str The string to add to the global scope.
89void CodegenContext::addToGlobalScope(std::string const &str)
90{
91 // Introduce proper indentation for multiline strings.
92 _code[0] += str;
93}
94
95/// @brief Since the squashed code represents all observables as a single flattened array, it is important
96/// to keep track of the start index for a vector valued observable which can later be expanded to access the correct
97/// element. For example, a vector valued variable x with 10 entries will be squashed to obs[start_idx + i].
98/// @param key The name of the node representing the vector valued observable.
99/// @param idx The start index (or relative position of the observable in the set of all observables).
100void CodegenContext::addVecObs(const char *key, int idx)
101{
102 const TNamed *namePtr = RooNameReg::known(key);
103 if (namePtr)
104 _vecObsIndices[namePtr] = idx;
105}
106
107/// @brief Adds the input string to the squashed code body. If a class implements a translate function that wants to
108/// emit something to the squashed code body, it must call this function with the code it wants to emit. In case of
109/// loops, automatically determines if code needs to be stored inside or outside loop scope.
110/// @param klass The class requesting this addition, usually 'this'.
111/// @param in String to add to the squashed code.
112void CodegenContext::addToCodeBody(RooAbsArg const *klass, std::string const &in)
113{
114 // If we are in a loop and the value is scope independent, save it at the top of the loop.
115 // else, just save it in the current scope.
117}
118
119/// @brief A variation of the previous addToCodeBody that takes in a bool value that determines
120/// if input is independent. This overload exists because there might other ways to determine if
121/// a value/collection of values is scope independent.
122/// @param in String to add to the squashed code.
123/// @param isScopeIndep The value determining if the input is scope dependent.
124void CodegenContext::addToCodeBody(std::string const &in, bool isScopeIndep /* = false */)
125{
126 TString indented = in;
127 indented = indented.Strip(TString::kBoth); // trim
128
129 std::string indent_str = "";
130 for (unsigned i = 0; i < _indent; ++i)
131 indent_str += " ";
132 indented = indented.Prepend(indent_str);
133
134 // FIXME: Multiline input.
135 // indent_str += "\n";
136 // indented = indented.ReplaceAll("\n", indent_str);
137
138 // If we are in a loop and the value is scope independent, save it at the top of the loop.
139 // else, just save it in the current scope.
140 if (_code.size() > 2 && isScopeIndep) {
141 _code[_code.size() - 2] += indented;
142 } else {
143 _code.back() += indented;
144 }
145}
146
147/// @brief Create a RAII scope for iterating over vector observables. You can't use the result of vector observables
148/// outside these loop scopes.
149/// @param in A pointer to the calling class, used to determine the loop dependent variables.
150std::unique_ptr<CodegenContext::LoopScope> CodegenContext::beginLoop(RooAbsArg const *in)
151{
152 pushScope();
153 unsigned loopLevel = _code.size() - 2; // subtract global + function scope.
154 std::string idx = "loopIdx" + std::to_string(loopLevel);
155
156 std::vector<TNamed const *> vars;
157 // set the results of the vector observables
158 for (auto const &it : _vecObsIndices) {
159 if (!in->dependsOn(it.first))
160 continue;
161
162 vars.push_back(it.first);
163 _nodeNames[it.first] = "obs[" + std::to_string(it.second) + " + " + idx + "]";
164 }
165
166 // TODO: we are using the size of the first loop variable to the the number
167 // of iterations, but it should be made sure that all loop vars are either
168 // scalar or have the same size.
169 std::size_t numEntries = 1;
170 for (auto &it : vars) {
171 std::size_t n = outputSize(it);
172 if (n > 1 && numEntries > 1 && n != numEntries) {
173 throw std::runtime_error("Trying to loop over variables with different sizes!");
174 }
175 numEntries = std::max(n, numEntries);
176 }
177
178 // Make sure that the name of this variable doesn't clash with other stuff
179 addToCodeBody(in, "for(int " + idx + " = 0; " + idx + " < " + std::to_string(numEntries) + "; " + idx + "++) {\n");
180
181 return std::make_unique<LoopScope>(*this, std::move(vars));
182}
183
185{
186 addToCodeBody("}\n");
187
188 // clear the results of the loop variables if they were vector observables
189 for (auto const &ptr : scope.vars()) {
190 if (_vecObsIndices.find(ptr) != _vecObsIndices.end())
191 _nodeNames.erase(ptr);
192 }
193 popScope();
194}
195
196/// @brief Get a unique variable name to be used in the generated code.
198{
199 return "t" + std::to_string(_tmpVarIdx++);
200}
201
202/// @brief A function to save an expression that includes/depends on the result of the input node.
203/// @param in The node on which the valueToSave depends on/belongs to.
204/// @param valueToSave The actual string value to save as a temporary.
205void CodegenContext::addResult(RooAbsArg const *in, std::string const &valueToSave)
206{
207 // std::string savedName = RooFit::Detail::makeValidVarName(in->GetName());
208 std::string savedName = getTmpVarName();
209
210 // Only save values if they contain operations.
211 bool hasOperations = valueToSave.find_first_of(":-+/*") != std::string::npos;
212
213 // If the name is not empty and this value is worth saving, save it to the correct scope.
214 // otherwise, just return the actual value itself
215 if (hasOperations) {
216 // If this is a scalar result, it will go just outside the loop because
217 // it doesn't need to be recomputed inside loops.
218 std::string outVarDecl = "const double " + savedName + " = " + valueToSave + ";\n";
220 } else {
222 }
223
225}
226
227/// @brief Function to save a RooListProxy as an array in the squashed code.
228/// @param in The list to convert to array.
229/// @return Name of the array that stores the input list in the squashed code.
231{
232 if (in.empty()) {
233 return "nullptr";
234 }
235
236 auto it = _listNames.find(in.uniqueId().value());
237 if (it != _listNames.end())
238 return it->second;
239
240 std::string savedName = getTmpVarName();
241 bool canSaveOutside = true;
242
243 std::stringstream declStrm;
244 declStrm << "double " << savedName << "[] = {";
245 for (const auto arg : in) {
246 declStrm << getResult(*arg) << ",";
248 }
249 declStrm.seekp(-1, declStrm.cur);
250 declStrm << "};\n";
251
253
254 _listNames.insert({in.uniqueId().value(), savedName});
255 return savedName;
256}
257
258std::string CodegenContext::buildArg(std::span<const double> arr)
259{
260 unsigned int n = arr.size();
261 std::string offset = std::to_string(_xlArr.size());
262 _xlArr.reserve(_xlArr.size() + n);
263 for (unsigned int i = 0; i < n; i++) {
264 _xlArr.push_back(arr[i]);
265 }
266 return "xlArr + " + offset;
267}
268
269CodegenContext::ScopeRAII::ScopeRAII(RooAbsArg const *arg, CodegenContext &ctx) : _ctx(ctx), _arg(arg)
270{
271 std::ostringstream os;
272 Option_t *opts = nullptr;
274 _fn = os.str();
275 const std::string info = "// Begin -- " + _fn;
276 _ctx._indent++;
278}
279
281{
282 const std::string info = "// End -- " + _fn + "\n";
283 _ctx.addToCodeBody(_arg, info);
284 _ctx._indent--;
285}
286
288{
289 _code.push_back("");
290}
291
293{
294 std::string active_scope = _code.back();
295 _code.pop_back();
296 _code.back() += active_scope;
297}
298
300{
301 return !in->isReducerNode() && outputSize(in->namePtr()) == 1;
302}
303
304/// @brief Register a function that is only know to the interpreter to the context.
305/// This is useful to dump the standalone C++ code for the computation graph.
306void CodegenContext::collectFunction(std::string const &name)
307{
308 _collectedFunctions.emplace_back(name);
309}
310
311/// @brief Assemble and return the final code with the return expression and global statements.
312/// @param returnExpr The string representation of what the squashed function should return, usually the head node.
313/// @return The name of the declared function.
314std::string
315CodegenContext::buildFunction(RooAbsArg const &arg, std::map<RooFit::Detail::DataKey, std::size_t> const &outputSizes)
316{
317 CodegenContext ctx;
318 ctx.pushScope(); // push our global scope.
321 // We only want to take over parameters and observables
322 for (auto const &item : _nodeNames) {
323 if (startsWith(item.second, "params[") || startsWith(item.second, "obs[")) {
324 ctx._nodeNames.insert(item);
325 }
326 }
327 ctx._xlArr = _xlArr;
329
330 static int iCodegen = 0;
331 auto funcName = "roo_codegen_" + std::to_string(iCodegen++);
332
333 // Make sure the codegen implementations are known to the interpreter
334 gInterpreter->Declare("#include <RooFit/CodegenImpl.h>\n");
335
336 ctx.pushScope();
337 std::string funcBody = ctx.getResult(arg);
338 ctx.popScope();
339 funcBody = ctx._code[0] + "\n return " + funcBody + ";\n";
340
341 // Declare the function
342 std::stringstream bodyWithSigStrm;
343 bodyWithSigStrm << "double " << funcName << "(double* params, double const* obs, double const* xlArr) {\n"
344 << funcBody << "\n}";
345 ctx._collectedFunctions.emplace_back(funcName);
346 if (!gInterpreter->Declare(bodyWithSigStrm.str().c_str())) {
347 std::stringstream errorMsg;
348 std::string debugFileName = "_codegen_" + funcName + ".cxx";
349 errorMsg << "Function " << funcName << " could not be compiled. See above for details. Full code dumped to file "
350 << debugFileName << "for debugging";
351 {
352 std::ofstream outFile;
353 outFile.open(debugFileName.c_str());
354 outFile << bodyWithSigStrm.str();
355 }
356 oocoutE(nullptr, InputArguments) << errorMsg.str() << std::endl;
357 throw std::runtime_error(errorMsg.str().c_str());
358 }
359
360 _xlArr = ctx._xlArr;
362
363 return funcName;
364}
365
366void declareDispatcherCode(std::string const &funcName)
367{
368 std::string dispatcherCode = R"(
369namespace RooFit {
370namespace Experimental {
371
372template <class Arg_t, int P>
373auto FUNC_NAME(Arg_t &arg, CodegenContext &ctx, Prio<P> p)
374{
375 if constexpr (std::is_same<Prio<P>, PrioLowest>::value) {
376 return FUNC_NAME(arg, ctx);
377 } else {
378 return FUNC_NAME(arg, ctx, p.next());
379 }
380}
381
382template <class Arg_t>
383struct Caller_FUNC_NAME {
384
385 static auto call(RooAbsArg &arg, CodegenContext &ctx)
386 {
387 return FUNC_NAME(static_cast<Arg_t &>(arg), ctx, PrioHighest{});
388 }
389};
390
391} // namespace Experimental
392} // namespace RooFit
393 )";
394
395 RooFit::Detail::replaceAll(dispatcherCode, "FUNC_NAME", funcName);
396 gInterpreter->Declare(dispatcherCode.c_str());
397}
398
400{
401 static bool codeDeclared = false;
402 if (!codeDeclared) {
403 declareDispatcherCode("codegenImpl");
404 codeDeclared = true;
405 }
406
407 using Func = void (*)(RooAbsArg &, CodegenContext &);
408
409 Func func;
410
411 TClass *tclass = arg.IsA();
412
413 // Cache the overload resolutions
414 static std::unordered_map<TClass *, Func> dispatchMap;
415
416 auto found = dispatchMap.find(tclass);
417
418 if (found != dispatchMap.end()) {
419 func = found->second;
420 } else {
421 // Can probably done with CppInterop in the future to avoid string manipulation.
422 std::stringstream cmd;
423 cmd << "&RooFit::Experimental::Caller_codegenImpl<" << tclass->GetName() << ">::call;";
424 func = reinterpret_cast<Func>(gInterpreter->ProcessLine(cmd.str().c_str()));
425 dispatchMap[tclass] = func;
426 }
427
428 return func(arg, ctx);
429}
430
431} // namespace Experimental
432} // namespace RooFit
bool startsWith(std::string_view str, std::string_view prefix)
#define oocoutE(o, a)
const char Option_t
Definition RtypesCore.h:66
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
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
char name[80]
Definition TGX11.cxx:110
#define gInterpreter
const_iterator end() const
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:77
bool dependsOn(const RooAbsCollection &serverList, const RooAbsArg *ignoreArg=nullptr, bool valueOnly=false) const
Test whether we depend on (ie, are served by) any object in the specified collection.
const TNamed * namePtr() const
De-duplicated pointer to this object's name.
Definition RooAbsArg.h:504
Int_t defaultPrintContents(Option_t *opt) const override
Define default contents to print.
virtual bool isReducerNode() const
Definition RooAbsArg.h:518
Abstract container object that can hold multiple RooAbsArg objects.
RooFit::UniqueId< RooAbsCollection > const & uniqueId() const
Returns a unique ID that is different for every instantiated RooAbsCollection.
A class to manage loop scopes using the RAII technique.
A class to maintain the context for squashing of RooFit models into code.
std::unordered_map< RooFit::UniqueId< RooAbsCollection >::Value_t, std::string > _listNames
A map to keep track of list names as assigned by addResult.
void addToGlobalScope(std::string const &str)
Adds the given string to the string block that will be emitted at the top of the squashed function.
std::string const & getResult(RooAbsArg const &arg)
Gets the result for the given node using the node name.
std::string getTmpVarName() const
Get a unique variable name to be used in the generated code.
void addResult(RooAbsArg const *key, std::string const &value)
A function to save an expression that includes/depends on the result of the input node.
void addToCodeBody(RooAbsArg const *klass, std::string const &in)
Adds the input string to the squashed code body.
std::unique_ptr< LoopScope > beginLoop(RooAbsArg const *in)
Create a RAII scope for iterating over vector observables.
void collectFunction(std::string const &name)
Register a function that is only know to the interpreter to the context.
void addVecObs(const char *key, int idx)
Since the squashed code represents all observables as a single flattened array, it is important to ke...
std::unordered_map< const TNamed *, int > _vecObsIndices
A map to keep track of the observable indices if they are non scalar.
std::map< RooFit::Detail::DataKey, std::size_t > _nodeOutputSizes
Map of node output sizes.
std::string buildFunction(RooAbsArg const &arg, std::map< RooFit::Detail::DataKey, std::size_t > const &outputSizes={})
Assemble and return the final code with the return expression and global statements.
void endLoop(LoopScope const &scope)
std::vector< std::string > _collectedFunctions
bool isScopeIndependent(RooAbsArg const *in) const
std::vector< std::string > _code
The code layered by lexical scopes used as a stack.
unsigned _indent
The indentation level for pretty-printing.
std::unordered_map< const TNamed *, std::string > _nodeNames
Map of node names to their result strings.
std::size_t outputSize(RooFit::Detail::DataKey key) const
Figure out the output size of a node.
ScopeRAII OutputScopeRangeComment(RooAbsArg const *arg)
std::string buildArg(RooAbsCollection const &x)
Function to save a RooListProxy as an array in the squashed code.
int _tmpVarIdx
Index to get unique names for temporary variables.
static const TNamed * known(const char *stringPtr)
If the name is already known, return its TNamed pointer. Otherwise return 0 (don't register the name)...
virtual StyleOption defaultPrintStyle(Option_t *opt) const
virtual void printStream(std::ostream &os, Int_t contents, StyleOption style, TString indent="") const
Print description of object on ostream, printing contents set by contents integer,...
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:84
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
TClass * IsA() const override
Definition TNamed.h:60
Basic string class.
Definition TString.h:139
@ kBoth
Definition TString.h:276
const Int_t n
Definition legend1.C:16
void replaceAll(std::string &inOut, std::string_view what, std::string_view with)
void declareDispatcherCode(std::string const &funcName)
void codegen(RooAbsArg &arg, CodegenContext &ctx)
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:65
@ InputArguments
ScopeRAII(RooAbsArg const *arg, CodegenContext &ctx)
constexpr Value_t value() const
Return numerical value of ID.
Definition UniqueId.h:59