Logo ROOT  
Reference Guide
Batches.h
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * Emmanouil Michalainas, CERN 3 March 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/**
14\file Batches.h
15\class Batch
16\class Batches
17\ingroup RooBatchCompute
18
19These classes encapsulate the necessary data for the computations.
20They are lightweight objects designed to be passed by value and also flexible,
21so that they can contain data for every kind of compute function.
22**/
23#ifndef ROOFIT_BATCHCOMPUTE_BATCHES_H
24#define ROOFIT_BATCHCOMPUTE_BATCHES_H
25
27
28#include <stdint.h>
29
30namespace RooBatchCompute {
31
32#ifdef __CUDACC__
33// In the CPU case we use std::vector instead of fixed size arrays to pass
34// around data, so no maximum size variables are necessary.
35constexpr uint8_t maxParams = 8;
36constexpr uint8_t maxExtraArgs = 16;
37#endif // #ifdef __CUDACC__
38constexpr uint16_t bufferSize = 64;
39
40namespace RF_ARCH {
41
42class Batch {
43private:
44 double _scalar = 0;
45 const double *__restrict _array = nullptr;
46 bool _isVector = false;
47
48public:
49 Batch() = default;
50 inline Batch(InputArr array, bool isVector) : _scalar{array[0]}, _array{array}, _isVector{isVector} {}
51
52 __roodevice__ constexpr bool isItVector() const { return _isVector; }
53 inline void set(double scalar, InputArr array, bool isVector)
54 {
55 _scalar = scalar;
56 _array = array;
57 _isVector = isVector;
58 }
59 inline void advance(size_t _nEvents) { _array += _isVector * _nEvents; }
60#ifdef __CUDACC__
61 __roodevice__ constexpr double operator[](size_t i) const noexcept { return _isVector ? _array[i] : _scalar; }
62#else
63 constexpr double operator[](size_t i) const noexcept { return _array[i]; }
64#endif // #ifdef __CUDACC__
65}; // end class Batch
66
67/////////////////////////////////////////////////////////////////////////////////////////////////////////
68
69class Batches {
70private:
71#ifdef __CUDACC__
72 // In the GPU case, we used fixed-size buffers to pass around input arrays by value.
73 Batch _arrays[maxParams];
74 double _extraArgs[maxExtraArgs];
75#else
76 std::vector<Batch> _arrays;
77 std::vector<double> _extraArgs;
78#endif // #ifdef __CUDACC__
79 size_t _nEvents = 0;
80 uint8_t _nBatches = 0;
81 uint8_t _nExtraArgs = 0;
82
83public:
85
86 Batches(RestrictArr output, size_t nEvents, const VarVector &vars, const ArgVector &extraArgs = {},
87 double *buffer = nullptr);
88
89#ifdef __CUDACC__
90#else
91 // As we don't pass around Batches by value in the CPU case, delete copying
92 // and moving to it's not done accidentally.
93 Batches(const Batches &) = delete;
94 Batches &operator=(const Batches &) = delete;
95 Batches(Batches &&) = delete;
96 Batches &operator=(Batches &&) = delete;
97#endif // #ifdef __CUDACC__
98
99 __roodevice__ size_t getNEvents() const { return _nEvents; }
100 __roodevice__ uint8_t getNExtraArgs() const { return _nExtraArgs; }
101 __roodevice__ double extraArg(uint8_t i) const { return _extraArgs[i]; }
102 __roodevice__ void setExtraArg(uint8_t i, double val) { _extraArgs[i] = val; }
103 __roodevice__ Batch operator[](int batchIdx) const { return _arrays[batchIdx]; }
104 inline void setNEvents(size_t n) { _nEvents = n; }
105 inline void advance(size_t nEvents)
106 {
107 for (int i = 0; i < _nBatches; i++)
108 _arrays[i].advance(nEvents);
109 _output += nEvents;
110 }
111}; // end class Batches
112
113#ifdef __CUDACC__
114// In the GPU case, we have to pass the Batches object to the compute functions by value.
115using BatchesHandle = Batches;
116#else
118#endif // #ifdef __CUDACC__
119
120} // End namespace RF_ARCH
121} // end namespace RooBatchCompute
122#endif // #ifdef ROOFIT_BATCHCOMPUTE_BATCHES_H
#define __roodevice__
These classes encapsulate the necessary data for the computations.
constexpr double operator[](size_t i) const noexcept
Definition: Batches.h:63
const double *__restrict _array
Definition: Batches.h:45
void set(double scalar, InputArr array, bool isVector)
Definition: Batches.h:53
void advance(size_t _nEvents)
Definition: Batches.h:59
__roodevice__ constexpr bool isItVector() const
Definition: Batches.h:52
Batch(InputArr array, bool isVector)
Definition: Batches.h:50
__roodevice__ Batch operator[](int batchIdx) const
Definition: Batches.h:103
Batches(const Batches &)=delete
__roodevice__ void setExtraArg(uint8_t i, double val)
Definition: Batches.h:102
void advance(size_t nEvents)
Definition: Batches.h:105
std::vector< Batch > _arrays
Definition: Batches.h:76
std::vector< double > _extraArgs
Definition: Batches.h:77
__roodevice__ size_t getNEvents() const
Definition: Batches.h:99
__roodevice__ uint8_t getNExtraArgs() const
Definition: Batches.h:100
Batches(RestrictArr output, size_t nEvents, const VarVector &vars, const ArgVector &extraArgs={}, double *buffer=nullptr)
Construct a Batches object.
Batches & operator=(Batches &&)=delete
Batches & operator=(const Batches &)=delete
__roodevice__ double extraArg(uint8_t i) const
Definition: Batches.h:101
const Int_t n
Definition: legend1.C:16
Batches & BatchesHandle
Definition: Batches.h:117
Namespace for dispatching RooFit computations to various backends.
std::vector< RooSpan< const double > > VarVector
const double *__restrict InputArr
constexpr uint16_t bufferSize
Definition: Batches.h:38
std::vector< double > ArgVector
double *__restrict RestrictArr
static void output()