[BUG] GridTransformer threaded transformGrid is not deterministic

Aperta
#2,432 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
cpp

Direzione di ricerca

Start in openvdb/openvdb/tools/GridTransformer.h: transformBBox (called per input leaf from the RangeProcessor, around line 815), the affine branch that steps the sample position incrementally (around lines 1014-1023), and join(), which calls Tree::merge (around line 854). Build the reproducer in the issue body and confirm threaded runs differ from the non-threaded reference. Done means repeated threaded runs produce identical values and signs, and the existing GridTransformer tests still pass.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug
Environment

Operating System: Ubuntu 24.04
Version / Commit SHA: OpenVDB 13.2 and 14.0

Describe the bug

Running tools::GridTransformer::transformGrid() twice on the same input with the same affine
transform gives different voxel values when threading is on (the default).
With a 180° rotation of a level-set box, voxels whose exact value is 0 become ±1.8e-14, so
some voxel change sign (100–400 differing voxels per run).
Topologically the results are the same.

If I deactivate the threading on the grid transformer with setThreaded(false) then
I do not get any issue.

To Reproduce

See the reproducer at the bottom, you can compile it against current OpenVDB 14.0.0
you will get something along that in the result (note since it is a scheduling issue, you will
definetly get different numbers than me)

FAILED: threaded=1 iteration 0: 122 differing values, 52 sign flips
FAILED: threaded=1 iteration 1: 324 differing values, 132 sign flips
FAILED: threaded=1 iteration 2: 226 differing values, 124 sign flips
FAILED: threaded=1 iteration 3: 154 differing values, 68 sign flips
FAILED: threaded=1 iteration 4: 130 differing values, 68 sign flips
FAILED: threaded=1 iteration 5: 108 differing values, 60 sign flips
FAILED: threaded=1 iteration 6: 302 differing values, 152 sign flips
FAILED: threaded=1 iteration 7: 202 differing values, 68 sign flips
FAILED: threaded=1 iteration 8: 322 differing values, 172 sign flips
FAILED: threaded=1 iteration 9: 234 differing values, 72 sign flips
FAILED (10 runs)

The reproducer is just calling grid transform with a rotation of 180 and comparing it to the next 10 same transforms.

Bug root cause

This bug happens because of two things:

  1. Resulting value does depend on the input leaf.

    • transformBBox is called by the RangeProcessor for every input leaf (here).
    • The output regions of neighbouring leaves overlap (Sampler::radius() padding), so a shared output voxel is computed by several leaves.
    • The affine branch steps the sample position incrementally from the leaf-specific outMin (inXYZ += deltaZ, here), so each leaf rounds differently.
    • sin(π) ≈ 1.2e-16 != 0, so the deltas are not exactly integer and the sample positions differ in the last bits.
  2. Which value will be written depends on TBB scheduling.

    • Each parallel_reduce body writes into its own tree.
    • join() calls Tree::merge() (here), which keeps the left tree's value on overlapping active voxels.
    • Split/join points vary between runs. When disabling threading we get deterministic results because the last leaf always wins.
Fix for this issue

Instead of using positions which might be different, use a fixed position depending of the coordinate for every leaf so when you merge back into the tree you get identical values

--- a/openvdb/openvdb/tools/GridTransformer.h
+++ b/openvdb/openvdb/tools/GridTransformer.h
@@ -1021,6 +1021,8 @@
                 if (interrupt && interrupt()) break;
                 Vec3R inXYZ = inStartY;
                 for (z = outMin.z(); z <= outMax.z(); ++z, inXYZ += deltaZ) {
+                    // Sample position as a pure function of the output voxel
+                    inXYZ = xform.invTransform(Vec3R(x, y, z));
                     ValueT result;
                     if (sampler.sample(inTree, inXYZ, result)) {
                         outTree.setValueOn(outXYZ, result);
Additional context
#include <openvdb/tools/GridOperators.h>
#include <openvdb/tools/GridTransformer.h>
#include <openvdb/tools/LevelSetSphere.h>
#include <openvdb/tools/LevelSetUtil.h>
#include <openvdb/tools/MeshToVolume.h>

#include <cmath>
#include <cstdio>
#include <cstdlib>


int main()
{
  const double voxel_size = 5.0;
  const openvdb::FloatGrid::Ptr input = openvdb::tools::createLevelSetBox<openvdb::FloatGrid>(
    openvdb::BBoxd({ -300.0, -150.0, 0.0 }, { 300.0, 150.0, 90.0 }), *openvdb::math::Transform::createLinearTransform(voxel_size));

  openvdb::Mat4R flip;
  flip.setToRotation(openvdb::Vec3R(1.0, 0.0, 0.0), -M_PI);

  const auto transformed = [&](const bool threaded) {
    openvdb::tools::GridTransformer transformer(flip);
    transformer.setThreaded(threaded);
    openvdb::FloatGrid::Ptr output = openvdb::FloatGrid::create(*input);
    transformer.transformGrid<openvdb::tools::BoxSampler>(*input, *output);
    return output;
  };

  int failures = 0;
  for (const bool threaded : { false, true })
  {
    const openvdb::FloatGrid::Ptr reference = transformed(threaded);

    for (int i = 0; i < 10; ++i)
    {
      const openvdb::FloatGrid::Ptr output = transformed(threaded);

      if (!reference->tree().hasSameTopology(output->tree()))
      {
        std::printf("FAILED: different topology, threaded=%d iteration %d\n", threaded, i);
        return EXIT_FAILURE;
      }

      const openvdb::FloatGrid::ConstAccessor accessor = output->getConstAccessor();
      size_t differing_values = 0;
      size_t differing_signs = 0;
      for (auto voxel = reference->tree().cbeginValueAll(); voxel; ++voxel)
      {
        const float value = accessor.getValue(voxel.getCoord());
        if (value != voxel.getValue())
        {
          ++differing_values;
          if (std::signbit(value) != std::signbit(voxel.getValue()))
          {
            ++differing_signs;
          }
        }
      }

      if (differing_values != 0)
      {
        std::printf("FAILED: threaded=%d iteration %d: %zu differing values, %zu sign flips\n", threaded, i, differing_values, differing_signs);
        ++failures;
      }
    }
  }
  std::printf(failures ? "FAILED (%d runs)\n" : "OK\n", failures);
  return failures ? EXIT_FAILURE : EXIT_SUCCESS;
}
Lingua principale
C++
Stelle
3.6k
Fork
807
Merge medio
6g 4h
PR unite (30g)
20

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di AcademySoftwareFoundation/openvdb

Tutte le issue di AcademySoftwareFoundation/openvdb

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.