Skip to content

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

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

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

Conversation

@berkgeveci

Copy link
Copy Markdown
Collaborator

Part of #318: step 2 (D2) of the cut filters' memory plan (docs/design/plans/cut-filters-memory.md). Stacked on #462 (D1, the allocation count), whose commit is the first here; review the commit after it, 6019495c. This merges after #462.

What changes

Clip made a record for every corner every kept cell uses and sorted them all, about one per connectivity entry. That merge was most of clip's memory. Now, as VTK's vtkTableBasedClipDataSet does:

  • Kept points by a map: every input point a cell keeps is written as its id and marked used in a map of one entry per input point. That covers the corners of a cell kept whole, the input points a cut cell's pieces use, and a centroid's members that are input points.
  • Records for crossings only: records are made only for crossings on cut edges, and only those are sorted.
  • Numbering, as the plan says: the output's points are numbered as the old sort of (low, high) keys numbered them.
    • A used point p comes after the used points before it and the crossings whose low end is below it, found by binary search over the sorted crossings.
    • A crossing comes after the used points up to its low end.
  • One renumbering pass: it replaces input ids with their numbers in the connectivity and the centroids' members. Crossings' slots and centroids' placeholders hold negative markers the pass leaves alone.

The same output

117 clips hash alike, every part of the output (types, offsets, connectivity, positions, every field), before and after, on the CPU and on Metal. They cover:

  • tetrahedra, hexahedra, wedges, pyramids, voxels, triangles, quads, pixels, images 2D and 3D, a tetrahedralized grid and a mixed mesh;
  • point fields and implicit spheres, both sides;
  • values at mesh points, all kept and all dropped;
  • hidden cells;
  • 911 centroid points.

Measured

5M cells, the M1 Max CPU (memory.py, counted by #462; warm times best of 7):

before after VTK
tetrahedra, peak 364 MiB 256 MiB 111 MiB
hexahedra, peak 574 MiB 315 MiB 210 MiB
tetrahedra, warm clip 43 ms 30 ms
hexahedra, warm clip 61 ms 54 ms

What is left of the peak is the count and scan of every cell's five outputs, about 210 MiB here. That is D3, batches of cells.

Tests

  • New: test_clip_holds_little_more_than_its_output, on every backend. Where clip keeps most of the mesh, its peak (by A count of Tack's own allocations per device (#318, D1) #462's peak_memory) is at most 3 times its output. It was 4.4 to 5 before (the test fails there) and is 1.4 to 2.6 now.
  • Every test module that clips (25 files, 2036 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_high_order.py, test_clip_polyhedra.py, test_masked_filters.py and test_degenerate_inputs.py, 254 passed with the CPU's.

Polyhedra (_clip_polyhedra.py's _face_keys) are the plan's next step, a pull request of their own.

🤖 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).
slots[record] = conn
record += 1
connectivity[conn] = p
used[p] = 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] Neighboring cells share original points, so parallel iterations of this kernel can write used[p] at the same time. Ordinary concurrent stores are a data race even when both store 1. The same pattern occurs below for centroid members and partial shapes. Please use tack.atomic_max(used, p, 1) for each mark, as _extract._kept_cells does. This issue is also retained in #465.

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 1905b57, and carried into #465 (429796e). Each mark is tack.atomic_max(used, p, 1) (the whole cell's corners, the pieces' input points, the centroids' members), as _extract._kept_cells marks. The map is i32, since Metal's and Level Zero's atomics take i32, u32 and f32 only. _scanned_map scans it into the output's id type: in place where that is i32, into a new array where it is i64.

Checked:

  • Same output: the 117 clips hash as before, on the CPU and on Metal.
  • Tests: the modules that clip pass (2036); on Metal, the clip, polyhedra, mixed-cell and masked-filter tests (388 with the CPU's).
  • Cost: a warm clip of 5M hexahedra takes 59 ms where it took 54 (eight shared points a cell); tetrahedra 31 ms as before. With clip counts and writes its cells in batches (#318, D3) #465's batches, hexahedra take 42 ms and tetrahedra 18.

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.

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