Skip to content

genseg: the clean-up, guarded by the parity tests (P5 part 2) - #1250

Merged
transfix merged 5 commits into
masterfrom
p5/genseg-cleanup
Nov 15, 2026
Merged

transfix merged 5 commits into
masterfrom
p5/genseg-cleanup

Conversation

@transfix

Copy link
Copy Markdown
Owner

P5 part 2 of docs/roadmap/VOLROVER-LIBRARY-PORTS-ROADMAP.md (§5 step 2): the clean-up of the vendored GenSeg, guarded by part 1's parity tests.

Stacked on #1248 (part 1); review the last commit (43eff09). It touches only src/cvc/volume/segmentation/genseg.{h,cpp}.

What changes

  • The hand-unrolled updates become loops. Legacy GetTime() and T_GetTime() are each six copies of one block, one per face neighbour. A script compared the six blocks after substituting the neighbour: they are identical, in the order x-1, x+1, y-1, y+1, z-1, z+1, and the inner tests visit the neighbours of a neighbour in the same order. They are now loops over the six neighbours in that order, clamped as before (at an edge the voxel stands in for its missing neighbour).
  • Each voxel's step cost is computed once. The cost is 0.01 exp(ALPHA |∇I|); the legacy code computed it again for every test, up to 36 times per voxel popped. Same floats, since exp() is still taken in double.
  • The heap: kept, with linear indices. The roadmap asked to pick the heap, and the pick is the legacy binary heap with its sift rules. Ties are everywhere (every seed starts at time 0, every border voxel waits at MAX_TIME + 1), the order in which ties come out decides the labels where fronts meet, and any other heap breaks parity. It now holds a voxel's linear index (std::size_t) instead of three coordinates.
  • The threshold sweep's bookkeeping. The unclassified automatic seeds are a vector, filtered each search in their order. Each search clears only the times the previous search set. The legacy code kept a linked list and, before each search, flood-filled out from every seed in it (InitialTime()). That cleared the same voxels, since every voxel a search sets is reached from one of its seeds through voxels it set. noisy_many_seeds, with 302 classifications, checks this.
  • The sweep's arrays (its heap and times) are allocated only when there are automatic seeds to classify.

The three undefined-behaviour fixes and the other changes from part 1 stay, listed at the top of genseg.cpp. genseg.cpp goes from 1,887 lines to 596.

Measured

  • Parity: labels and border lists identical to the legacy library on all six goldens, natively (MSVC) and under node (wasm-mt). All 18 segmentation_test tests pass on both.
  • Speed:
    • natively, RunsWhereTheLegacyLibraryCrashed takes 1.75 s (2.7 s in part 1) and the parity test 1.7 s (2.3 s);
    • under node the parity test takes 1.35 s (3.5 s);
    • T7 at budget 4096: slowest step 0.63 ms natively, 0.41 ms under node; stepping costs 1-4% over budget 0.
  • Memory: about 41 bytes a voxel while marching (the legacy code took 57), plus 8 more during the smoothing.

No CI job is added or renamed. Merges on Joe's OK, after part 1.

…rary voxel for voxel (P5 part 1)

VolumeRover's GenSeg seeded segmentation (docs/roadmap/VOLROVER-LIBRARY-PORTS-ROADMAP.md §5 step 1)
in src/cvc/volume/segmentation/: segment.cpp's globals become genseg_context's members, its
functions keep their bodies (the hand-unrolled neighbour updates included), and Segment(),
GetGradient(), GetSeeds() and SearchIndex() are coroutines that yield a unit at a time.
In-memory input and output: no files, no current directory.

- inc/cvc/volume/segmentation.h: segment_seeded(volume, point_classes, params), segment_stepper
  (checking, normalising, seeding, classifying, marching, writing), segment_seeded_job
  (cvc::result_job), seeded_segmentation (labels, per-class volumes, border, counts, warnings).
- inc/cvc/volume/segmentation_params.h: the param_table (thresholds in data units or the legacy
  0..255 scale, gamma, automatic seeding, variable and timestep, class volumes).
- Three fixes of the legacy code's undefined behaviour: an empty heap pop (the legacy executable
  crashes on such an input), a heap overflow, and a threshold sweep that ran on below 0.
- tools/volrover-goldens: three more GenSeg cases that run the threshold sweep (the first three
  never did), with their goldens from the legacy build.
- segmentation_test: T1 on all six goldens, T2-T8; in CVC_PORT_TESTS (the wasm-mt node lane)
  and SAN_CXX_TESTS.
docs/roadmap/VOLROVER-LIBRARY-PORTS-ROADMAP.md §5 step 2, on top of part 1. The labels and border
lists stay identical to the legacy library's on all six goldens, natively and under node.

- GetTime() and T_GetTime(), each six hand-unrolled copies of one block (checked by script: the
  same block for x-1, x+1, y-1, y+1, z-1, z+1), are loops over the six face neighbours in the
  legacy order.
- A voxel's step cost 0.01 exp(ALPHA |grad|) is computed once, not up to 36 times a pop.
- The heaps hold linear indices and keep the legacy sift rules (ties decide labels where fronts
  meet, so the heap stays the legacy one).
- The threshold sweep keeps its unclassified seeds in a vector and clears only what the last
  search set, instead of the linked list and InitialTime()'s flood fill from every seed.
- genseg.cpp: 1,887 lines to 596. The heavy parity cases run 1.5x faster natively and 2.6x
  under node; about 16 bytes less per voxel.
…on says

genseg.cpp and segmentation.cpp get -ffp-contract=off (/fp:precise on MSVC) like the other
deterministic sources: the marches compare float arrival times, and a contracted step can flip
which front wins a tie on targets with FMA (aarch64 macOS).
@transfix
transfix enabled auto-merge November 15, 2026 04:54
@transfix
transfix merged commit f36e986 into master Nov 15, 2026
28 checks passed
@transfix
transfix deleted the p5/genseg-cleanup branch November 15, 2026 12:08
transfix added a commit that referenced this pull request Nov 15, 2026
- ci.yml: the sanitizer list keeps this branch's tests (stream_test, net_ws_client_test,
  ariadne_host_test) and master's segmentation_test.
transfix added a commit that referenced this pull request Nov 15, 2026
Master now has P5 parts 1 and 2 (#1248 and #1250, the GenSeg core and its clean-up), the
4.0.0 bump (#1221), live textures (#1236) and Ewave's timeout (#1261). No conflicts: this
branch's CLI, pycvc and DSL sit on the cleaned-up genseg.cpp unchanged.
transfix added a commit that referenced this pull request Nov 15, 2026
No conflicts. Master brings 26 commits (#1171, #1191, #1221, #1236,
#1245, #1248, #1250 and others); none adds a writable global, a function
static or a signal handler to the hosts or examples, and none touches
the allowlist.
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