-
Notifications
You must be signed in to change notification settings - Fork 665
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add early-exit optimization for LeafNode::resetBackground() #1751
Add early-exit optimization for LeafNode::resetBackground() #1751
Conversation
Signed-off-by: Neil Bickford <[email protected]>
Hi @NBickford-NV, thanks for this. Your idea looks good but I don't believe the second condition is valid: math::negative(oldBackground) == newBackground The loop still needs to be executed when this is true - inactive values with a different sign will have their sign flipped. There is a question as to whether or not this comparison should be done by the parent function (because why would you be calling if (math::isExactlyEqual(oldBackground, newBackground)) return; openvdb/openvdb/openvdb/tree/InternalNode.h Lines 3011 to 3026 in 6b2d0e3
I am not a huge fan of the fact that these currently do the approx comparisons, but we could solve this by introducing a third optional tolerance parameter which defaults to Regardless I think the change is a good one, but it doesn't make sense for Internal/Leaf nodes to perform different variants of this optimisation - matching the implementation in |
I agree with @Idclip, adding a similar early-exit clause similar to the one in I will add that this method is known to be slow as it is single-threaded and the poor performance is particularly apparent when merging multiple grids as they need to be merged one-by-one. We introduced a much faster merging method recently that can parallelize across different levels of the tree and across multiple grids at once which significantly improves performance (https://github.com/AcademySoftwareFoundation/openvdb/blob/master/openvdb/openvdb/tools/Merge.h). This new method is now used for all CSG operations, but does not yet support the functionality in |
Hi @Idclip ! Yep, I think you're right; Yep, it turns out that even though |
Signed-off-by: Neil Bickford <[email protected]>
Looks like the macos-nanovdb CI's CMake fails when trying to find TBB. I don't think this merge request modifies that part of the code; anything I can do here? Thanks! |
That error is unrelated to this so no worries there, this just needs a second approval and can be merged |
9ae369e
into
AcademySoftwareFoundation:master
Hi Academy Software Foundation! This merge request adds a small early-exit check to
LeafNode::resetBackground()
. This results in a significant speedup when an app merges grids on a node-by-node level usingGrid::merge(..., openvdb::MergePolicy::MERGE_NODES)
.For a grid where voxels are either equal to the background value, or more than the default tolerance (1e-7) away from the (positive or negative) background value,
LeafNode::resetBackground()
is a no-op ifoldBackground == newBackground
oroldBackground == -newBackground
:This means that we can add this check to skip the
for
loop entirely:On my test application (which computes multiple grids in parallel in such a way that they can use
openvdb::MergePolicy::MERGE_NODES
), this improves performance of merging together several grids with the same background by about 20x, from ~0.1 seconds to ~0.005 seconds on a ~100 MB volume.There's one thing to consider here, though: If a grid has values that are ApproxEqual but not exactly equal to the background, then
resetBackground(x, +-x)
's current implementation "canonicalizes" these values to be exactly equal to the background. This is undocumented -- the documentation forresetBackground()
only talks about values that are exactly equal to the background -- but maybe there's an app out there that relies upon this.Thank you! Please let me know if there are any changes I can make to this.