HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PDG_NodeCallback.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  * COMMENTS:
7  */
8 
9 #ifndef __PDG_NODE_CALLBACK_H__
10 #define __PDG_NODE_CALLBACK_H__
11 
12 #include "PDG_API.h"
13 
14 #include "PDG_LogUtils.h"
15 #include "PDG_Node.h"
16 #include "PDG_RegisteredType.h"
17 
18 #include <UT/UT_ParallelUtil.h>
19 #include <UT/UT_PerfMonAutoEvent.h>
20 #include <UT/UT_Performance.h>
21 #include <UT/UT_WorkBuffer.h>
22 
23 #define _PDG_INVOKE_CALLBACK(result, type, state, node, item, ...) \
24 { \
25  PDG_LogUtils::nodeLog( \
26  PDG_LogUtils::eNodeDebugCallback, node, "CALLBACK on" #type, \
27  UT_StringHolder::theEmptyString); \
28  UT_WorkBuffer _event_path; \
29  bool path_has_item = node->performancePath(_event_path, #type, item); \
30  UT_PerfMonAutoPDGCookEvent event( \
31  _event_path.buffer(), (path_has_item ? nullptr : #type)); \
32  PDG_EvaluationContext::StateScope scope(SYSgetSTID(), state, \
33  false, node, item, node->context()); \
34  UTisolate([&]() \
35  { \
36  try \
37  {\
38  result = node->callback()->postInvoke( \
39  node->callback()->on ## type (__VA_ARGS__)); \
40  } \
41  catch (...) \
42  { \
43  result = PDG_CallbackResult::eFailure; \
44  }\
45  });\
46 }
47 
48 #define PDG_NODE_CALLBACK(result, type, state, node, ...) \
49 { \
50  result = PDG_CallbackResult::eMissing; \
51  if (node->callback()->hasCallback(PDG_NodeCallback::e ## type))\
52  {\
53  _PDG_INVOKE_CALLBACK( \
54  result, type, state, node, nullptr, __VA_ARGS__); \
55  }\
56 }
57 
58 #define PDG_TASK_CALLBACK(result, type, item) \
59 { \
60  result = PDG_CallbackResult::eMissing; \
61  if (item->node()->callback()->hasCallback(PDG_NodeCallback::e ## type)) \
62  {\
63  PDG_AttributeMap::ScopedLock<false> auto_lock(item->attributes()); \
64  _PDG_INVOKE_CALLBACK( \
65  result, type, PDG_EvaluationContext::eStateCooking, \
66  item->node(), item, item); \
67  } \
68 }
69 
71 class PDG_Node;
72 class PDG_NodeOptions;
74 class PDG_WorkItemHolder;
75 class PDG_WorkItem;
76 
78 {
79 public:
80  /// Work item generation type
82  {
83  eStaticGenerate = 0,
84  eDynamicGenerate = 1,
85 
86  eStaticRegenerate = 2,
87  eDynamicRegenerate = 3,
88  };
89 
90  /// Enumeration of node callbacks. These enum values are a
91  /// power of 2 since they go into a bit vector for checking
92  /// the presence of callbacks
94  {
95  eGenerate = 0x0001,
96  eRegenerate = 0x0002,
97 
98  eAddInternalDependencies = 0x0004,
99 
100  ePartition = 0x0008,
101  eMapStatic = 0x0010,
102  eMapDynamic = 0x0020,
103 
104  ePreCook = 0x0040,
105  ePostCook = 0x0080,
106 
107  ePrepareTask = 0x0100,
108  eCookTask = 0x0200,
109  ePostCookTask = 0x0400,
110 
111  eSelectTask = 0x0800,
112  eDeselectTask = 0x1000,
113 
114  eConfigureNode = 0x2000,
115 
116  eProcessorBits = eGenerate | eRegenerate | eConfigureNode,
117  ePartitionerBits = ePartition,
118  eMapperBits = eMapStatic | eMapDynamic,
119  };
120 
121 public:
123  const PDGT_ValueArgs& extra_args,
124  PDG_Node* node);
125  ~PDG_NodeCallback() override;
126 
127  /// Returns the memory usage of this callback
128  int64 getMemoryUsage(bool inclusive) const override;
129 
130  /// Returns true if the node callback has the specified callback installed,
131  /// else returns false
132  bool hasCallback(CallbackType callback_type) const;
133 
134  /// Enables a specific node callback
135  void enableCallback(CallbackType callback_type);
136 
137  /// Resets this node callback to match changes to the type object
138  bool reloadInstance(UT_WorkBuffer& errors) override;
139 
140  /// Performs post callback invocation logic and returns a final
141  /// callback result
143  postInvoke(PDG_CallbackResult invoke_result) const;
144 
145 
146  /// Generates static items via a work item holder. Called on processors.
147  virtual PDG_CallbackResult
148  onGenerate(PDG_WorkItemHolder*,
149  const PDG_WorkItemArray&,
150  GenerationType generation_type);
151 
152  /// Regenerates static items, only called when items already exist. The
153  /// callback can add new items, or dirty/delete existing items.
154  virtual PDG_CallbackResult
155  onRegenerate(PDG_WorkItemHolder*,
156  const PDG_WorkItemArray&,
157  const PDG_WorkItemArray&,
158  GenerationType generation_type);
159 
160  /// Adds dependencies between newly created work items. Called on
161  /// processors.
162  virtual PDG_CallbackResult
163  onAddInternalDependencies(PDG_DependencyHolder*,
164  const PDG_WorkItemArray&,
165  bool is_static);
166 
167  /// Groups upstream work items into partitions that can be non-disjoint and
168  /// do not necessarily cover the set of all items. Called on partitioner
169  /// nodes.
170  virtual PDG_CallbackResult
171  onPartition(PDG_PartitionHolder*,
172  const PDG_WorkItemArray&,
173  const PDG_WorkItem*);
174 
175  /// Adds dependencies between static itrems below a mapper node and static
176  /// ancestors above. Called on mapper nodes.
177  virtual PDG_CallbackResult
178  onMapStatic(PDG_DependencyHolder* holder,
179  const PDG_WorkItemArray& downstream_items,
180  const PDG_WorkItemArray& upstream_items);
181 
182  /// Adds dependencies between dynamic items above a mapper and static items
183  /// below the mapper.
184  virtual PDG_CallbackResult
185  onMapDynamic(PDG_DependencyHolder* holder,
186  const PDG_WorkItemArray& downstream_items,
187  const PDG_WorkItemArray& upstream_items);
188 
189  /// Called before the graph begins cooking. This is only called on nodes
190  /// that are on the active cook path, and is guaranteed to be called on each
191  /// node before any threaded cooking begins. If a Failure result is returned
192  /// from any of the nodes, the cook is aborted
193  virtual PDG_CallbackResult
194  onPreCook();
195 
196  /// Called on each node in the graph after the cook finishes.
197  virtual PDG_CallbackResult
198  onPostCook();
199 
200  /// Called when a work item has all of its dependencies satisfied, and will
201  /// be submitted to the scheduler or cooked in-process shortly.
202  virtual PDG_CallbackResult
203  onPrepareTask(PDG_WorkItem* work_item);
204 
205  /// Called when a work item wants to cook in-process, but does not have its
206  /// own cook implementation
207  virtual PDG_CallbackResult
208  onCookTask(PDG_WorkItem* work_item);
209 
210  /// Called when a work item has completed cooking.
211  virtual PDG_CallbackResult
212  onPostCookTask(PDG_WorkItem* work_item);
213 
214  /// Called when a work item is selected by TOPs.
215  virtual PDG_CallbackResult
216  onSelectTask(PDG_WorkItem* work_item);
217 
218  /// Called when a work item is deselcted
219  virtual PDG_CallbackResult
220  onDeselectTask(PDG_WorkItem* work_item);
221 
222  /// Called when the node should describe itself for the UI
223  virtual PDG_CallbackResult
224  onConfigureNode(PDG_NodeOptions* node_options);
225 
226  /// Returns the node associated with this callback
227  PDG_Node* node() const;
228 
229 
230  /// Convenience method for evaluating a parameter on the underlying node
231  template <typename T>
232  bool evaluate(
233  T& result,
234  const UT_StringHolder& name,
235  const PDG_WorkItem* work_item=nullptr,
236  int index=0,
237  int multi=-1) const
238  {
239  return myNode->evaluate(result, name, work_item, index, multi);
240  }
241 
242  /// Convenience method for evaluating a parameter as an array on the
243  /// underlying node
244  template <typename T>
246  T& result,
247  const UT_StringHolder& name,
248  const PDG_WorkItem* work_item=nullptr,
249  int multi=-1) const
250  {
251  return myNode->evaluate(result, name, work_item, multi);
252  }
253 
254  /// Evaluates a parameter as a string and returns the value back, instead
255  /// of an error code.
257  const UT_StringHolder& name,
258  const PDG_WorkItem* work_item=nullptr,
259  int index=0,
260  int multi=-1) const
261  {
262  UT_StringHolder result = "";
263  myNode->evaluate(result, name, work_item, index, multi);
264  return result;
265  }
266 
267  /// Evaluates a parameter as a raw string and returns the value back,
268  /// instead of an error code.
270  const UT_StringHolder& name,
271  const PDG_WorkItem* work_item=nullptr,
272  int index=0,
273  int multi=-1) const
274  {
275  UT_StringHolder result = "";
276  myNode->evaluateRaw(result, name, work_item, index, multi);
277  return result;
278  }
279 
280  /// Evaluates a parameter as a float and returns the value back, instead
281  /// of an error code.
283  const UT_StringHolder& name,
284  const PDG_WorkItem* work_item=nullptr,
285  int index=0,
286  int multi=-1) const
287  {
288  fpreal result = 0;
289  myNode->evaluate(result, name, work_item, index, multi);
290  return result;
291  }
292 
293  /// Evaluates a parameter as an integer and returns the value back, instead
294  /// of an error code.
296  const UT_StringHolder& name,
297  const PDG_WorkItem* work_item=nullptr,
298  int index=0,
299  int multi=-1) const
300  {
301  exint result = 0;
302  myNode->evaluate(result, name, work_item, index, multi);
303  return result;
304  }
305 
306  /// Evaluates a parameter as a boolean and returns the value back, instead
307  /// of an error code.
308  bool evaluateB(
309  const UT_StringHolder& name,
310  const PDG_WorkItem* work_item=nullptr,
311  int index=0,
312  int multi=-1) const
313  {
314  bool result = false;
315  myNode->evaluate(result, name, work_item, index, multi);
316  return result;
317  }
318 
319  /// Evaluates a parameter as an arbitrary type and returns the value back,
320  /// instad of an error code.
321  template <typename T>
323  const UT_StringHolder& name,
324  const PDG_WorkItem* work_item=nullptr,
325  int index=0,
326  int multi=-1) const
327  {
328  T result{};
329  myNode->evaluate(result, name, work_item, index, multi);
330  return result;
331  }
332 
333 protected:
336 };
337 
338 #endif
UT_StringHolder evaluateS(const UT_StringHolder &name, const PDG_WorkItem *work_item=nullptr, int index=0, int multi=-1) const
#define PDG_API
Definition: PDG_API.h:23
int64 exint
Definition: SYS_Types.h:125
fpreal evaluateF(const UT_StringHolder &name, const PDG_WorkItem *work_item=nullptr, int index=0, int multi=-1) const
**But if you need a result
Definition: thread.h:613
exint evaluateI(const UT_StringHolder &name, const PDG_WorkItem *work_item=nullptr, int index=0, int multi=-1) const
virtual bool reloadInstance(UT_WorkBuffer &errors)
virtual int64 getMemoryUsage(bool inclusive) const
T evaluateT(const UT_StringHolder &name, const PDG_WorkItem *work_item=nullptr, int index=0, int multi=-1) const
long long int64
Definition: SYS_Types.h:116
GLuint const GLchar * name
Definition: glcorearb.h:786
fpreal64 fpreal
Definition: SYS_Types.h:277
GLuint index
Definition: glcorearb.h:786
bool evaluateB(const UT_StringHolder &name, const PDG_WorkItem *work_item=nullptr, int index=0, int multi=-1) const
bool evaluateArray(T &result, const UT_StringHolder &name, const PDG_WorkItem *work_item=nullptr, int multi=-1) const
PDG_CallbackResult
Enumeration of callback return types.
Definition: PDG_NodeTypes.h:48
GenerationType
Work item generation type.
UT_StringHolder evaluateRaw(const UT_StringHolder &name, const PDG_WorkItem *work_item=nullptr, int index=0, int multi=-1) const
type
Definition: core.h:1059
A partition, which has a list of upstream components.
bool evaluate(T &result, const UT_StringHolder &name, const PDG_WorkItem *work_item=nullptr, int index=0, int multi=-1) const
Convenience method for evaluating a parameter on the underlying node.