Repository navigation
LBIE: LIBISOCONTOUR extraction takes the libContour lock the contour spectrum oracle takes - #919
Merged
Merged
Conversation
…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
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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()pointsglobal_treeat 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) andverbosehold more. So twocvc::iso(vol, iso, LIBISOCONTOUR)calls on two threads race, and so does one besidecompute_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 incontour_spectrum_libcontour.cpp, which builds whenever LBIE does (CVC_ENABLE_MESHER).do_libisocontour_extract()holds the lock fromnewDatasetRegthrough thedeletes 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 computecompute_contour_spectrum_libcontourand 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_test69/69 andcontour_spectrum_test25/25 pass, andgit clang-format --diff origin/masteris 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.