Skip to content

contour keeps each corner's point name alone (#318, D4) - #466

Open
berkgeveci wants to merge 9 commits into
Kitware:mainfrom
berkgeveci:vis/contour-edge-keys
Open

berkgeveci wants to merge 9 commits into
Kitware:mainfrom
berkgeveci:vis/contour-edge-keys

Conversation

@berkgeveci

Copy link
Copy Markdown
Collaborator

Part of #318: step 5 (D4) of the cut filters' memory plan (docs/design/plans/cut-filters-memory.md). Stacked on #401 (flying edges, which also changes contour.py) and #462 (D1, for the memory test's fixture). Review the last commit, 27b72f50; this merges after both.

What changes

Where contour names points by their edge (a continuous field on an H1 geometry, numbered by point), each triangle corner kept everything about its point: position, ends, weight, cell, level and name. That is 40 bytes per corner, carried through the dropping of triangles with no area and through the merge. Now:

  • Names only. A corner keeps its point's name alone, 8 bytes (_contour_names), and a triangle keeps its cell and level once.
  • Points computed once. Each point's position, ends and weight are computed once from its name, after the merge (_named_points), as every corner had computed them: the edge's ends placed first by position, then _crossing, or the corner the crossing lands on.
  • Cell-named points unchanged. Points named within cells (L2 fields and the like) keep their records, since a name does not give a position there.

The same output

51 contours hash alike before and after (topology, positions, every field), on the CPU and on Metal. They cover:

  • every shape, structured and unstructured, 2D and 3D;
  • mixed shapes, and polyhedra beside shapes (_mixed_contour);
  • hidden cells;
  • one and three values, a value at mesh points, unmerged points;
  • an L2 field (the unchanged path).

Measured

5M tetrahedra, counted by #462:

before after VTK
one value, peak 32 MiB 23 MiB 28 MiB
ten values, peak 248 MiB 142 MiB

Metal gives the same peaks. Times are unchanged: 10 and 45 ms on the CPU; on Metal 11 ms and 30 ms with this change.

What is left at ten values is mostly the merge's sort (110 MiB): its buckets are the points times the values (8.6 million here), and it was there before.

Tests

  • New: test_contour_holds_little_more_than_its_output, on every backend. Contour's peak is at most 5 times its output: 10.2 before (the test fails there), 3.5 now.
  • Every test module that contours or slices (46 files, 2897 passed); the quick tier on the CPU (except test_deprecations' registry test, which fails in any worktree).
  • On Metal, one process: test_dataset_filters.py, test_structured_contour.py, test_masked_filters.py and test_mixed_cells.py, 440 passed with the CPU's.

On #401

Merging main into #401 (82fc288, pushed to its branch) needed a hand: main moved contour's shape path into _triangle_parts after #401 branched. Git placed #401's flying-edges check inside that function, where its names (top, several, fields) are not defined. The textual merge was clean and the result would have raised. The check is back in contour(), and #401's own tests and every contour and slice module pass on the merge.

🤖 Generated with Claude Code

A structured grid of solid cells -- an image, a rectilinear or a
curvilinear grid -- with a point field and positions on the points is
contoured by flying edges, as VTK's vtkFlyingEdges3D: each point's
edges along +x, +y and +z classified once per value (a byte a point), the
points each row emits and the triangles each row of cells makes counted
and scanned, then written once, the triangles' pass walking each row of
cells with a running count for the four rows of points around it. No
per-corner arrays, no per-cell arrays, no sort.

Crossings are read through the same views, placed from the end first by
position and named by the end they land on (_end_hit), as the general
path places them, so the triangles, points and carried values are its
own, bit for bit, up to their order. On an 11M-cell image at ten values
(CPU, M1 Max) the peak above the input falls from 2.24 to 0.32 GB and the
time from 259 to 219 ms; on Metal, 201 to 136 ms.
slice by a Plane through an image or a rectilinear grid of solid cells
goes by flying edges over each row's stretch near the plane, as VTK's
vtkFlyingEdgesPlaneCutter. Each (row, value) has a window of points,
found from the plane's equation -- along a row the distance is affine in
i on an image and monotone in the axis on a rectilinear grid -- widened by
the largest change of distance along an edge and by rounding, so every
crossed edge is inside; only the windows' flags are kept. The distance is
computed where it is read, Plane.value of the point's position as
implicit_values stores it, so the output is the general path's, bit for
bit, up to its order.

The flying-edges passes take the windows for contour too, whose windows
are whole rows. A plane through a 432^3 image (80M cells, CPU, M1 Max)
took 177 ms and 0.71 GB above the input; it takes 14 ms and 0.03 GB.
Hidden and refined cells (Ghosts.MASKED_CELLS, an AMR box's) make no
triangles in the flying-edges contour and the plane cutter, and crossings
only they use are dropped afterwards, as for cells with a NaN corner. The
output is the general path's, which first extracts the other cells into
an unstructured mesh, bit for bit, up to order, ghost marks included;
contour no longer extracts them for a grid flying edges takes.

A plane through a curvilinear grid takes the cutter too: every row, the
distance computed where read rather than stored for every point.

At 256^3 (CPU, M1 Max): a plane through an image with a tenth of its
cells refined, 116 ms and 1.49 GB to 12 ms and 0.01 GB; through a
curvilinear grid, 47 ms to 7 ms.
A 2D structured grid (a sheet) takes the flying-edges points' passes, one
layer of cells along z, through an (i, j) reader of its cells' views
(_Sheet; _Solid for a solid grid), and two line passes, counting and
writing, that walk each row of cells with the first number of its two rows
of points. Each cell's lines are its crossings going round its sides
through its view, paired as contour._level_lines pairs them -- each rising
crossing with the next falling one, the values above on the line's right
-- and a line whose two crossings land on one point is dropped; a cell none
of whose sides is crossed is skipped from the points' codes. Hidden,
refined and NaN cells are left out as in 3D.

The lines, points and values are the general path's, bit for bit, up to
order, orientation included. On a 4096^2 image (CPU, M1 Max): at ten
values 0.18 GB above the input against 14.1, 252 ms against 251; at one
value 0.02 GB against 2.01, 33 ms against 53.
runtime/memory_stats (private) counts the bytes of every array Tack makes,
per device, with a peak: live, peak, reset_peak. The count is taken where
every backend's allocate_array is called, _allocate and _empty_for_mpi,
and given back by a weak reference's callback when the buffer is
collected (0.9 us an array; weakref.finalize was 1.4). A view is counted
once, with its buffer; memory from outside (wrap_ptr, DLPack,
array_from_ptr) not at all; a block Metal's pool or HIP's cache reuses
while an array holds it. The CPU, Metal, CUDA, HIP, Level Zero and Warp
are counted alike, where the allocator's statistics see the CPU only and
the drivers see the pools.

The root conftest's peak_memory fixture measures one call's peak with it,
for the plan's tests holding each cut filter's peak. The VTK comparison
gains a clip pair (vtkTableBasedClipDataSet against td.clip: the same
cells and points), and memory.py reports Tack's counted peak beside the
allocator's.
…arts on main: the flying-edges check stays in contour)
Where contour names points by their edge (a continuous field on an H1
geometry, numbered by point), each triangle corner kept its point's
position, ends, weight, cell, level and name: 40 bytes a corner, through
the compaction of triangles of no area and the merge. Now a corner keeps
its name alone (8 bytes) and a triangle its cell and level once
(_contour_names); each point's position, ends and weight are computed
once from its name after the merge (_named_points), as every corner had
computed them: the edge's ends placed first by position, then _crossing,
or the corner it lands on. Points named within cells (L2 fields and the
like) keep their records, since a name does not give a position there.

The output is the same bits: 51 contours (every shape, structured and
unstructured, mixed shapes, polyhedra beside shapes, hidden cells, one and
three values, values at mesh points, unmerged points, an L2 field) hash
alike before and after, on the CPU and on Metal. On 5M tetrahedra the peak
fell from 32 to 23 MiB at one value (VTK: 28) and from 248 to 142 MiB at
ten, on the CPU and on Metal; what is left at ten values is mostly the
merge's sort, whose buckets are the points times the values.

test_dataset_filters: contour's peak at most 5 times its output, on every
backend (10.2 before, 3.5 now).
starts, triangles = _scanned(counts, n_cells, odt)
# Points named by their edge (a continuous field numbered by point, on an H1
# geometry): each corner keeps its name alone.
if not per_cell and (not isinstance(space, H1) or space.dofs is None):

@berkgeveci berkgeveci Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] This branch also admits a continuous evaluated Space that is not H1, but _named_points_of later reads field.values by global point ID. Evaluated spaces can hold only per-cell placeholders. For example, test_evaluated_fields.Analytic with default_subdivision = 1 has 36 stored values for 80 mesh points. On the parent commit, contouring field f at 0.6 produced 35 cells with 27 finite points; at this head it produces the same counts with nonfinite positions from out-of-bounds reads. Please restrict this path to fields with point-indexed stored values, or reconstruct values through the field view.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 908e2b2. The names path now takes only a field in H1 without a dof table, on an H1 geometry without one: the spaces whose values hold the points' values first, which is what _named_points_of reads. Evaluated spaces, and anything else continuous, keep the records, as on the parent commit.

Checked:

  • New test: test_contour_without_subdividing_reads_it_through_its_view (test_evaluated_fields.py), your case: Analytic with default_subdivision = 1, contoured at 0.6, must give finite positions and the order-1 field's cells, points and positions. It fails before the fix and passes after.
  • CI's failures: test_expr_contour.py and the guide example visualization-expressions-many-and-unstored both pass; docs/examples/validate.py --arch cpu passes all 173 examples.
  • Every test module that contours or slices (49 files): 2965 passed, 46 skipped.

The names path read the field's and the geometry's values by point id for
any continuous field on an H1 geometry. An evaluated space's values are
placeholders, so it read out of bounds and gave nonfinite positions
(tack-expr's contour test, the expressions guide). Only H1 without a dof
table stores the points' values first; everything else keeps the records.

This branch has not been deployed

No deployments
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