Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:10:52

0001 // @(#)root/tmva/tmva/dnn:$Id$
0002 // Author: Simon Pfreundschuh 12/08/16
0003 
0004 /*************************************************************************
0005  * Copyright (C) 2016, Simon Pfreundschuh                                *
0006  * All rights reserved.                                                  *
0007  *                                                                       *
0008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0010  *************************************************************************/
0011 
0012 /////////////////////////////////////////////////////////////
0013 // CPU Buffer interface class for the generic data loader. //
0014 /////////////////////////////////////////////////////////////
0015 
0016 #ifndef TMVA_DNN_ARCHITECTURES_CPU_CPUBUFFER
0017 #define TMVA_DNN_ARCHITECTURES_CPU_CPUBUFFER
0018 
0019 #include "TMVA/DNN/DataLoader.h"
0020 
0021 #include <memory>
0022 
0023 namespace TMVA
0024 {
0025 namespace DNN
0026 {
0027 
0028 /** TCpuBuffer
0029  *
0030  * Since the memory on the CPU is homogeneous, only one buffer class is required.
0031  * The host and device buffer classes are the same and copying between the host
0032  * and device buffer is achieved by simply swapping the memory pointers.
0033  *
0034  * Memory is handled as a shared pointer to a pointer of type AFloat, which is
0035  * the floating point type used for the implementation.
0036  *
0037  * Copying and assignment of TCpuBuffer objects performs only a shallow copy
0038  * meaning the underlying data is shared between those objects.
0039  *
0040  * \tparam AFloat The floating point type used for the computations.
0041  */
0042 template<typename AFloat>
0043 class TCpuBuffer
0044 {
0045 private:
0046 
0047    size_t fSize;
0048    size_t fOffset;
0049    std::shared_ptr<AFloat *> fBuffer;
0050 
0051    struct TDestructor
0052    {
0053        void operator()(AFloat ** pointer);
0054        friend TCpuBuffer;
0055    } fDestructor;
0056 
0057 public:
0058 
0059    /** Construct buffer to hold \p size numbers of type \p AFloat.*/
0060     TCpuBuffer(size_t size);
0061     TCpuBuffer() = default;
0062     TCpuBuffer(const TCpuBuffer  &) = default;
0063     TCpuBuffer(      TCpuBuffer &&) = default;
0064     TCpuBuffer & operator=(const TCpuBuffer  &) = default;
0065     TCpuBuffer & operator=(      TCpuBuffer &&) = default;
0066 
0067     operator AFloat * () const {return (* fBuffer) + fOffset;}
0068 
0069     class FakeIteratorBegin{
0070     private:
0071       AFloat& fBeginRet;
0072     public:
0073       FakeIteratorBegin(AFloat& x): fBeginRet(x){}
0074       AFloat& operator*()
0075       {
0076          return fBeginRet;
0077       }
0078     };
0079     FakeIteratorBegin begin(){      
0080       return FakeIteratorBegin(*((* fBuffer) + fOffset));
0081    }
0082 
0083 
0084 
0085     /** Return sub-buffer of size \p start starting at element \p offset. */
0086     TCpuBuffer GetSubBuffer(size_t offset, size_t start) const;
0087 
0088     AFloat & operator[](size_t i)       {return (*fBuffer.get())[fOffset + i];}
0089     AFloat   operator[](size_t i) const {return (*fBuffer.get())[fOffset + i];}
0090 
0091     /** Copy data from another buffer. No real copying is performed, only the
0092      *  data pointers are swapped. */
0093     void CopyFrom(const TCpuBuffer &);
0094     /** Copy data to another buffer. No real copying is performed, only the
0095      *  data pointers are swapped. */
0096     void CopyTo(TCpuBuffer &) const;
0097 
0098     /**
0099      * copy pointer from an external
0100      */
0101 
0102     size_t GetSize() const {return fSize;}
0103 
0104     size_t GetUseCount() const { return fBuffer.use_count(); }
0105 };
0106 
0107 } // namespace DNN
0108 } // namespace TMVA
0109 
0110 #endif