Skip to content

LBIE: LIBISOCONTOUR extraction takes the libContour lock the contour spectrum oracle takes - #919

Merged
transfix merged 1 commit into
masterfrom
fix/lbie-libisocontour-mutex
Oct 31, 2026
Merged

transfix merged 1 commit into
masterfrom
fix/lbie-libisocontour-mutex

Conversation

@transfix

Copy link
Copy Markdown
Owner

LBIE's LIBISOCONTOUR extraction (LBIE::Mesher::do_libisocontour_extract()) calls the vendored libContour without the lock the contour spectrum's libContour oracle already takes. libContour's state is process-wide. IntTree::Done() points global_tree at the tree it sorts, and its qsort comparators read the cell ranges through that pointer. rangesweep.cpp (maxqsize, a static queue), range.cpp (static results), Data (static topology variables) and verbose hold more. So two cvc::iso(vol, iso, LIBISOCONTOUR) calls on two threads race, and so does one beside compute_contour_spectrum_libcontour. The default DUALLIB and FASTCONTOURING paths don't use libContour and aren't affected.

Change

  • libcontour_mutex() leaves the oracle's anonymous namespace. It is declared in a new internal header, src/cvc/geometry/cvc-mesher/libcontour_mutex.h, which is listed with the mesher headers and never installed. It is still defined in contour_spectrum_libcontour.cpp, which builds whenever LBIE does (CVC_ENABLE_MESHER).
  • do_libisocontour_extract() holds the lock from newDatasetReg through the deletes of the dataset and the contour.

Tests (lbie_mesher_test)

  • ConcurrentLibisocontourMeshesMatchTheSerialOnes: four threads, released together, each mesh a sphere volume of its own size (20-38 samples) with LIBISOCONTOUR. Each mesh's points and triangles must equal that volume's serial mesh, over 4 rounds. With identical volumes on every thread, a comparator reading another thread's tree would get the same ranges, so the race would stay hidden. That is why the sizes differ.
  • LibisocontourMeshesBesideTheLibcontourSpectrumMatchTheSerialOnes: the same, but two of the threads compute compute_contour_spectrum_libcontour and compare every field with the serial spectrum.

The race reproduces. Built with the LBIE lock commented out, both tests failed in every one of 5 repeats (224 failed checks): every concurrent mesh differed from its serial mesh, with the same vertex and triangle counts. The second test's visible failures come from its two mesh threads. The oracle's share of the race is its write to verbose, which only a race detector would flag. With the lock, both pass, in 0.1 s and 0.9 s (Release, Windows).

Local (Windows, Release): lbie_mesher_test 69/69 and contour_spectrum_test 25/25 pass, and git clang-format --diff origin/master is clean.

Follow-up

Removing libContour's process-wide state would let LIBISOCONTOUR meshes run in parallel and make this lock unnecessary. That is not part of this PR.

…spectrum oracle takes

LBIE::Mesher::do_libisocontour_extract() calls the vendored libContour
(newDatasetReg, getContour3d), whose state is process-wide: IntTree::Done()
points global_tree at the tree it sorts and its qsort comparators read the
cell ranges through it, and rangesweep.cpp, range.cpp, Data and `verbose`
keep more. The contour spectrum's libContour oracle serialized on a
file-local libcontour_mutex(); LBIE did not, so two
cvc::iso(..., LIBISOCONTOUR) calls on two threads, or one beside the
oracle, raced. The default DUALLIB and FASTCONTOURING paths do not use
libContour.

libcontour_mutex() moves out of the oracle's anonymous namespace behind an
internal header (geometry/cvc-mesher/libcontour_mutex.h), and the LBIE
extraction holds it from the dataset's creation through its deletes.

Two tests run LIBISOCONTOUR meshes on four threads at once (each on a
sphere volume of its own size) and compare each with its serial mesh, the
second with two of the threads computing the libContour spectrum instead.
With the lock removed both failed in every round of 5 repeats (every
concurrent mesh differed from its serial one); with it they pass in 0.1 s
and 0.9 s (Release).
@transfix
transfix merged commit 44826cc into master Oct 31, 2026
27 checks passed
@transfix
transfix deleted the fix/lbie-libisocontour-mutex branch October 31, 2026 08:44
transfix added a commit that referenced this pull request Oct 31, 2026
The vendored libContour kept mutable state shared by every caller in the
process, so #919 serialized its two callers (the contour spectrum's
libContour oracle and LBIE's LIBISOCONTOUR extraction) on
libcontour_mutex(). A sweep of every writable data symbol in the
library's objects (llvm-nm) found:

- IntTree's global_tree (inttree.cpp), set by Done() for its qsort
  comparators. Each seed now goes to qsort with the bound it is sorted
  by (BoundedSeed). The comparisons are the same, so qsort leaves the
  seeds, tied bounds included, in the order it did before.
- rangesweep.cpp: maxqsize and the MAXQUEUESIZE high-water printout
  (gone), and PropagateRegion's static IndexedQueue, now compSeeds' queue
  passed down (it is empty between calls).
- iqueue.h: hashFun_iq's lazily initialized static constant, now a local
  const.
- range.cpp: the static results of operator-=, operator^, Complement and
  Disjoint, now locals. operator^, which returned a reference to its
  static, returns by value; nothing calls it.
- Data: the static funtopol1/funtopol2, now members (0 for a
  one-variable dataset, as the statics were in libcvc, which builds no
  other kind), and the function-local static min_cutoff in readData and
  preprocessData, which each call reset before use, now locals.
- `verbose` (1 by default; the oracle zeroed it around each computation)
  is a constexpr 0 in contour_verbose.h. setVerboseLevel is gone.
- The errorHandler function pointer and setErrorHandler are gone.
  errorHandler is the old default handler, and its fatal branch throws
  std::runtime_error instead of calling exit(0). The only fatal caller is
  the file-reading Data constructor, which libcvc does not use.
- The lookup tables (cubes, adjfaces, cubeverts, cubeedges, unst2d,
  unst3d, tetadj, rect2d, edgeinfo, triCases, Datareg3::cellFaceVert) are
  const, so they live in read-only sections.

libcontour_mutex.h, its lock in LBIE (LBIE_Mesher.cpp is master's again)
and the oracle's lock and verbose toggle are gone. The rebuilt objects
hold no writable data.

Bit-identical: a dump of compute_contour_spectrum_libcontour (both unit
modes) and cvc::iso(..., LIBISOCONTOUR) meshes, as hex floats, over ten
volumes (spheres, noise and sines; uchar, ushort, float, double and int;
cubic and non-cubic) is byte-for-byte the same before and after (SHA-256
40EAB8CB...). #919's concurrency tests pass with no lock, 20 of 20
repeats.
transfix added a commit that referenced this pull request Oct 31, 2026
The vendored libContour kept mutable state shared by every caller in the
process, so #919 serialized its two callers (the contour spectrum's
libContour oracle and LBIE's LIBISOCONTOUR extraction) on
libcontour_mutex(). A sweep of every writable data symbol in the
library's objects (llvm-nm) found:

- IntTree's global_tree (inttree.cpp), set by Done() for its qsort
  comparators. Each seed now goes to qsort with the bound it is sorted
  by (BoundedSeed). The comparisons are the same, so qsort leaves the
  seeds, tied bounds included, in the order it did before.
- rangesweep.cpp: maxqsize and the MAXQUEUESIZE high-water printout
  (gone), and PropagateRegion's static IndexedQueue, now compSeeds' queue
  passed down (it is empty between calls).
- iqueue.h: hashFun_iq's lazily initialized static constant, now a local
  const.
- range.cpp: the static results of operator-=, operator^, Complement and
  Disjoint, now locals. operator^, which returned a reference to its
  static, returns by value; nothing calls it.
- Data: the static funtopol1/funtopol2, now members (0 for a
  one-variable dataset, as the statics were in libcvc, which builds no
  other kind), and the function-local static min_cutoff in readData and
  preprocessData, which each call reset before use, now locals.
- `verbose` (1 by default; the oracle zeroed it around each computation)
  is a constexpr 0 in contour_verbose.h. setVerboseLevel is gone.
- The errorHandler function pointer and setErrorHandler are gone.
  errorHandler is the old default handler, and its fatal branch throws
  std::runtime_error instead of calling exit(0). The only fatal caller is
  the file-reading Data constructor, which libcvc does not use.
- The lookup tables (cubes, adjfaces, cubeverts, cubeedges, unst2d,
  unst3d, tetadj, rect2d, edgeinfo, triCases, Datareg3::cellFaceVert) are
  const, so they live in read-only sections.

libcontour_mutex.h, its lock in LBIE (LBIE_Mesher.cpp is master's again)
and the oracle's lock and verbose toggle are gone. The rebuilt objects
hold no writable data.

Bit-identical: a dump of compute_contour_spectrum_libcontour (both unit
modes) and cvc::iso(..., LIBISOCONTOUR) meshes, as hex floats, over ten
volumes (spheres, noise and sines; uchar, ushort, float, double and int;
cubic and non-cubic) is byte-for-byte the same before and after (SHA-256
40EAB8CB...). #919's concurrency tests pass with no lock, 20 of 20
repeats.
transfix added a commit that referenced this pull request Nov 1, 2026
The vendored libContour kept mutable state shared by every caller in the
process, so #919 serialized its two callers (the contour spectrum's
libContour oracle and LBIE's LIBISOCONTOUR extraction) on
libcontour_mutex(). A sweep of every writable data symbol in the
library's objects (llvm-nm) found:

- IntTree's global_tree (inttree.cpp), set by Done() for its qsort
  comparators. Each seed now goes to qsort with the bound it is sorted
  by (BoundedSeed). The comparisons are the same, so qsort leaves the
  seeds, tied bounds included, in the order it did before.
- rangesweep.cpp: maxqsize and the MAXQUEUESIZE high-water printout
  (gone), and PropagateRegion's static IndexedQueue, now compSeeds' queue
  passed down (it is empty between calls).
- iqueue.h: hashFun_iq's lazily initialized static constant, now a local
  const.
- range.cpp: the static results of operator-=, operator^, Complement and
  Disjoint, now locals. operator^, which returned a reference to its
  static, returns by value; nothing calls it.
- Data: the static funtopol1/funtopol2, now members (0 for a
  one-variable dataset, as the statics were in libcvc, which builds no
  other kind), and the function-local static min_cutoff in readData and
  preprocessData, which each call reset before use, now locals.
- `verbose` (1 by default; the oracle zeroed it around each computation)
  is a constexpr 0 in contour_verbose.h. setVerboseLevel is gone.
- The errorHandler function pointer and setErrorHandler are gone.
  errorHandler is the old default handler, and its fatal branch throws
  std::runtime_error instead of calling exit(0). The only fatal caller is
  the file-reading Data constructor, which libcvc does not use.
- The lookup tables (cubes, adjfaces, cubeverts, cubeedges, unst2d,
  unst3d, tetadj, rect2d, edgeinfo, triCases, Datareg3::cellFaceVert) are
  const, so they live in read-only sections.

libcontour_mutex.h, its lock in LBIE (LBIE_Mesher.cpp is master's again)
and the oracle's lock and verbose toggle are gone. The rebuilt objects
hold no writable data.

Bit-identical: a dump of compute_contour_spectrum_libcontour (both unit
modes) and cvc::iso(..., LIBISOCONTOUR) meshes, as hex floats, over ten
volumes (spheres, noise and sines; uchar, ushort, float, double and int;
cubic and non-cubic) is byte-for-byte the same before and after (SHA-256
40EAB8CB...). #919's concurrency tests pass with no lock, 20 of 20
repeats.
transfix added a commit that referenced this pull request Nov 6, 2026
The vendored libContour kept mutable state shared by every caller in the
process, so #919 serialized its two callers (the contour spectrum's
libContour oracle and LBIE's LIBISOCONTOUR extraction) on
libcontour_mutex(). A sweep of every writable data symbol in the
library's objects (llvm-nm) found:

- IntTree's global_tree (inttree.cpp), set by Done() for its qsort
  comparators. Each seed now goes to qsort with the bound it is sorted
  by (BoundedSeed). The comparisons are the same, so qsort leaves the
  seeds, tied bounds included, in the order it did before.
- rangesweep.cpp: maxqsize and the MAXQUEUESIZE high-water printout
  (gone), and PropagateRegion's static IndexedQueue, now compSeeds' queue
  passed down (it is empty between calls).
- iqueue.h: hashFun_iq's lazily initialized static constant, now a local
  const.
- range.cpp: the static results of operator-=, operator^, Complement and
  Disjoint, now locals. operator^, which returned a reference to its
  static, returns by value; nothing calls it.
- Data: the static funtopol1/funtopol2, now members (0 for a
  one-variable dataset, as the statics were in libcvc, which builds no
  other kind), and the function-local static min_cutoff in readData and
  preprocessData, which each call reset before use, now locals.
- `verbose` (1 by default; the oracle zeroed it around each computation)
  is a constexpr 0 in contour_verbose.h. setVerboseLevel is gone.
- The errorHandler function pointer and setErrorHandler are gone.
  errorHandler is the old default handler, and its fatal branch throws
  std::runtime_error instead of calling exit(0). The only fatal caller is
  the file-reading Data constructor, which libcvc does not use.
- The lookup tables (cubes, adjfaces, cubeverts, cubeedges, unst2d,
  unst3d, tetadj, rect2d, edgeinfo, triCases, Datareg3::cellFaceVert) are
  const, so they live in read-only sections.

libcontour_mutex.h, its lock in LBIE (LBIE_Mesher.cpp is master's again)
and the oracle's lock and verbose toggle are gone. The rebuilt objects
hold no writable data.

Bit-identical: a dump of compute_contour_spectrum_libcontour (both unit
modes) and cvc::iso(..., LIBISOCONTOUR) meshes, as hex floats, over ten
volumes (spheres, noise and sines; uchar, ushort, float, double and int;
cubic and non-cubic) is byte-for-byte the same before and after (SHA-256
40EAB8CB...). #919's concurrency tests pass with no lock, 20 of 20
repeats.
transfix added a commit that referenced this pull request Nov 6, 2026
* libContour: no process-wide state; drop libcontour_mutex

The vendored libContour kept mutable state shared by every caller in the
process, so #919 serialized its two callers (the contour spectrum's
libContour oracle and LBIE's LIBISOCONTOUR extraction) on
libcontour_mutex(). A sweep of every writable data symbol in the
library's objects (llvm-nm) found:

- IntTree's global_tree (inttree.cpp), set by Done() for its qsort
  comparators. Each seed now goes to qsort with the bound it is sorted
  by (BoundedSeed). The comparisons are the same, so qsort leaves the
  seeds, tied bounds included, in the order it did before.
- rangesweep.cpp: maxqsize and the MAXQUEUESIZE high-water printout
  (gone), and PropagateRegion's static IndexedQueue, now compSeeds' queue
  passed down (it is empty between calls).
- iqueue.h: hashFun_iq's lazily initialized static constant, now a local
  const.
- range.cpp: the static results of operator-=, operator^, Complement and
  Disjoint, now locals. operator^, which returned a reference to its
  static, returns by value; nothing calls it.
- Data: the static funtopol1/funtopol2, now members (0 for a
  one-variable dataset, as the statics were in libcvc, which builds no
  other kind), and the function-local static min_cutoff in readData and
  preprocessData, which each call reset before use, now locals.
- `verbose` (1 by default; the oracle zeroed it around each computation)
  is a constexpr 0 in contour_verbose.h. setVerboseLevel is gone.
- The errorHandler function pointer and setErrorHandler are gone.
  errorHandler is the old default handler, and its fatal branch throws
  std::runtime_error instead of calling exit(0). The only fatal caller is
  the file-reading Data constructor, which libcvc does not use.
- The lookup tables (cubes, adjfaces, cubeverts, cubeedges, unst2d,
  unst3d, tetadj, rect2d, edgeinfo, triCases, Datareg3::cellFaceVert) are
  const, so they live in read-only sections.

libcontour_mutex.h, its lock in LBIE (LBIE_Mesher.cpp is master's again)
and the oracle's lock and verbose toggle are gone. The rebuilt objects
hold no writable data.

Bit-identical: a dump of compute_contour_spectrum_libcontour (both unit
modes) and cvc::iso(..., LIBISOCONTOUR) meshes, as hex floats, over ten
volumes (spheres, noise and sines; uchar, ushort, float, double and int;
cubic and non-cubic) is byte-for-byte the same before and after (SHA-256
40EAB8CB...). #919's concurrency tests pass with no lock, 20 of 20
repeats.

* libContour: iqueue.h includes <unistd.h> for sleep

rangesweep.h now includes iqueue.h before data.h, which was where
rangesweep.cpp got <unistd.h> from. Clang looks up the non-dependent
sleep() at the template's definition, so the macOS build failed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant