Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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>
43 class TCpuBuffer
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() = default;
62  TCpuBuffer(const TCpuBuffer &) = default;
63  TCpuBuffer( TCpuBuffer &&) = default;
64  TCpuBuffer & operator=(const TCpuBuffer &) = default;
65  TCpuBuffer & operator=( TCpuBuffer &&) = default;
66 
67  operator AFloat * () const {return (* fBuffer) + fOffset;}
68 
69  class FakeIteratorBegin{
70  private:
71  AFloat& fBeginRet;
72  public:
73  FakeIteratorBegin(AFloat& x): fBeginRet(x){}
74  AFloat& operator*()
75  {
76  return fBeginRet;
77  }
78  };
79  FakeIteratorBegin begin(){
80  return FakeIteratorBegin(*((* fBuffer) + fOffset));
81  }
82 
83 
84 
85  /** Return sub-buffer of size \p start starting at element \p offset. */
86  TCpuBuffer GetSubBuffer(size_t offset, size_t start) const;
87 
88  AFloat & operator[](size_t i) {return (*fBuffer.get())[fOffset + i];}
89  AFloat operator[](size_t i) const {return (*fBuffer.get())[fOffset + i];}
90 
91  /** Copy data from another buffer. No real copying is performed, only the
92  * data pointers are swapped. */
93  void CopyFrom(const TCpuBuffer &);
94  /** Copy data to another buffer. No real copying is performed, only the
95  * data pointers are swapped. */
96  void CopyTo(TCpuBuffer &) const;
97 
98  /**
99  * copy pointer from an external
100  */
101 
102  size_t GetSize() const {return fSize;}
103 
104  size_t GetUseCount() const { return fBuffer.use_count(); }
105 };
106 
107 } // namespace DNN
108 } // namespace TMVA
109 
110 #endif