HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PDG_Graph.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_GRAPH_H__
10 #define __PDG_GRAPH_H__
11 
12 #include "PDG_API.h"
13 
14 #include "PDG_AttributeOwner.h"
15 #include "PDG_AttributeTypes.h"
16 #include "PDG_Types.h"
17 #include "PDG_WorkItemDirty.h"
18 
20 #include <UT/UT_StringHolder.h>
21 #include <UT/UT_StringMap.h>
22 #include <UT/UT_RWLock.h>
23 
25 class PDG_GraphContext;
26 struct PDG_MemoryInfo;
27 class PDG_NodeInterface;
28 class PDG_Node;
30 class PDG_Port;
31 class PDG_WorkItem;
32 
33 class UT_WorkBuffer;
34 
36 {
37 public:
38  /// Nodes not created by TOPs will have id = nodeIdBase + counter
39  static constexpr int nodeIdBase = 1000000;
40 
41  /// Typedef for work item id -> work item
42  using IdMap = UT_ConcurrentHashMap<PDG_WorkItemID, PDG_WorkItem*>;
43 
44 public:
45  PDG_Graph(PDG_GraphContext* context);
46  ~PDG_Graph() override;
47 
48  /// Returns the total memory usage of the graph, and all the nodes and
49  /// work items in it
50  int64 getMemoryUsage(bool inclusive) const;
51 
52  /// Returns the total memory usage of the graph, and all the nodes and
53  /// work items in it.
54  void memoryInfo(PDG_MemoryInfo& memory_info,
55  bool inclusive) const;
56 
57  /// Returns a descriptive name of the attribute owner
58  UT_StringHolder attribOwnerName() const override;
59 
60  /// Checks if the specified flag is valid for this owner
61  bool isAttribFlagValid(
62  PDG_AttributeFlag flag) const override;
63 
64  /// Checks if the specified bit vector flags is valid for this owner
65  bool areAttribFlagsValid(uint16 flags) const override;
66 
67  /// Returns a human-readable description of the nodes in the graph
68  void description(UT_WorkBuffer& buffer) const;
69 
70  /// Serializes the nodes to Python, which can then be used to rebuild the
71  /// graph at a later point
72  void serialize(UT_WorkBuffer& buffer) const;
73 
74  /// Constructs a new PDG node with a given node callbakc type, name and
75  /// extra args
76  PDG_Node* createNode(PDG_NodeCallbackType* type_object,
77  const UT_StringHolder& node_name,
78  const PDGT_ValueArgs& args,
79  UT_WorkBuffer& errors,
80  int id = -1);
81 
82  /// Adds an existing node to the graph
83  void insertNode(PDG_NodePtr& node);
84 
85  /// Renames a node given i's current/old node name. Does not update any
86  /// external references to the node name
87  bool renameNode(
88  const UT_StringHolder& old_name,
89  const UT_StringHolder& new_name);
90 
91  /// Renames a node give its ID. Does not update any external references
92  /// tot he node name
93  bool renameNodeById(
94  int node_id,
95  const UT_StringHolder& new_name);
96 
97  /// Remove node/nodes from the graph
98  PDG_NodePtr removeNode(const UT_StringHolder& node_name);
99  PDG_NodePtr removeNodeById(int node_id);
100  void removeAllNodes();
101 
102  /// Accessors for nodes in the graph
103  PDG_Node* node(const UT_StringHolder& name) const;
104  PDG_Node* nodeById(int id) const;
105  int nodeCount() const
106  { return myNodeNameMap.size(); }
107  const PDG_NodePtrMap& nodes() const
108  { return myNodeNameMap; }
109 
110  /// Returns a unqiue name for a node by adding a numeric suffix until a
111  /// name is found that isn't already in use
112  UT_StringHolder uniqueNodeName(const UT_StringHolder& name);
113 
114  /// Looks up a port using the node.port_name syntax
115  PDG_Port* nodePort(const UT_StringHolder& node_name,
116  const UT_StringHolder& port_name) const;
117  PDG_Port* nodePort(const UT_StringHolder& full_name) const;
118 
119  /// Adds/ removes or queries external dependencies
120  PDG_DependencyPtr dependencyForKey(const UT_StringHolder& key) const;
121  PDG_DependencyPtr addDependency(const UT_StringHolder& type,
122  const UT_StringHolder& key,
123  const PDGT_ValueArgs& args,
124  UT_WorkBuffer& errors);
125  void addDependency(PDG_DependencyPtr dependency);
126  void removeDependency(PDG_DependencyPtr dependency);
127 
128  /// Returns the graph contex that owns this graph
129  PDG_GraphContext* context() const;
130 
131 
132  /// Adds a work item to the work item id -> pointer map
133  void addWorkItem(PDG_WorkItem* work_item);
134 
135  /// Removes a work item from the graph
136  void removeWorkItem(PDG_WorkItem* work_item);
137 
138 
139  /// Returns a work item for a given id
140  PDG_WorkItem* workItemById(PDG_WorkItemID id) const;
141 
142  /// Returns a work item for a given id. Iterates through all active
143  static PDG_WorkItem* workItemByGlobalId(PDG_WorkItemID id);
144 
145  /// Returns a list of work items for a given list of ids
146  void workItemsById(
147  PDG_WorkItemArray& work_items,
149  bool allow_null) const;
150 
151  /// Returns a work item for a given name
152  PDG_WorkItem* workItemByName(const UT_StringHolder& name) const;
153 
154  /// Returns a work item id from a name, assuming the name is using the
155  /// node_id compatibility format
156  PDG_WorkItemID workItemIdFromName(const UT_StringHolder& name) const;
157 
158  /// Returns the map of work item ids -> work item
159  IdMap workItemIdMap() const { return myWorkItemIdMap; }
160 
161  /// Returns a map of item->dependencies, item->dependents and ready items
162  void dependencyGraph(
163  PDG_WorkItemMap& dependencies,
164  PDG_WorkItemMap& dependents,
165  PDG_WorkItemArray& ready,
166  bool expand,
167  PDG_Scheduler* filter) const;
168 
169  /// Returns a static dependency graph as a map of work item -> dependencies
170  /// If "inverse" is true, returns the dependent map instead
171  void dependencyGraph(
172  PDG_WorkItemMap& graph,
173  bool inverse,
174  bool expand,
175  PDG_Scheduler* filter) const;
176 
177  /// Dirties all work items in the graph, faster than the standard dirty
178  /// methods as it can simply blow away everything
179  PDG_WorkItemDirty dirtyAll(bool remove_outputs, bool apply_dirty);
180 
181  /// Dirties a work item by id
182  ///
183  /// Note that this method acquires the serial cook write lock, preventing
184  /// any other code from accessing/generating static or dirtying work items
185  PDG_WorkItemDirty dirtyWorkItem(PDG_WorkItemID id,
186  bool should_delete,
187  bool remove_outputs,
188  bool apply_dirty);
189 
190  /// Dirties a work item by pointer
191  ///
192  /// Note that this method acquires the serial cook write lock, preventing
193  /// any other code from accessing/generating static or dirtying work items
194  PDG_WorkItemDirty dirtyWorkItem(PDG_WorkItem* work_item,
195  bool should_delete,
196  bool remove_outputs,
197  bool apply_dirty);
198 
199  /// Dirities an array of work items
200  ///
201  /// Note that this method acquires the serial cook write lock, preventing
202  /// any other code from accessing/generating static or dirtying work items
203  PDG_WorkItemDirty dirtyWorkItems(const PDG_WorkItemArray& work_items,
204  PDG_Node* source_node,
205  bool should_delete,
206  bool remove_outputs,
207  bool apply_dirty,
208  bool emit_events);
209 
210  /// Does a numeric data lookup, for use with the @ operator
211  PDG_AttributeCast numericData(
212  fpreal& param,
213  const PDG_AttributeEvaluator& evaluator) const;
214 
215  /// Does a string data lookup, for use with the @ operator
216  PDG_AttributeCast stringData(
218  const PDG_AttributeEvaluator& evaluator,
219  const PDG_EvaluationContext* local_context) const;
220 
221  /// Binds a global attribute to a specific work item
222  void bindGlobalAttribute(
223  const UT_StringHolder& attrib_name,
224  PDG_WorkItem* work_item);
225 
226  /// Returns the work iterm that an attribute is bound to
227  PDG_WorkItem* boundAttributeWorkItem(
228  const UT_StringHolder& attrib_name) const;
229 
230  /// Adjusts the unique id counter so the specified ID is included in the
231  /// range of used values.
232  static void addUniqueId(PDG_WorkItemID id);
233 
234  /// Returns a new unique work item id
235  static PDG_WorkItemID uniqueWorkItemId();
236 
237  /// Returns the current maximum unique work item id
238  static PDG_WorkItemID currentWorkItemId();
239 
240  /// Returns a new unique cache id
241  int uniqueCacheId();
242 
243  /// Returns a new unique node id
244  int uniqueNodeId();
245 
246  /// Increments the graph's cache id.
247  void bumpCacheId();
248 
249  /// Returns the serial cook RW lock. This is only so that PDG_Node is able
250  /// to use the lock to guard access to its static items, wrappers and done
251  /// item lists.
253  { return mySerialCookLock; }
254 
255  /// Runs a functor on the specified node with the node lock held.
256  template <typename Func>
257  void safeNodeAccess(int id, const Func& f) const
258  {
259  UT_AutoReadLock nodes_lock(myNodesLock);
260  auto it = myNodeIdMap.find(id);
261  if (it != myNodeIdMap.end())
262  f(it->second);
263  }
264 
265  /// Runs a functor on the node map with the node lock held.
266  template <typename Func>
267  void safeNodeMapAccess(const Func& f) const
268  {
269  UT_AutoReadLock nodes_lock(myNodesLock);
270  f(myNodeNameMap);
271  }
272 
273  /// Runs a functor on the specified work item with an ID map iterator
274  /// held.
275  template <typename Func>
277  const Func& f) const
278  {
279  IdMap::accessor access;
280  if (myWorkItemIdMap.find(access, id))
281  f(access->second);
282  }
283 
284 private:
285  friend class PDG_WorkItemDirty;
286 
287  /// Typedef for exteranl dependency map
289 
290  /// Typedef for node id -> node
292 
293  /// Map of work item ID to global attributes bound to that work item
295 
296  /// Inverse map for global attributes, mapping attribute name to work
297  /// item ID
299 
300  void applyDirty(bool remove_outputs);
301 
302 private:
303  PDG_NodePtrMap myNodeNameMap;
304  NodeIdMap myNodeIdMap;
305  IdMap myWorkItemIdMap;
306 
307  GlobalAttribMap myGlobalAttribMap;
308  GlobalAttribInvMap myGlobalAttribInvMap;
309 
310  SYS_AtomicCounter myNodeIdCounter;
311  int myCacheIdCounter;
312 
313  DepMap myDependencies;
314  PDG_GraphContext* myContext;
315 
316  mutable UT_RWLock myNodesLock;
317  mutable UT_RWLock mySerialCookLock;
318 };
319 
320 #endif /* __PDG_GRAPH_H__ */
GLbitfield flags
Definition: glcorearb.h:1596
unsigned short uint16
Definition: SYS_Types.h:38
virtual bool areAttribFlagsValid(uint16 flags) const
#define PDG_API
Definition: PDG_API.h:23
virtual UT_StringHolder attribOwnerName() const
void safeNodeMapAccess(const Func &f) const
Runs a functor on the node map with the node lock held.
Definition: PDG_Graph.h:267
UT_UniquePtr< PDG_Node > PDG_NodePtr
Array, set and map of nodes.
Definition: PDG_Types.h:72
IdMap workItemIdMap() const
Returns the map of work item ids -> work item.
Definition: PDG_Graph.h:159
int nodeCount() const
Definition: PDG_Graph.h:105
GLfloat f
Definition: glcorearb.h:1926
const PDG_NodePtrMap & nodes() const
Definition: PDG_Graph.h:107
Definition: core.h:760
void safeNodeAccess(int id, const Func &f) const
Runs a functor on the specified node with the node lock held.
Definition: PDG_Graph.h:257
GLuint GLint GLboolean GLint GLenum access
Definition: glcorearb.h:2222
void safeWorkItemAccess(PDG_WorkItemID id, const Func &f) const
Definition: PDG_Graph.h:276
PDG_AttributeCast
Enumeration of attribute cast results.
UT_ConcurrentHashMap< PDG_WorkItemID, PDG_WorkItem * > IdMap
Typedef for work item id -> work item.
Definition: PDG_Graph.h:42
long long int64
Definition: SYS_Types.h:116
void applyDirty(bool remove_outputs)
Applies the stored dirty operation.
A simple class to hold a rwlock and release it when it goes out of scope.
Definition: UT_RWLock.h:22
GLuint const GLchar * name
Definition: glcorearb.h:786
PDG_AttributeFlag
Enumeration of extra attribute flags. Flags can be ORed together.
GLenum GLfloat param
Definition: glcorearb.h:104
UT_RWLock & serialCookLock()
Definition: PDG_Graph.h:252
fpreal64 fpreal
Definition: SYS_Types.h:277
**If you just want to fire and args
Definition: thread.h:609
UT_SharedPtr< PDG_Dependency > PDG_DependencyPtr
Type defs for registered type objects.
Definition: PDG_Types.h:41
virtual bool isAttribFlagValid(PDG_AttributeFlag flag) const
exint PDG_WorkItemID
Type defs for unique work item IDs.
Definition: PDG_Types.h:48
Reader/Writer mutex class.
Definition: UT_RWLock.h:48
type
Definition: core.h:1059
GLuint * ids
Definition: glcorearb.h:652
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: glcorearb.h:1297