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