Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RunContext.cxx
Go to the documentation of this file.
1// Author: Stephan Hageboeck, CERN Jul 2020
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-2020, 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/**
18 * \struct RooBatchCompute::RunContext
19 *
20 * This struct enables passing computation data around between elements of a computation graph.
21 *
22 * ### Separating data and computation graph
23 * The RunContext stores read-only spans to data that has already been computed.
24 * This can be data of the observables (which is constant during a fit)
25 * or intermediate computation results from evaluating PDFs or formulae for every point in a dataset.
26 * The latter may change as fit parameters change.
27 *
28 * Instead of storing information about these data *inside* nodes of the computation graph (requiring a change
29 * of their state, possibly violating const-correctness), this information is stored in RunContext::spans using
30 * the pointer of the element that produced those results as a key. In this way, one or multiple RunContext
31 * instances can be passed around when computations are running, leaving the objects of the computation graph
32 * invariant.
33 *
34 * ### Memory ownership model
35 * The RunContext can provide memory for temporary data, that is, data that can vanish after a fit converges. Using
36 * RunContext::makeBatch(), a suitable amount of memory is allocated to store computation results.
37 * When intermediate data are cleared, this memory is *not freed*. In this way, temporary data can be invalidated
38 * when fit parameters change, but the memory is only allocated once per fit.
39 *
40 * When a RunContext goes out of scope, the memory is freed. That means that in between fit cycles, a RunContext should
41 * be cleared using clear(), or single results should be invalidated by removing these from RunContext::spans.
42 * The RunContext object should be destroyed only *after* a fit completes.
43 */
44
45
46#include "RunContext.h"
47
48#include <limits>
49
50class RooAbsReal;
51
52namespace RooBatchCompute {
53
54/// Check if there is a span of data corresponding to the object passed as owner.
56 const auto item = spans.find(owner);
57 if (item != spans.end())
58 return item->second;
59
60 return {};
61}
62
63
64/// Check if there is a writable span of data corresponding to the object passed as owner.
65/// The span can be used both for reading and writing.
67 auto item = ownedMemory.find(owner);
68 if (item != ownedMemory.end()) {
69 assert(spans.count(owner) > 0); // If we can write, the span must also be registered for reading
70 return RooSpan<double>(item->second);
71 }
72
73 return {};
74}
75
76
77/// Create a writable batch. If the RunContext already owns memory for the object
78/// `owner`, just resize the memory. If it doesn't exist yet, allocate it.
79/// \warning The memory will be uninitialised, so every entry **must** be overwritten.
80/// On first use, all values are initialised to `NaN` to help detect such errors.
81///
82/// A read-only reference to the memory will be stored in `spans`.
83/// \param owner RooFit object whose value should be written into the memory.
84/// \param size Requested size of the span.
85/// \return A writeable RooSpan of the requested size, whose memory is owned by
86/// the RunContext.
87RooSpan<double> RunContext::makeBatch(const RooAbsReal* owner, std::size_t size) {
88 auto item = ownedMemory.find(owner);
89 if (item == ownedMemory.end() || item->second.size() != size) {
90 std::vector<double>& data = ownedMemory[owner];
91 data.resize(size, std::numeric_limits<double>::quiet_NaN());
92#ifndef NDEBUG
93 data.assign(size, std::numeric_limits<double>::quiet_NaN());
94#endif
95 spans[owner] = RooSpan<const double>(data);
96 return {data};
97 }
98
99 spans[owner] = RooSpan<const double>(item->second);
100 return {item->second};
101}
102
103}
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition RooAbsReal.h:61
A simple container to hold a batch of data values.
Definition RooSpan.h:34
Namespace for dispatching RooFit computations to various backends.
std::unordered_map< const RooAbsReal *, RooSpan< const double > > spans
Once an object has computed its value(s), the span pointing to the results is registered here.
Definition RunContext.h:52
RooSpan< double > getWritableBatch(const RooAbsReal *owner)
Check if there is a writable span of data corresponding to the object passed as owner.
std::unordered_map< const RooAbsReal *, std::vector< double > > ownedMemory
Memory owned by this struct. It is associated to nodes in the computation graph using their pointers.
Definition RunContext.h:54
RooSpan< const double > getBatch(const RooArgProxy &proxy) const
RooSpan< double > makeBatch(const RooAbsReal *owner, std::size_t size)
Create a writable batch.