Apache Singa
A General Distributed Deep Learning Library
memory.h
1 
19 #ifndef SINGA_CORE_MEMORY_H_
20 #define SINGA_CORE_MEMORY_H_
21 
22 #include <mutex>
23 #include <atomic>
24 #include "singa/proto/core.pb.h"
25 #include "singa/singa_config.h"
26 
27 #ifdef USE_CUDA
28 #include "cnmem.h"
29 #endif
30 
31 
32 namespace singa {
33 
35 class VirtualMemory {};
36 
38  public:
39  virtual void Malloc(void** ptr, const size_t size) = 0;
40  virtual void Free(void* ptr) = 0;
41 
43  virtual std::pair<size_t, size_t> GetMemUsage() {
44  return std::make_pair(0u, 0u);
45  }
46  virtual ~DeviceMemPool(){};
47 
48  protected:
49  size_t usage_;
50 // size_t init_size_ = 0, max_size_ = 0;
51 };
52 
53 #ifdef USE_CUDA
54 class CnMemPool : public DeviceMemPool {
55  public:
56  // Create the mem pool by setting the devices [0, numDevices), and
57  // initial pool size (MB), and max pool size (no effect currently).
58  CnMemPool(int numDevices = 1, size_t init_size = 256, size_t max_size = 0);
59  CnMemPool(const MemPoolConf& conf);
60 
61  void Malloc(void** ptr, const size_t size);
62  void Free(void* ptr);
63 
64  std::pair<size_t, size_t> GetMemUsage() override;
65 
66  // release all memory and set cnmem manager to unintialized
67  ~CnMemPool();
68 
69  protected:
70  void Init();
71 
72 
73  private:
74 
75  MemPoolConf conf_;
76  // whether the (global) memory pool has been initialized
77  bool initialized_ = false;
78  // lock on the initialized variable
79  std::mutex mtx_;
80 
81  static std::atomic<int> pool_count;
82 };
83 
84 class CudaMemPool : public DeviceMemPool {
85  public:
86  void Malloc(void** ptr, const size_t size) override;
87  void Free(void* ptr) override;
88 };
89 #endif
90 } // namespace singa
91 #endif // SINGA_CORE_MEMORY_H_
Manage device memory pool including garbage collection, memory opt.
Definition: memory.h:35
virtual std::pair< size_t, size_t > GetMemUsage()
Return a pair for free and total memory managed by this pool.
Definition: memory.h:43
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: common.h:48
Definition: memory.h:37