HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
speculationNode.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_SPECULATION_NODE_H
8 #define PXR_EXEC_VDF_SPECULATION_NODE_H
9 
10 /// \file
11 
12 #include "pxr/pxr.h"
13 
14 #include "pxr/exec/vdf/api.h"
16 #include "pxr/exec/vdf/context.h"
17 #include "pxr/exec/vdf/node.h"
19 
20 #include <tbb/concurrent_hash_map.h>
21 
22 #include <memory>
23 
25 
26 class VdfSchedule;
27 
28 ///////////////////////////////////////////////////////////////////////////////
29 ///
30 /// \class VdfSpeculationNode
31 ///
32 /// \brief A node that pulls on a vector of value that are downstream of the
33 /// current execution position.
34 ///
36 {
37 public:
38  /// Construct a new speculation node.
39  ///
40  VDF_API
42  VdfNetwork *network,
43  const VdfInputSpecs &inputSpecs,
44  const VdfOutputSpecs &outputSpecs);
45 
46  /// Returns \c true, indicating that this node performs speculation.
47  ///
48  virtual bool IsSpeculationNode() const { return true; }
49 
50  /// Returns a predicate to determine the required read inputs.
51  ///
52  /// For speculation nodes, this is empty since speculation nodes technically
53  /// do not need input values (they compute them from their own
54  /// executors). Returning anything here would cause infinite loops.
55  ///
57  const VdfContext &context) const {
59  }
60 
61  /// Executes the speculation node.
62  ///
63  VDF_API
64  virtual void Compute(const VdfContext &context) const;
65 
66  /// Returns the schedule for this speculation node. Schedules
67  /// if necessary.
68  ///
69  VDF_API
70  const VdfSchedule &GetSchedule(const VdfSchedule *requestingSched) const;
71 
72 private:
73 
74  // Only a network is allowed to delete nodes.
75  VDF_API
76  virtual ~VdfSpeculationNode();
77 
78  /// Returns the executor from the given \p context.
79  ///
80  const VdfExecutorInterface &_GetContextExecutor(
81  const VdfContext &context) const {
82  return context._GetExecutor();
83  }
84 
85  /// Returns the request for this speculation node, given the requesting
86  /// schedule \p requestingSched. This is the request that this speculation
87  /// node must compute in order to satisfy its input requirements to meet
88  /// the output request by \p requestingSched.
89  ///
90  VdfRequest _GetInputRequest(const VdfSchedule &requestingSched) const;
91 
92  /// Looks up a schedule for the given \p inputRequest and schedules it if
93  /// necessary.
94  ///
95  const VdfSchedule *_GetSchedule(const VdfRequest &request) const;
96 
97  /// Overridden to provide sparse dependency information in the
98  /// input-to-output direction, since all outputs of a speculation
99  /// node don't depend on all inputs.
100  ///
102  const VdfConnection &inputConnection,
103  const VdfMask &inputDependencyMask,
104  const VdfOutput &output) const;
105 
106  /// Overridden to provide sparse dependency information in the
107  /// output-to-input direction, since all outputs of a speculation
108  /// node don't depend on all inputs.
109  ///
111  const VdfMaskedOutput &maskedOutput,
112  const VdfConnection &inputConnection) const;
113 
114 private:
115 
116  // A hash functor for VdfRequest.
117  struct _HashRequest {
118  bool equal(const VdfRequest &lhs, const VdfRequest &rhs) const {
119  return lhs == rhs;
120  }
121 
122  size_t hash(const VdfRequest &request) const {
123  return VdfRequest::Hash()(request);
124  }
125  };
126 
127  // This is a schedule map that holds the schedules that we will use to
128  // compute the node. Invalidation is automatic from the
129  // network for which they are scheduled.
130  using _ScheduleMap = tbb::concurrent_hash_map<
131  VdfRequest, std::unique_ptr<VdfSchedule>, _HashRequest>;
132  mutable _ScheduleMap _scheduleMap;
133 };
134 
136 
137 #endif
STATIC_INLINE size_t Hash(const char *s, size_t len)
Definition: farmhash.h:2099
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
Definition: node.h:52
IMATH_HOSTDEVICE constexpr bool equal(T1 a, T2 b, T3 t) IMATH_NOEXCEPT
Definition: ImathFun.h:105
A VdfMask is placed on connections to specify the data flowing through them.
Definition: mask.h:36
virtual void Compute(const VdfContext &context) const =0
#define VDF_API
Definition: api.h:25
Fast, compressed bit array which is capable of performing logical operations without first decompress...
Contains a specification of how to execute a particular VdfNetwork.
Definition: schedule.h:40
virtual bool IsSpeculationNode() const
A node that pulls on a vector of value that are downstream of the current execution position...
virtual VDF_API VdfMask _ComputeOutputDependencyMask(const VdfConnection &inputConnection, const VdfMask &inputDependencyMask, const VdfOutput &output) const
Class to hold on to an externally owned output and a mask.
Definition: maskedOutput.h:31
virtual VdfRequiredInputsPredicate GetRequiredInputsPredicate(const VdfContext &context) const
#define VDF_API_TYPE
Definition: api.h:26
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
This predicate determines whether a given input value is needed to fulfill the input dependencies req...
virtual VDF_API VdfMask::Bits _ComputeInputDependencyMask(const VdfMaskedOutput &maskedOutput, const VdfConnection &inputConnection) const
Abstract base class for classes that execute a VdfNetwork to compute a requested set of values...
static VdfRequiredInputsPredicate NoReads(const VdfNode &node)