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 
25 class CE_Context;
26 
27 /// This class implements a memory pool for OpenCL buffer objects, using an
28 /// LRUCache to limit the size of the pool.
30 {
31 public:
32  /// Initialize the pool with specified maximum size.
34  virtual ~CE_MemoryPool();
35 
36  /// Empty the memory pool of all held buffers.
37  void clear();
38 
39  /// Set the maximums size of the pool. This will prune the LRUCache as
40  /// needed to fit the size.
41  void setMaxSize(int64 size) { myLRUCache.setMaxSize(size); }
42 
43  /// Returns the maximum size of the pool.
44  int64 getMaxSize() const { return myLRUCache.maxSize(); }
45 
46  /// Dump memory usage statistics to stderr.
47  void reportUsage() const;
48 
49  /// Allocate a buffer for the provided context. If a buffer of the
50  /// specified size already exists in the pool, it will be returned.
51  /// Otherwise a new one will be created on the device.
52  cl::Buffer allocBuffer(CE_Context *, int64 size);
53 
54  /// Check the provided buffer's reference count. If it is about to be
55  /// released, return it to the pool if it will fit, else release it. If
56  /// the reference count is higher than 1, meaning it is still active in
57  /// an asynchronous operation like a kernel, put it on the inUse list to
58  /// check later in sweepInUse().
59  void returnBuffer(cl::Buffer &&buf);
60 
61  /// Sweep the inUse list, looking for buffers that are no longer being used
62  /// in an asynchronous operation and can be returned to the pool.
63  void sweepInUse();
64 
65 private:
66  // This class just holds a cl::Buffer while it's in the LRUCache. It's also
67  // responsible for alerting the CE_MemoryPool object when it's deleted
68  // by pruning or other means. Each cl::Buffer is owned by only one of
69  // these objects, as maintained by being non-copyable and having move
70  // semantics.
71  class ut_clBuffer : public UT_NonCopyable
72  {
73  public:
74  ut_clBuffer(CE_MemoryPool *pool, const cl::Buffer &buf, int64 size);
75 
76  ut_clBuffer(ut_clBuffer &&b);
77 
78  ~ut_clBuffer();
79 
80  ut_clBuffer &operator=(ut_clBuffer &&b);
81 
82  static exint size(const ut_clBuffer &b) { return b.mySize; }
83 
84  private:
85  int64 mySize;
86  cl::Buffer myBuffer;
87  CE_MemoryPool *myPool;
88  };
89 
90  friend class ut_clBuffer;
91 
92  bool getBufferFromPool(int64 size, cl::Buffer &buf);
93 
94  /// Add the buffer to the pool. Returns false if it operation fails.
95  bool appendToPool(const cl::Buffer &buf, int64 size);
96 
97  /// Remove the the buffer from the pool.
98  void releaseBuffer(const cl::Buffer &buf, int64 size);
99 
102  BufferCache myLRUCache;
103  UT_Array<cl::Buffer *> myInUseList;
104  BufferTable myBufferTable;
105  int64 myInUseSize;
106  int64 myTotalAllocSize;
107 
108  mutable UT_Lock myMemoryPoolLock;
109 };
110 
111 
112 #endif
113 #endif
#define CE_API
Definition: CE_API.h:10
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:41
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:44
GLsizeiptr size
Definition: glcorearb.h:664
Memory buffer interface.
Definition: cl.hpp:1867
**Note that the tasks the is the thread number *for the pool
Definition: thread.h:637