HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CE_MemoryPool.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: CE_MemoryPool.h ( CE Library, C++)
7  *
8  * COMMENTS: Compute Engine Memory Pool.
9  */
10 
11 #ifndef __CE_MemoryPool__
12 #define __CE_MemoryPool__
13 
14 #include "CE_API.h"
15 
16 #ifdef CE_ENABLED
17 
18 #include <SYS/SYS_Types.h>
19 
20 #include <UT/UT_Array.h>
21 #include <UT/UT_LRUCache.h>
22 #include <UT/UT_Map.h>
23 #include <UT/UT_Lock.h>
24 #include <iosfwd>
25 
26 class CE_Context;
27 
28 /// This class implements a memory pool for OpenCL buffer objects, using an
29 /// LRUCache to limit the size of the pool.
31 {
32 public:
33  /// Initialize the pool with specified maximum size.
35  virtual ~CE_MemoryPool();
36 
37  /// Empty the memory pool of all held buffers.
38  void clear();
39 
40  /// Set the maximums size of the pool. This will prune the LRUCache as
41  /// needed to fit the size.
42  void setMaxSize(int64 size) { myLRUCache.setMaxSize(size); }
43 
44  /// Returns the maximum size of the pool.
45  int64 getMaxSize() const { return myLRUCache.maxSize(); }
46 
47  /// Dump memory usage statistics to stderr.
48  void reportUsage() const;
49 
50  /// Dump memory to a stream
51  void reportUsage(std::ostream &os) const;
52 
53  /// Allocate a buffer for the provided context. If a buffer of the
54  /// specified size already exists in the pool, it will be returned.
55  /// Otherwise a new one will be created on the device.
56  cl::Buffer allocBuffer(CE_Context *, int64 size);
57 
58  /// Check the provided buffer's reference count. If it is about to be
59  /// released, return it to the pool if it will fit, else release it. If
60  /// the reference count is higher than 1, meaning it is still active in
61  /// an asynchronous operation like a kernel, put it on the inUse list to
62  /// check later in sweepInUse().
63  void returnBuffer(cl::Buffer &&buf);
64 
65  /// Sweep the inUse list, looking for buffers that are no longer being used
66  /// in an asynchronous operation and can be returned to the pool.
67  void sweepInUse();
68 
69 private:
70  // This class just holds a cl::Buffer while it's in the LRUCache. It's also
71  // responsible for alerting the CE_MemoryPool object when it's deleted
72  // by pruning or other means. Each cl::Buffer is owned by only one of
73  // these objects, as maintained by being non-copyable and having move
74  // semantics.
75  class ut_clBuffer : public UT_NonCopyable
76  {
77  public:
78  ut_clBuffer(CE_MemoryPool *pool, const cl::Buffer &buf, int64 size);
79 
80  ut_clBuffer(ut_clBuffer &&b);
81 
82  ~ut_clBuffer();
83 
84  ut_clBuffer &operator=(ut_clBuffer &&b);
85 
86  static exint size(const ut_clBuffer &b) { return b.mySize; }
87 
88  private:
89  int64 mySize;
90  cl::Buffer myBuffer;
91  CE_MemoryPool *myPool;
92  };
93 
94  friend class ut_clBuffer;
95 
96  bool getBufferFromPool(int64 size, cl::Buffer &buf);
97 
98  /// Add the buffer to the pool. Returns false if it operation fails.
99  bool appendToPool(const cl::Buffer &buf, int64 size);
100 
101  /// Remove the the buffer from the pool.
102  void releaseBuffer(const cl::Buffer &buf, int64 size);
103 
106  BufferCache myLRUCache;
107  UT_Array<cl::Buffer *> myInUseList;
108  BufferTable myBufferTable;
109  int64 myInUseSize;
110  int64 myTotalAllocSize;
111 
112  mutable UT_Lock myMemoryPoolLock;
113 };
114 
115 
116 #endif
117 #endif
#define CE_API
Definition: CE_API.h:11
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2540
Unsorted map container.
Definition: UT_Map.h:107
int64 exint
Definition: SYS_Types.h:125
void setMaxSize(int64 size)
Definition: CE_MemoryPool.h:42
long long int64
Definition: SYS_Types.h:116
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
int64 getMaxSize() const
Returns the maximum size of the pool.
Definition: CE_MemoryPool.h:45
GLsizeiptr size
Definition: glcorearb.h:664
LeafData & operator=(const LeafData &)=delete
Memory buffer interface.
Definition: cl.hpp:1867
**Note that the tasks the is the thread number *for the pool
Definition: thread.h:637