OpenVDB 13.0.1
Loading...
Searching...
No Matches
PointExecutable.h
Go to the documentation of this file.
1// Copyright Contributors to the OpenVDB Project
2// SPDX-License-Identifier: Apache-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 <llvm/Config/llvm-config.h>
24
25#include <unordered_map>
26
27struct TestPointExecutableAcc;
28
29/// @cond OPENVDB_DOCS_INTERNAL
30namespace llvm {
31class ExecutionEngine;
32class LLVMContext;
33namespace orc {
34class LLJIT;
35}
36}
37/// @endcond
38
39
40namespace openvdb {
42namespace OPENVDB_VERSION_NAME {
43namespace ax {
44
45class Compiler;
46
47/// @brief Object that encapsulates compiled AX code which can be executed on a
48/// collection of VDB Point Data grids. Executables are created by the
49/// compiler and hold the final immutable JIT compiled function and context.
50/// @details The PointExecutable is returned from the ax::Compiler when
51/// compiling AX code for point execution. The class represents a typical AX
52/// executable object; immutable except for execution settings and implements
53/// 'execute' functions which can be called multiple times for arbitrary sets
54/// of inputs. The intended usage of these executables is to configure their
55/// runtime arguments and then call PointExecutable::execute with your VDBs.
56/// For example:
57/// @code
58/// PointExecutable::Ptr exe = compiler.compile<PointExecutable>("@a += 1");
59/// exe->setCreateMissing(false); // fail on missing attributes
60/// exe->setGroupExecution("group1"); // only process points in group1
61/// exe->execute(vdbs); // run on a set of vdb point data grids
62/// exe->execute(points); // run on a single point data grid
63/// @endcode
64///
65/// The setCreateMissing is initialised with specific configurable settings:
66/// - Create Missing: True
67/// By default, create any missing attributes that were accessed
68/// @sa setCreateMissing
69/// - Group Execution: All
70/// By default, process all points regardless of their group membership
71/// @sa setGroupExecution
72/// - Grain size: 1
73/// The default grain sizes passed to the tbb partitioner for leaf level
74/// processing.
75/// @sa setGrainSize
76///
77/// For more in depth information, see the @ref vdbaxcompilerexe documentation.
79{
80public:
81 using Ptr = std::shared_ptr<PointExecutable>;
83
84 /// @brief Copy constructor. Shares the LLVM constructs but deep copies the
85 /// settings. Multiple copies of an executor can be used at the same time
86 /// safely.
88
89 ////////////////////////////////////////////////////////
90
91 /// @brief Run this point executable binary on a target PointDataGrid.
92 /// @details This method reads from the stored settings on the executable
93 /// to determine certain behaviour and runs the JIT compiled function
94 /// across every valid point. Point attributes may be created, deleted
95 /// collapsed or expanded, and points themselves may be added, deleted
96 /// or moved.
97 ///
98 /// This method is thread safe; it can be run concurrently from the same
99 /// PointExecutable instance on different inputs.
100 ///
101 /// @param grid The PointDataGrid to process
103
104 ////////////////////////////////////////////////////////
105
106 /// @brief Set a specific point group to execute over. The default is none,
107 /// which corresponds to all points. Note that this can also be compiled
108 /// into the AX function using the ingroup("mygroup") method.
109 /// @warning If the group does not exist during execute, a runtime error
110 /// will be thrown.
111 /// @param name The name of the group to execute over
112 void setGroupExecution(const std::string& name);
113 /// @return The points group to be processed. Default is empty, which is
114 /// all points.
115 const std::string& getGroupExecution() const;
116
117 /// @brief Set the behaviour when missing point attributes are accessed.
118 /// Default behaviour is true, which creates them with default initial
119 /// values. If false, a missing attribute runtime error will be thrown
120 /// on missing accesses.
121 /// @param flag Enables or disables the creation of missing attributes
122 void setCreateMissing(const bool flag);
123 /// @return Whether this executable will generate new point attributes.
124 bool getCreateMissing() const;
125
126 /// @brief Set the threading grain size. Default is 1. A value of 0 has the
127 /// effect of disabling multi-threading.
128 /// @param grain The grain size
129 void setGrainSize(const size_t grain);
130 /// @return The current grain size
131 size_t getGrainSize() const;
132
133 /// @brief Set attribute bindings.
134 /// @param bindings A map of attribute bindings to expected names on
135 /// the geometry to be executed over. By default the AX attributes will be
136 /// bound to point attributes of the same name. Supplying bindings
137 /// for a subset of the attributes will leave the others unchanged.
138 /// AX attributes can only bind to a single point attribute and vice versa.
139 /// However, in a single set call these can be swapped e.g. a -> b and b -> a.
140 /// When bindings are overriden through subsequent calls to this function,
141 /// any dangling point attributes will be automatically bound by name.
142 /// To reset these bindings call get function and create a target set of bindings
143 /// for each attribute of name -> name.
145 /// @return The current attribute bindings map
147
148 ////////////////////////////////////////////////////////
149
150 // foward declaration of settings for this executable
151 template <bool> struct Settings;
152
153 /// @brief Command Line Interface handling for the PointExecutable.
154 /// @details This class wraps the logic for converting commands specific
155 /// to the PointExecutable to the internal Settings. Subsequent
156 /// executables can be initialized from the CLI object that gets created
158 {
162 static CLI create(size_t argc, const char* argv[], bool* used=nullptr);
163 static void usage(std::ostream& os, const bool verbose);
164 private:
165 friend class PointExecutable;
166 CLI();
167 std::unique_ptr<Settings<true>> mSettings;
168 };
169
170 /// @brief Intialize the Settings of this executables from the CLI object
171 /// @param cli The CLI object
172 void setSettingsFromCLI(const CLI& cli);
173
174 ////////////////////////////////////////////////////////
175
176private:
177 friend class Compiler;
178 friend struct ::TestPointExecutableAcc;
179
180 /// @brief Private method used in the unit tests
181 bool usesAcceleratedKernel(const points::PointDataTree& tree) const;
182
183 /// @brief Constructor, expected to be invoked by the compiler. Should not
184 /// be invoked directly.
185 /// @param context Shared pointer to an llvm:LLVMContext associated with the
186 /// execution engine
187 /// @param engine Shared pointer to an llvm::ExecutionEngine used to build
188 /// functions. Context should be the associated LLVMContext
189 /// @param attributeRegistry Registry of attributes accessed by AX code
190 /// @param customData Custom data which will be shared by this executable.
191 /// It can be used to retrieve external data from within the AX code
192 /// @param functions A map of function names to physical memory addresses
193 /// which were built by llvm using engine
194 /// @param tree The AST linked to this executable. The AST is not stored
195 /// after compilation, but can be used during construction of the exe to
196 /// infer some pre/post processing optimisations.
198#if LLVM_VERSION_MAJOR <= 15
199 const std::shared_ptr<const llvm::LLVMContext>& context,
200 const std::shared_ptr<const llvm::ExecutionEngine>& engine,
201#else
202 const std::shared_ptr<const llvm::orc::LLJIT>& mExecutionEngine,
203#endif
204 const AttributeRegistry::ConstPtr& attributeRegistry,
205 const CustomData::ConstPtr& customData,
206 const std::unordered_map<std::string, uint64_t>& functions,
207 const ast::Tree& tree);
208
209private:
210#if LLVM_VERSION_MAJOR <= 15
211 // The Context and ExecutionEngine must exist _only_ for object lifetime
212 // management. The ExecutionEngine must be destroyed before the Context
213 const std::shared_ptr<const llvm::LLVMContext> mContext;
214 const std::shared_ptr<const llvm::ExecutionEngine> mExecutionEngine;
215#else
216 const std::shared_ptr<const llvm::orc::LLJIT> mExecutionEngine;
217#endif
218 const AttributeRegistry::ConstPtr mAttributeRegistry;
219 const CustomData::ConstPtr mCustomData;
220 const std::unordered_map<std::string, uint64_t> mFunctionAddresses;
221 std::unique_ptr<Settings<false>> mSettings;
222};
223
224} // namespace ax
225} // namespace OPENVDB_VERSION_NAME
226} // namespace openvdb
227
228#endif // OPENVDB_AX_COMPILER_POINT_EXECUTABLE_HAS_BEEN_INCLUDED
229
The Attribute Bindings class is used by the compiled Executables to handle the mapping of AX Attribut...
These classes contain lists of expected attributes and volumes which are populated by compiler during...
Access to the CustomData class which can provide custom user user data to the OpenVDB AX Compiler.
#define OPENVDB_AX_API
Definition Platform.h:312
Attribute-owned data structure for points. Point attributes are stored in leaf nodes and ordered by v...
This class wraps an interface for a map of attribute bindings. These map attributes in AX code to con...
Definition AttributeBindings.h:37
std::shared_ptr< const AttributeRegistry > ConstPtr
Definition AttributeRegistry.h:43
std::shared_ptr< const CustomData > ConstPtr
Definition CustomData.h:38
const std::string & getGroupExecution() const
void setCreateMissing(const bool flag)
Set the behaviour when missing point attributes are accessed. Default behaviour is true,...
const AttributeBindings & getAttributeBindings() const
std::shared_ptr< PointExecutable > Ptr
Definition PointExecutable.h:81
void execute(points::PointDataGrid &grid) const
Run this point executable binary on a target PointDataGrid.
PointExecutable(const PointExecutable &other)
Copy constructor. Shares the LLVM constructs but deep copies the settings. Multiple copies of an exec...
friend class Compiler
Definition PointExecutable.h:177
void setGrainSize(const size_t grain)
Set the threading grain size. Default is 1. A value of 0 has the effect of disabling multi-threading.
void setGroupExecution(const std::string &name)
Set a specific point group to execute over. The default is none, which corresponds to all points....
void setSettingsFromCLI(const CLI &cli)
Intialize the Settings of this executables from the CLI object.
void setAttributeBindings(const AttributeBindings &bindings)
Set attribute bindings.
tree::Tree< tree::RootNode< tree::InternalNode< tree::InternalNode< PointDataLeafNode< PointDataIndex32, 3 >, 4 >, 5 > > > PointDataTree
Point index tree configured to match the default VDB configurations.
Definition PointDataGrid.h:190
Grid< PointDataTree > PointDataGrid
Point data grid.
Definition PointDataGrid.h:195
Definition PointDataGrid.h:170
Definition Exceptions.h:13
Command Line Interface handling for the PointExecutable.
Definition PointExecutable.h:158
static void usage(std::ostream &os, const bool verbose)
friend class PointExecutable
Definition PointExecutable.h:165
static CLI create(size_t argc, const char *argv[], bool *used=nullptr)
Definition PointExecutable.h:151
A Tree is the highest concrete (non-abstract) node in the entire AX AST hierarchy....
Definition AST.h:563
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:218