HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dataManagerHashTable.h
Go to the documentation of this file.
1 //
2 // Copyright 2025 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_EXEC_VDF_DATA_MANAGER_HASH_TABLE_H
8 #define PXR_EXEC_VDF_DATA_MANAGER_HASH_TABLE_H
9 
10 ///\file
11 
12 #include "pxr/pxr.h"
13 
14 #include "pxr/exec/vdf/api.h"
18 #include "pxr/exec/vdf/smblData.h"
19 #include "pxr/exec/vdf/types.h"
20 
21 #include "pxr/base/tf/hash.h"
22 #include "pxr/base/tf/stl.h"
23 
24 #include <memory>
25 #include <unordered_map>
26 
28 
30 class VdfOutput;
31 class VdfNetwork;
32 
33 ///////////////////////////////////////////////////////////////////////////////
34 ///
35 /// \class Vdf_ExecutorDataManagerTraits<VdfDataManagerHashTable>
36 ///
37 /// \brief Type traits specialization for the VdfDataManagerHashTable.
38 ///
39 template<>
41 
42  /// The output data stored at each entry in the hash table.
43  ///
44  struct OutputData {
46  invalidationTimestamp(
47  VdfExecutorInvalidationData::InitialInvalidationTimestamp),
48  touched(false)
49  {}
50 
54  std::unique_ptr<VdfSMBLData> smblData;
55  bool touched;
56  };
57 
58  /// The data handle type. For the VdfDataManagerHashTable this is simply
59  /// a pointer to value stored in the hash table.
60  ///
61  typedef OutputData * DataHandle;
62 
63 };
64 
65 ///////////////////////////////////////////////////////////////////////////////
66 ///
67 /// \class VdfDataManagerHashTable
68 ///
69 /// \brief This is a data manager for executors that uses data stored in an
70 /// external hash table.
71 ///
73  public VdfExecutorDataManager<VdfDataManagerHashTable>
74 {
75 
76  // The output data stored at each entry.
77  typedef
79  VdfDataManagerHashTable>::OutputData
80  _OutputData;
81 
82  // Data type for map from outputs to their executor data.
83  using _DataMap = std::unordered_map<VdfId, _OutputData, TfHash>;
84 
85 public:
86 
87  /// The base class type.
88  ///
89  typedef
92 
93  /// The data handle type from the type traits class.
94  ///
95  typedef
99 
100  /// Resize the data manager to accommodate all the outputs in the given
101  /// network.
102  ///
103  VDF_API
104  void Resize(const VdfNetwork &network);
105 
106  /// Returns \c true if the given data \p handle is valid, i.e. it is valid
107  /// to ask for data for this given \p handle.
108  ///
109  /// Note that attempting to resolve data at an invalid handle results in
110  /// undefined behavior.
111  ///
112  bool IsValidDataHandle(const DataHandle handle) const {
113  return handle != nullptr;
114  }
115 
116  /// Returns an existing data handle, or creates a new one for the given
117  /// \p outputId.
118  ///
119  /// This method is guaranteed to return a valid data handle.
120  ///
121  DataHandle GetOrCreateDataHandle(const VdfId outputId) const {
122  _DataMap::iterator it = _outputData.find(outputId);
123  if (it == _outputData.end()) {
124  it = _outputData.emplace(
125  std::piecewise_construct,
126  std::forward_as_tuple(outputId),
127  std::forward_as_tuple())
128  .first;
129  }
130  return &it->second;
131  }
132 
133  /// Returns an existing data handle for the given \p outputId. This method
134  /// will return an invalid data handle, if no handle has been created
135  /// for the given \p outputId.
136  ///
137  DataHandle GetDataHandle(const VdfId outputId) const {
138  return TfMapLookupPtr(_outputData, outputId);
139  }
140 
141  /// Returns the VdfExecutorBufferData associated with the given \p handle.
142  ///
143  /// Note it is undefined behavior to call this method with an invalid
144  /// data \p handle.
145  ///
147  return &handle->bufferData;
148  }
149 
150  /// Returns the VdfExecutorInvalidationData associated with the given
151  /// \p handle.
152  ///
153  /// Note it is undefined behavior to call this method with an invalid
154  /// data \p handle.
155  ///
157  const DataHandle handle) const {
158  return &handle->invalidationData;
159  }
160 
161  /// Un-hide the GetInvalidationTimestamp method declared in the base class.
162  ///
164 
165  /// Returns the VdfInvalidationTimestamp associated with the given
166  /// \p handle.
167  ///
168  /// Note it is undefined behavior to call this method with an invalid
169  /// data \p handle.
170  ///
172  const DataHandle handle) const {
173  return handle->invalidationTimestamp;
174  }
175 
176  /// Sets the invalidation \p timestamp for the give data \p handle.
177  ///
178  /// Note it is undefined behavior to call this method with an invalid
179  /// data \p handle.
180  ///
182  const DataHandle handle,
183  VdfInvalidationTimestamp timestamp) {
184  handle->invalidationTimestamp = timestamp;
185  }
186 
187  /// Returns an existing \p VdfSMBLData associated with the given \p handle.
188  /// Returns \c nullptr if there is no SMBL data associated with this
189  /// data \p handle.
190  ///
191  /// Note it is undefined behavior to call this method with an invalid
192  /// data \p handle.
193  ///
194  VdfSMBLData * GetSMBLData(const DataHandle handle) const {
195  return handle->smblData.get();
196  }
197 
198  /// Returns an existing \p VdfSMBLData associated with the given \p handle
199  /// or creates a new one of none exists.
200  ///
201  /// Note it is undefined behavior to call this method with an invalid
202  /// data \p handle.
203  ///
205  if (!handle->smblData) {
206  handle->smblData.reset(new VdfSMBLData());
207  }
208  return handle->smblData.get();
209  }
210 
211  /// Returns \c true if the data at the given \p handle has been touched by
212  /// evaluation.
213  ///
214  /// Note it is undefined behavior to call this method with an invalid
215  /// data \p handle.
216  ///
217  bool IsTouched(const DataHandle handle) const {
218  return handle->touched;
219  }
220 
221  /// Marks the data at the given \p handle as having been touched by
222  /// evaluation.
223  ///
224  /// Note it is undefined behavior to call this method with an invalid
225  /// data \p handle.
226  ///
227  void Touch(const DataHandle handle) const {
228  handle->touched = true;
229  }
230 
231  /// Marks the data at the given \p handle as not having been touched by
232  /// evaluation. Returns \c true if the data has previously been touched.
233  ///
234  /// Note it is undefined behavior to call this method with an invalid
235  /// data \p handle.
236  ///
237  bool Untouch(const DataHandle handle) {
238  const bool wasTouched = handle->touched;
239  handle->touched = false;
240  return wasTouched;
241  }
242 
243  /// Clears the executor data for a specific output
244  ///
245  void ClearDataForOutput(const VdfId outputId) {
246  _outputData.erase(outputId);
247  }
248 
249  /// Clears all the data from this manager.
250  ///
251  void Clear() {
252  TfReset(_outputData);
253  }
254 
255  /// Returns \c true if this data manager is empty.
256  ///
257  bool IsEmpty() const {
258  return _outputData.empty();
259  }
260 
261 
262 private:
263 
264  // Map from outputs to their executor data.
265  mutable _DataMap _outputData;
266 
267 };
268 
269 ///////////////////////////////////////////////////////////////////////////////
270 
272 
273 #endif
bool Untouch(const DataHandle handle)
void TfReset(T &obj)
Definition: stl.h:170
bool IsTouched(const DataHandle handle) const
VdfInvalidationTimestamp GetInvalidationTimestamp() const
VdfExecutorDataManager< VdfDataManagerHashTable > Base
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
VdfExecutorBufferData * GetBufferData(const DataHandle handle) const
VdfInvalidationTimestamp GetInvalidationTimestamp(const DataHandle handle) const
VdfSMBLData * GetOrCreateSMBLData(const DataHandle handle) const
DataHandle GetDataHandle(const VdfId outputId) const
#define VDF_API
Definition: api.h:25
This class provides functionality to manage the executor specific data associated with each output in...
VdfSMBLData * GetSMBLData(const DataHandle handle) const
This is a data manager for executors that uses data stored in an external hash table.
Container::mapped_type * TfMapLookupPtr(Container &map, Key const &key)
Definition: stl.h:124
VdfSMBLData holds per-output data that is meant to be consumed by the executor. This data is an optio...
Definition: smblData.h:30
void ClearDataForOutput(const VdfId outputId)
This object is responsible for storing the executor buffer data, comprised of the executor cache vect...
Vdf_ExecutorDataManagerTraits< VdfDataManagerHashTable >::DataHandle DataHandle
void Touch(const DataHandle handle) const
VDF_API void Resize(const VdfNetwork &network)
VdfExecutorInvalidationData * GetInvalidationData(const DataHandle handle) const
bool IsValidDataHandle(const DataHandle handle) const
void SetInvalidationTimestamp(const DataHandle handle, VdfInvalidationTimestamp timestamp)
unsigned int VdfInvalidationTimestamp
Type of the timestamp that identifies the most recent round of invalidation.
Definition: types.h:74
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
DataHandle GetOrCreateDataHandle(const VdfId outputId) const
uint64_t VdfId
The unique identifier type for Vdf objects.
Definition: types.h:107