facontidavide / Bonxai

Fast, hierarchical, sparse Voxel Grid
Mozilla Public License 2.0
643 stars 54 forks source link

OpenVDB benchmark #5

Closed YoshuaNava closed 2 years ago

YoshuaNava commented 2 years ago

I found that the OpenVDB benchmark for iterating over all cells was using a voxel-On iterator, which is user-friendly, safe, and fast, but not super fast. After running the benchmarks:

------------------------------------------------------------------
Benchmark                        Time             CPU   Iterations
------------------------------------------------------------------
Treexy_Create              2363567 ns      2347860 ns          293
Treexy_Update              2474811 ns      2472939 ns          279
Treexy_IterateAllCells       62399 ns        62399 ns        11633 ###
Octomap_Create            40142650 ns     40142345 ns           17
Octomap_Update            14739847 ns     14739871 ns           46
Octomap_IterateAllCells     425256 ns       425099 ns         1667
OpenVDB_Create             2785683 ns      2713275 ns          267
OpenVDB_Update             2024528 ns      2024314 ns          347
OpenVDB_IterateAllCells     202940 ns       202938 ns         3445 ###
Lama_Create               10595711 ns     10595341 ns           67
Lama_Update               10171020 ns     10170439 ns           62
Lama_IterateAllCells       3109528 ns      3109444 ns          218

Reading through this post from the OpenVDB mailing list: https://lists.aswf.io/g/openvdb-dev/topic/32212211?p=,,,20,0,0,0::,,,0,0,0,32212211

I found that there is a class called LeafManager that allows access leaping from leaf to leaf, which also grants the chance of iterating over the voxels below a given leaf (similar to what Bonxai is doing):

#include <openvdb/tree/LeafManager.h>
...
// To make a fair comparison, use Log2DIM values similar to Treexy
using TreeType = openvdb::tree::Tree4<int32_t, 2, 2, 3>::Type;
using GridType = openvdb::Grid<TreeType>;
...

static void OpenVDB_IterateAllCells(benchmark::State& state)
{
...
  openvdb::tree::LeafManager<TreeType> leafManager(grid->tree());

  long count = 0;
  for (auto _ : state)
  {
    auto visitor = [&](const TreeType::LeafNodeType& leaf, size_t /*idx*/) {
      for (auto nodeIter = leaf.beginValueOn(); nodeIter; ++nodeIter)
      {
        count++;
      }
    };
    leafManager.foreach(visitor, false);
  }

I ran the benchmarks with this modification and observed a solid improvement:

------------------------------------------------------------------
Benchmark                        Time             CPU   Iterations
------------------------------------------------------------------
...
Treexy_IterateAllCells       56818 ns        56818 ns        12411
...
OpenVDB_IterateAllCells      84142 ns        84134 ns         8238
...

Furthermore, by switching the tree configuration to openvdb::tree::Tree3<int32_t, 4, 2>::Type;, I was able to get results that almost match those of Bonxai:

------------------------------------------------------------------
Benchmark                        Time             CPU   Iterations
------------------------------------------------------------------
...
Treexy_IterateAllCells       57986 ns        57983 ns        11952
...
OpenVDB_IterateAllCells      64163 ns        64159 ns        10636

And when enabling multi-threaded access:

------------------------------------------------------------------
Benchmark                        Time             CPU   Iterations
------------------------------------------------------------------
...
Treexy_IterateAllCells       57688 ns        57686 ns        12146
...
OpenVDB_IterateAllCells      39098 ns        39097 ns        17763
...
facontidavide commented 2 years ago

thanks, I will repeat the benchmark!!!

facontidavide commented 2 years ago

I applied your changes, thanks!

But my results are as follow:

------------------------------------------------------------------
Benchmark                        Time             CPU   Iterations
------------------------------------------------------------------
Treexy_IterateAllCells       20474 ns        20459 ns        32595
Octomap_IterateAllCells     230635 ns       230507 ns         2985
OpenVDB_IterateAllCells      55311 ns        55224 ns        12229

Could it be that you did not compile in Release?

YoshuaNava commented 2 years ago

@facontidavide You're right, I usually build with the RelWithDebInfo flag. What you show in your latest update is an impressive amount of iterations, wow!