Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooBatchCompute.h
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * Emmanouil Michalainas, CERN 6 January 2021
5 *
6 * Copyright (c) 2021, 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_BATCHCOMPUTE_ROOBATCHCOMPUTE_H
14#define ROOFIT_BATCHCOMPUTE_ROOBATCHCOMPUTE_H
15
16#include <ROOT/RSpan.hxx>
17
18#include <DllImport.h> //for R__EXTERN, needed for windows
19
20#include <cstddef>
21#include <initializer_list>
22#include <memory>
23#include <string>
24
25/**
26 * Namespace for dispatching RooFit computations to various backends.
27 *
28 * This namespace contains an interface for providing high-performance computation functions for use in
29 * RooAbsReal::doEval(), see RooBatchComputeInterface.
30 *
31 * Furthermore, several implementations of this interface can be created, which reside in RooBatchCompute::RF_ARCH,
32 * where RF_ARCH may be replaced by the architecture that this implementation targets, e.g. SSE, AVX, etc.
33 *
34 * Using the pointer RooBatchCompute::dispatch, a computation request can be dispatched to the fastest backend that is
35 * available on a specific platform.
36 */
37namespace RooBatchCompute {
38
39namespace CudaInterface {
40class CudaStream;
41} // namespace CudaInterface
42
43typedef std::span<const std::span<const double>> VarSpan;
44typedef std::span<double> ArgSpan;
45typedef const double *__restrict InputArr;
46
47constexpr std::size_t bufferSize = 64;
48
49int initCPU();
50int initCUDA();
51
52/// Minimal configuration struct to steer the evaluation of a single node with
53/// the RooBatchCompute library.
54class Config {
55public:
56 bool useCuda() const { return _cudaStream != nullptr; }
59
60 /// Number of threads to use for CPU batch computations and reductions.
61 /// Values smaller than two mean single-threaded evaluation. The CPU
62 /// backend may still evaluate small batches single-threaded to avoid
63 /// scheduling overhead.
65 int nThreads() const { return _nThreads; }
66
67private:
69 int _nThreads = 1;
70};
71
72enum class Architecture {
73 AVX512,
74 AVX2,
75 AVX,
76 SSE4,
77 GENERIC,
78 CUDA
79};
80
125
127 double nllSum = 0.0;
128 double nllSumCarry = 0.0;
129 std::size_t nInfiniteValues = 0;
130 std::size_t nNonPositiveValues = 0;
131 std::size_t nNaNValues = 0;
132};
133
135public:
136 virtual ~AbsBuffer() = default;
137
138 virtual double const *hostReadPtr() const = 0;
139 virtual double const *deviceReadPtr() const = 0;
140
141 virtual double *hostWritePtr() = 0;
142 virtual double *deviceWritePtr() = 0;
143
144 virtual void assignFromHost(std::span<const double> input) = 0;
145 virtual void assignFromDevice(std::span<const double> input) = 0;
146};
147
149public:
150 virtual ~AbsBufferManager() = default;
151
152 virtual std::unique_ptr<AbsBuffer> makeScalarBuffer() = 0;
153 virtual std::unique_ptr<AbsBuffer> makeCpuBuffer(std::size_t size) = 0;
154 virtual std::unique_ptr<AbsBuffer> makeGpuBuffer(std::size_t size) = 0;
155 virtual std::unique_ptr<AbsBuffer>
156 makePinnedBuffer(std::size_t size, CudaInterface::CudaStream *stream = nullptr) = 0;
157};
158
159/**
160 * \class RooBatchComputeInterface
161 * \ingroup roofit_dev_docs_batchcompute
162 * \brief The interface which should be implemented to provide optimised computation functions for implementations of
163 * RooAbsReal::doEval().
164 *
165 * The class RooBatchComputeInterface provides the mechanism for external modules (like RooFit) to call
166 * functions from the library. The power lies in the virtual functions that can resolve to different
167 * implementations for the functionality; for example, calling a function through dispatchCuda
168 * will resolve to efficient CUDA implementations.
169 *
170 * This interface contains the signatures of the compute functions of every PDF that has an optimised implementation
171 * available. These are the functions that perform the actual computations in batches.
172 *
173 * Several implementations of this interface may be provided, e.g. SSE, AVX, AVX2 etc. At run time, the fastest
174 * implementation of this interface is selected, and using a virtual call, the computation is dispatched to the best
175 * backend.
176 *
177 * \see RooBatchCompute::dispatch, RooBatchComputeClass, RF_ARCH
178 */
180public:
181 virtual ~RooBatchComputeInterface() = default;
182
183 /// Compute the values for a batch of events.
184 ///
185 /// The extra args (the last parameter) are read-only inputs for all
186 /// computers except `NormalizedPdf` and `LogNormalizedPdf`, which use the
187 /// first three as output parameters for their evaluation error counters.
188 /// In the CUDA implementation, these outputs are read back from the device
189 /// *asynchronously*: they only arrive in the caller's span with the next
190 /// synchronizeCudaStream() call on the stream of the passed config. The
191 /// memory backing the extra args of such a call must therefore stay valid
192 /// until that synchronization, so it must not live on the caller's stack.
193 virtual void compute(Config const &cfg, Computer, std::span<double> output, VarSpan, ArgSpan) = 0;
194
195 virtual double reduceSum(Config const &cfg, InputArr input, size_t n) = 0;
196 /// Reduce the probabilities to the negative log-likelihood. With
197 /// `logProbas`, the probabilities are given as logarithms.
198 virtual ReduceNLLOutput reduceNLL(Config const &cfg, std::span<const double> probas, std::span<const double> weights,
199 std::span<const double> offsetProbas, bool logProbas) = 0;
200
201 virtual Architecture architecture() const = 0;
202 virtual std::string architectureName() const = 0;
203
204 virtual std::unique_ptr<AbsBufferManager> createBufferManager() const = 0;
205
208 /// Wait until all work that was enqueued on the stream has completed.
210};
211
212/**
213 * This dispatch pointer points to an implementation of the compute library, provided one has been loaded.
214 * Using a virtual call, computation requests are dispatched to backends with architecture-specific functions
215 * such as SSE, AVX, AVX2, etc.
216 *
217 * \see RooBatchComputeInterface, RooBatchComputeClass, RF_ARCH
218 */
221
223{
224 return dispatchCPU->architecture();
225}
226
227inline std::string cpuArchitectureName()
228{
230}
231
232inline void compute(Config cfg, Computer comp, std::span<double> output, VarSpan vars, ArgSpan extraArgs = {})
233{
234 auto dispatch = cfg.useCuda() ? dispatchCUDA : dispatchCPU;
235 dispatch->compute(cfg, comp, output, vars, extraArgs);
236}
237
238/// It is not possible to construct a std::span directly from an initializer
239/// list (probably it will be with C++26). That's why we need an explicit
240/// overload for this.
241inline void compute(Config cfg, Computer comp, std::span<double> output,
242 std::initializer_list<std::span<const double>> vars, ArgSpan extraArgs = {})
243{
244 compute(cfg, comp, output, VarSpan{vars.begin(), vars.end()}, extraArgs);
245}
246
247inline double reduceSum(Config cfg, InputArr input, size_t n)
248{
249 auto dispatch = cfg.useCuda() ? dispatchCUDA : dispatchCPU;
250 return dispatch->reduceSum(cfg, input, n);
251}
252
253inline ReduceNLLOutput reduceNLL(Config cfg, std::span<const double> probas, std::span<const double> weights,
254 std::span<const double> offsetProbas, bool logProbas = false)
255{
256 auto dispatch = cfg.useCuda() ? dispatchCUDA : dispatchCPU;
257 return dispatch->reduceNLL(cfg, probas, weights, offsetProbas, logProbas);
258}
259
260std::string getBatchComputeChoice();
261void setBatchComputeChoice(std::string const &value);
262
263} // End namespace RooBatchCompute
264
265#endif
#define R__EXTERN
Definition DllImport.h:26
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
virtual std::unique_ptr< AbsBuffer > makeScalarBuffer()=0
virtual ~AbsBufferManager()=default
virtual std::unique_ptr< AbsBuffer > makeCpuBuffer(std::size_t size)=0
virtual std::unique_ptr< AbsBuffer > makeGpuBuffer(std::size_t size)=0
virtual std::unique_ptr< AbsBuffer > makePinnedBuffer(std::size_t size, CudaInterface::CudaStream *stream=nullptr)=0
virtual double const * deviceReadPtr() const =0
virtual ~AbsBuffer()=default
virtual void assignFromHost(std::span< const double > input)=0
virtual double const * hostReadPtr() const =0
virtual double * deviceWritePtr()=0
virtual void assignFromDevice(std::span< const double > input)=0
virtual double * hostWritePtr()=0
Minimal configuration struct to steer the evaluation of a single node with the RooBatchCompute librar...
void setCudaStream(CudaInterface::CudaStream *cudaStream)
CudaInterface::CudaStream * _cudaStream
void setNThreads(int nThreads)
Number of threads to use for CPU batch computations and reductions.
CudaInterface::CudaStream * cudaStream() const
The interface which should be implemented to provide optimised computation functions for implementati...
virtual double reduceSum(Config const &cfg, InputArr input, size_t n)=0
virtual void synchronizeCudaStream(CudaInterface::CudaStream *) const =0
Wait until all work that was enqueued on the stream has completed.
virtual std::string architectureName() const =0
virtual ReduceNLLOutput reduceNLL(Config const &cfg, std::span< const double > probas, std::span< const double > weights, std::span< const double > offsetProbas, bool logProbas)=0
Reduce the probabilities to the negative log-likelihood.
virtual std::unique_ptr< AbsBufferManager > createBufferManager() const =0
virtual CudaInterface::CudaStream * newCudaStream() const =0
virtual void deleteCudaStream(CudaInterface::CudaStream *) const =0
virtual Architecture architecture() const =0
virtual void compute(Config const &cfg, Computer, std::span< double > output, VarSpan, ArgSpan)=0
Compute the values for a batch of events.
const Int_t n
Definition legend1.C:16
Namespace for dispatching RooFit computations to various backends.
R__EXTERN RooBatchComputeInterface * dispatchCUDA
std::span< double > ArgSpan
std::string cpuArchitectureName()
std::string getBatchComputeChoice()
void compute(Config cfg, Computer comp, std::span< double > output, VarSpan vars, ArgSpan extraArgs={})
R__EXTERN RooBatchComputeInterface * dispatchCPU
This dispatch pointer points to an implementation of the compute library, provided one has been loade...
void setBatchComputeChoice(std::string const &value)
constexpr std::size_t bufferSize
ReduceNLLOutput reduceNLL(Config cfg, std::span< const double > probas, std::span< const double > weights, std::span< const double > offsetProbas, bool logProbas=false)
double reduceSum(Config cfg, InputArr input, size_t n)
Architecture cpuArchitecture()
const double *__restrict InputArr
std::span< const std::span< const double > > VarSpan
int initCPU()
Inspect hardware capabilities, and load the optimal library for RooFit computations.