Skip to content

Give the remaining partial sort comparators a total order - #953

Open
danielepanozzo wants to merge 1 commit into
danielepanozzo/concurrency-cleanupfrom
danielepanozzo/sort-total-order
Open

Give the remaining partial sort comparators a total order#953
danielepanozzo wants to merge 1 commit into
danielepanozzo/concurrency-cleanupfrom
danielepanozzo/sort-total-order

Conversation

@danielepanozzo

Copy link
Copy Markdown
Contributor

Give the remaining partial sort comparators a total order

A sweep of every std::sort / std::unique / priority-queue comparator in src/wmtk and the components, looking for the failure this repository keeps hitting.

The bug class. std::sort is not stable. When a comparator treats two distinct elements as equivalent, the order it leaves them in is unspecified — and differs between libc++ (macOS), libstdc++ (Linux) and MSVC. In each case below that order reaches the output, so the same input produces a different mesh depending on the platform. This has already been hit four times before: TetMesh::get_edges, igl::sortrows in the vertex dedup, sort_edges_by_length, and the morton partition sort in #952.

Six sites

Site What the comparator ignored How the order reaches the output
TetMesh::get_edges the tet id — every tet incident to an edge contributes an entry, so an edge shared by k tets appears k times std::unique kept an arbitrary one, so which tet the returned tuple lives in was unspecified; get_edges feeds the operation queues
TetMeshTriangleInsertionConn old_face_vids (tid, l_fid) of {v0,v1,v2,tid,l_fid} the survivor is consumed as tuple_from_face(info[3], info[4]) — the discarded fields are the output
TetMeshTriangleInsertionConn new_face_vids same survivor and residual order become the face tuples handed to triangle_insertion_after
orig/EdgeSplitter.h cmp_es v_ids, keeping only the edge length split priority queue: equal-length edges popped in an unspecified order, which is the order they are split in
TopoOffsetTetMesh.h sort_edges_by_length everything but the length marching-tets split order → new vertex ids and the frontier list that labels offset tets
TopoOffsetTriMesh.h sort_edges_by_length same (2D path) same

Two things the sweep surfaced

Four of the six share one idiom: sort on a partial key, std::unique to collapse duplicates, then read the very fields the comparator ignored. It reads as correct — the grouping works and the deduplication works; only the choice of which duplicate survives is left to the standard library. Where the fix widens the sort comparator to the whole array, the unique predicate still compares the vids alone, so the grouping it relies on is unchanged and the wider comparison only orders within each group.

cmp_es had two siblings that were already right. cmp_ec (EdgeCollapser.h) and cmp_er (EdgeRemover.h) both tie-break on v_ids; only the splitter was missed. That is an oversight with a clear fingerprint rather than a deliberate choice.

Scope

~150 std::sort call sites were examined; six were changed. Sorts whose comparator is the default one over a fully ordered type (size_t, std::array<size_t,N>, std::pair) are deliberately left alone: equivalent elements there are indistinguishable, so no order over them is observable, and tie-breaking them would be noise. Two further sites were examined and left: unique_face_tuples already uses std::stable_sort (so its survivor is deterministic without a key), and unique_directed_edge_tuples is dead code — both lambdas throw on entry.

A note for whoever merges this with #949

The tie-breaks here are unconditional. On danielepanozzo/integration-test-hashes (#949), the equivalents for TetMesh::get_edges and sort_edges_by_length sit behind #ifdef WMTK_FP_STRICT — that macro does not exist on this stack at all. Those hunks will conflict textually, and the merge forces a policy decision: is a total-order comparator a correctness property (always on) or a reproducibility one (strict builds only)? The cost is one integer comparison on a tie; the symptom is a mesh that differs by platform.

Testing

51/51 unit tests and Integration_Tests pass.

Worth being explicit about what that does not show: these fixes are reasoned, not measured. The golden-hash tests are what would prove them, and they live on #949 — a different stack — while most of these paths are multithreaded-sensitive anyway. Integration_Tests only checks termination, so green means nothing regressed, not that determinism improved.

Stacked on #952.

🤖 Generated with Claude Code

A sweep of every std::sort / std::unique / priority-queue comparator in src/wmtk
and the components, looking for the failure this repository keeps hitting: a
comparator that treats two *distinct* elements as equivalent. std::sort is not
stable, so the order it leaves such elements in is unspecified and differs between
libc++, libstdc++ and MSVC -- and in each case below that order reaches the output.

Six sites. Four share one idiom: sort on a partial key, std::unique to collapse
duplicates, and then read the very fields the comparator ignored. That reads as
correct, because the grouping and the deduplication both work; only the choice of
which duplicate survives is left to the standard library.

  TetMesh::get_edges -- entries are (v0, v1, tuple) and every tet incident to an
  edge contributes one, so an edge shared by k tets appears k times. The unique
  below kept an arbitrary one, so *which tet the returned tuple lives in* was
  unspecified, and get_edges feeds the operation queues. Tie-break on the tuple,
  whose operator< already orders totally.

  TetMeshTriangleInsertionConn, old_face_vids and new_face_vids -- arrays of
  {v0,v1,v2,tid,l_fid} ordered on the vids alone. The survivor is consumed as
  tuple_from_face(info[3], info[4]), so the discarded fields are the output.
  Comparing the whole array keeps the vid grouping the unique relies on -- the
  first three elements still dominate -- and adds (tid, l_fid) as the tie-break.

  orig/EdgeSplitter.h, cmp_es -- the split priority queue ordered on edge length
  and ignored v_ids, so equal-length edges, which symmetric input produces in
  quantity, were popped in an unspecified order, and that is the order they are
  split in. Its siblings cmp_ec and cmp_er already tie-break on v_ids; this one
  was missed.

  TopoOffsetTetMesh.h and TopoOffsetTriMesh.h, sort_edges_by_length -- ordered on
  length alone, and the order is the marching-tets split order, so it decides new
  vertex ids and the frontier vertex list that labels offset tets.

Sorts whose comparator is the default one over a fully ordered type are left alone:
equivalent elements there are indistinguishable, so no order over them is
observable. That covers the great majority of the ~150 call sites.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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