Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-15 09:11:50

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     inline operator AFloat * () const { return data(); }
0068     AFloat * data() const {return (* fBuffer) + fOffset;}
0069 
0070     class FakeIteratorBegin{
0071     private:
0072       AFloat& fBeginRet;
0073     public:
0074       FakeIteratorBegin(AFloat& x): fBeginRet(x){}
0075       AFloat& operator*()
0076       {
0077          return fBeginRet;
0078       }
0079     };
0080     FakeIteratorBegin begin(){      
0081       return FakeIteratorBegin(*((* fBuffer) + fOffset));
0082    }
0083 
0084 
0085 
0086     /** Return sub-buffer of size \p start starting at element \p offset. */
0087     TCpuBuffer GetSubBuffer(size_t offset, size_t start) const;
0088 
0089     AFloat & operator[](size_t i)       {return (*fBuffer.get())[fOffset + i];}
0090     AFloat   operator[](size_t i) const {return (*fBuffer.get())[fOffset + i];}
0091 
0092     /** Copy data from another buffer. No real copying is performed, only the
0093      *  data pointers are swapped. */
0094     void CopyFrom(const TCpuBuffer &);
0095     /** Copy data to another buffer. No real copying is performed, only the
0096      *  data pointers are swapped. */
0097     void CopyTo(TCpuBuffer &) const;
0098 
0099     /**
0100      * copy pointer from an external
0101      */
0102 
0103     size_t GetSize() const {return fSize;}
0104 
0105     size_t GetUseCount() const { return fBuffer.use_count(); }
0106 };
0107 
0108 } // namespace DNN
0109 } // namespace TMVA
0110 
0111 #endif