OpenVDB 14.0.0
Loading...
Searching...
No Matches
VolumeExecutable.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/VolumeExecutable.h
5///
6/// @authors Nick Avramoussis, Francisco Gochez, Richard Jones
7///
8/// @brief The VolumeExecutable, produced by the OpenVDB AX Compiler for
9/// execution over Numerical OpenVDB Grids.
10///
11
12#ifndef OPENVDB_AX_COMPILER_VOLUME_EXECUTABLE_HAS_BEEN_INCLUDED
13#define OPENVDB_AX_COMPILER_VOLUME_EXECUTABLE_HAS_BEEN_INCLUDED
14
15#include "CustomData.h"
16#include "AttributeRegistry.h"
17#include "AttributeBindings.h"
18
19#include <openvdb/version.h>
20#include <openvdb/Grid.h>
21
22
23#include <unordered_map>
24
25/// @cond OPENVDB_DOCS_INTERNAL
26namespace llvm {
27class ExecutionEngine;
28class LLVMContext;
29namespace orc {
30class LLJIT;
31}
32}
33/// @endcond
34
35namespace openvdb {
37namespace OPENVDB_VERSION_NAME {
38namespace ax {
39
40class Compiler;
41
42/// @brief Object that encapsulates compiled AX code which can be executed on a
43/// collection of VDB volume grids. Executables are created by the compiler
44/// and hold the final immutable JIT compiled function and context.
45/// @details The VolumeExecutable is returned from the ax::Compiler when
46/// compiling AX code for volume execution. The class represents a typical AX
47/// executable object; immutable except for execution settings and implements
48/// 'execute' functions which can be called multiple times for arbitrary sets
49/// of inputs. The intended usage of these executables is to configure their
50/// runtime arguments and then call VolumeExecutable::execute with your VDBs.
51/// For example:
52/// @code
53/// VolumeExecutable::Ptr exe = compiler.compile<VolumeExecutable>("@a += 1");
54/// exe->setTreeExecutionLevel(0); // only process leaf nodes
55/// exe->setValueIterator(VolumeExecutable::IterType::ALL); // process all values
56/// exe->execute(vdbs); // run on a set of vdbs
57/// exe->execute(grid); // run on a single vdb
58/// @endcode
59///
60/// The Volume executable is initialised with specific configurable settings:
61/// - Iteration Level: min=0, max=RootNode::Level.
62/// By default, processes the entire VDB tree hierarchy.
63/// @sa setTreeExecutionLevel
64/// - Iteration Type: ON
65/// By default, processes ACTIVE values.
66/// @sa setValueIterator
67/// - Active Tile Streaming: ON, OFF or AUTO depending on AX code.
68/// By default, if AX detects that the AX program may produce unique
69/// values for leaf level voxels that would otherwise comprise a
70/// given active tile, this setting is set to ON or AUTO. Otherwise it is
71/// set to OFF.
72/// @sa setActiveTileStreaming
73/// - Grain sizes: 1:32
74/// The default grain sizes passed to the tbb partitioner for leaf level
75/// processing and active tile processing.
76/// @sa setGrainSize
77/// @sa setActiveTileStreamingGrainSize
78/// - AttributeBindings: None
79/// Whether to indriect any AX accesses to different grid names.
80/// @sa setAttributeBindings
81///
82/// For more in depth information, see the @ref vdbaxcompilerexe documentation.
84{
85public:
86 using Ptr = std::shared_ptr<VolumeExecutable>;
88
89 /// @brief Copy constructor. Shares the LLVM constructs but deep copies the
90 /// settings. Multiple copies of an executor can be used at the same time
91 /// safely.
93
94 ////////////////////////////////////////////////////////
95
96 ///@{
97 /// @brief Run this volume executable binary on target volumes.
98 /// @details This method reads from the stored settings on the executable
99 /// to determine certain behaviour and runs the JIT compiled function
100 /// across every valid VDB value. Topology may be changed, deleted or
101 /// created.
102 ///
103 /// This method is thread safe; it can be run concurrently from the same
104 /// VolumeExecutable instance on different inputs.
105 ///
106 /// @param grids The VDB Volumes to process
107 void execute(openvdb::GridPtrVec& grids) const;
108 void execute(openvdb::GridBase& grids) const;
109 ///@}
110
111 ////////////////////////////////////////////////////////
112
113 /// @brief Set the behaviour when missing grids are accessed. Default
114 /// behaviour is true, which creates them with default transforms and
115 /// background values
116 /// @param flag Enables or disables the creation of missing attributes
117 void setCreateMissing(const bool flag);
118 /// @return Whether this executable will generate new grids.
119 bool getCreateMissing() const;
120
121 /// @brief Set the execution level for this executable. This controls what
122 /// nodes are processed when execute is called. Possible values depend on
123 /// the OpenVDB configuration in use, however a value of 0 will always
124 /// correspond to the lowest level (leaf-level). By default, the min
125 /// level is zero (LeafNodeType::LEVEL) and the max level is the root
126 /// node's level (RootNodeType::LEVEL). In other words, the default
127 /// execution level settings process the whole of the tree.
128 /// @note A value larger that the number of levels in the tree (i.e. larger
129 /// than the root node's level) will cause this method to throw a runtime
130 /// error.
131 /// @param min The minimum tree execution level to set
132 /// @param max The maximum tree execution level to set
133 void setTreeExecutionLevel(const Index min, const Index max);
134 /// @param level The tree execution level to set. Calls setTreeExecutionLevel
135 /// with min and max arguments as level.
136 void setTreeExecutionLevel(const Index level);
137 /// @brief Get the tree execution levels.
138 /// @param min The minimum tree execution level
139 /// @param max The maximum tree execution level
140 void getTreeExecutionLevel(Index& min, Index& max) const;
141
142 /// @brief The streaming type of active tiles during execution.
143 /// @param ON active tiles are temporarily densified (converted to leaf
144 /// level voxels) on an "as needed" basis and the subsequent voxel
145 /// values are processed. The temporarily densified node is added to the
146 /// tree only if a non constant voxel buffer is produced. Otherwise a
147 /// child tile may be created or the original tile's value may simply be
148 /// modified.
149 /// @param OFF tile topologies are left unchanged and their single value is
150 /// processed.
151 /// @param AUTO the volume executable analyzes the compiled kernel and
152 /// attempts to determine if expansion of active tiles would lead to
153 /// different, non-constant values in the respective voxels. This is
154 /// done on a per grid basis; ultimately each execution will be set to
155 /// ON or OFF. This option will always fall back to ON if there is any
156 /// chance the kernel may produce child nodes
157 ///
158 /// @note The volume executable always runs an AUTO check on creation and
159 /// will set itself to ON (if all grids always need child nodes), OFF (if
160 /// grids never need child nodes) or remains as AUTO (if this depends on
161 /// which grid is being processed).
162 ///
163 /// @details When an AX kernel is run over coarser levels of the tree (i.e.
164 /// not leaf voxels), it is often desirable to densify these areas into
165 /// unique voxels such that they can each receive a unique value. For
166 /// example, consider the following AX code which assigns a vector volume
167 /// to the world space position of each voxel:
168 /// @code
169 /// v@v = getvoxelpws();
170 /// @endcode
171 /// Active tiles hold a single value but comprise an area greater than
172 /// that of a single voxel. As the above kernel varies with respect to
173 /// a nodes position, we'd need to replace these tiles with leaf voxels
174 /// to get unique per node values. The stream flag is initialised to ON
175 /// in this case.
176 ///
177 /// This behaviour, however, is not always desirable .i.e:
178 /// @code
179 /// v@v = {1,2,3};
180 /// @endcode
181 /// In this instance, all values within a volume receive the same value
182 /// and are not dependent on any spatially or iteratively varying
183 /// metrics. The stream flag is set to OFF.
184 ///
185 /// The AUTO flag is set in cases where the runtime access pattern of the
186 /// inputs determines streaming:
187 /// @code
188 /// f@density = f@mask;
189 /// f@mask = 0;
190 /// @endcode
191 /// In this instance, the runtime topology and values of \@mask determines
192 /// whether child topology needs to be created in \@density, but \@mask
193 /// itself does not need streaming. Streaming will be set to ON for
194 /// density but OFF for mask.
195 ///
196 /// @note This behaviour is only applied to active tiles. If the value
197 /// iterator is set to OFF, this option is ignored.
198 /// @warning This option can generate large amounts of leaf level voxels.
199 /// It is recommended to use a good concurrent memory allocator (such as
200 /// jemalloc) for the best performance.
201 enum class Streaming { ON, OFF, AUTO };
202 /// @brief Controls the behaviour of expansion of active tiles.
203 /// @param s The behaviour to set
205 /// @return The current stream behaviour.
207 /// @return The current stream behaviour for a particular grid. This is
208 /// either ON or OFF.
209 /// @param name The name of the grid to query
210 /// @param type The grids type
211 Streaming getActiveTileStreaming(const std::string& name,
212 const ast::tokens::CoreType& type) const;
213
214 enum class IterType { ON, OFF, ALL };
215 /// @brief Set the value iterator type to use with this executable. Options
216 /// are ON, OFF, ALL. Default is ON.
217 /// @param iter The value iterator type to set
218 void setValueIterator(const IterType& iter);
219 /// @return The current value iterator type
221
222 ///@{
223 /// @brief Set the threading grain sizes used when iterating over nodes
224 /// in a VDB.
225 /// @details Two grain sizes are provided, the first of which (g1) is used
226 /// to determine the chunk size of nodes which are not being streamed (see
227 /// setActiveTileStream). Leaf node execution always uses this grain size.
228 /// The default value for g1 is 1 which is typically appropriate for most
229 /// AX kernels.
230 /// The second grain size is used when streaming execution over active
231 /// tiles in a VDB. This execution model differs significantly from
232 /// typical leaf node execution due to the potential for substantially
233 /// more memory to be allocated. The default value is 32, which works well
234 /// for the default configuration of OpenVDB. If streaming is disabled,
235 /// this value has no effect.
236 /// @note Setting g1 or g2 to zero has the effect of disabling
237 /// multi-threading for the respective node executions. Setting both to
238 /// zero will disable all multi-threading performed by the execute method.
239 void setGrainSize(const size_t g1);
240 void setActiveTileStreamingGrainSize(const size_t g2);
241 /// @return The current g1 grain size
242 /// @sa setGrainSize
243 size_t getGrainSize() const;
244 /// @return The current g2 grain size
245 /// @sa setActiveTileStreamingGrainSize
247 ///@}
248
249 /// @brief Set attribute bindings.
250 /// @param bindings A map of attribute bindings to expected names on
251 /// the geometry to be executed over. By default the AX attributes will be
252 /// bound to volumes of the same name. Supplying bindings
253 /// for a subset of the attributes will leave the others unchanged.
254 /// AX attributes can only bind to a single volume and vice versa.
255 /// However, in a single set call these can be swapped e.g. a -> b and b -> a.
256 /// When bindings are overriden through subsequent calls to this function,
257 /// any dangling volumes will be automatically bound by name.
258 /// To reset these bindings call get function and create a target set of bindings
259 /// for each attribute of name -> name.
261 /// @return The current attribute bindings map
263
264 ////////////////////////////////////////////////////////
265
266 // foward declaration of settings for this executable
267 template <bool> struct Settings;
268
269 /// @brief Command Line Interface handling for the VolumeExecutable.
270 /// @details This class wraps the logic for converting commands specific
271 /// to the VolumeExecutable to the internal Settings. Subsequent
272 /// executables can be initialized from the CLI object that gets created.
274 {
278 static CLI create(size_t argc, const char* argv[], bool* used=nullptr);
279 static void usage(std::ostream& os, const bool verbose);
280 private:
281 friend class VolumeExecutable;
282 CLI();
283 std::unique_ptr<Settings<true>> mSettings;
284 };
285
286 /// @brief Intialize the Settings of this executables from the CLI object
287 /// @param cli The CLI object
288 /// @{
289 void setSettingsFromCLI(const CLI& cli);
290 /// @}
291
292private:
293 friend class Compiler;
294
295 /// @brief Constructor, expected to be invoked by the compiler. Should not
296 /// be invoked directly.
297 /// @param context Shared pointer to an llvm:LLVMContext associated with the
298 /// execution engine
299 /// @param engine Shared pointer to an llvm::ExecutionEngine used to build
300 /// functions. Context should be the associated LLVMContext
301 /// @param accessRegistry Registry of volumes accessed by AX code
302 /// @param customData Custom data which will be shared by this executable.
303 /// It can be used to retrieve external data from within the AX code
304 /// @param functions A map of function names to physical memory addresses
305 /// which were built by llvm using engine
306 /// @param tree The AST linked to this executable. The AST is not stored
307 /// after compilation but can be used during construction of the exe to
308 /// infer some pre/post processing optimisations.
310 const std::shared_ptr<const llvm::orc::LLJIT>& mExecutionEngine,
311 const AttributeRegistry::ConstPtr& accessRegistry,
312 const CustomData::ConstPtr& customData,
313 const std::unordered_map<std::string, uint64_t>& functions,
314 const ast::Tree& tree);
315
316private:
317 const std::shared_ptr<const llvm::orc::LLJIT> mExecutionEngine;
318 const AttributeRegistry::ConstPtr mAttributeRegistry;
319 const CustomData::ConstPtr mCustomData;
320 const std::unordered_map<std::string, uint64_t> mFunctionAddresses;
321 std::unique_ptr<Settings<false>> mSettings;
322};
323
324} // namespace ax
325} // namespace OPENVDB_VERSION_NAME
326} // namespace openvdb
327
328#endif // OPENVDB_AX_COMPILER_VOLUME_EXECUTABLE_HAS_BEEN_INCLUDED
329
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:307
Abstract base class for typed grids.
Definition Grid.h:79
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
void setActiveTileStreamingGrainSize(const size_t g2)
Set the threading grain sizes used when iterating over nodes in a VDB.
void setCreateMissing(const bool flag)
Set the behaviour when missing grids are accessed. Default behaviour is true, which creates them with...
const AttributeBindings & getAttributeBindings() const
void execute(openvdb::GridBase &grids) const
Run this volume executable binary on target volumes.
friend class Compiler
Definition VolumeExecutable.h:293
void getTreeExecutionLevel(Index &min, Index &max) const
Get the tree execution levels.
Streaming getActiveTileStreaming() const
std::shared_ptr< VolumeExecutable > Ptr
Definition VolumeExecutable.h:86
void setValueIterator(const IterType &iter)
Set the value iterator type to use with this executable. Options are ON, OFF, ALL....
void setActiveTileStreaming(const Streaming &s)
Controls the behaviour of expansion of active tiles.
void setGrainSize(const size_t g1)
Set the threading grain sizes used when iterating over nodes in a VDB.
Streaming getActiveTileStreaming(const std::string &name, const ast::tokens::CoreType &type) const
void setTreeExecutionLevel(const Index min, const Index max)
Set the execution level for this executable. This controls what nodes are processed when execute is c...
void setSettingsFromCLI(const CLI &cli)
Intialize the Settings of this executables from the CLI object.
Streaming
The streaming type of active tiles during execution.
Definition VolumeExecutable.h:201
@ OFF
Definition VolumeExecutable.h:201
@ ON
Definition VolumeExecutable.h:201
@ AUTO
Definition VolumeExecutable.h:201
void setTreeExecutionLevel(const Index level)
IterType
Definition VolumeExecutable.h:214
@ ALL
Definition VolumeExecutable.h:214
void execute(openvdb::GridPtrVec &grids) const
Run this volume executable binary on target volumes.
void setAttributeBindings(const AttributeBindings &bindings)
Set attribute bindings.
size_t getActiveTileStreamingGrainSize() const
VolumeExecutable(const VolumeExecutable &other)
Copy constructor. Shares the LLVM constructs but deep copies the settings. Multiple copies of an exec...
CoreType
Definition Tokens.h:32
Definition PointDataGrid.h:48
std::vector< GridBase::Ptr > GridPtrVec
Definition Grid.h:508
Index32 Index
Definition Types.h:34
Definition Exceptions.h:13
Command Line Interface handling for the VolumeExecutable.
Definition VolumeExecutable.h:274
friend class VolumeExecutable
Definition VolumeExecutable.h:281
static void usage(std::ostream &os, const bool verbose)
static CLI create(size_t argc, const char *argv[], bool *used=nullptr)
Definition VolumeExecutable.h:267
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:291