48#include <unordered_set>
56void assignSpan(std::span<T> &to, std::span<T>
const &from)
79 auto log = [](std::string_view message) {
84 log(
"using generic CPU library compiled with no vectorizations");
89 log(
"using CUDA computation library");
104 std::shared_ptr<RooBatchCompute::AbsBuffer>
buffer;
150 throw std::runtime_error(
"Can't create Evaluator in CUDA mode because RooBatchCompute CUDA could not be loaded!");
166 std::map<RooFit::Detail::DataKey, NodeInfo *>
nodeInfos;
170 std::size_t iNode = 0;
178 nodeInfo.originalOperMode = arg->operMode();
185 arg->setDataToken(iNode);
195 info.serverInfos.reserve(
info.absArg->servers().size());
206 _nodes.back().isValueServer =
true;
207 for (
auto iter =
_nodes.rbegin(); iter !=
_nodes.rend(); ++iter) {
208 if (!iter->isValueServer)
253 throw std::runtime_error(
"Evaluator can only take device array as input in CUDA mode!");
278 info.fromArrayInput =
true;
279 info.absArg->setDataToken(
info.iNode);
287 if (
info.outputSize <= 1) {
315 std::map<RooFit::Detail::DataKey, std::size_t>
sizeMap;
317 if (
info.fromArrayInput) {
327 auto found =
sizeMap.find(key);
328 return found !=
sizeMap.
end() ? found->second : -1;
346 if (!
info.isVariable) {
347 info.absArg->resetDataToken();
357 using namespace Detail;
359 const std::size_t
nOut =
info.outputSize;
361 double *buffer =
nullptr;
363 buffer = &
info.scalarBuffer;
371 <<
" could not be evaluated on the GPU because the class doesn't support it. "
372 "Consider requesting or implementing it to benefit from a speed up."
374 info.hasLogged =
true;
380 buffer =
info.buffer->hostWritePtr();
387 if (
info.isCategory) {
392 throw std::runtime_error(
"RooFit::Evaluator - non-scalar category values are not supported!");
400 if (
info.copyAfterEvaluation) {
413 auto *var =
static_cast<RooRealVar const *
>(node);
414 if (
nodeInfo.lastSetValCount != var->valueResetCounter()) {
415 nodeInfo.lastSetValCount = var->valueResetCounter();
430 if (
nodeInfo.lastCatVal != cat->getCurrentIndex()) {
431 nodeInfo.lastCatVal = cat->getCurrentIndex();
495 info.remClients =
info.clientInfos.size();
496 if (
info.buffer && !
info.fromArrayInput) {
510 if (!
info.fromArrayInput) {
511 if (
info.computeInGPU) {
558 for (
auto &
action :
ctx->_deferredActions) {
561 ctx->_deferredActions.clear();
571 using namespace Detail;
575 const std::size_t
nOut =
info.outputSize;
577 double *buffer =
nullptr;
579 buffer = &
info.scalarBuffer;
584 buffer =
info.buffer->deviceWritePtr();
589 if (
info.copyAfterEvaluation) {
602 info.computeInGPU =
false;
603 if (!
info.absArg->canComputeBatchWithCuda()) {
608 info.computeInGPU =
true;
616 info.copyAfterEvaluation =
false;
618 if (!
info.isScalar()) {
621 info.copyAfterEvaluation =
true;
639 if (
info.isVariable) {
672 auto out = std::make_unique<ChangeOperModeRAII>();
673 std::unordered_set<RooAbsArg *>
visited;
675 std::vector<RooAbsArg *> queue;
676 queue.reserve(
_nodes.size());
678 queue.push_back(
info.absArg);
681 while (!queue.empty()) {
684 if (!
visited.insert(node).second)
687 out->change(node,
opMode);
702 std::cout <<
"--- RooFit BatchMode evaluation ---\n";
704 std::vector<int>
widths{9, 37, 20, 9, 10, 20};
707 const char separator =
' ';
708 os << separator << std::left << std::setw(
widths[
iCol]) << std::setfill(separator) << t;
717 for (
int i = 0; i <
n; i++) {
736 for (std::size_t iNode = 0; iNode <
_nodes.size(); ++iNode) {
793 if (
nodeInfo.absArg->isReducerNode()) {
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 mode
const_iterator end() const
Common abstract base class for objects that represent a value and a "shape" in RooFit.
const RefCountList_t & valueClients() const
List of all value clients of this object. Value clients receive value updates.
A space to attach TBranches.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
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...
RooArgSet is a container object that can hold multiple RooAbsArg objects.
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)
std::span< const double > at(RooAbsArg const *arg, RooAbsArg const *caller=nullptr)
void resetVectorBuffers()
void enableVectorBuffers(bool enable)
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 syncDataTokens()
If there are servers with the same name that got de-duplicated in the _nodes list,...
std::unordered_map< TNamed const *, NodeInfo * > _nodesMap
std::unique_ptr< ChangeOperModeRAII > _operModeChanges
std::vector< NodeInfo > _nodes
bool _needToUpdateOutputSizes
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
std::unique_ptr< RooBatchCompute::AbsBufferManager > _bufferManager
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
RooFit::EvalContext _evalContextCPU
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.
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.
const char * GetName() const override
Returns name of object.
virtual const char * ClassName() const
Returns name of class to which the object belongs.
RVec< PromoteType< T > > log(const RVec< T > &v)
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...
void getSortedComputationGraph(RooAbsArg const &func, RooArgSet &out)
A struct used by the Evaluator to store information on the RooAbsArgs in the computation graph.
std::size_t lastSetValCount
std::vector< NodeInfo * > serverInfos
RooAbsArg::OperMode originalOperMode
std::vector< NodeInfo * > clientInfos
std::shared_ptr< RooBatchCompute::AbsBuffer > buffer
void decrementRemainingClients()
Check the servers of a node that has been computed and release its resources if they are no longer ne...