Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
Evaluator.cxx
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * Jonas Rembser, CERN 2021
5 * Emmanouil Michalainas, CERN 2021
6 *
7 * Copyright (c) 2021, 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
14/**
15\file Evaluator.cxx
16\class RooFit::Evaluator
17\ingroup Roofitcore
18
19Evaluates a RooAbsReal object in other ways than recursive graph
20traversal. Currently, it is being used for evaluating a RooAbsReal object and
21supplying the value to the minimizer, during a fit. The class scans the
22dependencies and schedules the computations in a secure and efficient way. The
23computations take place in the RooBatchCompute library and can be carried off
24by either the CPU or a CUDA-supporting GPU. The Evaluator class takes care
25of data transfers. An instance of this class is created every time
26RooAbsPdf::fitTo() is called and gets destroyed when the fitting ends.
27**/
28
29#include <RooFit/Evaluator.h>
30
31#include <RooAbsCategory.h>
32#include <RooAbsData.h>
33#include <RooAbsReal.h>
34#include <RooRealVar.h>
35#include <RooBatchCompute.h>
36#include <RooMsgService.h>
37#include <RooNameReg.h>
38#include <RooSimultaneous.h>
39
40
42#include "RooFitImplHelpers.h"
43
44#include <atomic>
45#include <iomanip>
46#include <mutex>
47#include <numeric>
48#include <set>
49#include <unordered_set>
50
51namespace RooFit {
52
53namespace {
54
55// To avoid deleted move assignment.
56template <class T>
57void assignSpan(std::span<T> &to, std::span<T> const &from)
58{
59 to = from;
60}
61
62void logArchitectureInfo(bool useGPU)
63{
64 // We have to exit early if the message stream is not active. Otherwise it's
65 // possible that this function skips logging because it thinks it has
66 // already logged, but actually it didn't.
67 if (!RooMsgService::instance().isActive(nullptr, RooFit::Fitting, RooFit::INFO)) {
68 return;
69 }
70
71 // Don't repeat logging architecture info if the useGPU option didn't change
72 {
73 // Second element of pair tracks whether this function has already been called
74 static std::pair<bool, bool> lastUseGPU;
75 if (lastUseGPU.second && lastUseGPU.first == useGPU)
76 return;
77 lastUseGPU = {useGPU, true};
78 }
79
80 auto log = [](std::string_view message) {
81 oocxcoutI(static_cast<RooAbsArg *>(nullptr), Fitting) << message << std::endl;
82 };
83
85 log("using generic CPU library compiled with no vectorizations");
86 } else {
87 log(std::string("using CPU computation library compiled with -m") + RooBatchCompute::cpuArchitectureName());
88 }
89 if (useGPU) {
90 log("using CUDA computation library");
91 }
92}
93
94/// Advise the user to implement CUDA support for a class that had to be
95/// evaluated on the CPU even though the CUDA backend was requested. Just like
96/// for the analogous message about the missing batch evaluation interface in
97/// RooAbsReal::doEval(), the message is only printed once per class, because
98/// the computation graph is evaluated many times, e.g. in every minimizer
99/// iteration.
100void logMissingCudaSupport(RooAbsArg const &arg)
101{
103 return;
104 }
105 static std::set<std::string> warnedClasses;
106 static std::mutex warnedClassesMutex;
107 std::scoped_lock guard{warnedClassesMutex};
108 if (warnedClasses.insert(arg.ClassName()).second) {
109 oocoutI(&arg, FastEvaluations) << "The class " << arg.ClassName()
110 << " could not be evaluated on the GPU because it doesn't support it."
111 << " Consider requesting or implementing it to benefit from a speed up."
112 << std::endl;
113 }
114}
115
116} // namespace
117
118/// A struct used by the Evaluator to store information on the RooAbsArgs in
119/// the computation graph.
120struct NodeInfo {
121
122 bool isScalar() const { return outputSize == 1; }
123
124 RooAbsArg *absArg = nullptr;
126
127 std::shared_ptr<RooBatchCompute::AbsBuffer> buffer;
128 std::size_t iNode = 0;
130 bool fromArrayInput = false;
131 bool isVariable = false;
132 bool isDirty = true;
133 bool isCategory = false;
134 bool hasLogged = false;
135 bool computeInGPU = false;
136 bool isValueServer = false; // if this node is a value server to the top node
137 std::size_t outputSize = 1;
138 std::size_t lastSetValCount = std::numeric_limits<std::size_t>::max();
139 int lastCatVal = std::numeric_limits<int>::max();
140 double scalarBuffer = 0.0;
141 std::vector<NodeInfo *> serverInfos;
142 std::vector<NodeInfo *> clientInfos;
143};
144
145/// Construct a new Evaluator. The constructor analyzes and saves metadata about the graph,
146/// useful for the evaluation of it that will be done later. In case the CUDA mode is selected,
147/// there's also some CUDA-related initialization.
148///
149/// \param[in] absReal The RooAbsReal object that sits on top of the
150/// computation graph that we want to evaluate.
151/// \param[in] useGPU Whether the evaluation should be preferably done on the GPU.
152Evaluator::Evaluator(const RooAbsReal &absReal, bool useGPU)
153 : _topNode{const_cast<RooAbsReal &>(absReal)}, _useGPU{useGPU}
154{
156 if (useGPU && RooBatchCompute::initCUDA() != 0) {
157 throw std::runtime_error("Can't create Evaluator in CUDA mode because RooBatchCompute CUDA could not be loaded!");
158 }
159 // Some checks and logging of used architectures
160 logArchitectureInfo(_useGPU);
161
164
165 RooArgSet serverSet;
167
168 _evalContextCPU.resize(serverSet.size());
169 if (useGPU) {
170 _evalContextCUDA.resize(serverSet.size());
171 }
172
173 std::map<RooFit::Detail::DataKey, NodeInfo *> nodeInfos;
174
175 // Fill the ordered nodes list and initialize the node info structs.
176 _nodes.reserve(serverSet.size());
177 std::size_t iNode = 0;
178 for (RooAbsArg *arg : serverSet) {
179
180 _nodes.emplace_back();
181 auto &nodeInfo = _nodes.back();
182 _nodesMap[arg->namePtr()] = &nodeInfo;
183
184 nodeInfo.absArg = arg;
185 nodeInfo.originalOperMode = arg->operMode();
186 nodeInfo.iNode = iNode;
187 nodeInfos[arg] = &nodeInfo;
188
189 if (dynamic_cast<RooRealVar const *>(arg)) {
190 nodeInfo.isVariable = true;
191 } else {
192 arg->setDataToken(iNode, this);
193 }
194 if (dynamic_cast<RooAbsCategory const *>(arg)) {
195 nodeInfo.isCategory = true;
196 }
197
198 ++iNode;
199 }
200
201 for (NodeInfo &info : _nodes) {
202 info.serverInfos.reserve(info.absArg->servers().size());
203 for (RooAbsArg *server : info.absArg->servers()) {
204 if (server->isValueServer(*info.absArg)) {
205 auto *serverInfo = nodeInfos.at(server);
206 info.serverInfos.emplace_back(serverInfo);
207 serverInfo->clientInfos.emplace_back(&info);
208 }
209 }
210 }
211
212 // Figure out which nodes are value servers to the top node
213 _nodes.back().isValueServer = true; // the top node itself
214 for (auto iter = _nodes.rbegin(); iter != _nodes.rend(); ++iter) {
215 if (!iter->isValueServer)
216 continue;
217 for (auto &serverInfo : iter->serverInfos) {
218 serverInfo->isValueServer = true;
219 }
220 }
221
223
224 if (_useGPU) {
225 // Create the single CUDA stream on which all GPU computations and data
226 // transfers of this Evaluator are enqueued. The graph is evaluated in
227 // topological order, so ordering the operations by the stream is enough
228 // to guarantee correct results.
232 for (auto &info : _nodes) {
233 _evalContextCUDA.setConfig(info.absArg, cfg);
234 }
235 }
236}
237
238/// If there are servers with the same name that got de-duplicated in the
239/// `_nodes` list, we need to set their data tokens too. We find such nodes by
240/// visiting the servers of every known node.
242{
243 for (NodeInfo &info : _nodes) {
244 std::size_t iValueServer = 0;
245 for (RooAbsArg *server : info.absArg->servers()) {
246 if (server->isValueServer(*info.absArg)) {
247 auto *knownServer = info.serverInfos[iValueServer]->absArg;
248 if (knownServer->hasDataToken()) {
249 server->setDataToken(knownServer->dataToken());
250 }
251 ++iValueServer;
252 }
253 }
254 }
255}
256
257void Evaluator::setInput(std::string const &name, std::span<const double> inputArray, bool isOnDevice)
258{
259 if (isOnDevice && !_useGPU) {
260 throw std::runtime_error("Evaluator can only take device array as input in CUDA mode!");
261 }
262
263 // Check if "name" is used in the computation graph. If yes, add the span to
264 // the data map and set the node info accordingly.
265
266 auto found = _nodesMap.find(RooNameReg::ptr(name.c_str()));
267
268 if (found == _nodesMap.end())
269 return;
270
272
273 // Invalidate the caches that reducer nodes key on the input data, like the
274 // cached sum of event weights in RooNLLVarNew. The counter is global so
275 // that generation values can never alias between different Evaluators.
276 {
277 static std::atomic<std::size_t> nextInputGeneration{1};
278 const std::size_t gen = ++nextInputGeneration;
281 }
282
283 NodeInfo &info = *found->second;
284
285 info.fromArrayInput = true;
286 info.absArg->setDataToken(info.iNode);
287 info.outputSize = inputArray.size();
288
289 if (!_useGPU) {
290 _evalContextCPU.set(info.absArg, inputArray);
291 return;
292 }
293
294 if (info.outputSize <= 1) {
295 // Empty or scalar observables from the data don't need to be
296 // copied to the GPU.
297 _evalContextCPU.set(info.absArg, inputArray);
298 _evalContextCUDA.set(info.absArg, inputArray);
299 return;
300 }
301
302 // For simplicity, we put the data on both host and device for
303 // now. This could be optimized by inspecting the clients of the
304 // variable.
305 if (isOnDevice) {
306 _evalContextCUDA.set(info.absArg, inputArray);
307 auto gpuSpan = _evalContextCUDA.at(info.absArg);
308 info.buffer = _bufferManager->makeCpuBuffer(gpuSpan.size());
309 info.buffer->assignFromDevice(gpuSpan);
310 _evalContextCPU.set(info.absArg, {info.buffer->hostReadPtr(), gpuSpan.size()});
311 } else {
312 _evalContextCPU.set(info.absArg, inputArray);
313 auto cpuSpan = _evalContextCPU.at(info.absArg);
314 info.buffer = _bufferManager->makeGpuBuffer(cpuSpan.size());
315 info.buffer->assignFromHost(cpuSpan);
316 _evalContextCUDA.set(info.absArg, {info.buffer->deviceReadPtr(), cpuSpan.size()});
317 }
318}
319
321{
322 std::map<RooFit::Detail::DataKey, std::size_t> sizeMap;
323 for (auto &info : _nodes) {
324 if (info.fromArrayInput) {
325 sizeMap[info.absArg] = info.outputSize;
326 } else {
327 // any buffer for temporary results is invalidated by resetting the output sizes
328 info.buffer.reset();
329 }
330 }
331
332 auto outputSizeMap =
333 RooFit::BatchModeDataHelpers::determineOutputSizes(_topNode, [&](RooFit::Detail::DataKey key) -> int {
334 auto found = sizeMap.find(key);
335 return found != sizeMap.end() ? found->second : -1;
336 });
337
338 for (auto &info : _nodes) {
339 info.outputSize = outputSizeMap.at(info.absArg);
340 info.isDirty = true;
341 }
342
343 if (_useGPU) {
344 markGPUNodes();
345 }
346
348}
349
351{
352 for (auto &info : _nodes) {
353 // Nodes that were deleted before the Evaluator were unregistered
354 if (!info.isVariable && info.absArg) {
355 info.absArg->resetDataToken();
356 }
357 }
358 if (_cudaStream) {
360 }
361}
362
363/// Called from RooAbsArg::~RooAbsArg() when a node of the computation graph is
364/// deleted while this Evaluator is still alive. The Evaluator can't be used
365/// anymore after that, but its destructor must not touch the deleted node.
366/// For internal use only.
368{
369 auto found = _nodesMap.find(arg.namePtr());
370 if (found != _nodesMap.end() && found->second->absArg == &arg) {
371 found->second->absArg = nullptr;
372 }
373}
374
376{
377 using namespace Detail;
378
379 const std::size_t nOut = info.outputSize;
380
381 double *buffer = nullptr;
382 if (nOut == 1) {
383 buffer = &info.scalarBuffer;
384 if (_useGPU) {
385 _evalContextCUDA.set(node, {buffer, nOut});
386 }
387 } else {
388 // Advising to implement the CUDA evaluation makes only sense if the batch was not a scalar.
389 // Otherwise, there would be no speedup benefit.
390 if (!info.hasLogged && _useGPU) {
391 logMissingCudaSupport(*info.absArg);
392 info.hasLogged = true;
393 }
394 if (!info.buffer) {
395 info.buffer = info.copyAfterEvaluation ? _bufferManager->makePinnedBuffer(nOut, _cudaStream)
396 : _bufferManager->makeCpuBuffer(nOut);
397 }
398 buffer = info.buffer->hostWritePtr();
399 }
400 assignSpan(_evalContextCPU._currentOutput, {buffer, nOut});
401 _evalContextCPU.set(node, {buffer, nOut});
402 if (nOut > 1) {
404 }
405 if (info.isCategory) {
406 auto nodeAbsCategory = static_cast<RooAbsCategory const *>(node);
407 if (nOut == 1) {
408 buffer[0] = nodeAbsCategory->getCurrentIndex();
409 } else {
410 throw std::runtime_error("RooFit::Evaluator - non-scalar category values are not supported!");
411 }
412 } else {
413 auto nodeAbsReal = static_cast<RooAbsReal const *>(node);
414 nodeAbsReal->doEval(_evalContextCPU);
415 }
418 if (info.copyAfterEvaluation) {
419 // The deviceReadPtr() call triggers the copy of the result to the GPU.
420 // The copy is ordered by the CUDA stream, so GPU clients enqueued later
421 // will see the result without any further synchronization.
422 _evalContextCUDA.set(node, {info.buffer->deviceReadPtr(), nOut});
423 }
424}
425
426/// Process a variable in the computation graph. This is a separate non-inlined
427/// function such that we can see in performance profiles how long this takes.
429{
430 RooAbsArg *node = nodeInfo.absArg;
431 auto *var = static_cast<RooRealVar const *>(node);
432 if (nodeInfo.lastSetValCount != var->valueResetCounter()) {
433 nodeInfo.lastSetValCount = var->valueResetCounter();
434 for (NodeInfo *clientInfo : nodeInfo.clientInfos) {
435 clientInfo->isDirty = true;
436 }
437 computeCPUNode(node, nodeInfo);
438 nodeInfo.isDirty = false;
439 }
440}
441
442/// Process a category in the computation graph. This is a separate non-inlined
443/// function such that we can see in performance profiles how long this takes.
445{
446 RooAbsArg *node = nodeInfo.absArg;
447 auto *cat = static_cast<RooAbsCategory const *>(node);
448 if (nodeInfo.lastCatVal != cat->getCurrentIndex()) {
449 nodeInfo.lastCatVal = cat->getCurrentIndex();
450 for (NodeInfo *clientInfo : nodeInfo.clientInfos) {
451 clientInfo->isDirty = true;
452 }
453 computeCPUNode(node, nodeInfo);
454 nodeInfo.isDirty = false;
455 }
456}
457
458/// Flags all the clients of a given node dirty. This is a separate non-inlined
459/// function such that we can see in performance profiles how long this takes.
461{
462 for (NodeInfo *clientInfo : nodeInfo.clientInfos) {
463 clientInfo->isDirty = true;
464 }
465}
466
467/// Returns the value of the top node in the computation graph
468std::span<const double> Evaluator::run()
469{
472
474
475 // Discard leftover deferred actions in case a previous evaluation was
476 // aborted by an exception.
479
480 if (_useGPU) {
481 return getValHeterogeneous();
482 }
483
484 for (auto &nodeInfo : _nodes) {
485 if (!nodeInfo.fromArrayInput) {
486 if (nodeInfo.isVariable) {
487 processVariable(nodeInfo);
488 } else if (nodeInfo.isCategory) {
489 processCategory(nodeInfo);
490 } else {
491 if (nodeInfo.isDirty) {
492 setClientsDirty(nodeInfo);
493 computeCPUNode(nodeInfo.absArg, nodeInfo);
494 nodeInfo.isDirty = false;
495 }
496 }
497 }
498 }
499
500 for (auto &action : _evalContextCPU._deferredActions) {
501 action();
502 }
504
505 // return the final output
506 return _evalContextCPU.at(&_topNode);
507}
508
509/// Returns the value of the top node in the computation graph
510std::span<const double> Evaluator::getValHeterogeneous()
511{
512 // Iterate over the nodes in topological order. Like in the CPU-only path
513 // in run(), only dirty nodes are recomputed: a node is dirty if one of its
514 // value servers was recomputed in this evaluation, or if a variable
515 // changed its value since the last evaluation. Clean nodes keep the output
516 // buffers from their last evaluation, which is why the buffers persist
517 // across evaluations in this mode instead of being released as soon as
518 // the clients of a node are computed. During a minimization, most
519 // likelihood evaluations change only one or two parameters (e.g. for the
520 // numeric gradient and the Hessian), so skipping the clean nodes saves
521 // most of the GPU work and the kernel launches. The price is the device
522 // memory for the intermediate results of the whole computation graph,
523 // which is small compared to the input data for the usual graphs where
524 // most nodes are scalars.
525 //
526 // Nodes that are computed on the GPU only enqueue their computation on the
527 // single CUDA stream and return immediately, so independent CPU nodes that
528 // come later in the ordering naturally overlap with the GPU computations.
529 // Ordering by the stream guarantees that GPU nodes see the results of
530 // their GPU servers, and host-side reads of GPU results synchronize on the
531 // stream in the buffer implementation. Since no buffer is recycled within
532 // an evaluation, the results of clean nodes can't be overwritten by
533 // enqueued work either.
534 try {
535 for (auto &info : _nodes) {
536 if (info.fromArrayInput) {
537 continue;
538 }
539 if (info.isVariable) {
540 processVariable(info);
541 } else if (info.isCategory) {
542 processCategory(info);
543 } else if (info.isDirty) {
544 setClientsDirty(info);
545 if (info.computeInGPU) {
546 assignToGPU(info);
547 } else {
548 computeCPUNode(info.absArg, info);
549 }
550 info.isDirty = false;
551 }
552 }
553 } catch (...) {
554 // The evaluation was aborted, but readbacks that compute() calls
555 // deferred may still be armed. Deliver them now, while the destination
556 // memory in the nodes of the computation graph is guaranteed to be
557 // alive, so that no armed readback survives into a later evaluation.
558 // The node that threw stays dirty and gets recomputed in the next
559 // evaluation, together with its clients that were already flagged.
560 try {
562 } catch (...) {
563 // The stream is in an unrecoverable error state. The deferred
564 // readbacks are dropped together with the scratch memory when the
565 // stream gets deleted.
566 }
569 throw;
570 }
571
572 // Ensure that all enqueued GPU work has completed when run() returns. For
573 // the usual likelihood evaluations this is mostly a no-op, because the
574 // final reduction has synchronized the stream already. It also delivers
575 // the deferred readbacks like the evaluation error counters.
577
578 // Run the deferred actions now that all results have arrived on the host,
579 // e.g. the logging of evaluation errors that were counted on the GPU.
580 // Nodes evaluated on the CPU register their actions in the CPU context,
581 // so both contexts are drained.
582 for (auto *ctx : {&_evalContextCUDA, &_evalContextCPU}) {
583 for (auto &action : ctx->_deferredActions) {
584 action();
585 }
586 ctx->_deferredActions.clear();
587 }
588
589 // return the final value
591}
592
593/// Enqueue the computation of a node on the GPU.
595{
596 using namespace Detail;
597
598 auto node = static_cast<RooAbsReal const *>(info.absArg);
599
600 const std::size_t nOut = info.outputSize;
601
602 double *buffer = nullptr;
603 if (nOut == 1) {
604 buffer = &info.scalarBuffer;
605 _evalContextCPU.set(node, {buffer, nOut});
606 } else {
607 if (!info.buffer) {
608 info.buffer = info.copyAfterEvaluation ? _bufferManager->makePinnedBuffer(nOut, _cudaStream)
609 : _bufferManager->makeGpuBuffer(nOut);
610 }
611 buffer = info.buffer->deviceWritePtr();
612 }
613 assignSpan(_evalContextCUDA._currentOutput, {buffer, nOut});
614 _evalContextCUDA.set(node, {buffer, nOut});
615 node->doEval(_evalContextCUDA);
616 if (info.copyAfterEvaluation) {
617 // The hostReadPtr() call triggers the copy of the result to the host,
618 // which waits for the enqueued computation via the CUDA stream.
619 _evalContextCPU.set(node, {info.buffer->hostReadPtr(), nOut});
620 }
621}
622
623/// Decides which nodes are assigned to the GPU in a CUDA fit.
625{
626 // Decide which nodes get evaluated on the GPU: we select nodes that support
627 // CUDA evaluation and have at least one input of size greater than one.
628 for (auto &info : _nodes) {
629 info.computeInGPU = false;
630 if (!info.absArg->canComputeBatchWithCuda()) {
631 continue;
632 }
633 for (NodeInfo const *serverInfo : info.serverInfos) {
634 if (serverInfo->outputSize > 1) {
635 info.computeInGPU = true;
636 break;
637 }
638 }
639 }
640
641 // In a second pass, figure out which nodes need to copy over their results.
642 for (auto &info : _nodes) {
643 info.copyAfterEvaluation = false;
644 // scalar nodes don't need copying
645 if (!info.isScalar()) {
646 for (auto *clientInfo : info.clientInfos) {
647 if (info.computeInGPU != clientInfo->computeInGPU) {
648 info.copyAfterEvaluation = true;
649 break;
650 }
651 }
652 }
653 }
654}
655
656/// \brief Sets the number of threads to use for the evaluation of a single node.
657///
658/// With a value greater than one, the computation functions and reductions of
659/// the CPU backend process large batches multi-threaded, using up to the
660/// given number of threads. Nodes evaluated on the CPU with fewer events than
661/// an internal threshold are still evaluated single-threaded, so requesting
662/// multiple threads never introduces scheduling overhead for small fits.
663void Evaluator::setNThreads(int nThreads)
664{
665 for (auto &info : _nodes) {
666 if (info.isVariable) {
667 continue;
668 }
670 cfg.setNThreads(nThreads);
671 _evalContextCPU.setConfig(info.absArg, cfg);
672 }
673}
674
675/// Temporarily change the operation mode of a RooAbsArg until the
676/// Evaluator gets deleted.
678{
679 if (!_operModeChanges)
680 _operModeChanges = std::make_unique<ChangeOperModeRAII>();
681 _operModeChanges->change(arg, opMode);
682}
683
684// Change the operation modes of all RooAbsArgs in the computation graph.
685// The changes are reset when the returned RAII object goes out of scope.
686//
687// We also walk transitively through value clients of the nodes to cover any
688// node that RooAbsReal::doEval (the fallback scalar implementation) might
689// inadvertently propagate the ADirty mode to via its recursive restore: that
690// helper sets servers temporarily to AClean and then calls
691// setOperMode(oldOperMode) to restore, which recurses to value clients when
692// oldOperMode is ADirty. If we did not protect those clients here, any node
693// outside the computation graph that shares a fundamental (e.g. a parameter
694// like a RooRealVar) would be left permanently in ADirty after the first
695// minimization, dramatically slowing down later scalar evaluations.
696std::unique_ptr<ChangeOperModeRAII> Evaluator::setOperModes(RooAbsArg::OperMode opMode)
697{
698 auto out = std::make_unique<ChangeOperModeRAII>();
699 std::unordered_set<RooAbsArg *> visited;
700
701 std::vector<RooAbsArg *> queue;
702 queue.reserve(_nodes.size());
703 for (auto &info : _nodes) {
704 queue.push_back(info.absArg);
705 }
706
707 while (!queue.empty()) {
708 RooAbsArg *node = queue.back();
709 queue.pop_back();
710 if (!visited.insert(node).second)
711 continue;
712
713 out->change(node, opMode);
714
715 // Only follow value-client links: that is exactly the propagation path
716 // used by RooAbsArg::setOperMode with mode==ADirty.
717 if (opMode == RooAbsArg::ADirty) {
718 for (auto *client : node->valueClients()) {
719 queue.push_back(client);
720 }
721 }
722 }
723 return out;
724}
725
726void Evaluator::print(std::ostream &os)
727{
728 std::cout << "--- RooFit BatchMode evaluation ---\n";
729
730 std::vector<int> widths{9, 37, 20, 9, 10, 20};
731
732 auto printElement = [&](int iCol, auto const &t) {
733 const char separator = ' ';
734 os << separator << std::left << std::setw(widths[iCol]) << std::setfill(separator) << t;
735 os << "|";
736 };
737
738 auto printHorizontalRow = [&]() {
739 int n = 0;
740 for (int w : widths) {
741 n += w + 2;
742 }
743 for (int i = 0; i < n; i++) {
744 os << '-';
745 }
746 os << "|\n";
747 };
748
749 printHorizontalRow();
750
751 os << "|";
752 printElement(0, "Index");
753 printElement(1, "Name");
754 printElement(2, "Class");
755 printElement(3, "Size");
756 printElement(4, "From Data");
757 printElement(5, "1st value");
758 std::cout << "\n";
759
760 printHorizontalRow();
761
762 for (std::size_t iNode = 0; iNode < _nodes.size(); ++iNode) {
763 auto &nodeInfo = _nodes[iNode];
764 RooAbsArg *node = nodeInfo.absArg;
765
766 auto span = _evalContextCPU.at(node);
767
768 os << "|";
769 printElement(0, iNode);
770 printElement(1, node->GetName());
771 printElement(2, node->ClassName());
772 printElement(3, nodeInfo.outputSize);
773 printElement(4, nodeInfo.fromArrayInput);
774 printElement(5, span[0]);
775
776 std::cout << "\n";
777 }
778
779 printHorizontalRow();
780}
781
782/// Gets all the parameters of the RooAbsReal. This is in principle not
783/// necessary, because we can always ask the RooAbsReal itself, but the
784/// Evaluator has the cached information to get the answer quicker.
785/// Therefore, this is not meant to be used in general, just where it matters.
786/// \warning If we find another solution to get the parameters efficiently,
787/// this function might be removed without notice.
789{
790 RooArgSet parameters;
791 for (auto &nodeInfo : _nodes) {
792 if (nodeInfo.isValueServer && nodeInfo.absArg->isFundamental()) {
793 parameters.add(*nodeInfo.absArg);
794 }
795 }
796 // Just like in RooAbsArg::getParameters(), we sort the parameters alphabetically.
797 parameters.sort();
798 return parameters;
799}
800
801/// \brief Sets the offset mode for evaluation.
802///
803/// This function sets the offset mode for evaluation to the specified mode.
804/// It updates the offset mode for both CPU and CUDA evaluation contexts.
805///
806/// \param mode The offset mode to be set.
807///
808/// \note This function marks reducer nodes as dirty if the offset mode is
809/// changed, because only reducer nodes can use offsetting.
811{
813 return;
814
817
818 for (auto &nodeInfo : _nodes) {
819 if (nodeInfo.absArg->isReducerNode()) {
820 nodeInfo.isDirty = true;
821 }
822 }
823}
824
825} // namespace RooFit
#define oocoutI(o, a)
#define oocxcoutI(o, a)
Option_t Option_t TPoint TPoint const char mode
char name[80]
Definition TGX11.cxx:142
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:77
const TNamed * namePtr() const
De-duplicated pointer to this object's name.
Definition RooAbsArg.h:483
const RefCountList_t & valueClients() const
List of all value clients of this object. Value clients receive value updates.
Definition RooAbsArg.h:140
void setDataToken(std::size_t index, RooFit::Evaluator *evaluator=nullptr)
Sets the token for retrieving results in the BatchMode.
OperMode operMode() const
Query the operation mode of this node.
Definition RooAbsArg.h:399
A space to attach TBranches.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
Storage_t::size_type size() const
void sort(bool reverse=false)
Sort collection using std::sort and name comparison.
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:63
virtual void doEval(RooFit::EvalContext &) const
Base function for computing multiple values of a RooAbsReal.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
Minimal configuration struct to steer the evaluation of a single node with the RooBatchCompute librar...
void setCudaStream(CudaInterface::CudaStream *cudaStream)
void setNThreads(int nThreads)
Number of threads to use for CPU batch computations and reductions.
virtual void synchronizeCudaStream(CudaInterface::CudaStream *) const =0
Wait until all work that was enqueued on the stream has completed.
virtual std::unique_ptr< AbsBufferManager > createBufferManager() const =0
virtual CudaInterface::CudaStream * newCudaStream() const =0
virtual void deleteCudaStream(CudaInterface::CudaStream *) const =0
std::size_t _inputGeneration
std::vector< std::function< void()> > _deferredActions
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)
void enableVectorBuffers(bool enable)
OffsetMode _offsetMode
RooBatchCompute::Config config(RooAbsArg const *arg) const
void setConfig(RooAbsArg const *arg, RooBatchCompute::Config const &config)
std::span< double > _currentOutput
void resize(std::size_t n)
void print(std::ostream &os)
void setClientsDirty(NodeInfo &nodeInfo)
Flags all the clients of a given node dirty.
std::unique_ptr< ChangeOperModeRAII > setOperModes(RooAbsArg::OperMode opMode)
RooArgSet getParameters() const
Gets all the parameters of the RooAbsReal.
void setOffsetMode(RooFit::EvalContext::OffsetMode)
Sets the offset mode for evaluation.
void unregisterNode(RooAbsArg const &arg)
Called from RooAbsArg::~RooAbsArg() when a node of the computation graph is deleted while this Evalua...
void syncDataTokens()
If there are servers with the same name that got de-duplicated in the _nodes list,...
const bool _useGPU
Definition Evaluator.h:70
std::unordered_map< TNamed const *, NodeInfo * > _nodesMap
Definition Evaluator.h:76
std::unique_ptr< ChangeOperModeRAII > _operModeChanges
Definition Evaluator.h:77
std::vector< NodeInfo > _nodes
Definition Evaluator.h:75
bool _needToUpdateOutputSizes
Definition Evaluator.h:72
std::span< const double > getValHeterogeneous()
Returns the value of the top node in the computation graph.
std::span< const double > run()
Returns the value of the top node in the computation graph.
Evaluator(const RooAbsReal &absReal, bool useGPU=false)
Construct a new Evaluator.
void setNThreads(int nThreads)
Sets the number of threads to use for the evaluation of a single node.
void processVariable(NodeInfo &nodeInfo)
Process a variable in the computation graph.
void processCategory(NodeInfo &nodeInfo)
Process a category in the computation graph.
RooBatchCompute::CudaInterface::CudaStream * _cudaStream
Definition Evaluator.h:79
std::unique_ptr< RooBatchCompute::AbsBufferManager > _bufferManager
Definition Evaluator.h:68
void markGPUNodes()
Decides which nodes are assigned to the GPU in a CUDA fit.
void assignToGPU(NodeInfo &info)
Enqueue the computation of a node on the GPU.
void setInput(std::string const &name, std::span< const double > inputArray, bool isOnDevice)
RooFit::EvalContext _evalContextCUDA
Definition Evaluator.h:74
RooFit::EvalContext _evalContextCPU
Definition Evaluator.h:73
void computeCPUNode(const RooAbsArg *node, NodeInfo &info)
void setOperMode(RooAbsArg *arg, RooAbsArg::OperMode opMode)
Temporarily change the operation mode of a RooAbsArg until the Evaluator gets deleted.
RooAbsReal & _topNode
Definition Evaluator.h:69
static RooMsgService & instance()
Return reference to singleton instance.
static const TNamed * ptr(const char *stringPtr)
Return a unique TNamed pointer for given C++ string.
Variable that can be changed from the outside.
Definition RooRealVar.h:37
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:226
RVec< PromoteType< T > > log(const RVec< T > &v)
Definition RVec.hxx:1821
const Int_t n
Definition legend1.C:16
R__EXTERN RooBatchComputeInterface * dispatchCUDA
std::string cpuArchitectureName()
R__EXTERN RooBatchComputeInterface * dispatchCPU
This dispatch pointer points to an implementation of the compute library, provided one has been loade...
Architecture cpuArchitecture()
int initCPU()
Inspect hardware capabilities, and load the optimal library for RooFit computations.
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:73
@ FastEvaluations
void getSortedComputationGraph(RooAbsArg const &func, RooArgSet &out)
A struct used by the Evaluator to store information on the RooAbsArgs in the computation graph.
RooAbsArg * absArg
bool isScalar() const
std::size_t iNode
std::size_t lastSetValCount
std::vector< NodeInfo * > serverInfos
RooAbsArg::OperMode originalOperMode
std::size_t outputSize
std::vector< NodeInfo * > clientInfos
std::shared_ptr< RooBatchCompute::AbsBuffer > buffer