Logo ROOT  
Reference Guide
Buffers.cxx
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * Jonas Rembser, CERN 11/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
14
15#include <RooBatchCompute.h>
16
17#include <functional>
18#include <queue>
19#include <map>
20
21namespace ROOT {
22namespace Experimental {
23namespace Detail {
24
26public:
29 if (size != 1) throw std::runtime_error("ScalarBufferContainer can only be of size 1");
30 }
31 std::size_t size() const { return 1; }
32
33 double const *cpuReadPtr() const { return &_val; }
34 double const *gpuReadPtr() const { return &_val; }
35
36 double *cpuWritePtr() { return &_val; }
37 double *gpuWritePtr() { return &_val; }
38
39private:
40 double _val;
41};
42
44public:
46 CPUBufferContainer(std::size_t size) : _vec(size) {}
47 std::size_t size() const { return _vec.size(); }
48
49 double const *cpuReadPtr() const { return _vec.data(); }
50 double const *gpuReadPtr() const
51 {
52 throw std::bad_function_call();
53 return nullptr;
54 }
55
56 double *cpuWritePtr() { return _vec.data(); }
57 double *gpuWritePtr()
58 {
59 throw std::bad_function_call();
60 return nullptr;
61 }
62
63private:
64 std::vector<double> _vec;
65};
66
68public:
71 {
72 _data = static_cast<double*>(RooBatchCompute::dispatchCUDA->cudaMalloc(size * sizeof(double)));
73 _size = size;
74 }
76 {
77 if (_data)
79 }
82 GPUBufferContainer(GPUBufferContainer &&other) { *this = std::move(other); }
84 {
85 _data = other._data;
86 other._data = nullptr;
87 _size = other._size;
88 other._size = 0;
89 return *this;
90 }
91 std::size_t size() const { return _size; }
92
93 double const *cpuReadPtr() const
94 {
95 throw std::bad_function_call();
96 return nullptr;
97 }
98 double const *gpuReadPtr() const { return static_cast<double *>(_data); }
99
100 double *cpuWritePtr() const
101 {
102 throw std::bad_function_call();
103 return nullptr;
104 }
105 double *gpuWritePtr() const { return static_cast<double *>(_data); }
106
107private:
108 double *_data = nullptr;
109 std::size_t _size;
110};
111
113public:
116 {
117 _data = static_cast<double*>(RooBatchCompute::dispatchCUDA->cudaMallocHost(size * sizeof(double)));
118 _size = size;
120 }
123 PinnedBufferContainer(PinnedBufferContainer &&other) { *this = std::move(other); }
125 {
126 _data = other._data;
127 other._data = nullptr;
128 _size = other._size;
129 other._size = 0;
130 _gpuBuffer = std::move(other._gpuBuffer);
131 return *this;
132 }
133 std::size_t size() const { return _size; }
134
135 void setCudaStream(cudaStream_t *stream) { _cudaStream = stream; }
136
137 double const *cpuReadPtr() const
138 {
139
142 }
143
145 return static_cast<double *>(_data);
146 }
147 double const *gpuReadPtr() const
148 {
149
152 }
153
155 return _gpuBuffer.gpuReadPtr();
156 }
157
158 double *cpuWritePtr()
159 {
161 return static_cast<double *>(_data);
162 }
163 double *gpuWritePtr()
164 {
166 return _gpuBuffer.gpuWritePtr();
167 }
168
169private:
170 enum class LastAccessType { CPU_READ, GPU_READ, CPU_WRITE, GPU_WRITE };
171
172 double *_data = nullptr;
173 std::size_t _size;
175 cudaStream_t *_cudaStream = nullptr;
177};
178
179template <class Container>
180class BufferImpl : public AbsBuffer {
181public:
182 using QueuesMap = std::map<std::size_t, std::queue<Container>>;
183
184 BufferImpl(std::size_t size)
185 {
186 std::queue<Container> &q = queues()[size];
187 if (q.empty()) {
188 _vec = Container(size);
189 } else {
190 _vec = std::move(q.front());
191 q.pop();
192 }
193 }
194
195 static QueuesMap& queues() {
196 static QueuesMap queuesMap;
197 return queuesMap;
198 }
199
200 ~BufferImpl() override { queues().at(_vec.size()).emplace(std::move(_vec)); }
201
202 double const *cpuReadPtr() const override { return _vec.cpuReadPtr(); }
203 double const *gpuReadPtr() const override { return _vec.gpuReadPtr(); }
204
205 double *cpuWritePtr() override { return _vec.cpuWritePtr(); }
206 double *gpuWritePtr() override { return _vec.gpuWritePtr(); }
207
208 Container _vec;
209};
210
215
216std::unique_ptr<AbsBuffer> makeScalarBuffer()
217{
218 return std::make_unique<ScalarBuffer>(1);
219}
220std::unique_ptr<AbsBuffer> makeCpuBuffer(std::size_t size)
221{
222 return std::make_unique<CPUBuffer>(size);
223}
224std::unique_ptr<AbsBuffer> makeGpuBuffer(std::size_t size)
225{
226 return std::make_unique<GPUBuffer>(size);
227}
228std::unique_ptr<AbsBuffer> makePinnedBuffer(std::size_t size, cudaStream_t *stream)
229{
230 auto out = std::make_unique<PinnedBuffer>(size);
231 out->_vec.setCudaStream(stream);
232 return out;
233}
234
235} // end namespace Detail
236} // end namespace Experimental
237} // end namespace ROOT
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
float * q
Definition: THbookFile.cxx:89
std::map< std::size_t, std::queue< Container > > QueuesMap
Definition: Buffers.cxx:182
double const * gpuReadPtr() const override
Definition: Buffers.cxx:203
double const * cpuReadPtr() const override
Definition: Buffers.cxx:202
GPUBufferContainer(const GPUBufferContainer &)=delete
GPUBufferContainer & operator=(const GPUBufferContainer &)=delete
GPUBufferContainer(GPUBufferContainer &&other)
Definition: Buffers.cxx:82
GPUBufferContainer & operator=(GPUBufferContainer &&other)
Definition: Buffers.cxx:83
PinnedBufferContainer & operator=(const PinnedBufferContainer &)=delete
PinnedBufferContainer(const PinnedBufferContainer &)=delete
PinnedBufferContainer & operator=(PinnedBufferContainer &&other)
Definition: Buffers.cxx:124
PinnedBufferContainer(PinnedBufferContainer &&other)
Definition: Buffers.cxx:123
virtual void memcpyToCPU(void *, const void *, size_t, cudaStream_t *=nullptr)
virtual void memcpyToCUDA(void *, const void *, size_t, cudaStream_t *=nullptr)
std::unique_ptr< AbsBuffer > makeGpuBuffer(std::size_t size)
Definition: Buffers.cxx:224
std::unique_ptr< AbsBuffer > makeScalarBuffer()
Definition: Buffers.cxx:216
std::unique_ptr< AbsBuffer > makeCpuBuffer(std::size_t size)
Definition: Buffers.cxx:220
std::unique_ptr< AbsBuffer > makePinnedBuffer(std::size_t size, cudaStream_t *stream=nullptr)
Definition: Buffers.cxx:228
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
R__EXTERN RooBatchComputeInterface * dispatchCUDA