Logo ROOT  
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
21#include <memory>
22
23namespace TMVA
24{
25namespace 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 */
42template<typename AFloat>
44{
45private:
46
47 size_t fSize;
48 size_t fOffset;
49 std::shared_ptr<AFloat *> fBuffer;
50
52 {
53 void operator()(AFloat ** pointer);
54 friend TCpuBuffer;
56
57public:
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;
66
67 operator AFloat * () const {return (* fBuffer) + fOffset;}
68
70 private:
71 AFloat& fBeginRet;
72 public:
74 AFloat& operator*()
75 {
76 return fBeginRet;
77 }
78 };
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
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
TCpuBuffer GetSubBuffer(size_t offset, size_t start) const
Return sub-buffer of size start starting at element offset.
Definition: CpuBuffer.cxx:47
size_t GetSize() const
copy pointer from an external
Definition: CpuBuffer.h:102
void CopyTo(TCpuBuffer &) const
Copy data to another buffer.
Definition: CpuBuffer.cxx:65
TCpuBuffer & operator=(const TCpuBuffer &)=default
AFloat & operator[](size_t i)
Definition: CpuBuffer.h:88
AFloat operator[](size_t i) const
Definition: CpuBuffer.h:89
TCpuBuffer(TCpuBuffer &&)=default
FakeIteratorBegin begin()
Definition: CpuBuffer.h:79
void CopyFrom(const TCpuBuffer &)
Copy data from another buffer.
Definition: CpuBuffer.cxx:57
std::shared_ptr< AFloat * > fBuffer
Definition: CpuBuffer.h:49
TCpuBuffer & operator=(TCpuBuffer &&)=default
TCpuBuffer(const TCpuBuffer &)=default
struct TMVA::DNN::TCpuBuffer::TDestructor fDestructor
size_t GetUseCount() const
Definition: CpuBuffer.h:104
Double_t x[n]
Definition: legend1.C:17
create variable transformations
void operator()(AFloat **pointer)
Definition: CpuBuffer.cxx:30