Loading...
Searching...
No Matches
network.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_NETWORK_H
8#define PXR_EXEC_VDF_NETWORK_H
9
11
12#include "pxr/pxr.h"
13
14#include "pxr/exec/vdf/api.h"
15#include "pxr/exec/vdf/types.h"
16
17#include "pxr/base/tf/hash.h"
18#include "pxr/base/tf/hashset.h"
20#include "pxr/base/tf/span.h"
21
22#include "tbb/concurrent_queue.h"
23#include "tbb/concurrent_unordered_map.h"
24#include "tbb/concurrent_vector.h"
25
26#include <atomic>
27#include <cstdint>
28#include <vector>
29#include <iosfwd>
30#include <memory>
31#include <unordered_map>
32
33PXR_NAMESPACE_OPEN_SCOPE
34
35class TfToken;
36
37class VdfConnection;
38class VdfInput;
39class VdfObjectPtr;
40class VdfMask;
41class VdfMaskedOutput;
42class VdfNode;
43class VdfOutput;
44class VdfSchedule;
46class VdfPoolChainIndexer;
48class Vdf_InputAndOutputSpecsRegistry;
49class Vdf_ScheduleInvalidator;
50
51typedef TfHashSet<const VdfOutput *, TfHash> VdfOutputPtrSet;
52
60{
61public:
62
66 static const int AppendConnection = -1;
67
70 VDF_API
72
75 VDF_API
77
80 VDF_API
81 const std::string GetNodeDebugName(const VdfNode *node) const;
82
87 uint32_t GetOutputCapacity() const {
88 return _outputCapacity.load(std::memory_order_acquire);
89 }
90
95 size_t GetNodeCapacity() const { return _nodes.size(); }
96
99 size_t GetNumOwnedNodes() const {
100 return _nodes.size() - _freeNodeIds.unsafe_size();
101 }
102
105 const VdfNode *GetNode(size_t i) const {
106 return i < _nodes.size() ? _nodes[i] : nullptr;
107 }
108
111 VdfNode *GetNode(size_t i) {
112 return i < _nodes.size() ? _nodes[i] : nullptr;
113 }
114
117 VDF_API
118 const VdfNode *GetNodeById(const VdfId nodeId) const;
119
122 VDF_API
123 VdfNode *GetNodeById(const VdfId nodeId);
124
130
138 class VDF_API_TYPE EditMonitor
139 {
140 public:
141 VDF_API
142 virtual ~EditMonitor();
143
150 virtual void WillClear() = 0;
151
154 virtual void DidConnect(const VdfConnection *connection) = 0;
155
158 virtual void DidAddNode(const VdfNode *node) = 0;
159
162 virtual void WillDelete(const VdfNode *node) = 0;
163
166 virtual void WillDelete(const VdfConnection *connection) = 0;
167 };
168
171 VDF_API
172 void Clear();
173
179 VDF_API
181 VdfOutput *output,
182 VdfNode *inputNode,
183 const TfToken &inputName,
184 const VdfMask &mask,
185 int atIndex = AppendConnection);
186
192 VDF_API
194 const VdfMaskedOutput &maskedOutput,
195 VdfNode *inputNode,
196 const TfToken &inputName,
197 int atIndex = AppendConnection);
198
208 VDF_API
209 bool Delete(VdfNode *node);
210
213 VDF_API
214 void Disconnect(VdfConnection *connection);
215
220 VDF_API
222
235 VDF_API
237 VdfInput *input,
238 const TfSpan<const VdfConnectionVector::size_type> &newToOldIndices);
239
244 VDF_API
246
249 VDF_API
251
259 size_t GetVersion() const {
260 return _version.load(std::memory_order_acquire);
261 }
262
264
265
268
273 VDF_API
274 size_t DumpStats(std::ostream &os) const;
275
277
280
283 VDF_API
285
287
288private:
289
290 friend class VdfNode;
291 friend class VdfIsolatedSubnetwork;
292 friend class VdfOutput;
293
294 // Registers a name for a given node.
295 //
296 // Only VdfNode should call this function.
297 //
298 void _RegisterNodeDebugName(
299 const VdfNode &node, VdfNodeDebugNameCallback &&callback);
300
301 // Unregisters a name for a given node.
302 //
303 // Should only be called by VdfNode's destructor.
304 //
305 void _UnregisterNodeDebugName(const VdfNode &node);
306
307 // Adds a node to this network.
308 //
309 // Returns the index of node that was added. Once a node is added to the
310 // network, the network takes ownership. Only VdfNode should call
311 // this function.
312 //
313 void _AddNode(VdfNode *node);
314
315 // Delete a node helper.
316 //
317 void _DeleteNode(VdfNode *node);
318
319 // Removes the connection from the network, but does not delete it. Sends
320 // out the deletion notification, as well as invalidates any structures
321 // dependent on the connection state.
322 //
323 void _RemoveConnection(VdfConnection *connection);
324
325 // Delete a connection helper function. Note that this method won't
326 // send out deletion notification.
327 //
328 void _DeleteConnection(VdfConnection *connection);
329
330 // Removes \p node from the network, but doesn't delete it. Uses \p monitor
331 // to notify about any reindexing.
332 //
333 void _RemoveNode(VdfNode *node);
334
335 // Informs the network that the affects mask for \p output has changed.
336 //
337 void _DidChangeAffectsMask(VdfOutput &output);
338
339 // Returns an output id for a newly created output to use.
340 //
341 size_t _AcquireOutputId();
342
343 // Releases an output id for use by a future output.
344 //
345 void _ReleaseOutputId(const VdfId id);
346
347 // Increment the network edit version.
348 //
349 void _IncrementVersion();
350
351 // \name Schedule Management
352 //
353 // These are methods that can only be called by VdfSchedule to register
354 // an unregister itself from a network.
355 //
356 // These methods are thread-safe and can safely be called from
357 // within execution. This is needed for clients that schedule during
358 // execution (e.g. speculation).
359 //
360 // @{
361
362 friend class VdfSchedule;
363
364 // Sets that a schedule is referencing this network.
365 //
366 void _RegisterSchedule(VdfSchedule *schedule) const;
367
368 // Clears the schedule from the list of schedules referencing this network.
369 //
370 void _UnregisterSchedule(VdfSchedule *schedule) const;
371
372 // Returns a pointer to the schedule invalidator.
373 //
374 Vdf_ScheduleInvalidator* _GetScheduleInvalidator() const {
375 return _scheduleInvalidator.get();
376 }
377
378 // @}
379
380 // Returns the input/output specs registry for this network.
381 //
382 Vdf_InputAndOutputSpecsRegistry &_GetInputOutputSpecsRegistry() {
383 return *_specsRegistry;
384 }
385
386 // The complete list of nodes managed by this network.
387 tbb::concurrent_vector<VdfNode *> _nodes;
388
389 // A queue of ids that are free to be assigned to new nodes.
390 tbb::concurrent_queue<VdfId> _freeNodeIds;
391
392 // The version with which to initialize new nodes
393 VdfVersion _initialNodeVersion;
394
395 // The number of output indices in this network.
396 std::atomic<uint32_t> _outputCapacity;
397
398 // The free output indices in the network.
399 tbb::concurrent_queue<VdfId> _freeOutputIds;
400
401 // The list of static edit monitors registered with this network.
402 using _EditMonitorVector = TfSmallVector<EditMonitor *, 1>;
403 _EditMonitorVector _monitors;
404
405 // Helper class for invalidating schedules after topological changes.
406 std::unique_ptr<Vdf_ScheduleInvalidator> _scheduleInvalidator;
407
408 // Pool Chain Indexer
409 std::unique_ptr<VdfPoolChainIndexer> _poolChainIndexer;
410
411 // Input and output specs registry.
412 std::unique_ptr<Vdf_InputAndOutputSpecsRegistry> _specsRegistry;
413
414 // Edit version.
415 std::atomic<size_t> _version;
416
417 // Map from node to debug name.
418 using _NodeDebugNamesMap = tbb::concurrent_unordered_map<
419 VdfIndex,
420 std::unique_ptr<Vdf_ExecNodeDebugName>,
421 TfHash>;
422
423 _NodeDebugNamesMap _nodeDebugNames;
424};
425
427
428PXR_NAMESPACE_CLOSE_SCOPE
429
430#endif
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:472
This is a small-vector class with local storage optimization, the local storage can be specified via ...
Definition: smallVector.h:157
Represents a range of contiguous elements.
Definition: span.h:71
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
Stores all necessary information to lazily construct a node debug name.
A class that fully represents a connection between two VdfNodes.
Definition: connection.h:30
A VdfInput is used to connect a VdfNode to one or more VdfNodes' outputs.
Definition: input.h:36
A VdfIsolatedSubnetwork builds a collection of VdfNodes and VdfConnections that are disconnected from...
A VdfMask is placed on connections to specify the data flowing through them.
Definition: mask.h:37
Class to hold on to an externally owned output and a mask.
Definition: maskedOutput.h:32
An abstract class to monitor network edit operations.
Definition: network.h:139
virtual void DidConnect(const VdfConnection *connection)=0
Will be called after a connection has been made.
virtual void WillDelete(const VdfNode *node)=0
Will be called before node is deleted.
virtual void WillClear()=0
Will be called before a network is to be cleared out.
virtual void WillDelete(const VdfConnection *connection)=0
Will be called before a connection is deleted.
virtual void DidAddNode(const VdfNode *node)=0
Will be called after a node has been added to the network.
A VdfNetwork is a collection of VdfNodes and their connections.
Definition: network.h:60
VDF_API const std::string GetNodeDebugName(const VdfNode *node) const
Retrieves a debug name for a given node.
VDF_API bool DisconnectAndDelete(VdfNode *node)
Disconnects and deletes node.
VDF_API void UnregisterEditMonitor(EditMonitor *monitor)
Unregisters an edit monitor for this network.
VDF_API VdfConnection * Connect(const VdfMaskedOutput &maskedOutput, VdfNode *inputNode, const TfToken &inputName, int atIndex=AppendConnection)
Connects the maskedOutput to the given inputNode's input inputName.
VDF_API VdfNode * GetNodeById(const VdfId nodeId)
Returns the non-const node with id nodeId, if it exists.
VDF_API void ReorderInputConnections(VdfInput *input, const TfSpan< const VdfConnectionVector::size_type > &newToOldIndices)
Reorders all input connections for input according to the mapping defined by newToOldIndices.
VDF_API VdfPoolChainIndex GetPoolChainIndex(const VdfOutput &output) const
Returns the pool chain index from the pool chain indexer.
VDF_API size_t DumpStats(std::ostream &os) const
Prints useful statistics about the network to os.
VDF_API bool Delete(VdfNode *node)
Deletes node from the network.
static const int AppendConnection
Constant for connection API to indicate to place the connection at the end.
Definition: network.h:66
size_t GetNumOwnedNodes() const
Returns the number of nodes that are currently owned by the network.
Definition: network.h:99
VDF_API VdfConnection * Connect(VdfOutput *output, VdfNode *inputNode, const TfToken &inputName, const VdfMask &mask, int atIndex=AppendConnection)
Connects the output to the given inputNode's input inputName with mask.
size_t GetNodeCapacity() const
Returns the number of entries currently available for nodes and for which it is valid to call GetNode...
Definition: network.h:95
VdfNode * GetNode(size_t i)
Returns the non-const node at index i.
Definition: network.h:111
VDF_API ~VdfNetwork()
Destructs the network and the nodes managed by it.
VDF_API const VdfNode * GetNodeById(const VdfId nodeId) const
Returns the node with id nodeId, if it exists.
VDF_API void RegisterEditMonitor(EditMonitor *monitor)
Registers an edit monitor for this network.
VDF_API void Disconnect(VdfConnection *connection)
Deletes connection from the network.
size_t GetVersion() const
Returns the current edit version of the network.
Definition: network.h:259
VDF_API VdfNetwork()
Constructs an empty network.
VDF_API void Clear()
Clears all nodes from the network.
const VdfNode * GetNode(size_t i) const
Returns the node at index i.
Definition: network.h:105
uint32_t GetOutputCapacity() const
Returns the number of indices currently available for outputs.
Definition: network.h:87
This is the base class for all nodes in a VdfNetwork.
Definition: node.h:53
An universal class to represent pointers to various Vdf types.
Definition: object.h:40
A VdfOutput represents an output on a node.
Definition: output.h:32
Opaque pool chain index type.
Contains a specification of how to execute a particular VdfNetwork.
Definition: schedule.h:41
std::function< std::string()> VdfNodeDebugNameCallback
Type of callback for building a node debug name.
Definition: types.h:71
uint64_t VdfId
The unique identifier type for Vdf objects.
Definition: types.h:107
uint32_t VdfIndex
The index type for Vdf objects.
Definition: types.h:110
uint32_t VdfVersion
The version type for Vdf objects.
Definition: types.h:113