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(PDG_NodeSet& dirty,
232  bool dynamic_inputs) override;
233 
234  /// Adds dependencies on input nodes based on the current generation
235  /// settings
236  void addInputDeps(PDG_Node* input_node) override;
237 
238  /// Returns true if the work item should be scheduled, or false if it
239  /// should be immediately marked as cooked.
240  bool shouldSchedule(
241  const PDG_WorkItem* work_item) const override;
242 
243 private:
244  /// Enumeration of service status
245  enum ServiceStatus
246  {
247  /// Services have not yet been checked/started
248  eServiceStatusUnstarted,
249 
250  /// Services have been successfully started
251  eServiceStatusStarted,
252 
253  /// Services failed to start
254  eServiceStatusFailed,
255 
256  /// Services are not used/disabled
257  eServiceStatusUnused,
258  };
259 
260  /// Map of parent work item ID to the set of work items that have
261  /// that work item as a parent
262  using ParentMap = UT_ConcurrentHashMap<PDG_WorkItemID, PDG_WorkItemIDMap>;
263 
264  /// Queries for existing work items generate in this node for the
265  /// specified upstream item.
266  bool workItemsForParent(
267  PDG_WorkItemArray& work_items,
268  PDG_BatchWorkItemSet& batches,
269  const PDG_WorkItemArray& upstream_items);
270 
271  /// Generates work items from an upstream work item
272  bool generateFromUpstream(
273  PDGE_Resolutions& resolutions,
274  const PDG_WorkItemArray& upstream_items);
275 
276  /// Adds internal dependencies between the specified work items, i.e.
277  /// dependencies between sibling work items.
278  void addInternalDependencies(
279  const PDG_WorkItemArray& work_items);
280 
281  /// Adds the specified work items to this node.
282  void addWorkItems(
283  const PDG_WorkItemArray& all_items,
284  const PDG_WorkItemArray& work_items,
285  const PDG_BatchWorkItemSet& batches,
286  const PDG_WorkItemPairArray& pairs,
288  bool compact_items,
289  bool sort_items);
290 
291  /// Sets the label for a specific work item
292  void setWorkItemLabel(
293  PDG_WorkItem* work_item,
294  int thread) const;
295 
296  /// Sets the label and command for the specified work items
297  void configureWorkItems(
298  const PDG_WorkItemArray& work_items,
299  PDG_EvaluationContext::State state) const;
300 
301  /// Configures services for this node
302  void configureServices();
303 
304  /// Checks if any of the expected output files in the node have
305  /// duplicate paths, and emits a warning if they do
306  void checkDuplicateOutputs() const;
307 
308 private:
309  ParentMap myParentMap;
310  PDG_WorkItemArray myPendingWorkItems;
311  PDG_BatchWorkItemSet myPendingBatches;
312 
313  PriorityType myPriorityType;
314  LabelType myLabelType;
315  GenerateWhen myGenerateWhen;
316  CacheMode myCacheMode;
317  PDG_WorkItemCookType myCookType;
318 
319  ServiceStatus myServiceStatus;
320  PDG_ServiceResetWhen myServiceResetWhen;
321  PDG_ServiceResetType myServiceResetType;
322 
323  mutable UT_Lock myServiceStatusLock;
324  mutable UT_TBBSpinLock myPendingLock;
325  bool myHadInjection;
326  bool myUseScheduleCondition;
327 };
328 
329 #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:615
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:656
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:648
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:666
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.
virtual bool cacheBuiltinParms(PDG_NodeSet &dirty, bool dynamic_inputs)
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.