Repository navigation
clip numbers its kept points through a point map (#318, D2) - #463
berkgeveci wants to merge 3 commits into
Conversation
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 |
There was a problem hiding this comment.
[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.
There was a problem hiding this comment.
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.
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
vtkTableBasedClipDataSetdoes:(low, high)keys numbered them.pcomes after the used points before it and the crossings whose low end is below it, found by binary search over the sorted crossings.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:
Measured
5M cells, the M1 Max CPU (
memory.py, counted by #462; warm times best of 7):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
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'speak_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.test_deprecations' registry test, which fails in any worktree).test_clip.py,test_clip_high_order.py,test_clip_polyhedra.py,test_masked_filters.pyandtest_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