HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PDG_WorkItem.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_WORKITEM_H__
10 #define __PDG_WORKITEM_H__
11 
12 #include "PDG_API.h"
13 
14 #include "PDG_AttributeMap.h"
15 #include "PDG_AttributeOwner.h"
16 #include "PDG_AttributeTypes.h"
17 #include "PDG_EventEmitter.h"
18 #include "PDG_File.h"
19 #include "PDG_LoopInfo.h"
20 #include "PDG_Types.h"
21 #include "PDG_WorkItemDirty.h"
22 #include "PDG_WorkItemStats.h"
23 
24 #include <PDGE/PDGE_Dependency.h>
26 #include <PDGE/PDGE_Resolutions.h>
27 #include <PDGT/PDGT_Value.h>
28 
29 #include <UT/UT_ArrayStringSet.h>
30 #include <UT/UT_StringHolder.h>
31 #include <UT/UT_TBBSpinLock.h>
32 #include <UT/UT_WorkBuffer.h>
33 
35 class PDG_BatchWorkItem;
36 class PDG_Graph;
37 class PDG_GraphContext;
38 struct PDG_MemoryInfo;
39 class PDG_Node;
40 class PDG_Scheduler;
41 class PDG_WorkItemData;
42 
44  public PDG_AttributeOwner,
46 {
47 public:
48  /// Default, invalid work item ID value.
49  static constexpr PDG_WorkItemID theInvalidId = -1;
50 
51  /// Enumeration of pending dirty states
53  {
54  /// The item does not require any sort of dirtying
56 
57  /// The item needs to be dirtied, but not deleted
59 
60  /// the item needs to be deleted
61  ePendingDelete
62  };
63 
64 public:
65  /// Constructs and initialize a new work item. This method will get a
66  /// unique id for the item from the PDG_Graph, unless a valid id is
67  /// passed in
69  PDG_GraphContext* context,
70  bool is_static = true,
72  PDG_WorkItemID id = theInvalidId);
73 
74  /// Deletes this work item. Does not delete any dependencies or remove
75  /// references in other items or node. The appropriate dirty method must
76  /// be called first.
77  ~PDG_WorkItem() override;
78 
79  /// Returns the list of supported event types
81 
82  /// Returns memory usage for this work item
83  int64 getMemoryUsage(bool inclusive) const override;
84 
85  /// Returns a break down of the memory used by the work item
86  virtual void memoryInfo(PDG_MemoryInfo& memory_info,
87  bool inclusive) const;
88 
89  /// Returns the owning node's attribute lock
90  UT_RWLock* attribGlobalLock() const override;
91 
92  /// Returns true if the work item's attributes are writeable
93  bool attribCanWrite(
94  const PDG_EvaluationContext* ctx) const override;
95 
96  /// Called when the attributes have been unlocked
97  void attribUnlock() const override;
98 
99  /// Called when an attributed owned by the work item is modified
100  void attribModify(
101  PDG_AttributeType attrib_type,
102  const UT_StringHolder& attrib_name) const override;
103 
104  /// Called when an attribute operation produces a non-fatal warning
105  void attribWarn(
106  const UT_StringHolder& message) const override;
107 
108  /// Returns a descriptive name of the attribute owner
109  UT_StringHolder attribOwnerName() const override;
110 
111  /// Resets the work item's dependency objects
112  void resetOwner() override;
113 
114  /// Returns the debug name for this work item, used by PDGE for
115  /// logging and debuggin features
116  UT_StringHolder debugName() const override
117  { return name(); }
118 
119  /// Returns the debug group for this work item, used by PDGE for
120  /// outputting a DOT graph.
121  UT_StringHolder debugGroup() const override;
122 
123 
124  /// Returns the can cook dependency object for this work item
126  { return &myCanCookDep; }
127 
128  /// Returns the is cooked dependency object for this work item
130  { return &myIsCookedDep; }
131 
132  /// Returns the work item type
134  { return myType; }
135 
136  /// Returns the work item execution type
138  { return myExecutionType; }
139 
140  /// Returns the work item cook type
142  { return myCookType; }
143 
144  /// Returns the id of the work item, guaranteed to be unique
146  { return myId; }
147 
148 
149  /// Returns the name of the work item, guaranteed to be unique
150  UT_StringHolder name() const;
151 
152  /// Returns true if the work item has a command line string set, for
153  /// any platform
154  bool hasCommand() const;
155 
156  /// Returns true if the work item has platform-specific command line
157  /// strings
158  bool hasPlatformCommand() const;
159 
160  /// Returns the generic command associated with the work item
161  const UT_StringHolder& command() const;
162 
163  /// Returns the platform specific command associated with the work item,
164  /// or the command for the current platform if no platform is specified
165  const UT_StringHolder& platformCommand(
166  PDG_Platform platform=
168 
169  /// Returns true if the command should run in the shell
170  bool shouldRunInShell() const;
171 
172  /// Returns the label assocaited with the work item
173  UT_StringHolder label() const;
174 
175  /// Returns true if the work item has a custom label
176  bool hasLabel() const;
177 
178  /// Returns the custom state string associated with the work item, if
179  /// one has be set.
180  const UT_StringHolder& customState() const;
181 
182  /// Returns true if the work item has a custom state string
183  bool hasCustomState() const;
184 
185 
186  /// Returns the node that owns the work item
187  PDG_Node* node() const
188  { return myNode; }
189 
190  /// Returns the context that owns the work item
192  { return myContext; }
193 
194  /// Returns the graph that the work item belongs to
195  PDG_Graph* graph() const;
196 
197 
198  /// Returns the batch item that owns the work item, or nullptr if
199  /// the item is not in a batch
201  { return myBatchParent; }
202 
203  /// Returns the clone target for the work item, or nullptr if a specific
204  /// target was not set
205  const PDG_WorkItem* cloneTarget() const
206  { return myCloneTarget; }
207 
208  /// Const acessor to the work item's parent
209  const PDG_WorkItem* parent() const;
210 
211  /// Returns the work item that this work item copied its attributes
212  /// from. If the work item is a partition, this method returns nullptr.
213  const PDG_WorkItem* attributeSource() const;
214 
215 
216  /// Returns the work item index
217  int index() const
218  { return myIndex; }
219 
220  /// Returns the work item's internal index
221  int internalIndex() const
222  { return myInternalIndex; }
223 
224  /// Returns the work items index within its batch, or -1 if the work item
225  /// is not in a batch
226  int batchIndex() const
227  { return myBatchIndex; }
228 
229  /// Returns the work item priority
230  int priority() const
231  { return myPriority; }
232 
233 
234  /// Returns the cache id of the work item
235  int cacheId() const
236  { return myCacheId; }
237 
238  /// Returns the work item frame
239  fpreal frame() const
240  { return myFrame; }
241 
242  /// Returns true if the frame has been set
243  bool hasFrame() const
244  { return myHasFrame; }
245 
246  /// Returns the work item step size, primarily used with batch items
248  { return myFrameStep; }
249 
250  /// Returns true if the work item has any warning log messages
251  bool hasWarnings() const
252  { return myHasWarnings; }
253 
254  /// Returns true if the item is a static work item
255  bool isStatic() const
256  { return myIsStatic; }
257 
258  /// Returns true if the work item is a regular item or batch item
259  bool isRegular() const;
260 
261  /// Returns true if the work item is a partition type
262  bool isPartition() const;
263 
264  /// Returns true if the work item is a dynamic, regular work item
265  bool isDynamic() const;
266 
267  /// Returns true if the work tiem is a batch
268  bool isBatch() const;
269 
270  /// Returns true if the work item is frozen
271  bool isFrozen() const
272  { return myIsFrozen; }
273 
274  /// Returns true if the work item is in process
275  bool isInProcess() const
276  { return (myCookType ==
278 
279  /// Returns true if the work item is marked as a service task
280  bool isServiceMode() const
281  { return (myCookType ==
283 
284  /// Returns true if the work item is scheduled out of process
285  bool isOutOfProcess() const
286  { return (myCookType ==
288 
289  /// Returns true if the work item is scheduled (either service, in, or out
290  /// of process).
291  bool isScheduled() const
292  {
293  if (myCookType ==
295  {
296  return false;
297  }
298 
299  if (myCookType ==
301  {
302  return false;
303  }
304 
305  return true;
306  }
307 
308  /// Returns true if the work item has post-cook logic
309  bool isPostCook() const
310  { return myIsPostCook; }
311 
312  /// Returns true if the work item is flagged as not being allowed to
313  /// generate child items.
314  bool isNoGenerate() const
315  { return myIsNoGenerate; }
316 
317  /// Returns true if the work item is marked as needing to clone output
318  /// files from its parent
319  bool isCloneOutputFiles() const
320  { return myIsCloneOutputFiles; }
321 
322  /// Returns true if the work item has to cook on the main thread, when
323  /// it cooks in process. This is determined by the script info from
324  /// the owning node
325  bool isMainThread() const;
326 
327  /// Returns the work item cook state
329  { return myState; }
330 
331  /// Returns the worst dependency state, e.g. if any of the deps have failed
332  /// or canceled.
334  { return myDepState; }
335 
336  /// Returns true if the work item is in a cooked state
337  bool isCooked() const;
338 
339  /// Returns true if the work item is in a successful cooked state
340  bool isSuccessful() const;
341 
342  /// Returns true if the work item is in a unsucessful cooked state
343  bool isUnsuccessful() const;
344 
345  /// Returns true if the work item is uncooked
346  bool isUncooked() const;
347 
348  /// Returns true if the work item has an unsucessful dependency
349  bool isUnsuccessfulDep() const;
350 
351  /// Returns true if the work item is an cooking/scheduled state
352  bool isProcessing() const;
353 
354  /// Returns true if the work item is long running
355  bool isLongRunning() const;
356 
357  /// Returns an enum indicating if work item is ready to cook, blocked, or
358  /// has failed dependencies
359  PDG_WorkItemEvalState evaluationState() const;
360 
361 
362  /// Returns the edge lock for the work item
364  { return myEdgeLock; }
365 
366  /// Returns the dependencies, e.g. work items that this item depends on. Not
367  /// thread safe.
368  const PDG_WorkItemSet& dependenciesUnsafe() const;
369 
370  /// Returns the dependents, e.g. work items that depend on this item. Not
371  /// thread safe.
372  const PDG_WorkItemSet& dependentsUnsafe() const;
373 
374  /// Returns a copy of the dependencies in an array
375  void dependencies(
376  PDG_WorkItemConstArray& dependency_array) const;
377 
378  /// Returns the dependency ids in a set
379  void dependencyIds(
380  PDG_WorkItemIDSet& dependencyIdsSet) const;
381 
382  /// Returns the number of dependencies
383  int dependencySize() const;
384 
385  /// Returns a copy of the dependents in an array
386  void dependents(
387  PDG_WorkItemConstArray& dependent_array) const;
388 
389  /// Returns the dependent ids in a set
390  void dependentIds(
391  PDG_WorkItemIDSet& dependentIdsSet) const;
392 
393  /// Returns a copy of the "visual" dependencies, e.g. the ones the user
394  /// would see if they used TOPs or wanted to print out the DAG itself. This
395  /// strips out any intenal items/dependencies used by TOPs
396  void visualDependencies(
397  PDG_WorkItemSet& deps,
398  bool expand,
399  PDG_Scheduler* filter) const;
400 
401  /// Inverse of the above
402  void visualDependents(
403  PDG_WorkItemSet& deps,
404  bool expand,
405  PDG_Scheduler* filter) const;
406 
407  /// Returns the require dependencies, e.g. deps that own this item. This is
408  /// used with partitioning to force the partition to by delete when one of
409  /// it's required dependencies is deleted
411  requiredDependencies() const;
412 
413  /// Returns true if the item is a partition, and stores the partition
414  /// components into the array passed in as a parameter
415  bool partitionItems(
416  PDG_WorkItemConstArray& components) const;
417 
418  /// Flattens dependencies by query all dependencies of the work item,
419  /// and if the the dependency is a wrapper expands it into real items that
420  /// the wrapper depends on.
421  void sortedDependencies(
422  PDG_WorkItemConstArray& sorted_depencies,
423  bool only_upstream=false) const;
424 
425  /// Adds a dependency to the work item, and a corresponding dependent
426  /// back pointer to the incoming item.
427  ///
428  /// Returns false if the dependency already exists, and returns true if the
429  /// dependency does not
430  bool addDependency(
431  PDG_WorkItem* work_item,
432  bool required = false,
433  bool user_defined = false);
434 
435  /// Marks an existing dependency as a required dep. The work item passed in
436  /// *must* already be a dependency
437  void addRequiredDependency(
438  const PDG_WorkItem* work_item,
439  bool user_defined);
440 
441  /// Checks if the work item has a given item as a dependency
442  bool hasDependency(PDG_WorkItem* work_item);
443 
444  /// Removes a dependency and the corresponding back pointer from the target
445  /// work item.
446  bool removeDependency(PDG_WorkItem* work_item);
447 
448  /// Updates the loop stack, with the specified parent
449  void pushLoopInfoStack(
450  const PDG_WorkItem* parent_item);
451 
452  /// Sets the loop stack contents
453  void setLoopInfoStack(
454  const PDG_LoopInfo::Stack& loop_stack);
455 
456  /// Returns the loop stack entry at the specified index;
457  PDG_WorkItem* loopInfoStackEntry(int index) const;
458 
459  /// Returns the loop iteration of the work item
460  int loopInfoIteration() const;
461 
462  /// Returns the loop number of the work item
463  int loopInfoNumber() const;
464 
465  /// Returns the size of the loop that contains the work item
466  int loopInfoSize() const;
467 
468  /// Returns the loop depth of the work item
469  int loopInfoDepth() const;
470 
471  /// Returns the loop service lock ID of the work item
472  PDG_WorkItemID loopInfoLock() const;
473 
474  /// Returns the loop stack for this work item
476  { return myLoopInfoStack; }
477 
478  /// Compares the loop info stack of another work item with this one,
479  /// returning true if they put in the same partition or false if they're
480  /// from independent loops.
481  bool compareLoopInfoStack(
482  const PDG_WorkItem* other) const;
483 
484  /// Sets the is post cook flag
485  void setIsPostCook(bool postcook);
486 
487  /// Sets the work item no generate flag
488  void setIsNoGenerate(bool no_gen);
489 
490  /// Sets the work item's clone output files flag
491  void setIsCloneOutputFiles(bool clone);
492 
493  /// Sets the work item state
494  bool setState(
495  PDG_WorkItemState state,
496  bool emit=true);
497 
498  /// Sets the frozen flag on the work item
499  void setIsFrozen(bool is_frozen);
500 
501  /// Sets the execution type of the work item
502  void setExecutionType(
503  PDG_WorkItemExecutionType execution_type);
504 
505  /// Sets the cook type for the work item
506  void setCookType(PDG_WorkItemCookType cook_type);
507 
508  /// Returns the work item's modified flag
509  bool wasModified() const
510  { return myWasModified; }
511 
512  /// Sets the work item index
513  void setIndex(int index);
514 
515  /// Sets the work items internal index, used for dirtying
517  { myInternalIndex = index; }
518 
519  /// Sets the work item priority
520  void setPriority(int priority, bool emit=true);
521 
522  /// Boosts the priority of the work item and its dependencies, if they
523  /// have not yet started to cook
524  void boostPriority();
525 
526  /// Sets the work item frame. This methods takes a step size since it gets
527  /// overridden by batch work items to initialize the frame of each sub item
528  /// as frame + index*step_size;
529  bool clearFrame();
530  bool setFrame(fpreal frame);
531  bool setFrame(fpreal frame, fpreal frame_step);
532 
533  /// Sets the work item node
534  void setNode(PDG_Node* node, bool emit);
535 
536  /// Sets the pending dirty flag on the work item, used for regeneration
537  virtual void setPendingDirty(PendingDirty pending);
538 
539  /// Called when the work item should begin regenerating
540  void startRegenerating();
541 
542  /// Called when the work item is finished regenerating, in order to
543  /// determine if it needs to be deleted or dirtied
544  PendingDirty doneRegenerating();
545 
546  /// Sets the clone target, e.g. the item from which this item will
547  /// clone attributes
548  void setCloneTarget(
549  const PDG_WorkItem* clone_target);
550 
551  /// Sets the batch item info, e.g. the batch item pointer and the index
552  /// within the batch.
553  void setBatchInfo(PDG_BatchWorkItem* item,
554  int index,
555  int batch_index,
556  int priority);
557 
558  /// Updates the loop iteration and number, at the head of the current
559  /// loop stack
560  void setLoopInfo(
561  int iteration,
562  int number,
563  int size,
564  PDG_WorkItemID service_lock=theInvalidId);
565 
566  /// Sets the work item command directly
567  void setCommand(
568  const UT_StringHolder& command,
569  bool shell=false);
570 
571  /// Sets a per-platform work item command
572  void setPlatformCommands(
573  const UT_StringHolder& linux,
574  const UT_StringHolder& mac,
575  const UT_StringHolder& windows,
576  bool shell=false);
577 
578  /// Sets the work item label
579  void setLabel(const UT_StringHolder& label);
580 
581  /// Clears the work item label
582  void clearLabel();
583 
584  /// Sets the custom state string. Note that custom states are cleared
585  /// any time the actual work item state changes.
586  void setCustomState(const UT_StringHolder& state);
587 
588  /// Clears the custom state string
589  void clearCustomState();
590 
591  bool hasCookPercent() const;
592  fpreal cookPercent() const;
593  void setCookPercent(const fpreal &cook_percent);
594 
595  /// Appends a formatted error message to the work item's in process
596  /// log buffer
597  template <typename... Args>
599  const char* fmt,
600  Args&&... args) const
601  {
602  appendLogFmt(
603  true,
604  0,
605  "ERROR",
606  fmt,
607  std::forward<Args>(args)...);
608  }
609 
610  /// Appends a plain error message to the work item's in process
611  /// log buffer
612  void addError(
613  const UT_StringHolder& log,
614  bool timestamp=true) const
615  {
616  appendLogPrefix(
617  timestamp,
618  0,
619  "ERROR",
620  log);
621  }
622 
623  /// Appends a formatted warning message to the work item's in process
624  /// log buffer
625  template <typename... Args>
627  const char* fmt,
628  Args&&... args) const
629  {
630  appendLogFmt(
631  true,
632  0,
633  "WARNING",
634  fmt,
635  std::forward<Args>(args)...);
636  myHasWarnings = true;
637  }
638 
639  /// Appends a plain warning message to the work item's in process
640  /// log buffer
642  const UT_StringHolder& log,
643  bool timestamp=true,
644  int verbosity=0) const
645  {
646  appendLogPrefix(
647  timestamp,
648  verbosity,
649  "WARNING",
650  log);
651  myHasWarnings = true;
652  }
653 
654  /// Appends a formatted message to the work item's in process
655  /// log buffer
656  template <typename... Args>
658  const char* fmt,
659  Args&&... args) const
660  {
661  appendLogFmt(
662  true,
663  0,
665  fmt,
666  std::forward<Args>(args)...);
667  }
668 
669  /// Appends a plain message to the work item's in process log buffer
671  const UT_StringHolder& log,
672  bool timestamp=true,
673  int verbosity=0) const
674  {
675  appendLogPrefix(
676  timestamp,
677  verbosity,
679  log);
680  }
681 
682  /// Appends text data to the log with the given log type
683  void addLog(
685  const UT_StringHolder& log,
686  bool timestamp=true,
687  int verbosity=0) const;
688 
689  /// Returns the current in process log buffer
690  const UT_WorkBuffer& logMessages() const
691  { return myLogMessages; }
692 
693  /// Returns the application path to the work item, e.g. /node/workitem
694  void appPath(UT_WorkBuffer& buffer) const;
695 
696  /// Dirties the work item by initiating a dirty operation from the graph.
697  /// Probably the method you want to call to dirty the item if you aren't
698  /// working in $PDG directly
699  void dirty(bool should_delete, bool remove_outputs);
700 
701  /// Prepares the work item for dirtying, but does not actually modify the
702  /// item or its dependencies in any way.
703  PDG_WorkItemDirty prepareDirty(bool should_delete);
704 
705  /// Cancels the work item's execution, if it's running with a scheduler
706  void cancel();
707 
708  /// Freezes the attributes of the work item and clears out all references
709  /// to upstream work items and data.
710  void freeze();
711 
712  /// Invalidates the file cache of this work item
713  void invalidateCache();
714 
715  /// Syncs work item data with its parent
716  void syncData(bool force_recursive);
717 
718  /// Getter/setter for the backing work item data object, which might be a
719  /// user defined class
720  PDG_WorkItemData* data() const;
721  void setData(PDG_WorkItemDataPtr data);
722  bool compareDataType(
723  const PDG_BaseType* other_type,
724  bool deep=true) const;
725  SYS_HashType hash() const;
726 
727  /// Does a numeric data lookup, for use with the @ operator
728  PDG_AttributeCast numericData(
729  fpreal& param,
730  const PDG_AttributeEvaluator& evaluator,
731  fpreal frame,
732  int thread) const;
733 
734  /// Does a string data lookup, for use with the @ operator
735  PDG_AttributeCast stringData(
737  const PDG_AttributeEvaluator& evaluator,
738  fpreal frame,
739  int thread) const;
740 
741  /// Serialization of underlying PDG_WorkItemData object
742  UT_StringHolder serializeData() const;
743  bool serializeDataToFile(
744  const UT_StringHolder& file) const;
745 
746  /// Compares the work item with another item
747  bool compare(PDG_WorkItem* item) const;
748 
749 
750  /// Runs selection logic from selecting a work item in TOPs
751  bool uiSelect(bool select);
752 
753 
754  /// Returns true if the work item has at least one output file
755  bool hasOutputFiles() const;
756 
757  /// Returns the raw output file array for the work item. The paths in this
758  /// array are unlocalized, i.e. they may contain __PDG_DIR__
759  bool outputFiles(
760  PDG_File::Array& files) const;
761 
762  /// Localizes output files and returns it back in the supplied array.
763  /// Returns false if the work item has no node or the node has no
764  /// scheduler.
765  bool localizedOutputFiles(
766  PDG_File::Array& files) const;
767 
768  /// Returns the local temporary file directory from the sechduler
769  /// associated with this work item.
770  UT_StringHolder tempDir() const;
771 
772  /// Returns all output files that are a substring-match for the given tag
773  bool outputFilesForTag(
774  PDG_File::Array& files,
775  const UT_StringHolder& tag,
776  bool include_expected = false) const;
777 
778  /// Localizes a file path with the scheduler associated with the work
779  /// item
780  UT_StringHolder localizePath(
781  const UT_StringHolder& path) const;
782 
783  /// Returns the first output file that is a substring-match for the given
784  /// tag
785  UT_StringHolder firstOutputFileForTag(
786  const UT_StringHolder& tag) const;
787 
788  /// Returns the list of expected output files
789  bool expectedOutputFiles(
790  PDG_File::Array& files) const;
791 
792  /// Adds an expected output file to the work item
793  void addExpectedOutputFile(
794  const UT_StringHolder& path,
795  const UT_StringHolder& tag,
796  bool own);
797 
798  /// Adds an array of expected output file paths to the work item
799  void addExpectedOutputFiles(
800  const UT_StringArray& paths,
801  const UT_StringHolder& tag,
802  bool own);
803 
804  /// Adds an expected output file to the work item
805  void addExpectedOutputFile(const PDG_File& file);
806 
807  /// Appends the given output file to the work item
808  void addOutputFile(
809  const UT_StringHolder& path,
810  const UT_StringHolder& tag,
811  PDG_File::Hash hash_code,
812  bool own);
813 
814  /// Appends the given array of output files to the work item. If the
815  /// length of the hashes array is less than the paths, a default value
816  /// of 0 will be used for paths without a corresponding hash code. I.e
817  /// an empty hash array can be passed in to use 0 for each file. Assumes
818  /// that the same tag is used for all work items
819  void addOutputFiles(
820  const UT_StringArray& paths,
821  const UT_StringHolder& tag,
822  const PDG_File::HashArray& hashes,
823  bool own);
824 
825  /// Appends the given array of output files to the work item. If the length
826  /// of the hashes or tags array is less than the paths, a default value of
827  /// 0 and "" are used respectively.
828  void addOutputFiles(
829  const UT_StringArray& paths,
830  const UT_StringArray& tags,
831  const PDG_File::HashArray& hashes,
832  bool own);
833 
834  /// Replaces the output file at the given index, if that index is valid
835  void updateOutputFile(
836  int index,
837  const UT_StringHolder& path,
838  const UT_StringHolder& tag,
839  PDG_File::Hash hash_code,
840  bool owner);
841 
842  /// Returns the output files for immediate dependencies
843  void inputFiles(
844  PDG_File::Array& files,
845  bool include_expected) const;
846 
847  /// Checks if the work items output files are valid, e.g. they exist
848  bool validateOutputFiles();
849 
850  /// Transfers all dependent files to the remote directory, using the
851  /// supplied scheduler
852  bool transferFiles(bool update_cache);
853 
854  /// Clears all output files
855  void clearOutputFiles();
856 
857  /// Clears expected outputs
858  void clearExpectedOutputFiles();
859 
860  /// Clears output files added during the cook. Note that this method is
861  /// only safe to call if the item has not yet been marked as cooked.
862  void clearRuntimeOutputFiles();
863 
864  /// Sets the clear environment flagging, effectively stopping any upstream
865  /// env vars from being considered beyond this work item
866  void clearEnvironment();
867 
868  /// Adds an environment variable and value to this work items env var map
869  bool addEnvironmentVar(
870  const UT_StringHolder& var_name,
871  const PDGT_Value& value,
872  UT_WorkBuffer& errors);
873 
874  /// Returns true if the work item has the specified env var
875  bool hasEnvironmentVar(
876  const UT_StringHolder& var_name) const;
877 
878  /// Globs the full set of environment variables for this work item by
879  /// searching through it's parents.
880  void globEnvironment(
881  PDGT_Value::Map& environment) const;
882 
883  /// Looks up a key in the environment, returns true if the key exists
884  bool envLookup(const char* key,
885  PDGT_Value& value) const;
886 
887  /// Adds a warning to the owning node
888  void addNodeWarning(
889  const UT_StringHolder& message) const;
890 
891  /// Starts cooking a sub item in a batch, and optionally waits for it to
892  /// be available. Returns false if the item cannot start due to a failure.
893  bool startSubItem(bool wait, UT_WorkBuffer& errors);
894 
895  /// Returns true if the sub item is ready
896  bool checkSubItem(UT_WorkBuffer& errors);
897 
898  /// Marks a work item is cooking, updates it cook start time, and prints
899  /// log messages.
900  bool startWorkItem();
901 
902  /// Marks the work item as cooked, if it is an in-process batch item
903  bool cookSubItem(
904  PDG_WorkItemState state,
905  fpreal duration);
906 
907  /// Returns the scheduler associated with the work item, looked up from the
908  /// work item's node.
909  PDG_Scheduler* scheduler() const;
910 
911  /// Returns true if the work item is schduled by the specified schduler
912  bool isScheduledBy(PDG_Scheduler* scheduler) const;
913 
914  /// Reports a performance monitor event for this work item
915  void performanceEvent() const;
916 
917  /// Sets a stat to the current time
918  virtual fpreal setTimeStat(PDG_WorkItemStats::StatType stat,
919  bool batched);
920 
921  /// Returns the perf stats object for the work item
922  const PDG_WorkItemStats& stats() const
923  { return myStats; }
925  { return myStats; }
926 
927  /// Replaces env vars in the target string with respect to the environment
928  /// and properties of this work item. If py_replace is true, replaces the
929  /// env vars with a valid python token instead of
930  void varReplace(const char* input,
931  UT_WorkBuffer& output) const;
932 
933  /// Saves the work item to a .json file or string
934  bool saveJSONFile(
935  const UT_StringHolder& path,
936  bool pretty_print,
937  bool global_attributes) const;
938  UT_StringHolder saveJSONString(
939  bool pretty_print,
940  bool global_attributes) const;
941 
942  /// Saves the array of work items to a .json file or string
943  static bool saveArrayJSONFile(
944  const UT_StringHolder& path,
945  const PDG_WorkItemArray& items,
946  bool pretty_print,
947  bool global_attributes);
948  static UT_StringHolder saveArrayJSONString(
949  const PDG_WorkItemArray& items,
950  bool pretty_print,
951  bool global_attributes);
952 
953  /// Loads a work item from a .json file or string
954  static PDG_WorkItem* loadJSONFile(
955  const UT_StringHolder& path,
956  bool make_active,
957  UT_WorkBuffer& errors);
958  static PDG_WorkItem* loadJSONString(
959  const UT_StringHolder& data,
960  bool make_active,
961  UT_WorkBuffer& errors);
962 
963  /// Creates a JSON patch from the work item as a string
964  UT_StringHolder createJSONPatch(bool pretty_print) const;
965 
966 protected:
967  friend class PDG_BatchWorkItem;
968  friend class PDG_Scheduler;
969  friend class PDG_WorkItemHolder;
970  friend class PDG_WorkItemDirty;
971 
972  /// Dirties or deletes the work item and propogates the necessary changes
973  /// to downstream items and dependents.
974  void prepareDirty(PDG_WorkItemDirty& dirty_state,
975  bool is_delete,
976  bool is_dirty_data) const;
977 
978  /// Applies a dirty operation that was stored to the dirty state
979  void applyDirty(
980  const PDG_WorkItemDirty& dirty_state,
981  bool remove_outputs);
982 
983  /// Runs custom dirty handlers and deletes output files
984  void handleDirty(
985  const UT_ArrayStringSet& keep_set,
986  bool should_delete,
987  bool remove_outputs);
988 
989  /// Returns the list of files owned by this work item
990  void ownedFiles(
991  PDG_File::Set& files,
992  bool include_expected) const;
993 
994  /// Updates the file cache of this work item so the ID is at least as
995  /// large as the input ID
996  void updateCacheId(int cache_id);
997 
998 
999  /// Called when one of the dependencies owned by this work item
1000  /// is resolved.
1002  PDGE_Resolutions& resolutions,
1003  const PDGE_Evaluator& evaluator,
1004  PDGE_Dependency* dependency) override;
1005 
1006  /// Called when one of the dependencies owned by this work item
1007  /// is unresolved;
1008  int evalUnresolve(
1009  PDGE_Dependency::Array& extras,
1010  PDGE_Dependency* dependency) override;
1011 
1012  /// Called when an item is about to be scheduled, to check if it has
1013  /// cached outputs on disk. Returns Uncooked if the item is not cached
1014  /// and should cook, Failure if the read files mode fails, or CookedCache
1015  /// if the item is cooked from cache.
1016  virtual PDG_WorkItemState checkCached(bool add_outputs);
1017 
1018  /// Upgrades expected outputs to runtime outputs for this work item
1019  bool upgradeExpectedOutputFiles(
1020  bool check_disk,
1021  bool clear);
1022 
1023  /// Called by the scheduler to mark out of process items as cooked
1024  void postCook(PDGE_Resolutions& resolutions,
1026  int batch_index,
1027  fpreal duration,
1028  bool notify_all,
1029  bool static_cook,
1030  bool unresolve);
1031 
1032  /// Notifies this work item that is has cooked.
1033  virtual void notifyCook(PDGE_Resolutions& resolutions,
1035  int batch_index,
1036  fpreal duration,
1037  bool notify_all,
1038  bool static_cook,
1039  bool unresolve);
1040 
1041  /// Syncs a work item's cache with its dependents
1042  virtual void syncCacheId();
1043 
1044  /// Does an inprocess cook of the work item, returning true on success
1045  bool cook();
1046 
1047  /// Internal method for setting the frame
1048  virtual bool setFrame(fpreal frame,
1049  fpreal frame_step,
1050  bool check_dirty);
1051 
1052  /// Adds a parent and copies attributes by inheriting them from the parent
1053  /// of another work item. This is used for generating dynamic sub items
1054  /// in a batch, and copying the parent information from the last batch
1055  /// item onto the new item
1056  void inheritParent(const PDG_WorkItem* work_item);
1057 
1058 private:
1059  /// Removes all dependencies from the work item, and removes them work item
1060  /// from the dependents list of all dependencies
1061  void removeAllDependencies();
1062 
1063  /// Updates the dependency state, given the incoming state
1064  void updateDependencyState(
1065  PDG_WorkItemState dep_state);
1066 
1067  /// Returns a copy of the dependencies in an array, and filters out
1068  /// work items from the target node
1069  void upstreamDependencies(
1070  PDG_WorkItemConstArray& dependency_array) const;
1071 
1072  /// Called before the work item is scheduled
1073  bool prepareTask();
1074 
1075  /// Called when a work item should be scheduled for execution
1076  PDG_WorkItemState schedule();
1077 
1078  /// Appends a log message with a time stamp and a new line
1079  void appendLogPrefix(
1080  bool timestamp,
1081  int verbosity,
1082  const UT_StringHolder& prefix,
1083  const UT_StringHolder& msg) const;
1084 
1085  /// Appends a format string to the work item's log
1086  template <typename... Args>
1087  void appendLogFmt(
1088  bool timestamp,
1089  int verbosity,
1090  const UT_StringHolder& prefix,
1091  const char* fmt,
1092  Args&&... args) const
1093  {
1095  message.format(
1096  fmt, std::forward<Args>(args)...);
1097  appendLogPrefix(
1098  timestamp,
1099  verbosity,
1100  prefix,
1101  message.buffer());
1102  }
1103 
1104  /// Processes this work item as a priority boost dep
1105  void processBoostDependency(PDG_WorkItemSet& deps);
1106 
1107  /// Gathers the input dependencies from priority boosting
1108  void gatherBoostDependencies(PDG_WorkItemSet& deps);
1109 
1110 protected:
1113 
1115 
1119 
1121 
1123 
1126 
1129 
1131 
1134 
1136  int myIndex;
1141 
1148 
1150 
1151  bool myHasFrame:1;
1152  mutable bool myHasWarnings:1;
1153 
1154  bool myAttributesDirty:1;
1155  bool myWasModified:1;
1156  bool myDidCook:1;
1157 
1158  bool myIsPostCook:1;
1159  bool myIsStatic:1;
1160  bool myIsNoGenerate:1;
1161  bool myIsCloneOutputFiles:1;
1162  bool myIsFrozen:1;
1163  bool myIsBatchReady:1;
1164 };
1165 
1166 #endif /* __PDG_WORKITEM_H__ */
bool isNoGenerate() const
Definition: PDG_WorkItem.h:314
const PDG_LoopInfo::Stack & loopInfoStack() const
Returns the loop stack for this work item.
Definition: PDG_WorkItem.h:475
bool isScheduled() const
Definition: PDG_WorkItem.h:291
GLuint GLsizei const GLchar * message
Definition: glcorearb.h:2543
bool isPostCook() const
Returns true if the work item has post-cook logic.
Definition: PDG_WorkItem.h:309
bool isServiceMode() const
Returns true if the work item is marked as a service task.
Definition: PDG_WorkItem.h:280
GLuint GLsizei const GLchar * label
Definition: glcorearb.h:2545
void addMessageFmt(const char *fmt, Args &&...args) const
Definition: PDG_WorkItem.h:657
PDG_Node * node() const
Returns the node that owns the work item.
Definition: PDG_WorkItem.h:187
No specified platform.
bool hasFrame() const
Returns true if the frame has been set.
Definition: PDG_WorkItem.h:243
void addMessage(const UT_StringHolder &log, bool timestamp=true, int verbosity=0) const
Appends a plain message to the work item's in process log buffer.
Definition: PDG_WorkItem.h:670
GLboolean * data
Definition: glcorearb.h:131
void addErrorFmt(const char *fmt, Args &&...args) const
Definition: PDG_WorkItem.h:598
int index() const
Returns the work item index.
Definition: PDG_WorkItem.h:217
virtual UT_RWLock * attribGlobalLock() const
UT_StringHolder debugName() const override
Definition: PDG_WorkItem.h:116
#define PDG_API
Definition: PDG_API.h:23
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
virtual UT_StringHolder attribOwnerName() const
SYS_FORCE_INLINE const char * buffer() const
std::size_t SYS_HashType
Define the type for hash values.
Definition: SYS_Hash.h:19
int batchIndex() const
Definition: PDG_WorkItem.h:226
**But if you need a result
Definition: thread.h:613
bool isOutOfProcess() const
Returns true if the work item is scheduled out of process.
Definition: PDG_WorkItem.h:285
PDG_WorkItemCookType
Enumeration of work item cook types.
Definition: PDG_Types.h:486
PDG_WorkItemType myType
bool isCloneOutputFiles() const
Definition: PDG_WorkItem.h:319
PDG_WorkItemStats myStats
void addWarning(const UT_StringHolder &log, bool timestamp=true, int verbosity=0) const
Definition: PDG_WorkItem.h:641
PDG_WorkItemSet myDependencies
bool wasModified() const
Returns the work item's modified flag.
Definition: PDG_WorkItem.h:509
The work item cooks in process.
unsigned char uint8
Definition: SYS_Types.h:36
UT_SharedPtr< PDG_WorkItemData > PDG_WorkItemDataPtr
Definition: PDG_Types.h:45
CompareResults OIIO_API compare(const ImageBuf &A, const ImageBuf &B, float failthresh, float warnthresh, ROI roi={}, int nthreads=0)
PDG_Node * myNode
virtual bool attribCanWrite(const PDG_EvaluationContext *context) const
int64 Hash
The file hash/modtime type.
Definition: PDG_File.h:38
Definition: core.h:760
PDGE_Dependency myIsCookedDep
PDG_WorkItemState myState
PDGE_Dependency * isCookedDep()
Returns the is cooked dependency object for this work item.
Definition: PDG_WorkItem.h:129
vint4 select(const vbool4 &mask, const vint4 &a, const vint4 &b)
Definition: simd.h:4816
PDG_GraphContext * context() const
Returns the context that owns the work item.
Definition: PDG_WorkItem.h:191
fpreal frame() const
Returns the work item frame.
Definition: PDG_WorkItem.h:239
static const UT_StringHolder theEmptyString
virtual void attribModify(PDG_AttributeType attrib_type, const UT_StringHolder &attrib_name) const
PDG_AttributeCast
Enumeration of attribute cast results.
PDG_WorkItemLogType
Enumeration of work item log message types.
Definition: PDG_Types.h:377
The work item cooks as a service.
PDG_WorkItemExecutionType
Enum of work item runtime types.
Definition: PDG_Types.h:472
const UT_WorkBuffer & logMessages() const
Returns the current in process log buffer.
Definition: PDG_WorkItem.h:690
PDG_BatchWorkItem * batchParent() const
Definition: PDG_WorkItem.h:200
long long int64
Definition: SYS_Types.h:116
PDG_WorkItemConstBoolMap myRequiredDependencies
void applyDirty(bool remove_outputs)
Applies the stored dirty operation.
bool isFrozen() const
Returns true if the work item is frozen.
Definition: PDG_WorkItem.h:271
virtual int64 getMemoryUsage(bool inclusive) const
Returns the memory usage of this owner instance.
fpreal frameStep() const
Returns the work item step size, primarily used with batch items.
Definition: PDG_WorkItem.h:247
virtual Filter & supportedEventTypes() const =0
Returns the list of supported event types for this emitter.
PDG_WorkItemState
Enum of possible work item states.
Definition: PDG_Types.h:413
bool isInProcess() const
Returns true if the work item is in process.
Definition: PDG_WorkItem.h:275
PDG_WorkItemType type() const
Returns the work item type.
Definition: PDG_WorkItem.h:133
PDG_WorkItemID myId
PDG_Platform
Enumeration of supported platforms, for work item command line array.
Definition: PDG_Types.h:89
void addError(const UT_StringHolder &log, bool timestamp=true) const
Definition: PDG_WorkItem.h:612
fpreal myFrameStep
StatType
Enumeration of stats stored in this object.
PDG_WorkItemEvalState
Definition: PDG_Types.h:364
*tasks wait()
PendingDirty
Enumeration of pending dirty states.
Definition: PDG_WorkItem.h:52
UT_TBBSpinLock myEdgeLock
Unspecified/automatic cook type.
GLsizeiptr size
Definition: glcorearb.h:664
**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
PDG_BatchWorkItem * myBatchParent
GLenum GLfloat param
Definition: glcorearb.h:104
size_t format(const char *fmt, const Args &...args)
PDG_AttributeType
Enumeration of possible attribute types.
PDG_WorkItemExecutionType myExecutionType
The item needs to be dirtied, but not deleted.
Definition: PDG_WorkItem.h:58
fpreal64 fpreal
Definition: SYS_Types.h:277
UT_TBBSpinLock & edgeLock() const
Returns the edge lock for the work item.
Definition: PDG_WorkItem.h:363
PDG_WorkItemDataPtr myData
PDG_LoopInfo::Stack myLoopInfoStack
PDG_WorkItemID id() const
Returns the id of the work item, guaranteed to be unique.
Definition: PDG_WorkItem.h:145
GLuint index
Definition: glcorearb.h:786
PDGE_Dependency * canCookDep()
Returns the can cook dependency object for this work item.
Definition: PDG_WorkItem.h:125
PDG_WorkItemState dependencyState() const
Definition: PDG_WorkItem.h:333
PDGE_Dependency myCanCookDep
void addWarningFmt(const char *fmt, Args &&...args) const
Definition: PDG_WorkItem.h:626
virtual PDGE_Dependency::State evalResolve(PDGE_Resolutions &, const PDGE_Evaluator &, PDGE_Dependency *)
Called when a dependency owned by this object is resolved.
void setInternalIndex(int index)
Sets the work items internal index, used for dirtying.
Definition: PDG_WorkItem.h:516
PDG_WorkItemState myDepState
virtual UT_StringHolder debugGroup() const
bool hasWarnings() const
Returns true if the work item has any warning log messages.
Definition: PDG_WorkItem.h:251
**If you just want to fire and args
Definition: thread.h:609
PDG_WorkItemStats & stats()
Definition: PDG_WorkItem.h:924
PDG_WorkItemType
Enum of work item types.
Definition: PDG_Types.h:457
void updateCacheId(const PDG_WorkItem *dependent, const PDG_WorkItem *dependency)
Updates the cache ID of the work item for a dependency.
PDG_WorkItemCookType myCookType
int cacheId() const
Returns the cache id of the work item.
Definition: PDG_WorkItem.h:235
The item does not require any sort of dirtying.
Definition: PDG_WorkItem.h:55
Definition: core.h:1131
virtual void attribWarn(const UT_StringHolder &msg) const
UT_WorkBuffer myLogMessages
const PDG_WorkItemStats & stats() const
Returns the perf stats object for the work item.
Definition: PDG_WorkItem.h:922
PDG_WorkItemState state() const
Returns the work item cook state.
Definition: PDG_WorkItem.h:328
exint PDG_WorkItemID
Type defs for unique work item IDs.
Definition: PDG_Types.h:48
PDG_GraphContext * myContext
int priority() const
Returns the work item priority.
Definition: PDG_WorkItem.h:230
PDG_WorkItemCookType cookType() const
Returns the work item cook type.
Definition: PDG_WorkItem.h:141
virtual int evalUnresolve(PDGE_Dependency::Array &, PDGE_Dependency *)
OIIO_FORCEINLINE T log(const T &v)
Definition: simd.h:7688
PDG_WorkItemSet myDependents
bool isStatic() const
Returns true if the item is a static work item.
Definition: PDG_WorkItem.h:255
The work item cooks out of process.
Reader/Writer mutex class.
Definition: UT_RWLock.h:48
type
Definition: core.h:1059
virtual void resetOwner()
Resets the owner.
PDG_WorkItemExecutionType executionType() const
Returns the work item execution type.
Definition: PDG_WorkItem.h:137
const PDG_WorkItem * myCloneTarget
virtual void attribUnlock() const
void prepareDirty(PDG_WorkItem *work_item)
Adds a work item and its dependencies to the dirty sets.
Definition: format.h:895
PendingDirty myPendingDirty
const PDG_WorkItem * cloneTarget() const
Definition: PDG_WorkItem.h:205
int internalIndex() const
Returns the work item's internal index.
Definition: PDG_WorkItem.h:221
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: glcorearb.h:1297