Skip to content

clip of polyhedra numbers its kept points through a point map (#318, D2) - #464

Open
berkgeveci wants to merge 6 commits into
Kitware:mainfrom
berkgeveci:vis/clip-polyhedra-point-map
Open

berkgeveci wants to merge 6 commits into
Kitware:mainfrom
berkgeveci:vis/clip-polyhedra-point-map

Conversation

@berkgeveci

@berkgeveci berkgeveci commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

Part of #318: D2 for polyhedra, step 3 of the cut filters' memory plan (docs/design/plans/cut-filters-memory.md). Stacked on #463 (D2 for shapes), which is stacked on #462 (D1); review the commit after #463's, 7b74f665. This merges after #463.

What changes

The polyhedral clip (_clip_polyhedra.py) named every point its clipped faces use, (a, a) for a kept point and (low, high) for a crossing, and sorted them all along with the caps' iso-vertices. Now, as #463 does for shapes:

  • Kept points by a map: a kept face point is written as its id and marked used in a map of one entry per input point. So is a cap vertex at a point whose value is the level, which López names (a, a).
  • Records for crossings only: only crossings get records, and only those are sorted.
  • Numbering: points are numbered as before, by clip numbers its kept points through a point map (#318, D2) #463's kernels (_number_points, _renumber).
  • Polyhedra beside shapes (clip_mixed, through clip_parts) take the same path.

The same output

32 clips hash alike before and after, on the CPU and on Metal:

  • polyhedral hexahedra and tetrahedra (as_polyhedra) and a mixed mesh of hexahedra and polyhedra, one with hidden cells;
  • both sides, implicit spheres, all kept and all dropped;
  • a value at a mesh point, which made 16 cap vertices at points.

Measured

Where the clip keeps most of the mesh (27,000 polyhedral hexahedra, 135,000 polyhedral tetrahedra), its peak by #462's count:

before after
peak / output 3.8 to 4.5 2.5 to 3.4

Tests

  • New: test_a_polyhedral_clip_holds_little_more_than_its_output, at most 3.6 times the output (fails before at 3.76 and 4.04). It runs on the backends with f64, as the module does.
  • Every test module that clips (25 files, 2038 passed); the quick tier on the CPU (except test_deprecations' registry test, which fails in any worktree).
  • On Metal, one process: test_clip.py, test_clip_polyhedra.py, test_mixed_cells.py and test_masked_filters.py, 390 passed with the CPU's.

D3, clip counting in batches, is next.

🤖 Generated with Claude Code

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.
Every input point a cell keeps (the corners of a cell kept whole, the input
points a cut cell's pieces use, a centroid's members that are input points)
is written as its id and marked used in a map of one entry per input
point; records are made only for crossings on cut edges, and only those
are sorted. Points are numbered as the sort of every point's (low, high)
key numbered them: a used point p after the used points before it and the
crossings whose low end is below it (a binary search of the sorted
crossings), a crossing after the used points up to its low end. One pass
renumbers the connectivity and the centroids' members; crossings' slots
and centroids' placeholders hold negative markers it leaves alone.

The output is the same bits: 117 clips (every shape, 2D and 3D, both sides,
values at mesh points, implicit functions, hidden cells, 911 centroid
points) hash alike before and after, on the CPU and on Metal. On 5M cells
(CPU) the counted peak fell from 364 to 256 MiB on tetrahedra and from 574
to 315 MiB on hexahedra (the count and scan of every cell's outputs are the
peak now, D3), and a warm clip from 43 to 30 ms and from 61 to 54 ms.

test_clip: clip's peak at most 3 times its output where it keeps most of
the mesh, on every backend (4.4 to 5 before, 1.4 to 2.6 now).
…e#318, D2)

The polyhedral clip named every point its clipped faces use -- (a, a) for a
kept point, (low, high) for a crossing -- and sorted them all, with the
caps' iso-vertices. Now a kept face point, and a cap vertex at a point
whose value is the level (López's (a, a)), is written as its id and marked
used in a map of one entry per input point; only crossings get records,
and only those are sorted. Points are numbered as before, by the shapes'
clip's kernels (_number_points, _renumber), and polyhedra beside shapes
(clip_mixed, through clip_parts) take the same path.

The output is the same bits: 32 clips of polyhedral hexahedra and
tetrahedra and of a mixed mesh, hidden cells among them, both sides, with
16 cap vertices at mesh points, hash alike before and after, on the CPU and
on Metal. Where the clip keeps most of the mesh, its peak fell from 3.8 to
4.5 times its output to 2.5 to 3.4; test_clip_polyhedra holds it to 3.6.
if va >= iso:
keys[at] = [a, a]
point_of[at] = a
used[a] = 1

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] Different faces can contain the same point, so parallel _face_keys iterations can write used[a] concurrently. _cap_names has the same issue for repeated cap vertices at line 405. Please mark used atomically at both sites, for example with tack.atomic_max; _extract._kept_cells already uses that for shared points.

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 3eacd6c, after merging #463's fix. _face_keys and _cap_names mark with tack.atomic_max(used, p, 1) into the i32 map, scanned by clip's _scanned_map.

Checked: the 32 polyhedral and mixed clips hash as before on the CPU and on Metal (#465 too), and the modules that clip pass (2038).

Neighbouring cells share points, so two iterations could store 1 to one
entry of the map at once: a data race under the language contract, however
equal the stores. Each mark is tack.atomic_max(used, p, 1), as
_extract._kept_cells marks, into an i32 map (every backend's atomics take
i32), scanned in place where the output's ids are i32 and into a new array
where they are i64 (_scanned_map). The 117 clips hash as before; a warm
clip of 5M hexahedra takes 59 ms where it took 54 (61 before the map),
tetrahedra 31 as before.
Faces sharing a point, and repeated cap vertices at a point, marked its
entry of the map from different iterations: each mark is
tack.atomic_max(used, p, 1) into an i32 map, scanned by clip's
_scanned_map. The 32 polyhedral and mixed clips hash as before.

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