Logo ROOT   6.10/09
Reference Guide
CpuBuffer.h
Go to the documentation of this file.
1 // @(#)root/tmva/tmva/dnn:$Id$
2 // Author: Simon Pfreundschuh 12/08/16
3 
4 /*************************************************************************
5  * Copyright (C) 2016, Simon Pfreundschuh *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 /////////////////////////////////////////////////////////////
13 // CPU Buffer interface class for the generic data loader. //
14 /////////////////////////////////////////////////////////////
15 
16 #ifndef TMVA_DNN_ARCHITECTURES_CPU_CPUBUFFER
17 #define TMVA_DNN_ARCHITECTURES_CPU_CPUBUFFER
18 
19 #include "TMVA/DNN/DataLoader.h"
20 #include <vector>
21 #include <memory>
22 
23 namespace TMVA
24 {
25 namespace DNN
26 {
27 
28 /** TCpuBuffer
29  *
30  * Since the memory on the CPU is homogeneous, only one buffer class is required.
31  * The host and device buffer classes are the same and copying between the host
32  * and device buffer is achieved by simply swapping the memory pointers.
33  *
34  * Memory is handled as a shared pointer to a pointer of type AFloat, which is
35  * the floating point type used for the implementation.
36  *
37  * Copying and assignment of TCpuBuffer objects performs only a shallow copy
38  * meaning the underlying data is shared between those objects.
39  *
40  * \tparam AFloat The floating point type used for the computations.
41  */
42 template<typename AFloat>
44 {
45 private:
46 
47  size_t fSize;
48  size_t fOffset;
49  std::shared_ptr<AFloat *> fBuffer;
50 
51  struct TDestructor
52  {
53  void operator()(AFloat ** pointer);
54  friend TCpuBuffer;
55  } fDestructor;
56 
57 public:
58 
59  /** Construct buffer to hold \p size numbers of type \p AFloat.*/
60  TCpuBuffer(size_t size);
61  TCpuBuffer(const TCpuBuffer &) = default;
62  TCpuBuffer( TCpuBuffer &&) = default;
63  TCpuBuffer & operator=(const TCpuBuffer &) = default;
64  TCpuBuffer & operator=( TCpuBuffer &&) = default;
65 
66  operator AFloat * () const {return (* fBuffer) + fOffset;}
67 
68  /** Return subbuffer of siez \p start starting at element \p offset. */
69  TCpuBuffer GetSubBuffer(size_t offset, size_t start);
70 
71  AFloat & operator[](size_t i) {return (*fBuffer.get())[fOffset + i];}
72  AFloat operator[](size_t i) const {return (*fBuffer.get())[fOffset + i];}
73 
74  /** Copy data from another buffer. No real copying is performed, only the
75  * data pointers are swapped. */
76  void CopyFrom(TCpuBuffer &);
77  /** Copy data to another buffer. No real copying is performed, only the
78  * data pointers are swapped. */
79  void CopyTo(TCpuBuffer &);
80 
81  size_t GetSize() const {return fSize;}
82 };
83 
84 } // namespace DNN
85 } // namespace TMVA
86 
87 #endif
88 
void CopyTo(TCpuBuffer &)
Copy data to another buffer.
Definition: CpuBuffer.cxx:65
std::shared_ptr< AFloat * > fBuffer
Definition: CpuBuffer.h:49
void operator()(AFloat **pointer)
Definition: CpuBuffer.cxx:30
AFloat operator[](size_t i) const
Definition: CpuBuffer.h:72
AFloat & operator[](size_t i)
Definition: CpuBuffer.h:71
TCpuBuffer & operator=(const TCpuBuffer &)=default
TCpuBuffer.
Definition: CpuBuffer.h:43
Abstract ClassifierFactory template that handles arbitrary types.
void CopyFrom(TCpuBuffer &)
Copy data from another buffer.
Definition: CpuBuffer.cxx:58
TCpuBuffer GetSubBuffer(size_t offset, size_t start)
Return subbuffer of siez start starting at element offset.
Definition: CpuBuffer.cxx:48
size_t GetSize() const
Definition: CpuBuffer.h:81
struct TMVA::DNN::TCpuBuffer::TDestructor fDestructor