Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 std::size_t maxParams = 8;
36constexpr std::size_t maxExtraArgs = 16;
37#endif // #ifdef __CUDACC__
38constexpr std::size_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(std::size_t _nEvents) { _array += _isVector * _nEvents; }
60#ifdef __CUDACC__
61 __roodevice__ constexpr double operator[](std::size_t i) const noexcept { return _isVector ? _array[i] : _scalar; }
62#else
63 constexpr double operator[](std::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 double *const _extraArgs = nullptr;
78#endif // #ifdef __CUDACC__
79 std::size_t _nEvents = 0;
80 std::size_t _nBatches = 0;
81 std::size_t _nExtraArgs = 0;
82
83public:
85
86 Batches(RestrictArr output, std::size_t nEvents, const VarVector &vars, 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 so 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__ std::size_t getNEvents() const { return _nEvents; }
100 __roodevice__ std::size_t getNExtraArgs() const { return _nExtraArgs; }
101 __roodevice__ double extraArg(std::size_t i) const { return _extraArgs[i]; }
102 __roodevice__ void setExtraArg(std::size_t i, double val) { _extraArgs[i] = val; }
103 __roodevice__ Batch operator[](int batchIdx) const { return _arrays[batchIdx]; }
104 inline void setNEvents(std::size_t n) { _nEvents = n; }
105 inline void advance(std::size_t nEvents)
106 {
107 for (std::size_t 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.
void advance(std::size_t _nEvents)
Definition Batches.h:59
const double *__restrict _array
Definition Batches.h:45
void set(double scalar, InputArr array, bool isVector)
Definition Batches.h:53
constexpr double operator[](std::size_t i) const noexcept
Definition Batches.h:63
__roodevice__ constexpr bool isItVector() const
Definition Batches.h:52
Batch(InputArr array, bool isVector)
Definition Batches.h:50
Batches(RestrictArr output, std::size_t nEvents, const VarVector &vars, ArgVector &extraArgs, double *buffer=nullptr)
__roodevice__ std::size_t getNEvents() const
Definition Batches.h:99
__roodevice__ Batch operator[](int batchIdx) const
Definition Batches.h:103
Batches(const Batches &)=delete
__roodevice__ std::size_t getNExtraArgs() const
Definition Batches.h:100
void advance(std::size_t nEvents)
Definition Batches.h:105
__roodevice__ void setExtraArg(std::size_t i, double val)
Definition Batches.h:102
std::vector< Batch > _arrays
Definition Batches.h:76
__roodevice__ double extraArg(std::size_t i) const
Definition Batches.h:101
void setNEvents(std::size_t n)
Definition Batches.h:104
Batches & operator=(Batches &&)=delete
Batches & operator=(const Batches &)=delete
const Int_t n
Definition legend1.C:16
Namespace for dispatching RooFit computations to various backends.
std::vector< RooSpan< const double > > VarVector
constexpr std::size_t bufferSize
Definition Batches.h:38
const double *__restrict InputArr
std::vector< double > ArgVector
double *__restrict RestrictArr
static void output()