HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CE_RampCache.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_RampCache.h ( CE Library, C++)
7  *
8  * COMMENTS: Caches UT_Ramps on the GPU to avoid re-building.
9  */
10 
11 #ifndef __CE_RampCache__
12 #define __CE_RampCache__
13 
14 #include "CE_API.h"
15 
16 #include <UT/UT_Array.h>
17 #include <UT/UT_Lock.h>
18 #include <UT/UT_LRUCache.h>
19 #include <UT/UT_Map.h>
20 #include <UT/UT_MemoryResource.h>
21 #include <UT/UT_UniquePtr.h>
22 #include <UT/UT_Ramp.h>
23 #include <UT/UT_NonCopyable.h>
24 #include <SYS/SYS_Types.h>
25 
26 class CE_Context;
27 
28 
30 {
31 public:
33  {
34  }
35  CE_RampKey(UT_SharedPtr<UT_Ramp> ramp, int tuplesize, int rampsize)
36  {
37  myRamp = ramp;
38  myTupleSize = tuplesize;
39  myRampSize = rampsize;
40 
41  // Compute hash.
42  myHash = myRamp.get() ? myRamp->hash() : 0;
43  SYShashCombine(myHash, myTupleSize);
44  SYShashCombine(myHash, myRampSize);
45  }
46 
47  exint numBytes() const
48  {
49  exint nbytes = sizeof(float);
50  nbytes *= myTupleSize;
51  nbytes *= myRampSize;
52  return nbytes;
53  }
54 
55  SYS_HashType hash() const { return myHash; }
56  bool operator==(const CE_RampKey &from) const
57  {
58  if (myHash != from.myHash) return false;
59  if (myTupleSize != from.myTupleSize) return false;
60  if (myRampSize != from.myRampSize) return false;
61  if (from.myRamp.get() != myRamp.get())
62  {
63  if (!from.myRamp.get() || !myRamp.get())
64  return false;
65 
66  if (*from.myRamp != *myRamp)
67  return false;
68  }
69  return true;
70  }
71 
72 protected:
75  int myTupleSize = 0;
76  int myRampSize = 0;
77 };
78 
79 /// This must be defined before the map.
80 template <>
81 struct std::hash<CE_RampKey>
82 {
83  std::size_t operator()(const CE_RampKey &key) const noexcept
84  {
85  return key.hash();
86  }
87 };
88 
89 /// This class implements a pool for UT_Ramps on the GPU.
91 {
92 public:
93  /// Initialize the pool with specified maximum size.
95  ~CE_RampCache();
96 
97  /// Empty the memory pool of all held buffers.
98  void clear();
99 
100  /// Returns empty buffer if not in cache.
101  /// This buffer remains owned by the cache; insofar as calling
102  /// CE_Context::releaseBuffer.
103  cl::Buffer lookupRamp(UT_SharedPtr<UT_Ramp> ramp, int tuplesize, int rampsize);
104 
105  /// Adds to the cache
106  void cacheRamp(cl::Buffer buffer, UT_SharedPtr<UT_Ramp> ramp, int tuplesize, int rampsize);
107 
108 public:
109 
110  // This is the owner of the buffer, and it is responsilbe for
111  // releasing the buffer to CE_Context, so must not be copyable
112  // or we could get multiple releases.
113  class RampEntry : public UT_NonCopyable
114  {
115  public:
117  RampEntry(cl::Buffer buffer, exint numbytes)
118  {
119  myBuffer = buffer;
120  myNumBytes = numbytes;
121  }
123  {
124  myNumBytes = b.myNumBytes;
125  myBuffer = std::move(b.myBuffer);
126  b.destroyBuffers();
127  }
128 
130  {
131  destroyBuffers();
132  myNumBytes = b.myNumBytes;
133  myBuffer = std::move(b.myBuffer);
134  b.destroyBuffers();
135  return *this;
136  }
137 
138  ~RampEntry();
139 
140  void destroyBuffers();
141 
142  cl::Buffer buffer() const { return myBuffer; }
143  exint numBytes() const { return myNumBytes; }
144 
145  static exint computeNumBytes(const RampEntry &entry)
146  { return entry.numBytes(); }
147 
148 
149  protected:
150  exint myNumBytes = 0;
152  };
153 
154 
155 public:
156 
157  /// Query the current usage; total is the total amount of memory held by the
158  /// pool, not_in_use is the amount that can be freed immediately on demand.
159  void getUsage(exint& total, exint& not_in_use) const;
160 
161  /// Attempts to release the requested amount of memory back to the system.
162  /// Returns the amount actually freed.
163  exint freeMemory(exint amount);
164 
165  /// This memory client is used to ensure that this memory pool plays nicely
166  /// with other users of memory on the OpenCL device.
168  {
169  public:
170  MemoryClient(CE_RampCache& cache) : myRampCache(cache) {}
171 
172  const char* name() const override
173  {
174  return "OpenCL Ramp Cache";
175  }
176 
177  bool freeMemoryRequest(const UT_MemoryResource* resource,
179  exint amount, exint& freed_amount) override
180  {
181  // Ramp caches are first against the wall.
182  freed_amount = myRampCache.freeMemory(amount);
183  return freed_amount > 0;
184  }
185 
186  bool memoryUse(const UT_MemoryResource* resource, exint& in_use,
187  exint& cache) override
188  {
189  myRampCache.getUsage(in_use, cache);
190  return true;
191  }
192 
193  private:
194  CE_RampCache &myRampCache;
195  };
196 
197 protected:
201 
202  /// The memory client that's registered with the OpenCL device.
204 
205  friend size_t hash_value(const CE_RampKey &val);
206 };
207 
209 
210 
211 #endif
#define CE_API
Definition: CE_API.h:13
UT_SharedPtr< UT_Ramp > myRamp
Definition: CE_RampCache.h:74
Definition: CE_RampCache.h:113
int myTupleSize
Definition: CE_RampCache.h:75
SYS_HashType myHash
Definition: CE_RampCache.h:73
int64 exint
Definition: SYS_Types.h:125
UT_Lock myRampCacheLock
Definition: CE_RampCache.h:200
std::size_t SYS_HashType
Define the type for hash values.
Definition: SYS_Hash.h:19
MemoryClient(CE_RampCache &cache)
Definition: CE_RampCache.h:170
RampEntry(cl::Buffer buffer, exint numbytes)
Definition: CE_RampCache.h:117
GLuint buffer
Definition: glcorearb.h:660
bool freeMemoryRequest(const UT_MemoryResource *resource, RequestSeverity severity, Niceness niceness, exint amount, exint &freed_amount) override
Definition: CE_RampCache.h:177
MemoryClient myMemoryClient
The memory client that's registered with the OpenCL device.
Definition: CE_RampCache.h:203
cl::Buffer myBuffer
Definition: CE_RampCache.h:151
RampLRU myRampCache
Definition: CE_RampCache.h:199
RampEntry(RampEntry &&b)
Definition: CE_RampCache.h:122
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:36
exint numBytes() const
Definition: CE_RampCache.h:143
CE_API CE_RampCache * CEgetRampCache()
int myRampSize
Definition: CE_RampCache.h:76
GLenum GLenum severity
Definition: glcorearb.h:2539
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
RampEntry & operator=(RampEntry &&b)
Definition: CE_RampCache.h:129
bool operator==(const CE_RampKey &from) const
Definition: CE_RampCache.h:56
This class implements a pool for UT_Ramps on the GPU.
Definition: CE_RampCache.h:90
std::size_t operator()(const CE_RampKey &key) const noexcept
Definition: CE_RampCache.h:83
RampEntry()
Definition: CE_RampCache.h:116
SYS_HashType hash() const
Definition: CE_RampCache.h:55
GLsizeiptr size
Definition: glcorearb.h:664
IMATH_NAMESPACE::V2f IMATH_NAMESPACE::Box2i std::string this attribute is obsolete as of OpenEXR v3 float
cl::Buffer buffer() const
Definition: CE_RampCache.h:142
CE_RampKey(UT_SharedPtr< UT_Ramp > ramp, int tuplesize, int rampsize)
Definition: CE_RampCache.h:35
static exint computeNumBytes(const RampEntry &entry)
Definition: CE_RampCache.h:145
Niceness
How willingly a client can free memory for other clients.
GLuint GLfloat * val
Definition: glcorearb.h:1608
Memory buffer interface.
Definition: cl.hpp:1867
bool memoryUse(const UT_MemoryResource *resource, exint &in_use, exint &cache) override
Definition: CE_RampCache.h:186
Wrapper for a client of a limit memory resource.
RequestSeverity
The urgency of the memory free request.
const char * name() const override
Printable English name of the client.
Definition: CE_RampCache.h:172
size_t hash_value(const CH_ChannelRef &ref)
exint numBytes() const
Definition: CE_RampCache.h:47