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