# Utils

## Memory Handle

namespace `paddle`

  Typedefs

  typedef std::shared_ptr<MemoryHandle> `MemoryHandlePtr`
  typedef std::shared_ptr<CpuMemoryHandle> `CpuMemHandlePtr`
  typedef std::shared_ptr<GpuMemoryHandle> `GpuMemHandlePtr`
  class `MemoryHandle`

    Subclassed by paddle::CpuMemoryHandle, paddle::GpuMemoryHandle

    Public Functions

    void *`getBuf`() const
    size_t `getSize`() const
    size_t `getAllocSize`() const

    Protected Functions

    `MemoryHandle`(size_t size)

      Calculate the actual allocation size according to the required size.

    virtual `~MemoryHandle`()

    Protected Attributes

    PoolAllocator *`allocator_`
    size_t `size_`
    size_t `allocSize_`
    int `deviceId_`
    void *`buf_`
  class `GpuMemoryHandle`
    #include <MemoryHandle.h>

    Wrapper class for raw gpu memory handle.

    The raw handle will be released at destructor

    Inherits from paddle::MemoryHandle

    Public Functions

    `GpuMemoryHandle`(size_t size)
    `~GpuMemoryHandle`()
  class `CpuMemoryHandle`
    #include <MemoryHandle.h>

    Wrapper class for raw cpu memory handle.

    The raw handle will be released at destructor

    Inherits from paddle::MemoryHandle

    Public Functions

    `CpuMemoryHandle`(size_t size)
    `~CpuMemoryHandle`()

namespace `paddle`
  class `Allocator`
    #include <Allocator.h>

    Allocator base class.

    This is the base class of all Allocator class.

    Subclassed by paddle::CpuAllocator, paddle::CudaHostAllocator, paddle::GpuAllocator

    Public Functions

    virtual `~Allocator`()
    virtual void *`alloc`(size_t size) = 0
    virtual void `free`(void *ptr) = 0
    virtual std::string `getName`() = 0
  class `CpuAllocator`
    #include <Allocator.h>

    CPU allocator implementation.

    Inherits from paddle::Allocator

    Public Functions

    `~CpuAllocator`()
    virtual void *`alloc`(size_t size)

      Aligned allocation on CPU.

      Return
        Pointer to the allocated memory
      Parameters
        - `size`: Size to be allocated.
    virtual void `free`(void *ptr)

      Free the memory space.

      Parameters
        - `ptr`: Pointer to be free.
    virtual std::string `getName`()
  class `GpuAllocator`
    #include <Allocator.h>

    GPU allocator implementation.

    Inherits from paddle::Allocator

    Public Functions

    `~GpuAllocator`()
    virtual void *`alloc`(size_t size)

      Allocate GPU memory.

      Return
        Pointer to the allocated memory
      Parameters
        - `size`: Size to be allocated.
    virtual void `free`(void *ptr)

      Free the GPU memory.

      Parameters
        - `ptr`: Pointer to be free.
    virtual std::string `getName`()
  class `CudaHostAllocator`
    #include <Allocator.h>

    CPU pinned memory allocator implementation.

    Inherits from paddle::Allocator

    Public Functions

    `~CudaHostAllocator`()
    virtual void *`alloc`(size_t size)

      Allocate pinned memory.

      Return
        Pointer to the allocated memory
      Parameters
        - `size`: Size to be allocated.
    virtual void `free`(void *ptr)

      Free the pinned memory.

      Parameters
        - `ptr`: Pointer to be free.
    virtual std::string `getName`()

namespace `paddle`
  class `PoolAllocator`
    #include <PoolAllocator.h>

    Memory pool allocator implementation.

    Public Functions

    `PoolAllocator`( Allocator *allocator, size_t sizeLimit = 0, const std::string &name = "pool")

      constructor.

      Parameters
        - `allocator`: a Allocator object.
        - `sizeLimit`: The maximum size memory can be managed, if sizeLimit == 0, the pool allocator is a simple wrapper of allocator.
    `~PoolAllocator`()

      destructor.

    void *`alloc`(size_t size)
    void `free`(void *ptr, size_t size)
    std::string `getName`()

    Private Functions

    void `freeAll`()
    void `printAll`()

    Private Members

    std::unique_ptr<Allocator> `allocator_`
    std::mutex `mutex_`
    std::unordered_map<size_t, std::vector<void *>> `pool_`
    size_t `sizeLimit_`
    size_t `poolMemorySize_`
    std::string `name_`

namespace `paddle`
  class `StorageEngine`
    #include <Storage.h>

    Storage manager for multiple devices.

    Public Functions

    PoolAllocator *`getGpuAllocator`(int deviceId)

      Return
        return one gpu allocator by deviceId
    PoolAllocator *`getCpuAllocator`()

      Return
        return cpu allocator

    Public Static Functions

    StorageEngine *`singleton`()

      Return
        Storage singleton

    Protected Functions

    `StorageEngine`()
    `~StorageEngine`()

    Protected Attributes

    RWLock `lock_`
    std::vector<PoolAllocator *> `gpuAllocator_`
    PoolAllocator *`cpuAllocator_`