OpenVDB  9.0.1
PointExecutable.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 
4 /// @file compiler/PointExecutable.h
5 ///
6 /// @authors Nick Avramoussis, Francisco Gochez, Richard Jones
7 ///
8 /// @brief The PointExecutable, produced by the OpenVDB AX Compiler for
9 /// execution over OpenVDB Points Grids.
10 ///
11 
12 #ifndef OPENVDB_AX_COMPILER_POINT_EXECUTABLE_HAS_BEEN_INCLUDED
13 #define OPENVDB_AX_COMPILER_POINT_EXECUTABLE_HAS_BEEN_INCLUDED
14 
15 #include "CustomData.h"
16 #include "AttributeRegistry.h"
17 #include "AttributeBindings.h"
18 
19 #include <openvdb/openvdb.h>
20 #include <openvdb/version.h>
22 
23 #include <unordered_map>
24 
25 class TestPointExecutable;
26 
27 namespace llvm {
28 class ExecutionEngine;
29 class LLVMContext;
30 }
31 
32 namespace openvdb {
34 namespace OPENVDB_VERSION_NAME {
35 namespace ax {
36 
37 class Compiler;
38 
39 /// @brief Object that encapsulates compiled AX code which can be executed on a
40 /// collection of VDB Point Data grids. Executables are created by the
41 /// compiler and hold the final immutable JIT compiled function and context.
42 /// @details The PointExecutable is returned from the ax::Compiler when
43 /// compiling AX code for point execution. The class represents a typical AX
44 /// executable object; immutable except for execution settings and implements
45 /// 'execute' functions which can be called multiple times for arbitrary sets
46 /// of inputs. The intended usage of these executables is to configure their
47 /// runtime arguments and then call PointExecutable::execute with your VDBs.
48 /// For example:
49 /// @code
50 /// PointExecutable::Ptr exe = compiler.compile<PointExecutable>("@a += 1");
51 /// exe->setCreateMissing(false); // fail on missing attributes
52 /// exe->setGroupExecution("group1"); // only process points in group1
53 /// exe->execute(vdbs); // run on a set of vdb point data grids
54 /// exe->execute(points); // run on a single point data grid
55 /// @endcode
56 ///
57 /// The setCreateMissing is initialised with specific configurable settings:
58 /// - Create Missing: True
59 /// By default, create any missing attributes that were accessed
60 /// @sa setCreateMissing
61 /// - Group Execution: All
62 /// By default, process all points regardless of their group membership
63 /// @sa setGroupExecution
64 /// - Grain size: 1
65 /// The default grain sizes passed to the tbb partitioner for leaf level
66 /// processing.
67 /// @sa setGrainSize
68 ///
69 /// For more in depth information, see the @ref vdbaxcompilerexe documentation.
71 {
72 public:
73  using Ptr = std::shared_ptr<PointExecutable>;
74  ~PointExecutable();
75 
76  /// @brief Copy constructor. Shares the LLVM constructs but deep copies the
77  /// settings. Multiple copies of an executor can be used at the same time
78  /// safely.
79  PointExecutable(const PointExecutable& other);
80 
81  ////////////////////////////////////////////////////////
82 
83  /// @brief Run this point executable binary on a target PointDataGrid.
84  /// @details This method reads from the stored settings on the executable
85  /// to determine certain behaviour and runs the JIT compiled function
86  /// across every valid point. Point attributes may be created, deleted
87  /// collapsed or expanded, and points themselves may be added, deleted
88  /// or moved.
89  ///
90  /// This method is thread safe; it can be run concurrently from the same
91  /// PointExecutable instance on different inputs.
92  ///
93  /// @param grid The PointDataGrid to process
94  void execute(points::PointDataGrid& grid) const;
95 
96  ////////////////////////////////////////////////////////
97 
98  /// @brief Set a specific point group to execute over. The default is none,
99  /// which corresponds to all points. Note that this can also be compiled
100  /// into the AX function using the ingroup("mygroup") method.
101  /// @warning If the group does not exist during execute, a runtime error
102  /// will be thrown.
103  /// @param name The name of the group to execute over
104  void setGroupExecution(const std::string& name);
105  /// @return The points group to be processed. Default is empty, which is
106  /// all points.
107  const std::string& getGroupExecution() const;
108 
109  /// @brief Set the behaviour when missing point attributes are accessed.
110  /// Default behaviour is true, which creates them with default initial
111  /// values. If false, a missing attribute runtime error will be thrown
112  /// on missing accesses.
113  /// @param flag Enables or disables the creation of missing attributes
114  void setCreateMissing(const bool flag);
115  /// @return Whether this executable will generate new point attributes.
116  bool getCreateMissing() const;
117 
118  /// @brief Set the threading grain size. Default is 1. A value of 0 has the
119  /// effect of disabling multi-threading.
120  /// @param grain The grain size
121  void setGrainSize(const size_t grain);
122  /// @return The current grain size
123  size_t getGrainSize() const;
124 
125  /// @brief Set attribute bindings.
126  /// @param attributeBindings A map of attribute bindings to expected names on
127  /// the geometry to be executed over. By default the AX attributes will be
128  /// bound to point attributes of the same name. Supplying bindings
129  /// for a subset of the attributes will leave the others unchanged.
130  /// AX attributes can only bind to a single point attribute and vice versa.
131  /// However, in a single set call these can be swapped e.g. a -> b and b -> a.
132  /// When bindings are overriden through subsequent calls to this function,
133  /// any dangling point attributes will be automatically bound by name.
134  /// To reset these bindings call get function and create a target set of bindings
135  /// for each attribute of name -> name.
136  void setAttributeBindings(const AttributeBindings& bindings);
137  /// @return The current attribute bindings map
138  const AttributeBindings& getAttributeBindings() const;
139 
140  ////////////////////////////////////////////////////////
141 
142  // foward declaration of settings for this executable
143  struct Settings;
144 
145 private:
146  friend class Compiler;
147  friend class ::TestPointExecutable;
148 
149  /// @brief Constructor, expected to be invoked by the compiler. Should not
150  /// be invoked directly.
151  /// @param context Shared pointer to an llvm:LLVMContext associated with the
152  /// execution engine
153  /// @param engine Shared pointer to an llvm::ExecutionEngine used to build
154  /// functions. Context should be the associated LLVMContext
155  /// @param attributeRegistry Registry of attributes accessed by AX code
156  /// @param customData Custom data which will be shared by this executable.
157  /// It can be used to retrieve external data from within the AX code
158  /// @param functions A map of function names to physical memory addresses
159  /// which were built by llvm using engine
160  /// @param tree The AST linked to this executable. The AST is not stored
161  /// after compilation, but can be used during construction of the exe to
162  /// infer some pre/post processing optimisations.
163  PointExecutable(const std::shared_ptr<const llvm::LLVMContext>& context,
164  const std::shared_ptr<const llvm::ExecutionEngine>& engine,
165  const AttributeRegistry::ConstPtr& attributeRegistry,
166  const CustomData::ConstPtr& customData,
167  const std::unordered_map<std::string, uint64_t>& functions,
168  const ast::Tree& tree);
169 
170 private:
171  // The Context and ExecutionEngine must exist _only_ for object lifetime
172  // management. The ExecutionEngine must be destroyed before the Context
173  const std::shared_ptr<const llvm::LLVMContext> mContext;
174  const std::shared_ptr<const llvm::ExecutionEngine> mExecutionEngine;
175  const AttributeRegistry::ConstPtr mAttributeRegistry;
176  const CustomData::ConstPtr mCustomData;
177  const std::unordered_map<std::string, uint64_t> mFunctionAddresses;
178  std::unique_ptr<Settings> mSettings;
179 };
180 
181 } // namespace ax
182 } // namespace OPENVDB_VERSION_NAME
183 } // namespace openvdb
184 
185 #endif // OPENVDB_AX_COMPILER_POINT_EXECUTABLE_HAS_BEEN_INCLUDED
186 
Object that encapsulates compiled AX code which can be executed on a collection of VDB Point Data gri...
Definition: PointExecutable.h:70
Definition: Compiler.h:31
std::shared_ptr< PointExecutable > Ptr
Definition: PointExecutable.h:73
std::shared_ptr< const CustomData > ConstPtr
Definition: CustomData.h:38
The compiler class. This holds an llvm context and set of compiler options, and constructs executable...
Definition: Compiler.h:49
The Attribute Bindings class is used by the compiled Executables to handle the mapping of AX Attribut...
Definition: Exceptions.h:13
std::shared_ptr< const AttributeRegistry > ConstPtr
Definition: AttributeRegistry.h:42
Container class that associates a tree with a transform and metadata.
Definition: Grid.h:28
These classes contain lists of expected attributes and volumes which are populated by compiler during...
A Tree is the highest concrete (non-abstract) node in the entire AX AST hierarchy. It represents an entire conversion of a valid AX string.
Definition: AST.h:561
This class wraps an interface for a map of attribute bindings. These map attributes in AX code to con...
Definition: AttributeBindings.h:36
Attribute-owned data structure for points. Point attributes are stored in leaf nodes and ordered by v...
Access to the CustomData class which can provide custom user user data to the OpenVDB AX Compiler...
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:202