15 #ifndef OPENVDB_TREE_LEAFMANAGER_HAS_BEEN_INCLUDED 16 #define OPENVDB_TREE_LEAFMANAGER_HAS_BEEN_INCLUDED 20 #include <tbb/blocked_range.h> 21 #include <tbb/parallel_for.h> 22 #include <tbb/parallel_reduce.h> 25 #include <type_traits> 38 static const bool IsConstTree =
false;
41 template<
typename TreeT>
struct TreeTraits<const TreeT> {
42 static const bool IsConstTree =
true;
52 template<
typename ManagerT>
55 using RangeT =
typename ManagerT::RangeType;
56 using LeafT =
typename ManagerT::LeafType;
57 using BufT =
typename ManagerT::BufferType;
60 LeafT** leafs,
BufT* bufs,
size_t bufsPerLeaf)
62 for (
size_t n = r.begin(), m = r.end(), N = bufsPerLeaf; n != m; ++n) {
63 leafs[n]->swap(bufs[n * N + auxBufferIdx]);
83 template<
typename TreeT>
87 using TreeType = TreeT;
123 return mRange.mLeafManager.getBuffer(mPos, bufferIdx);
126 size_t pos()
const {
return mPos; }
128 bool isValid()
const {
return mPos>=mRange.mBegin && mPos<=mRange.mEnd; }
130 bool test()
const {
return mPos < mRange.mEnd; }
132 operator bool()
const {
return this->test(); }
134 bool empty()
const {
return !this->test(); }
137 return (mPos != other.mPos) || (&mRange != &other.mRange);
150 , mGrainSize(grainSize)
151 , mLeafManager(leafManager)
159 size_t size()
const {
return mEnd - mBegin; }
165 bool empty()
const {
return !(mBegin < mEnd);}
172 , mGrainSize(r.mGrainSize)
173 , mLeafManager(r.mLeafManager)
178 size_t mEnd, mBegin, mGrainSize;
184 size_t middle = r.mBegin + (r.mEnd - r.mBegin) / 2u;
192 LeafManager(TreeType& tree,
size_t auxBuffersPerLeaf=0,
bool serial=
false)
196 , mAuxBuffersPerLeaf(auxBuffersPerLeaf)
198 this->rebuild(serial);
205 size_t auxBuffersPerLeaf=0,
bool serial=
false)
207 , mLeafCount(end-begin)
209 , mAuxBuffersPerLeaf(auxBuffersPerLeaf)
210 , mLeafPtrs(new
LeafType*[mLeafCount])
211 , mLeafs(mLeafPtrs.get())
213 size_t n = mLeafCount;
214 LeafType **target = mLeafs, **source = begin;
215 while (n--) *target++ = *source++;
216 if (auxBuffersPerLeaf) this->initAuxBuffers(serial);
224 , mLeafCount(other.mLeafCount)
225 , mAuxBufferCount(other.mAuxBufferCount)
226 , mAuxBuffersPerLeaf(other.mAuxBuffersPerLeaf)
227 , mLeafs(other.mLeafs)
228 , mAuxBuffers(other.mAuxBuffers)
240 this->initLeafArray(serial);
241 this->initAuxBuffers(serial);
245 void rebuild(
size_t auxBuffersPerLeaf,
bool serial=
false)
247 mAuxBuffersPerLeaf = auxBuffersPerLeaf;
248 this->rebuild(serial);
250 void rebuild(TreeType& tree,
bool serial=
false)
253 this->rebuild(serial);
255 void rebuild(TreeType& tree,
size_t auxBuffersPerLeaf,
bool serial=
false)
258 mAuxBuffersPerLeaf = auxBuffersPerLeaf;
259 this->rebuild(serial);
268 mAuxBuffersPerLeaf = auxBuffersPerLeaf;
269 this->initAuxBuffers(serial);
277 this->removeAuxBuffers();
278 this->initLeafArray(serial);
293 return tbb::parallel_reduce(this->leafRange(),
Index64(0),
295 for (
const auto& leaf: range) { sum += leaf.onVoxelCount(); }
302 const TreeType&
tree()
const {
return *mTree; }
305 TreeType&
tree() {
return *mTree; }
318 LeafType&
leaf(
size_t leafIdx)
const { assert(leafIdx<mLeafCount);
return *mLeafs[leafIdx]; }
332 assert(leafIdx < mLeafCount);
333 assert(bufferIdx == 0 || bufferIdx - 1 < mAuxBuffersPerLeaf);
334 return bufferIdx == 0 ? mLeafs[leafIdx]->buffer()
335 : mAuxBuffers[leafIdx * mAuxBuffersPerLeaf + bufferIdx - 1];
347 return LeafRange(0, mLeafCount, *
this, grainsize);
361 namespace ph = std::placeholders;
362 if (bufferIdx == 0 || bufferIdx > mAuxBuffersPerLeaf || this->isConstTree())
return false;
363 mTask = std::bind(&LeafManager::doSwapLeafBuffer, ph::_1, ph::_2, bufferIdx - 1);
364 this->cook(serial ? 0 : 512);
371 bool swapBuffer(
size_t bufferIdx1,
size_t bufferIdx2,
bool serial =
false)
373 namespace ph = std::placeholders;
374 const size_t b1 =
std::min(bufferIdx1, bufferIdx2);
375 const size_t b2 =
std::max(bufferIdx1, bufferIdx2);
376 if (b1 == b2 || b2 > mAuxBuffersPerLeaf)
return false;
378 if (this->isConstTree())
return false;
379 mTask = std::bind(&LeafManager::doSwapLeafBuffer, ph::_1, ph::_2, b2-1);
381 mTask = std::bind(&LeafManager::doSwapAuxBuffer, ph::_1, ph::_2, b1-1, b2-1);
383 this->cook(serial ? 0 : 512);
397 namespace ph = std::placeholders;
398 if (bufferIdx == 0 || bufferIdx > mAuxBuffersPerLeaf)
return false;
399 mTask = std::bind(&LeafManager::doSyncAuxBuffer, ph::_1, ph::_2, bufferIdx - 1);
400 this->cook(serial ? 0 : 64);
409 namespace ph = std::placeholders;
410 switch (mAuxBuffersPerLeaf) {
411 case 0:
return false;
412 case 1: mTask = std::bind(&LeafManager::doSyncAllBuffers1, ph::_1, ph::_2);
break;
413 case 2: mTask = std::bind(&LeafManager::doSyncAllBuffers2, ph::_1, ph::_2);
break;
414 default: mTask = std::bind(&LeafManager::doSyncAllBuffersN, ph::_1, ph::_2);
break;
416 this->cook(serial ? 0 : 64);
482 template<
typename LeafOp>
483 void foreach(
const LeafOp& op,
bool threaded =
true,
size_t grainSize=1)
485 LeafTransformer<LeafOp> transform(op);
486 transform.run(this->leafRange(grainSize), threaded);
531 template<
typename LeafOp>
532 void reduce(LeafOp& op,
bool threaded =
true,
size_t grainSize=1)
534 LeafReducer<LeafOp> transform(op);
535 transform.run(this->leafRange(grainSize), threaded);
549 size_t getPrefixSum(
size_t*& offsets,
size_t& size,
size_t grainSize=1)
const 551 if (offsets ==
nullptr || size < mLeafCount) {
553 offsets =
new size_t[mLeafCount];
557 if ( grainSize > 0 ) {
558 PrefixSum tmp(this->leafRange( grainSize ), offsets, prefix);
560 for (
size_t i=0; i<mLeafCount; ++i) {
562 prefix += mLeafs[i]->onVoxelCount();
574 if (mTask) mTask(const_cast<LeafManager*>(
this), r);
580 void initLeafArray(
bool serial =
false)
585 using NonConstLeafParentT =
typename NodeChainT::template Get<1>;
588 std::deque<LeafParentT*> leafParents;
589 mTree->getNodes(leafParents);
593 std::vector<Index32> leafCounts;
595 leafCounts.reserve(leafParents.size());
596 for (LeafParentT* leafParent : leafParents) {
597 leafCounts.push_back(leafParent->childCount());
600 leafCounts.resize(leafParents.size());
605 tbb::blocked_range<size_t>(0, leafParents.size(), 64),
606 [&](tbb::blocked_range<size_t>& range)
608 for (
size_t i = range.begin(); i < range.end(); i++) {
609 leafCounts[i] = leafParents[i]->childCount();
617 for (
size_t i = 1; i < leafCounts.size(); i++) {
618 leafCounts[i] += leafCounts[i-1];
621 const size_t leafCount = leafCounts.empty() ? 0 : leafCounts.back();
625 if (leafCount != mLeafCount) {
627 mLeafPtrs.reset(
new LeafType*[leafCount]);
628 mLeafs = mLeafPtrs.get();
633 mLeafCount = leafCount;
636 if (mLeafCount == 0)
return;
642 for (LeafParentT* leafParent : leafParents) {
643 for (
auto iter = leafParent->beginChildOn(); iter; ++iter) {
644 *leafPtr++ = &iter.getValue();
649 tbb::blocked_range<size_t>(0, leafParents.size()),
650 [&](tbb::blocked_range<size_t>& range)
652 size_t i = range.begin();
654 if (i > 0) leafPtr += leafCounts[i-1];
655 for ( ; i < range.end(); i++) {
656 for (
auto iter = leafParents[i]->beginChildOn(); iter; ++iter) {
657 *leafPtr++ = &iter.getValue();
665 void initAuxBuffers(
bool serial)
667 const size_t auxBufferCount = mLeafCount * mAuxBuffersPerLeaf;
668 if (auxBufferCount != mAuxBufferCount) {
669 if (auxBufferCount > 0) {
671 mAuxBuffers = mAuxBufferPtrs.get();
673 mAuxBufferPtrs.reset();
674 mAuxBuffers =
nullptr;
676 mAuxBufferCount = auxBufferCount;
678 this->syncAllBuffers(serial);
681 void cook(
size_t grainsize)
684 tbb::parallel_for(this->getRange(grainsize), *
this);
686 (*this)(this->getRange());
690 void doSwapLeafBuffer(
const RangeType& r,
size_t auxBufferIdx)
693 r, auxBufferIdx, mLeafs, mAuxBuffers, mAuxBuffersPerLeaf);
696 void doSwapAuxBuffer(
const RangeType& r,
size_t auxBufferIdx1,
size_t auxBufferIdx2)
698 for (
size_t N = mAuxBuffersPerLeaf, n = N*r.begin(), m = N*r.end(); n != m; n+=N) {
699 mAuxBuffers[n + auxBufferIdx1].swap(mAuxBuffers[n + auxBufferIdx2]);
703 void doSyncAuxBuffer(
const RangeType& r,
size_t auxBufferIdx)
705 for (
size_t n = r.begin(), m = r.end(), N = mAuxBuffersPerLeaf; n != m; ++n) {
706 mAuxBuffers[n*N + auxBufferIdx] = mLeafs[n]->buffer();
710 void doSyncAllBuffers1(
const RangeType& r)
712 for (
size_t n = r.begin(), m = r.end(); n != m; ++n) {
713 mAuxBuffers[n] = mLeafs[n]->buffer();
717 void doSyncAllBuffers2(
const RangeType& r)
719 for (
size_t n = r.begin(), m = r.end(); n != m; ++n) {
720 const BufferType& leafBuffer = mLeafs[n]->buffer();
721 mAuxBuffers[2*n ] = leafBuffer;
722 mAuxBuffers[2*n+1] = leafBuffer;
726 void doSyncAllBuffersN(
const RangeType& r)
728 for (
size_t n = r.begin(), m = r.end(), N = mAuxBuffersPerLeaf; n != m; ++n) {
729 const BufferType& leafBuffer = mLeafs[n]->buffer();
730 for (
size_t i=n*N, j=i+N; i!=j; ++i) mAuxBuffers[i] = leafBuffer;
736 template<
typename LeafOp>
737 struct LeafTransformer
739 LeafTransformer(
const LeafOp &leafOp) : mLeafOp(leafOp)
742 void run(
const LeafRange &range,
bool threaded)
const 744 threaded ? tbb::parallel_for(range, *
this) : (*this)(range);
746 void operator()(
const LeafRange &range)
const 748 for (
typename LeafRange::Iterator it = range.begin(); it; ++it) mLeafOp(*it, it.pos());
750 const LeafOp mLeafOp;
755 template<
typename LeafOp>
758 LeafReducer(LeafOp &leafOp) : mLeafOp(&leafOp)
761 LeafReducer(
const LeafReducer &other, tbb::split)
762 : mLeafOpPtr(std::make_unique<LeafOp>(*(other.mLeafOp), tbb::split()))
763 , mLeafOp(mLeafOpPtr.get())
766 void run(
const LeafRange& range,
bool threaded)
768 threaded ? tbb::parallel_reduce(range, *
this) : (*this)(range);
770 void operator()(
const LeafRange& range)
772 LeafOp &op = *mLeafOp;
773 for (
typename LeafRange::Iterator it = range.begin(); it; ++it) op(*it, it.pos());
775 void join(
const LeafReducer& other) { mLeafOp->join(*(other.mLeafOp)); }
776 std::unique_ptr<LeafOp> mLeafOpPtr;
777 LeafOp *mLeafOp =
nullptr;
783 PrefixSum(
const LeafRange& r,
size_t* offsets,
size_t& prefix)
786 tbb::parallel_for( r, *
this);
787 for (
size_t i=0, leafCount = r.size(); i<leafCount; ++i) {
788 size_t tmp = offsets[i];
793 inline void operator()(
const LeafRange& r)
const {
794 for (
typename LeafRange::Iterator i = r.begin(); i; ++i) {
795 mOffsets[i.pos()] = i->onVoxelCount();
801 using FuncType =
typename std::function<void (LeafManager*, const RangeType&)>;
804 size_t mLeafCount, mAuxBufferCount, mAuxBuffersPerLeaf;
805 std::unique_ptr<LeafType*[]> mLeafPtrs;
807 std::unique_ptr<NonConstBufferType[]> mAuxBufferPtrs;
809 FuncType mTask =
nullptr;
814 template<
typename TreeT>
833 #endif // OPENVDB_TREE_LEAFMANAGER_HAS_BEEN_INCLUDED typename std::remove_const< ToType >::type Type
Definition: Types.h:400
typename TreeT::LeafCIter LeafIterType
Definition: LeafManager.h:43
LeafManager(TreeType &tree, size_t auxBuffersPerLeaf=0, bool serial=false)
Constructor from a tree reference and an auxiliary buffer count.
Definition: LeafManager.h:192
bool empty() const
Definition: LeafManager.h:165
Index32 Index
Definition: Types.h:54
LeafType LeafNodeType
Definition: LeafManager.h:92
typename ManagerT::RangeType RangeT
Definition: LeafManager.h:818
uint64_t Index64
Definition: Types.h:53
Definition: LeafManager.h:104
void rebuild(TreeType &tree, bool serial=false)
Repopulate the leaf array and delete and reallocate auxiliary buffers.
Definition: LeafManager.h:250
LeafRange(LeafRange &r, tbb::split)
Definition: LeafManager.h:169
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
Mat3< typename promote< T0, T1 >::type > operator*(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Multiply m0 by m1 and return the resulting matrix.
Definition: Mat3.h:597
bool isValid() const
Return true if the position of this iterator is in a valid range.
Definition: LeafManager.h:128
LeafType & operator*() const
Return a reference to the leaf node to which this iterator is pointing.
Definition: LeafManager.h:116
const LeafManager & leafManager() const
Definition: LeafManager.h:163
void rebuild(TreeType &tree, size_t auxBuffersPerLeaf, bool serial=false)
Repopulate the leaf array and delete and reallocate auxiliary buffers.
Definition: LeafManager.h:255
size_t getPrefixSum(size_t *&offsets, size_t &size, size_t grainSize=1) const
Generate a linear array of prefix sums of offsets into the active voxels in the leafs. So offsets[n]+m is the offset to the mth active voxel in the nth leaf node (useful for user-managed value buffers, e.g. in tools/LevelSetAdvect.h).
Definition: LeafManager.h:549
void rebuild(size_t auxBuffersPerLeaf, bool serial=false)
Repopulate the leaf array and delete and reallocate auxiliary buffers.
Definition: LeafManager.h:245
Iterator & operator++()
Advance to the next leaf node.
Definition: LeafManager.h:114
typename ManagerT::LeafType LeafT
Definition: LeafManager.h:56
bool syncAuxBuffer(size_t bufferIdx, bool serial=false)
Sync up the specified auxiliary buffer with the corresponding leaf node buffer.
Definition: LeafManager.h:395
typename CopyConstness< TreeType, NonConstBufferType >::Type BufferType
Definition: LeafManager.h:95
Definition: Exceptions.h:65
void reduce(LeafOp &op, bool threaded=true, size_t grainSize=1)
Threaded method that applies a user-supplied functor to each leaf node in the LeafManager. Unlike foreach (defined above) this method performs a reduction on all the leaf nodes.
Definition: LeafManager.h:532
void rebuildAuxBuffers(size_t auxBuffersPerLeaf, bool serial=false)
Change the number of auxiliary buffers.
Definition: LeafManager.h:266
void rebuild(bool serial=false)
(Re)initialize by resizing (if necessary) and repopulating the leaf array and by deleting existing au...
Definition: LeafManager.h:238
typename TreeType::RootNodeType RootNodeType
Definition: LeafManager.h:89
const TreeType & tree() const
Return a const reference to tree associated with this manager.
Definition: LeafManager.h:302
size_t auxBuffersPerLeaf() const
Return the number of auxiliary buffers per leaf node.
Definition: LeafManager.h:284
static void doSwapLeafBuffer(const RangeT &, size_t, LeafT **, BufT *, size_t)
Definition: LeafManager.h:822
typename TreeType::ValueType ValueType
Definition: LeafManager.h:88
typename ManagerT::RangeType RangeT
Definition: LeafManager.h:55
Iterator begin() const
Definition: LeafManager.h:155
void operator()(const RangeType &r) const
Used internally by tbb::parallel_for() - never call it directly!
Definition: LeafManager.h:572
const LeafRange & leafRange() const
Definition: LeafManager.h:140
typename TreeT::LeafIter LeafIterType
Definition: LeafManager.h:39
typename CopyConstness< TreeType, NonConstLeafType >::Type LeafType
Definition: LeafManager.h:91
bool swapBuffer(size_t bufferIdx1, size_t bufferIdx2, bool serial=false)
Swap any two buffers for each leaf node.
Definition: LeafManager.h:371
OPENVDB_AX_API void run(const char *ax, openvdb::GridBase &grid, const AttributeBindings &bindings={})
Run a full AX pipeline (parse, compile and execute) on a single OpenVDB Grid.
size_t grainsize() const
Definition: LeafManager.h:161
typename LeafType::Buffer NonConstBufferType
Definition: LeafManager.h:94
bool is_divisible() const
Definition: LeafManager.h:167
static void doSwapLeafBuffer(const RangeT &r, size_t auxBufferIdx, LeafT **leafs, BufT *bufs, size_t bufsPerLeaf)
Definition: LeafManager.h:59
bool operator==(const Iterator &other) const
Definition: LeafManager.h:139
bool test() const
Return true if this iterator is not yet exhausted.
Definition: LeafManager.h:130
bool isConstTree() const
Return true if the tree associated with this manager is immutable.
Definition: LeafManager.h:314
LeafManager(const LeafManager &other)
Definition: LeafManager.h:222
size_t auxBufferCount() const
Return the total number of allocated auxiliary buffers.
Definition: LeafManager.h:282
RangeType getRange(size_t grainsize=1) const
Return a tbb::blocked_range of leaf array indices.
Definition: LeafManager.h:342
typename ManagerT::BufferType BufT
Definition: LeafManager.h:820
const RootNodeType & root() const
Return a const reference to root node associated with this manager.
Definition: LeafManager.h:308
Definition: Exceptions.h:13
Iterator end() const
Definition: LeafManager.h:157
bool swapLeafBuffer(size_t bufferIdx, bool serial=false)
Swap each leaf node's buffer with the nth corresponding auxiliary buffer, where n = bufferIdx...
Definition: LeafManager.h:359
Iterator(const LeafRange &range, size_t pos)
Definition: LeafManager.h:107
BufferType & getBuffer(size_t leafIdx, size_t bufferIdx) const
Return the leaf or auxiliary buffer for the leaf node at index leafIdx. If bufferIdx is zero...
Definition: LeafManager.h:330
size_t size() const
Definition: LeafManager.h:159
LeafRange(size_t begin, size_t end, const LeafManager &leafManager, size_t grainSize=1)
Definition: LeafManager.h:147
TreeType & tree()
Return a reference to the tree associated with this manager.
Definition: LeafManager.h:305
void rebuildLeafArray(bool serial=false)
Remove the auxiliary buffers and rebuild the leaf array.
Definition: LeafManager.h:275
size_t pos() const
Return the index into the leaf array of the current leaf node.
Definition: LeafManager.h:126
typename ManagerT::LeafType LeafT
Definition: LeafManager.h:819
Definition: LeafManager.h:101
void removeAuxBuffers()
Remove the auxiliary buffers, but don't rebuild the leaf array.
Definition: LeafManager.h:272
Useful traits for Tree types.
Definition: LeafManager.h:37
LeafType * operator->() const
Return a pointer to the leaf node to which this iterator is pointing.
Definition: LeafManager.h:118
The root node of an OpenVDB tree.
LeafManager(TreeType &tree, LeafType **begin, LeafType **end, size_t auxBuffersPerLeaf=0, bool serial=false)
Construct directly from an existing array of leafnodes.
Definition: LeafManager.h:204
bool operator!=(const Iterator &other) const
Definition: LeafManager.h:135
typename leafmgr::TreeTraits< TreeType >::LeafIterType LeafIterType
Definition: LeafManager.h:93
bool syncAllBuffers(bool serial=false)
Sync up all auxiliary buffers with their corresponding leaf node buffers.
Definition: LeafManager.h:407
typename TreeType::LeafNodeType NonConstLeafType
Definition: LeafManager.h:90
This class manages a linear array of pointers to a given tree's leaf nodes, as well as optional auxil...
Definition: LeafManager.h:84
Index64 activeLeafVoxelCount() const
Return the number of active voxels in the leaf nodes.
Definition: LeafManager.h:291
size_t leafCount() const
Return the number of leaf nodes.
Definition: LeafManager.h:287
Definition: LeafManager.h:53
static bool isValid(const void *p)
return true if the specified pointer is aligned and not NULL
Definition: NanoVDB.h:504
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
BufferType & buffer(size_t bufferIdx)
Return the nth buffer for the leaf node to which this iterator is pointing, where n = bufferIdx and n...
Definition: LeafManager.h:121
RootNodeType & root()
Return a reference to the root node associated with this manager.
Definition: LeafManager.h:311
LeafRange leafRange(size_t grainsize=1) const
Return a TBB-compatible LeafRange.
Definition: LeafManager.h:345
LeafType & leaf(size_t leafIdx) const
Return a pointer to the leaf node at index leafIdx in the array.
Definition: LeafManager.h:318
typename SubtreeT::template Append< HeadT > Type
Definition: RootNode.h:1003
bool empty() const
Return true if this iterator is exhausted.
Definition: LeafManager.h:134
tbb::blocked_range< size_t > RangeType
Definition: LeafManager.h:96
typename ManagerT::BufferType BufT
Definition: LeafManager.h:57
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:212