HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PDG_Processor.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_PROCESSOR_H__
10 #define __PDG_PROCESSOR_H__
11 
12 #include "PDG_API.h"
13 
14 #include "PDG_EvaluationContext.h"
15 #include "PDG_Node.h"
16 
18 #include <UT/UT_Lock.h>
19 #include <UT/UT_TBBSpinLock.h>
20 #include <UT/UT_UniquePtr.h>
21 
22 class PDG_WorkItemHolder;
23 
24 /**
25  * Concrete node type that creates new work items from upstream items. This
26  * node can either create items each item an upstream item is cooked, or when
27  * all upstream items are generated or cooked. Work items can also be injected
28  * into the node.
29  */
31 {
32 public:
33  /// Prioritization schemes that node can use when generating work items.
35  {
36  /// Inheirt the priority from the upstream work items, or 0 if there
37  /// are no upstream items
39 
40  /// Set the priority based on an expression
42 
43  /// Set the priority using logic in the node. Only used by
44  /// ROP Fetch
45  ePriorityNode
46  };
47 
48  /// Labeling scheme that node can use when generating work item.
49  enum LabelType
50  {
51  /// Use the default label field on the root graph, or no label
52  /// if there is no default
54 
55  /// Inherit the label from the parent item
57 
58  /// Set the label based on an expression
60 
61  /// Set the label baed on custom node logic
62  eLabelNode
63  };
64 
65  /// The caching mode, used to control whether or not the node should
66  /// attempt to read existing files from disk prior to cooking
67  enum CacheMode
68  {
69  /// Automatic - checks if the file exists and uses it if it does
71 
72  /// Always Read - always tries to read, and fails if the file does not
73  /// exist
75 
76  /// Always Write - always writes, always spawns a job to do the cook
77  /// and possibly overrwrites existing files
79 
80  /// Same automatic, but the node will not invalidate work items due to
81  /// upstream cache changes
83  };
84 
85  /// Enumeration of generation conditions
87  {
88  /// Generate work items each time an input work item cooks
90 
91  /// Generate work items when all inputs work items are generated
93 
94  /// Automatic generation mode
96 
97  /// Generate work items when all inputs work items are cooked
99  };
100 
101 public:
102  /// Constructs an new processor from graph context, name, callback type
103  /// and custom id. Most of the initialization for this class is handled
104  /// in the PDG_Node base.
106  PDG_GraphContext* context,
107  const UT_StringHolder& name,
108  const PDG_NodeCallbackType* type_object,
109  int custom_id = -1);
110 
111  ~PDG_Processor() override {}
112 
113  /// Returns the total memory usage of the node, including any work items
114  /// owned by it.
115  void memoryInfo(PDG_MemoryInfo& memory_info,
116  bool inclusive) const override;
117 
118  /// Returns the priority scheme used by this node. The default is to
119  /// inherit from the upstream work item, if any.
120  inline PriorityType prioritizeBy() const
121  { return myPriorityType; }
122 
123  /// Returns the cook type for work items created by this node.
125  { return myCookType; }
126 
127  /// Returns true if the processor has been configured to use services
128  /// for the current cook.
129  inline bool useServices() const
130  {
131  return (myServiceStatus ==
132  eServiceStatusStarted);
133  }
134 
135  /// Returns the condition when this node resets service clients
137  { return myServiceResetWhen; }
138 
139  /// Returns the reset type for service clients used by this node
141  { return myServiceResetType; }
142 
143  /// Constructs a new work item holder for this node, for work item
144  /// injection. The caller takes ownership of the object.
146  workItemHolder();
147 
148  /// Injects work items using a holder, which should be constructed with the
149  /// this->workItemHolder() method. If compact_items is true, the node's
150  /// work item array is compacted, prioritized and sorted after injecting
151  /// items.
152  void injectStaticItems(
153  PDG_WorkItemHolder& holder,
154  bool compact_items,
155  bool sort_items);
156 
157  /// Resets this dependency owner
158  void resetOwner() override;
159 
160  /// Returns the priority for the specified work item
161  int itemPriority(const PDG_WorkItem* work_item,
162  PriorityType priority_type) const;
163 
164  /// Deserialized the specified work item into this node. Most of the
165  /// work is handled by the parent class, but processor nodes need to
166  /// update the myParentMap.
167  bool deserializeWorkItem(
168  PDG_WorkItem* work_item) override;
169 
170  /// Called after deserializing a work item and adding its dependencies
171  void commitWorkItem(PDG_WorkItem* work_item) override;
172 
173  /// Returns the cache state of a work item in this node
175  const PDG_WorkItem* work_item) const override;
176 
177 protected:
178  /// Runs node-specific pre cook logic. Called before anything in the graph
179  /// evaluates.
180  bool preCook(bool is_dirty,
181  bool has_filter) override;
182 
183  /// Runs node-specific post cook logic. Called after all work items have
184  /// finished cooking.
185  void postCook() override;
186 
187 
188  /// Called when this node should clear all work items. Management of the
189  /// main work item array is handled by the parent class, but processors
190  /// need to clear their myParentMap contents.
191  void clearAllWorkItems() override;
192 
193  /// Called when this node should clear a specific work item. Processor
194  /// nodes need to update the myParentMap accordingly.
195  void clearWorkItem(
196  const PDG_WorkItem* work_item) override;
197 
198  /// Called when this node should add a work item. Processor nodes need to
199  /// update the myParentMap accordingly.
200  void addWorkItem(PDG_WorkItem* work_item) override;
201 
202  /// Syncs data for the work item, using node-specific logic
203  bool syncWorkItem(
204  PDG_WorkItem* work_item,
205  const PDG_WorkItem* clone_target) override;
206 
207  /// Checks if a work item is cached based on its expected outputs and the
208  /// current output cache state
210  PDG_WorkItem* work_item) const override;
211 
212  /// Called when a dependency owned by this node is resolved. This method
213  /// will generate work items when the generation mode is set to either
214  /// AllInputsCooked or AllInputsGenerated.
216  PDGE_Resolutions& resolutions,
217  const PDGE_Evaluator& evaluator,
218  PDGE_Dependency* dependency) override;
219 
220  /// Called when a dependency owned by thhis node receives a partial
221  /// resolution. This method will generate work items when the generation
222  /// mode is set to EachInputCooked.
224  PDGE_Resolutions& resolutions,
225  const PDGE_Evaluator& evaluator,
226  PDGE_Dependency* dependency,
227  const PDGE_DependencyOwner::Array& owners)
228  override;
229 
230  /// Caches any built in parm state for the current cook
231  bool cacheBuiltinParms(
232  PDG_NodeSet& dirty,
233  int num_inputs,
234  bool dynamic_inputs) override;
235 
236  /// Adds dependencies on input nodes based on the current generation
237  /// settings
238  void addInputDeps(PDG_Node* input_node) override;
239 
240  /// Returns true if the work item should be scheduled, or false if it
241  /// should be immediately marked as cooked.
242  bool shouldSchedule(
243  const PDG_WorkItem* work_item) const override;
244 
245 private:
246  /// Enumeration of service status
247  enum ServiceStatus
248  {
249  /// Services have not yet been checked/started
250  eServiceStatusUnstarted,
251 
252  /// Services have been successfully started
253  eServiceStatusStarted,
254 
255  /// Services failed to start
256  eServiceStatusFailed,
257 
258  /// Services are not used/disabled
259  eServiceStatusUnused,
260  };
261 
262  /// Map of parent work item ID to the set of work items that have
263  /// that work item as a parent
264  using ParentMap = UT_ConcurrentHashMap<PDG_WorkItemID, PDG_WorkItemIDMap>;
265 
266  /// Queries for existing work items generate in this node for the
267  /// specified upstream item.
268  bool workItemsForParent(
269  PDG_WorkItemArray& work_items,
270  PDG_BatchWorkItemSet& batches,
271  const PDG_WorkItemArray& upstream_items);
272 
273  /// Generates work items from an upstream work item
274  bool generateFromUpstream(
275  PDGE_Resolutions& resolutions,
276  const PDG_WorkItemArray& upstream_items);
277 
278  /// Adds internal dependencies between the specified work items, i.e.
279  /// dependencies between sibling work items.
280  void addInternalDependencies(
281  const PDG_WorkItemArray& work_items);
282 
283  /// Adds the specified work items to this node.
284  void addWorkItems(
285  const PDG_WorkItemArray& all_items,
286  const PDG_WorkItemArray& work_items,
287  const PDG_BatchWorkItemSet& batches,
288  const PDG_WorkItemPairArray& pairs,
290  bool compact_items,
291  bool sort_items);
292 
293  /// Sets the label for a specific work item
294  void setWorkItemLabel(
295  PDG_WorkItem* work_item,
296  int thread) const;
297 
298  /// Sets the label and command for the specified work items
299  void configureWorkItems(
300  const PDG_WorkItemArray& work_items,
301  PDG_EvaluationContext::State state) const;
302 
303  /// Configures services for this node
304  void configureServices();
305 
306  /// Checks if any of the expected output files in the node have
307  /// duplicate paths, and emits a warning if they do
308  void checkDuplicateOutputs() const;
309 
310 private:
311  ParentMap myParentMap;
312  PDG_WorkItemArray myPendingWorkItems;
313  PDG_BatchWorkItemSet myPendingBatches;
314 
315  PriorityType myPriorityType;
316  LabelType myLabelType;
317  GenerateWhen myGenerateWhen;
318  CacheMode myCacheMode;
319  PDG_WorkItemCookType myCookType;
320 
321  ServiceStatus myServiceStatus;
322  PDG_ServiceResetWhen myServiceResetWhen;
323  PDG_ServiceResetType myServiceResetType;
324 
325  mutable UT_Lock myServiceStatusLock;
326  mutable UT_TBBSpinLock myPendingLock;
327  bool myHadInjection;
328  bool myUseScheduleCondition;
329 };
330 
331 #endif /* __PDG_PROCESSOR_H__ */
PDG_ServiceResetType serviceResetType() const
Returns the reset type for service clients used by this node.
Automatic - checks if the file exists and uses it if it does.
Definition: PDG_Processor.h:70
LabelType
Labeling scheme that node can use when generating work item.
Definition: PDG_Processor.h:49
PDG_ServiceResetType
virtual PDGE_Dependency::State evalPartial(PDGE_Resolutions &, const PDGE_Evaluator &, PDGE_Dependency *, const Array &)
void addWorkItems(const PDG_WorkItemArray &work_items, const PDG_BatchWorkItemSet &batches, PDG_EvaluationContext::State state, bool sort_items)
#define PDG_API
Definition: PDG_API.h:23
virtual bool deserializeWorkItem(PDG_WorkItem *work_item)
State
Evaluation context state.
PriorityType
Prioritization schemes that node can use when generating work items.
Definition: PDG_Processor.h:34
virtual PDG_WorkItemCacheState outputCacheState(const PDG_WorkItem *work_item) const
Returns the cache state of a work item in this node.
virtual void clearAllWorkItems()
friend class PDG_Processor
Definition: PDG_Node.h:619
PDG_WorkItemCookType cookType() const
Returns the cook type for work items created by this node.
GenerateWhen
Enumeration of generation conditions.
Definition: PDG_Processor.h:86
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
virtual void postCook()
virtual void clearWorkItem(const PDG_WorkItem *work_item)
Generate work items when all inputs work items are generated.
Definition: PDG_Processor.h:92
PDG_WorkItemState
Enum of possible work item states.
Inherit the label from the parent item.
Definition: PDG_Processor.h:56
PDG_ServiceResetWhen
When service clients should be reset.
virtual PDG_WorkItemState updateOutputCache(PDG_WorkItem *work_item) const
Definition: PDG_Node.h:661
Generate work items when all inputs work items are cooked.
Definition: PDG_Processor.h:98
PDG_WorkItemCookType
Enumeration of work item cook types.
GLuint const GLchar * name
Definition: glcorearb.h:786
Set the priority based on an expression.
Definition: PDG_Processor.h:41
virtual bool syncWorkItem(PDG_WorkItem *work_item, const PDG_WorkItem *clone_target)
Definition: PDG_Node.h:653
virtual void addInputDeps(PDG_Node *input_node)
PDG_WorkItemCacheState
**Note that the tasks the is the thread number *for the or if it s being executed by a non pool thread(this *can happen in cases where the whole pool is occupied and the calling *thread contributes to running the work load).**Thread pool.Have fun
PriorityType prioritizeBy() const
Set the label based on an expression.
Definition: PDG_Processor.h:59
bool preCook(PDG_NodeSet &dirty, PDG_NodeSet &visited, bool &save_scene, bool has_filter)
Runs precook logic for this node and recurses into input nodes.
virtual void commitWorkItem(PDG_WorkItem *work_item)
Called after a work item is completely deserialized.
~PDG_Processor() override
virtual bool shouldSchedule(const PDG_WorkItem *work_item) const
Definition: PDG_Node.h:673
Generate work items each time an input work item cooks.
Definition: PDG_Processor.h:89
virtual PDGE_Dependency::State evalResolve(PDGE_Resolutions &, const PDGE_Evaluator &, PDGE_Dependency *)
Called when a dependency owned by this object is resolved.
PDG_ServiceResetWhen serviceResetWhen() const
Returns the condition when this node resets service clients.
void resetOwner() override
Resets the owner.
virtual void addWorkItem(PDG_WorkItem *work_item)
Automatic generation mode.
Definition: PDG_Processor.h:95
bool useServices() const
virtual void memoryInfo(PDG_MemoryInfo &memory_info, bool inclusive) const
Returns the memory usage as a PDG_MemoryInfo struct.
virtual bool cacheBuiltinParms(PDG_NodeSet &dirty, int num_inputs, bool dynamic_inputs)