From 9a3e79587a54f6e4b87fe9fa2084e380268b0347 Mon Sep 17 00:00:00 2001 From: Berk Geveci Date: Sun, 15 Nov 2026 15:38:39 -0500 Subject: [PATCH 1/5] A count of Tack's own allocations per device (#318, D1) 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. --- CLAUDE.md | 2 +- benchmarks/vtk_compare/memory.py | 21 ++- benchmarks/vtk_compare/pairs.py | 14 ++ conftest.py | 21 +++ packages/tack-core/src/tack/lang/array.py | 13 +- .../src/tack/runtime/memory_stats.py | 105 ++++++++++++ packages/tack-core/tests/test_memory_stats.py | 150 ++++++++++++++++++ 7 files changed, 316 insertions(+), 10 deletions(-) create mode 100644 packages/tack-core/src/tack/runtime/memory_stats.py create mode 100644 packages/tack-core/tests/test_memory_stats.py diff --git a/CLAUDE.md b/CLAUDE.md index 35a2a3cb7..e6a6d61ae 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -177,7 +177,7 @@ Tack is a Python-first GPU compute framework inspired by Taichi. Kernels are dec The measurements behind these choices (wait times, launch counts, thread schedules) are in `docs/design/backend-implementations.md` ("How each backend queues and waits") and `docs/design/memory-and-aliasing.md` ("New arrays, fills and one-element reads"). -`tack.empty()` (under every array constructor) calls `backend.allocate_array()` through `_allocate`, which, on a `MemoryError` (every GPU backend raises `runtime.backend.DeviceMemoryError` when an allocation runs out), collects Python's garbage, waits for the device and tries once more: a dropped array in a reference cycle (a topology's cached locator holds a dataset of it) keeps its device memory until the cycle collector runs (`test_out_of_memory.py`). A new array's contents are unspecified on every backend; `tack.zeros` fills it, and `Array.fill` first offers the fill to `Backend.fill_array`, which the CPU backend answers for 256 KiB or more by running `tack.algorithms.copy._fill_kernel` through the normal dispatch, so the workers fill (and first-touch) the pages. On Linux `TACK_CPU_MALLOC=1` tells glibc to keep freed pages (`_tune_malloc`; off by default, since it is process-wide). `Array.__getitem__` reads one element through `DeviceBuffer.read_range(start, count)`, which copies just that range after the queued launches (each GPU backend checks the range against the allocation); `__setitem__` and `__iter__` raise. +`tack.empty()` (under every array constructor) calls `backend.allocate_array()` through `_allocate`, which, on a `MemoryError` (every GPU backend raises `runtime.backend.DeviceMemoryError` when an allocation runs out), collects Python's garbage, waits for the device and tries once more: a dropped array in a reference cycle (a topology's cached locator holds a dataset of it) keeps its device memory until the cycle collector runs (`test_out_of_memory.py`). Every buffer `_allocate` and `_empty_for_mpi` make is counted on its device (`runtime/memory_stats`, private: `live`, `peak`, `reset_peak`; its bytes come back through a weak reference's callback when the buffer is collected, about 0.9 us an array): views once, with their buffer; memory from outside (`wrap_ptr`, DLPack, `array_from_ptr`) not at all; a pooled block while an array holds it. The root conftest's `peak_memory` fixture measures one call's peak with it, on any backend (`test_memory_stats.py`; the cut filters' memory plan, D1). A new array's contents are unspecified on every backend; `tack.zeros` fills it, and `Array.fill` first offers the fill to `Backend.fill_array`, which the CPU backend answers for 256 KiB or more by running `tack.algorithms.copy._fill_kernel` through the normal dispatch, so the workers fill (and first-touch) the pages. On Linux `TACK_CPU_MALLOC=1` tells glibc to keep freed pages (`_tune_malloc`; off by default, since it is process-wide). `Array.__getitem__` reads one element through `DeviceBuffer.read_range(start, count)`, which copies just that range after the queued launches (each GPU backend checks the range against the allocation); `__setitem__` and `__iter__` raise. ### Backend contract diff --git a/benchmarks/vtk_compare/memory.py b/benchmarks/vtk_compare/memory.py index 81f032093..0307c06b9 100644 --- a/benchmarks/vtk_compare/memory.py +++ b/benchmarks/vtk_compare/memory.py @@ -15,7 +15,10 @@ before it (temporaries and output); - ``kept``: what it holds after the call while the output is alive (the output, and whatever the filter derived and kept on the input); -- the call's time. +- the call's time; +- for Tack, ``counted``: the most its arrays held during the call beyond what + they held before (`tack.runtime.memory_stats`, the count of every array Tack + makes), which sees any device and none of NumPy's or the allocator's own. The memory measured is what the allocator has handed out and not taken back (``malloc_zone_statistics`` on macOS, ``mallinfo2`` on Linux): every live @@ -180,17 +183,25 @@ def run(data, mesh): mesh = build(m.SIZES[size]) source, input_bytes = prepare(mesh) _release_free_memory() + counted = None + if system == "tack": + from tack.runtime import memory_stats + + counted = memory_stats.reset_peak() before = read() with _Sampler(read) as sampler: start = time.perf_counter() result = run(source, mesh) seconds = time.perf_counter() - start + if counted is not None: + counted = memory_stats.peak() - counted _release_free_memory() kept = read() - before del result return {"mesh": mesh_name, "size": size, "cells": mesh.num_cells, "filter": filter_name, "system": system, "threads": threads, "input": input_bytes, - "peak": sampler.peak - before, "kept": kept, "seconds": seconds} + "peak": sampler.peak - before, "kept": kept, "seconds": seconds, + "counted": counted} def _child(args): @@ -228,7 +239,7 @@ def main(): names = [q.name for q in p.PAIRS if not args.filters or q.name in args.filters.split(",")] records = [] print(f"{'mesh':>4} {'size':>5} {'filter':<15} {'system':<5} {'input MB':>8} " - f"{'peak MB':>8} {'kept MB':>8} {'s':>7} peak/input", flush=True) + f"{'peak MB':>8} {'kept MB':>8} {'s':>7} peak/input counted MB", flush=True) for mesh in args.meshes.split(","): for size in args.sizes.split(","): for name in names: @@ -242,7 +253,9 @@ def main(): continue print(f"{mesh:>4} {size:>5} {name:<15} {system:<5} {_gb(r['input'])} " f"{_gb(r['peak'])} {_gb(r['kept'])} {r['seconds']:7.3f} " - f"{r['peak'] / max(r['input'], 1):5.2f}", flush=True) + f"{r['peak'] / max(r['input'], 1):5.2f}" + + (f" {_gb(r['counted'])}" if r.get("counted") is not None else ""), + flush=True) if args.output: with open(args.output, "w") as f: json.dump(records, f, indent=1) diff --git a/benchmarks/vtk_compare/pairs.py b/benchmarks/vtk_compare/pairs.py index 3d0eb08aa..7b1ac6571 100644 --- a/benchmarks/vtk_compare/pairs.py +++ b/benchmarks/vtk_compare/pairs.py @@ -104,6 +104,16 @@ def _threshold(grid, mesh, form): return f +def _table_clip(grid, mesh, form): + from vtkmodules.vtkFiltersGeneral import vtkTableBasedClipDataSet + + # Keeps p above the value, as td.clip keeps it at or above (off every + # point's value, as the contour is). + f = vtkTableBasedClipDataSet(input_data=grid, value=CONTOUR_ISOVALUE) + f.SetInputArrayToProcess(0, 0, 0, 0, "p") + return f + + def _geometry_filter(grid, mesh, form): from vtkmodules.vtkFiltersGeometry import vtkGeometryFilter @@ -208,6 +218,10 @@ def _tack(): vtk=_plane_cutter, tack=lambda d, m: _tack()[0].slice_plane(d, tuple(_center(m)), tuple(NORMAL)), point_fields=("p",), output="surface"), + Pair("clip", + vtk=_table_clip, + tack=lambda d, m: _tack()[0].clip(d, "p", CONTOUR_ISOVALUE), + point_fields=("p",), forms=("shape",), output="volume"), Pair("threshold", vtk=_threshold, tack=lambda d, m: _tack()[0].threshold(d, "c", *THRESHOLD), diff --git a/conftest.py b/conftest.py index 7aa4d8fb0..af42e0ae0 100644 --- a/conftest.py +++ b/conftest.py @@ -227,6 +227,27 @@ def no_debug(): tack.set_debug(was) +@pytest.fixture +def peak_memory(): + """A function measuring the most memory Tack's arrays held on a device during + one call, above what they held before it: ``result, nbytes = peak_memory(fn, + *args, device=None, **kwargs)``. It reads the count of every array Tack makes + (`tack.runtime.memory_stats`), the same on every backend; garbage is collected + first, so arrays dropped before the call are not in it. For a test that holds a + filter's peak to a multiple of its output (the cut filters' memory plan).""" + import gc + + from tack.runtime import memory_stats + + def measure(fn, *args, device=None, **kwargs): + gc.collect() + start = memory_stats.reset_peak(device) + result = fn(*args, **kwargs) + return result, memory_stats.peak(device) - start + + return measure + + @pytest.fixture def no_disk_cache(): """For a test that counts compile work in this process, which a disk-cache diff --git a/packages/tack-core/src/tack/lang/array.py b/packages/tack-core/src/tack/lang/array.py index 827530365..ffb2e321e 100644 --- a/packages/tack-core/src/tack/lang/array.py +++ b/packages/tack-core/src/tack/lang/array.py @@ -39,6 +39,7 @@ mat, vec, ) +from tack.runtime import memory_stats class DeviceBuffer: @@ -1102,16 +1103,18 @@ def _allocate(backend, dtype, shape, **options): memory: a 16 GB GPU ran out with dropped meshes still on it. The wait lets frees a backend defers (Level Zero's, a device without memory pools) complete before the retry, and buffers the collection returned to - a backend's cache (HIP's, Metal's) are freed (``release_memory``).""" + a backend's cache (HIP's, Metal's) are freed (``release_memory``). The + buffer is counted on its device (`tack.runtime.memory_stats`).""" try: - return backend.allocate_array(dtype, shape, **options) + buffer = backend.allocate_array(dtype, shape, **options) except MemoryError: import gc gc.collect() backend.synchronize() backend.release_memory() # what the collection gave back to a cache - return backend.allocate_array(dtype, shape, **options) + buffer = backend.allocate_array(dtype, shape, **options) + return memory_stats.counted(backend, buffer) def _empty_for_mpi(shape, dtype, device) -> Array: @@ -1124,8 +1127,8 @@ def _empty_for_mpi(shape, dtype, device) -> Array: scalar = dtype.scalar_dtype if isinstance(dtype, ElementType) else dtype _check_dtype(backend, scalar) storage = (int(math.prod(shape)) * dtype.size,) if isinstance(dtype, ElementType) else shape - return _array_view(dtype, shape, backend.allocate_mpi_array(scalar, storage), - device=backend) + buffer = memory_stats.counted(backend, backend.allocate_mpi_array(scalar, storage)) + return _array_view(dtype, shape, buffer, device=backend) def zeros(shape, dtype=None, *, device=None, layout="aos") -> Array: diff --git a/packages/tack-core/src/tack/runtime/memory_stats.py b/packages/tack-core/src/tack/runtime/memory_stats.py new file mode 100644 index 000000000..6bd55d9c8 --- /dev/null +++ b/packages/tack-core/src/tack/runtime/memory_stats.py @@ -0,0 +1,105 @@ +# SPDX-FileCopyrightText: Copyright (c) Kitware Inc. +# SPDX-License-Identifier: BSD-3-Clause + +"""The memory Tack's own arrays hold, counted per device, with a peak (private). + +Every array Tack makes is counted where it is allocated (`tack.empty`'s +``_allocate`` and ``_empty_for_mpi``, which every backend's ``allocate_array`` +goes through), and its bytes are given back when its buffer is collected. So +the CPU, Metal, CUDA, HIP, Level Zero and Warp are counted alike, which the +process's allocator statistics (the CPU only) and the drivers (behind CUDA's +pool, HIP's buffer cache and Metal's pool) cannot do. What is counted: + +- the bytes an array asked for (``DeviceBuffer.nbytes``), not what a pool + rounded them up to; +- memory from outside (``wrap_ptr``, ``array_from_ptr``, a DLPack or NumPy + import without a copy) is not Tack's, and is not counted; +- a view shares its array's buffer, and is counted once, with the buffer: the + memory is held while any view of it is; +- a block a backend's cache hands to a new array is counted while that array + holds it, not while it waits in the cache (``release_memory`` empties the + caches); +- the few bytes of a compiled kernel's scalar packs are not counted. + +`reset_peak` makes the peak what is live, so a test measures one call: +``reset_peak(d); start = live(d); f(); peak(d) - start``. Each function takes a +`tack.Device`, a spec (``"cuda:1"``), a backend, or None for the default +device. The cut filters' memory plan (``docs/design/plans/cut-filters-memory.md``, +D1) is why it exists; spilling to host memory under a budget (#387) will read +it too. +""" + +import threading +import weakref + +# A backend's count, kept while the backend lives. What a buffer gives back is +# held with its weak reference, the count and not the backend, so a buffer +# never keeps its backend alive here. +_counts = weakref.WeakKeyDictionary() +# Each counted buffer's weak reference: (count, bytes). A weak reference with a +# callback costs a third of `weakref.finalize` (0.2 against 0.8 us), and every +# array pays it. +_held = {} +# Reentrant: a buffer's finalizer can run inside a count's update, when the +# update's allocation sets off the garbage collector. +_lock = threading.RLock() + + +class _Count: + __slots__ = ("live", "peak") + + def __init__(self): + self.live = 0 + self.peak = 0 + + +def _count_of(backend): + count = _counts.get(backend) + if count is None: + with _lock: + count = _counts.setdefault(backend, _Count()) + return count + + +def _released(ref): + count, nbytes = _held.pop(ref) + with _lock: + count.live -= nbytes + + +def counted(backend, buffer): + """``buffer``, just allocated on ``backend``, added to the backend's count until + it is collected.""" + nbytes = int(buffer.nbytes) + count = _count_of(backend) + with _lock: + count.live += nbytes + if count.live > count.peak: + count.peak = count.live + _held[weakref.ref(buffer, _released)] = (count, nbytes) + return buffer + + +def _backend(device): + from tack.lang.array import _target + + return _target(device) + + +def live(device=None) -> int: + """The bytes Tack's arrays hold on ``device`` now.""" + return _count_of(_backend(device)).live + + +def peak(device=None) -> int: + """The most bytes Tack's arrays have held on ``device`` at once since it was + first used or `reset_peak` was called.""" + return _count_of(_backend(device)).peak + + +def reset_peak(device=None) -> int: + """Make ``device``'s peak what is live now, and return it.""" + count = _count_of(_backend(device)) + with _lock: + count.peak = count.live + return count.peak diff --git a/packages/tack-core/tests/test_memory_stats.py b/packages/tack-core/tests/test_memory_stats.py new file mode 100644 index 000000000..9e6d7f3f3 --- /dev/null +++ b/packages/tack-core/tests/test_memory_stats.py @@ -0,0 +1,150 @@ +# SPDX-FileCopyrightText: Copyright (c) Kitware Inc. +# SPDX-License-Identifier: BSD-3-Clause + +"""The count of Tack's own allocations, per device (`tack.runtime.memory_stats`; +the cut filters' memory plan, D1): every array Tack makes is counted where it is +allocated and given back when its buffer is collected, on every backend alike.""" + +import gc +import threading + +import numpy as np +import pytest + +import tack +from tack.lang.array import _empty_for_mpi +from tack.runtime import dispatch +from tack.runtime import memory_stats as ms +from tack.runtime.cpu import CPUBackend + +pytestmark = pytest.mark.quick + + +def _settled(device=None): + gc.collect() + return ms.live(device) + + +def test_an_array_is_counted_until_it_is_collected(backend): + start = _settled() + a = tack.empty((1000,), tack.f32) + assert ms.live() - start == 4000 + b = tack.zeros((10, 10), tack.vec3(tack.i32)) + assert ms.live() - start == 4000 + 1200 + del a + assert _settled() - start == 1200 + del b + assert _settled() == start + + +def test_a_view_is_counted_with_its_buffer_once(backend): + start = _settled() + a = tack.zeros((100, 4), tack.f32) + rows = a[10:20] + flat = a.reshape((400,)) + assert ms.live() - start == 1600 + del a, flat + assert _settled() - start == 1600 # held while a view of it is + del rows + assert _settled() == start + + +def test_soa_components_are_counted_each(backend): + start = _settled() + a = tack.empty((50,), tack.vec3(tack.f32), layout="soa") + assert ms.live() - start == 3 * 50 * 4 + del a + assert _settled() == start + + +def test_arrays_for_mpi_are_counted(backend): + start = _settled() + a = _empty_for_mpi((25,), tack.i64, None) + assert ms.live() - start == 200 + del a + assert _settled() == start + + +def test_memory_from_outside_is_not_counted(): + tack.set_default_device("cpu") + host = np.arange(1000, dtype=np.float32) + start = _settled() + shared = tack.from_dlpack(host) + pointed = tack.array_from_ptr(host.ctypes.data, (1000,), tack.f32) + assert ms.live() == start + copied = tack.array(host) + assert ms.live() - start == 4000 + del shared, pointed, copied + + +def test_the_peak_is_kept_until_reset(backend): + _settled() + base = ms.reset_peak() + a = tack.empty((2000,), tack.u8) + b = tack.empty((3000,), tack.u8) + del a, b + gc.collect() + assert ms.peak() - base == 5000 + assert ms.reset_peak() == ms.live() + assert ms.peak() == ms.live() + + +def test_a_reused_block_is_counted_while_an_array_holds_it(backend): + """Metal's pool and HIP's cache keep a dropped array's block for the next array: + it is counted while an array holds it, not while it waits.""" + start = _settled() + for _ in range(3): + a = tack.empty((1 << 19,), tack.f32) # 2 MiB, pooled where there is one + assert ms.live() - start == 1 << 21 + del a + assert _settled() == start + + +def test_each_device_has_its_own_count(): + class Second(CPUBackend): + @property + def memory_domain(self): + return ("cpu", 1) + + second = dispatch.as_device(Second()) + tack.set_default_device("cpu") + start, start_second = _settled(), _settled(second) + a = tack.empty((100,), tack.f32, device=second) + assert ms.live(second) - start_second == 400 + assert ms.live() == start + assert ms.live("cpu") == start + del a + assert _settled(second) == start_second + + +def test_threads_allocating_at_once_leave_the_count_as_it_was(backend): + start = _settled() + device = tack.device(str(dispatch.as_device(dispatch.get_backend()))) + + def churn(): + with device: + for _ in range(200): + held = [tack.empty((64,), tack.f32) for _ in range(5)] + del held + + threads = [threading.Thread(target=churn) for _ in range(4)] + for t in threads: + t.start() + for t in threads: + t.join() + assert _settled() == start + + +def test_the_fixture_measures_one_calls_peak(backend, peak_memory): + keep = tack.empty((1000,), tack.f32) # live before: not in the peak + + def work(): + a = tack.empty((1000,), tack.f32) + b = tack.empty((2000,), tack.f32) + del a + return b + + result, peak = peak_memory(work) + assert peak == 12000 + assert result.shape == (2000,) + del keep From 6019495cd9c2114a47d6245f3392fac27fd4cd9b Mon Sep 17 00:00:00 2001 From: Berk Geveci Date: Sun, 15 Nov 2026 16:10:05 -0500 Subject: [PATCH 2/5] clip numbers its kept points through a point map (#318, D2) 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). --- changelog.d/clip-point-map.md | 8 ++ .../src/tack/dataset/algorithms/clip.py | 132 ++++++++++++------ packages/tack-vis/tests/test_clip.py | 24 ++++ 3 files changed, 124 insertions(+), 40 deletions(-) create mode 100644 changelog.d/clip-point-map.md diff --git a/changelog.d/clip-point-map.md b/changelog.d/clip-point-map.md new file mode 100644 index 000000000..b8c0106b1 --- /dev/null +++ b/changelog.d/clip-point-map.md @@ -0,0 +1,8 @@ +### Changed + +- `clip` numbers the input points its cells keep through a map of one entry per + input point, and sorts only the crossings on cut edges, as VTK's + `vtkTableBasedClipDataSet` does: it made a record for every kept corner and + sorted them all. The output is the same bits. On 5M cells (CPU) its peak fell + from 364 to 256 MiB on tetrahedra and from 574 to 315 MiB on hexahedra, and a + warm clip from 43 to 30 ms and from 61 to 54 ms. diff --git a/packages/tack-vis/src/tack/dataset/algorithms/clip.py b/packages/tack-vis/src/tack/dataset/algorithms/clip.py index 9bd87fd3b..a88d02171 100644 --- a/packages/tack-vis/src/tack/dataset/algorithms/clip.py +++ b/packages/tack-vis/src/tack/dataset/algorithms/clip.py @@ -41,7 +41,7 @@ int(shapes.WEDGE): (13, [0, 2, 1, 3, 5, 4])} -_KINDS = 5 # cells, connectivity, records, centroids, centroid members +_KINDS = 5 # cells, connectivity, crossings, centroids, centroid members # Entries of any kind one cell can make, at most: a bound for the output's ids. @@ -102,7 +102,6 @@ def _clip_counts(cells, values, value, invert, data, cases, remap, counts, marks if whole: n_cells = 1 n_conn = cells.NUM_POINTS - n_records = cells.NUM_POINTS elif not dropped: at = cases[case] shapes_left = data[at] @@ -114,12 +113,14 @@ def _clip_counts(cells, values, value, invert, data, cases, remap, counts, marks if kind == 0: n_centroids += 1 n_members += count - n_records += count + for k in range(count): + if data[at + k] >= 8: + n_records += 1 else: n_cells += 1 n_conn += count for k in range(count): - if data[at + k] != 20: + if data[at + k] >= 8 and data[at + k] != 20: n_records += 1 at += count counts[e] = [n_cells, n_conn, n_records, n_centroids, n_members] @@ -128,7 +129,10 @@ def _clip_counts(cells, values, value, invert, data, cases, remap, counts, marks @tack.kernel def _clip_emit(cells, values, value, invert, data, cases, edges, remap, whole_type, whole_order, starts, types, offsets, connectivity, sources, keys, slots, - centroid_offsets, centroid_cells, marks, hidden): + centroid_offsets, centroid_cells, members, used, crossing, marks, hidden): + # An input point a cell keeps is written as its id and marked used; a + # crossing gets a record (its edge, and the slot that takes its point) and + # `crossing` in its slot until the records are merged. for c in cells: e = cells.entity_id(c) full = (1 << cells.NUM_POINTS) - 1 @@ -152,9 +156,8 @@ def _clip_emit(cells, values, value, invert, data, cases, edges, remap, whole_ty sources[cell] = e for k in range(cells.NUM_POINTS): p = cells.point_id(c, whole_order[k]) - keys[record] = [p, p] - slots[record] = conn - record += 1 + connectivity[conn] = p + used[p] = 1 conn += 1 elif not dropped: at = cases[case] @@ -169,17 +172,17 @@ def _clip_emit(cells, values, value, invert, data, cases, edges, remap, whole_ty centroid_cells[centroid] = e for k in range(count): entry = data[at + k] - pa = 0 - pb = 0 if entry < 8: - pa = cells.point_id(c, remap[entry]) - pb = pa + p = cells.point_id(c, remap[entry]) + members[member] = p + used[p] = 1 else: pa = cells.point_id(c, remap[edges[2 * (entry - 8)]]) pb = cells.point_id(c, remap[edges[2 * (entry - 8) + 1]]) - keys[record] = [min(pa, pb), max(pa, pb)] - slots[record] = -(member + 1) - record += 1 + keys[record] = [min(pa, pb), max(pa, pb)] + slots[record] = -(member + 1) + members[member] = crossing + record += 1 member += 1 else: types[cell] = tack.u8(kind) @@ -196,43 +199,74 @@ def _clip_emit(cells, values, value, invert, data, cases, edges, remap, whole_ty connectivity[conn] = -(centroid + 1) # resolved later else: entry = data[at + k] - pa = 0 - pb = 0 if entry < 8: - pa = cells.point_id(c, remap[entry]) - pb = pa + p = cells.point_id(c, remap[entry]) + connectivity[conn] = p + used[p] = 1 else: pa = cells.point_id(c, remap[edges[2 * (entry - 8)]]) pb = cells.point_id(c, remap[edges[2 * (entry - 8) + 1]]) - keys[record] = [min(pa, pb), max(pa, pb)] - slots[record] = conn - record += 1 + keys[record] = [min(pa, pb), max(pa, pb)] + slots[record] = conn + connectivity[conn] = crossing + record += 1 conn += 1 at += count +@tack.kernel +def _number_points(used, keys, order, run_offsets, crossings, number, ends, weights): + # The output's points are numbered as a sort of (low, high) edge keys over + # every point use would number them: each used input point p, then the + # crossings on its edges to higher points. So p's number is the used points + # before it (``used``, scanned) and the crossings whose low end is below it. + for p in range(number.shape[0]): + if used[p + 1] > used[p]: + lo = 0 + hi = crossings + while lo < hi: + mid = (lo + hi) // 2 + if keys[order[run_offsets[mid]]][0] < p: + lo = mid + 1 + else: + hi = mid + r = used[p] + lo + number[p] = r + ends[r] = [p, p] + weights[r] = 0.0 + + +@tack.kernel +def _renumber(entries, number): + # Input point ids to their output numbers; the negative markers (crossings' + # slots, centroids) stay. + for i in range(entries.shape[0]): + p = entries[i] + if p >= 0: + entries[i] = number[p] + + @tack.kernel def _resolve_points(keys, order, run_offsets, slots, values, positions, value, connectivity, - members, ends, weights, count): - # Each run of equal keys is one output point: a corner, or an edge crossing - # interpolated from its end first by position, as contour's are, so every - # piece holding the edge computes it alike. + members, ends, weights, used, count): + # Each run of equal keys is one crossing, numbered after the used points up to + # its low end, and interpolated from its end first by position, as contour's + # are, so every piece holding the edge computes it alike. for r in range(count): key = keys[order[run_offsets[r]]] a = key[0] b = key[1] - if a != b and not _first_end(positions[a], positions[b], a, b): + point = used[a + 1] + r + if not _first_end(positions[a], positions[b], a, b): a, b = b, a - ends[r] = [a, b] - weights[r] = 0.0 - if a != b: - weights[r] = _crossing(value, values[a], values[b]) + ends[point] = [a, b] + weights[point] = _crossing(value, values[a], values[b]) for i in range(run_offsets[r], run_offsets[r + 1]): slot = slots[order[i]] if slot >= 0: - connectivity[slot] = r + connectivity[slot] = point else: - members[-slot - 1] = r + members[-slot - 1] = point @tack.kernel @@ -403,29 +437,47 @@ def clip(data, by, value=0.0, inside=False, fields=None, subdivision=None, toler offsets = tack.zeros((n_cells + 1,), odt) connectivity = tack.empty((n_conn,), odt) sources = tack.empty((n_cells,), idt) + # Records only for crossings: the input points cells keep are numbered through + # a map of one entry per input point (VTK's point map), so only the cut's new + # points are sorted. A record for every kept corner made the merge most of + # clip's memory (the cut filters' memory plan, D2). keys = tack.empty((n_records,), tack.vec(2, idt)) slots = tack.empty((n_records,), odt) centroid_offsets = tack.empty((n_centroids + 1,), odt) centroid_cells = tack.empty((n_centroids,), idt) + members = tack.empty((n_members,), odt) + num_points = data.num_points + used = tack.zeros((num_points + 1,), odt) + crossing = -(n_centroids + 1) # below every centroid's placeholder for group, (table, cases, edges, remap, whole_type, whole_order) in groups: _clip_emit(group.view(), values, float(value), invert, table, cases, edges, remap, whole_type, whole_order, starts, types, offsets, connectivity, sources, - keys, slots, centroid_offsets, centroid_cells, marks, hidden) - del starts # before the merge, the clip's peak + keys, slots, centroid_offsets, centroid_cells, members, used, crossing, + marks, hidden) + del starts _close(offsets, n_cells, n_conn) _close(centroid_offsets, n_centroids, n_members) - order = key_order(keys, data.num_points, odt) - point_runs, count = run_offsets(keys, order) - members = tack.empty((n_members,), odt) + kept = exclusive_scan(used, used, num_points, close=True) if num_points else 0 + order = key_order(keys, num_points, odt) + point_runs, crossings = run_offsets(keys, order) + count = kept + crossings ends = tack.empty((count,), tack.vec(2, idt)) # Weights are fractions even when the values are integers (Viskores interpolates # an integer field's crossings the same way). weights = tack.empty((count,), precision) - if count: + if kept: + number = tack.empty((num_points,), odt) + _number_points(used, keys, order, point_runs, crossings, number, ends, weights) + if n_conn: + _renumber(connectivity, number) + if n_members: + _renumber(members, number) + del number + if crossings: _resolve_points(keys, order, point_runs, slots, values, data.geometry.values, float(value), connectivity, - members, ends, weights, count) + members, ends, weights, used, crossings) if n_conn: _centroid_entries(connectivity, count) out = UnstructuredTopology(types, offsets, connectivity, num_points=count + n_centroids) diff --git a/packages/tack-vis/tests/test_clip.py b/packages/tack-vis/tests/test_clip.py index 99c47fe15..952dffc3b 100644 --- a/packages/tack-vis/tests/test_clip.py +++ b/packages/tack-vis/tests/test_clip.py @@ -158,3 +158,27 @@ def test_integer_clip_converts_before_subtracting(backend): positions = out.positions() assert sum(np.all(np.isclose(positions, [1, 0.5, 0]), axis=1)) == 1 np.testing.assert_array_equal(field.values.numpy(), values) + + +def _clip_output_bytes(out): + t = out.topology + return sum(a.nbytes for a in (t.types, t.offsets, t.connectivity, out.geometry.values, + *(f.values for f in out.fields.values()))) + + +@pytest.mark.parametrize("kind", ["tetrahedra", "hexahedra"]) +def test_clip_holds_little_more_than_its_output(backend, peak_memory, kind): + """The input points cells keep are numbered through a map of one entry per input + point, and only the crossings are sorted (the cut filters' memory plan, D2): + with a record and a sort for every kept corner, clip held 4.4 to 5 times its + output at its peak, where it keeps most of the mesh; now 1.4 to 2.6.""" + grid = td.uniform_grid((41, 41, 41), spacing=(0.025, 0.025, 0.025)) + data = (td.tetrahedralize(grid) if kind == "tetrahedra" + else td.extract_cells(grid, np.arange(grid.num_cells))) + data = td.DataSet(data.topology, data.geometry) + x = data.positions() + data.fields["p"] = td.Field(td.H1(data), tack.array((x[:, 0] + 0.3 * x[:, 1]).astype(np.float32))) + td.clip(data, "p", 0.5) # compiled outside the measurement + for value in (0.05, 0.5): + out, peak = peak_memory(td.clip, data, "p", value) + assert peak <= 3 * _clip_output_bytes(out), (value, peak / _clip_output_bytes(out)) From 7b74f6659790dc89201bda081d0d74f348c1d0c1 Mon Sep 17 00:00:00 2001 From: Berk Geveci Date: Sun, 15 Nov 2026 16:37:47 -0500 Subject: [PATCH 3/5] clip of polyhedra numbers its kept points through a point map (#318, D2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- changelog.d/clip-polyhedra-point-map.md | 8 ++ .../dataset/algorithms/_clip_polyhedra.py | 118 +++++++++++++----- .../tack-vis/tests/test_clip_polyhedra.py | 26 ++++ 3 files changed, 123 insertions(+), 29 deletions(-) create mode 100644 changelog.d/clip-polyhedra-point-map.md diff --git a/changelog.d/clip-polyhedra-point-map.md b/changelog.d/clip-polyhedra-point-map.md new file mode 100644 index 000000000..f05ab29c5 --- /dev/null +++ b/changelog.d/clip-polyhedra-point-map.md @@ -0,0 +1,8 @@ +### Changed + +- `clip` of polyhedra (a polyhedral topology, or polyhedra beside shapes) numbers + the input points its clipped faces keep through a map of one entry per input + point, and sorts only the crossings, as the shapes' clip now does: it named + every kept face point and sorted them all. The output is the same bits; where + it keeps most of the mesh, its peak fell from 3.8 to 4.5 times its output to + 2.5 to 3.4. diff --git a/packages/tack-vis/src/tack/dataset/algorithms/_clip_polyhedra.py b/packages/tack-vis/src/tack/dataset/algorithms/_clip_polyhedra.py index d9295de8b..5a83a9339 100644 --- a/packages/tack-vis/src/tack/dataset/algorithms/_clip_polyhedra.py +++ b/packages/tack-vis/src/tack/dataset/algorithms/_clip_polyhedra.py @@ -12,12 +12,13 @@ from tack.arrays import size_of from tack.dataset import ids from tack.dataset.algorithms._common import _close, _crossing, _first_end +from tack.dataset.algorithms.clip import _number_points, _renumber from tack.dataset.buckets import key_order, run_offsets from tack.dataset.carry import Interpolate, Take, carry @tack.kernel -def _face_sizes(face_offsets, face_points, values, iso, sizes, kept): +def _face_sizes(face_offsets, face_points, values, iso, sizes, kept, crossings): # Each face's clipped polygon: its kept points, and a crossing on each edge # strictly between the sides (a point at the value is kept, so an edge from # it has no other crossing). Below is not at or above, so a NaN is below, @@ -27,6 +28,7 @@ def _face_sizes(face_offsets, face_points, values, iso, sizes, kept): start = face_offsets[f] n = face_offsets[f + 1] - start k = 0 + x = 0 for j in range(n): va = values[face_points[start + j]] vb = values[face_points[start + (j + 1 if j + 1 < n else 0)]] @@ -34,29 +36,38 @@ def _face_sizes(face_offsets, face_points, values, iso, sizes, kept): k += 1 if (va > iso and not vb >= iso) or (not va >= iso and vb > iso): k += 1 + x += 1 sizes[f] = k if k >= 3 else 0 kept[f] = 1 if k >= 3 else 0 + crossings[f] = x if k >= 3 else 0 @tack.kernel -def _face_keys(face_offsets, face_points, values, iso, sizes, starts, keys): - # The clipped polygon's points, named as contour names crossings: (a, a) for a - # kept point, (low, high) for an edge's crossing. +def _face_keys(face_offsets, face_points, values, iso, sizes, starts, record_starts, keys, + slots, point_of, used, crossing): + # The clipped polygon's points: a kept point as its id, marked used; an edge's + # crossing as a record named (low, high), as contour names crossings, its + # slot holding `crossing` until the records are merged. for f in range(sizes.shape[0]): if sizes[f] > 0: start = face_offsets[f] n = face_offsets[f + 1] - start at = starts[f] + record = record_starts[f] for j in range(n): a = face_points[start + j] b = face_points[start + (j + 1 if j + 1 < n else 0)] va = values[a] vb = values[b] if va >= iso: - keys[at] = [a, a] + point_of[at] = a + used[a] = 1 at += 1 if (va > iso and not vb >= iso) or (not va >= iso and vb > iso): - keys[at] = [min(a, b), max(a, b)] + keys[record] = [min(a, b), max(a, b)] + slots[record] = at + point_of[at] = crossing + record += 1 at += 1 @@ -92,19 +103,22 @@ def _cell_counts(cell_offsets, cell_faces, face_offsets, face_points, values, is @tack.kernel -def _point_runs(keys, order, offsets, values, positions, iso, ends, weights, point_of, count): - # One output point per run of equal names: a kept point (weight 0) or an - # edge's crossing, interpolated from its end first by position as contour's. +def _point_runs(keys, order, offsets, slots, values, positions, iso, ends, weights, used, + point_of, count): + # One output point per run of equal crossing names, numbered after the used + # points up to its low end (`clip`'s `_number_points` numbers those), and + # interpolated from its end first by position as contour's. for r in range(count): key = keys[order[offsets[r]]] a = key[0] b = key[1] - if a != b and not _first_end(positions[a], positions[b], a, b): + point = used[a + 1] + r + if not _first_end(positions[a], positions[b], a, b): a, b = b, a - ends[r] = [a, b] - weights[r] = 0.0 if a == b else _crossing(iso, values[a], values[b]) + ends[point] = [a, b] + weights[point] = _crossing(iso, values[a], values[b]) for i in range(offsets[r], offsets[r + 1]): - point_of[order[i]] = r + point_of[slots[order[i]]] = point @tack.kernel @@ -259,31 +273,60 @@ def clip_parts(data, values, iso, inside): n_faces, n_cells = size_of(t.face_offsets) - 1, data.num_cells sizes = tack.empty((n_faces,), tack.i32) face_kept = tack.empty((n_faces,), tack.i32) + face_crossings = tack.empty((n_faces,), tack.i32) if n_faces: - _face_sizes(t.face_offsets, t.face_points, values, iso, sizes, face_kept) + _face_sizes(t.face_offsets, t.face_points, values, iso, sizes, face_kept, + face_crossings) odt = ids.for_output(data, 2 * size_of(t.face_points) + cap_count + n_faces) starts = tack.empty((n_faces,), odt) face_keys = exclusive_scan(sizes, starts, n_faces) if n_faces else 0 face_slots = tack.empty((n_faces,), odt) kept_faces = exclusive_scan(face_kept, face_slots, n_faces) if n_faces else 0 - # Every point's name: the clipped faces' points, then the caps' iso-vertices. - keys = tack.empty((face_keys + cap_count,), tack.vec(2, idt)) - if face_keys: - _face_keys(t.face_offsets, t.face_points, values, iso, sizes, starts, keys) + # Every point the clipped faces and the caps use: an input point as its id, + # numbered through a map of one entry per input point, as `clip`'s shapes + # are; a crossing by a record, and only the records are sorted (the cut + # filters' memory plan, D2). The faces' points come first, then the caps'. + face_records = tack.empty((n_faces,), odt) + face_crossing_count = (exclusive_scan(face_crossings, face_records, n_faces) + if n_faces else 0) + del face_crossings + cap_records = tack.empty((cap_count,), odt) + cap_crossing_count = 0 if cap_count: - _cap_names(cap_ends, keys, face_keys, cap_count) + flags = tack.empty((cap_count,), tack.i32) + _cap_crossings(cap_ends, flags, cap_count) + cap_crossing_count = exclusive_scan(flags, cap_records, cap_count) + del flags + n_records = face_crossing_count + cap_crossing_count + keys = tack.empty((n_records,), tack.vec(2, idt)) + slots = tack.empty((n_records,), odt) total = face_keys + cap_count point_of = tack.empty((total,), odt) - count = 0 - if total: - order = key_order(keys, data.num_points, odt) - runs, count = run_offsets(keys, order) + num_points = data.num_points + used = tack.zeros((num_points + 1,), odt) + crossing = -1 + if face_keys: + _face_keys(t.face_offsets, t.face_points, values, iso, sizes, starts, face_records, + keys, slots, point_of, used, crossing) + if cap_count: + _cap_names(cap_ends, cap_records, keys, slots, point_of, used, face_keys, + face_crossing_count, crossing, cap_count) + del face_records, cap_records + kept_points = exclusive_scan(used, used, num_points, close=True) if num_points else 0 + order = key_order(keys, num_points, odt) + runs, crossings = run_offsets(keys, order) + count = kept_points + crossings ends = tack.empty((count,), tack.vec(2, idt)) weights = tack.empty((count,), weight_dtype) - if count: - _point_runs(keys, order, runs, values, data.geometry.values, iso, - ends, weights, point_of, count) + if kept_points: + number = tack.empty((num_points,), odt) + _number_points(used, keys, order, runs, crossings, number, ends, weights) + _renumber(point_of, number) + del number + if crossings: + _point_runs(keys, order, runs, slots, values, data.geometry.values, iso, + ends, weights, used, point_of, crossings) # Cells: each kept one's faces left and caps. first = tack.zeros((n_cells,), odt) @@ -342,12 +385,29 @@ def _negate(values, out, infinity): @tack.kernel -def _cap_names(ends, keys, offset, n): +def _cap_crossings(ends, flags, n): + for i in range(n): + e = ends[i] + flags[i] = 1 if e[0] != e[1] else 0 + + +@tack.kernel +def _cap_names(ends, record_starts, keys, slots, point_of, used, offset, first_record, + crossing, n): # A cap's iso-vertex named as a face's crossing is, (low id, high id): contour - # gives its ends first by position, which only interpolation reads. + # gives its ends first by position, which only interpolation reads. One at a + # point whose value is the level (ends (a, a)) is that point, as a face's + # kept point is. for i in range(n): e = ends[i] - keys[offset + i] = [min(e[0], e[1]), max(e[0], e[1])] + if e[0] == e[1]: + point_of[offset + i] = e[0] + used[e[0]] = 1 + else: + record = first_record + record_starts[i] + keys[record] = [min(e[0], e[1]), max(e[0], e[1])] + slots[record] = offset + i + point_of[offset + i] = crossing # ── Polyhedra beside shapes ───────────────────────────────────────── diff --git a/packages/tack-vis/tests/test_clip_polyhedra.py b/packages/tack-vis/tests/test_clip_polyhedra.py index d82022e2f..1dfaef4a4 100644 --- a/packages/tack-vis/tests/test_clip_polyhedra.py +++ b/packages/tack-vis/tests/test_clip_polyhedra.py @@ -134,3 +134,29 @@ def _volume_of_surface(surface): for i in range(1, len(p) - 1): total += np.dot(p[0], np.cross(p[i], p[i + 1])) return total / 6.0 + + +def _polyhedral_output_bytes(out): + t = out.topology + return (sum(getattr(t, name).nbytes for name in ("face_offsets", "face_points", "cell_offsets", + "cell_faces", "cell_face_sides")) + + out.geometry.values.nbytes + sum(f.values.nbytes for f in out.fields.values())) + + +@pytest.mark.parametrize("kind", ["hexahedra", "tetrahedra"]) +def test_a_polyhedral_clip_holds_little_more_than_its_output(peak_memory, kind): + """The faces' kept points are numbered through a map of one entry per input + point, and only the crossings are sorted (the cut filters' memory plan, D2): with + a name and a sort for every kept face point, the clip held 3.8 to 4.5 times its + output at its peak, where it keeps most of the mesh; now 2.5 to 3.4.""" + grid = td.uniform_grid((31, 31, 31), spacing=(1 / 30,) * 3) + shaped = (td.tetrahedralize(grid) if kind == "tetrahedra" + else td.extract_cells(grid, np.arange(grid.num_cells))) + data = td.as_polyhedra(td.DataSet(shaped.topology, shaped.geometry.values)) + x = data.positions() + data.fields["p"] = td.Field(td.H1(data), tack.array((x[:, 0] + 0.3 * x[:, 1]).astype(np.float32))) + td.clip(data, "p", 0.5) # compiled outside the measurement + for value in (0.05, 0.5): + out, peak = peak_memory(td.clip, data, "p", value) + ratio = peak / _polyhedral_output_bytes(out) + assert ratio <= 3.6, (value, ratio) From 1905b579badfc958762d977ee9a18ba6f63827f9 Mon Sep 17 00:00:00 2001 From: Berk Geveci Date: Sun, 15 Nov 2026 18:29:34 -0500 Subject: [PATCH 4/5] clip's point map marked atomically 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. --- .../src/tack/dataset/algorithms/clip.py | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/tack-vis/src/tack/dataset/algorithms/clip.py b/packages/tack-vis/src/tack/dataset/algorithms/clip.py index a88d02171..5cf77edfb 100644 --- a/packages/tack-vis/src/tack/dataset/algorithms/clip.py +++ b/packages/tack-vis/src/tack/dataset/algorithms/clip.py @@ -157,7 +157,7 @@ def _clip_emit(cells, values, value, invert, data, cases, edges, remap, whole_ty for k in range(cells.NUM_POINTS): p = cells.point_id(c, whole_order[k]) connectivity[conn] = p - used[p] = 1 + tack.atomic_max(used, p, 1) conn += 1 elif not dropped: at = cases[case] @@ -175,7 +175,7 @@ def _clip_emit(cells, values, value, invert, data, cases, edges, remap, whole_ty if entry < 8: p = cells.point_id(c, remap[entry]) members[member] = p - used[p] = 1 + tack.atomic_max(used, p, 1) else: pa = cells.point_id(c, remap[edges[2 * (entry - 8)]]) pb = cells.point_id(c, remap[edges[2 * (entry - 8) + 1]]) @@ -202,7 +202,7 @@ def _clip_emit(cells, values, value, invert, data, cases, edges, remap, whole_ty if entry < 8: p = cells.point_id(c, remap[entry]) connectivity[conn] = p - used[p] = 1 + tack.atomic_max(used, p, 1) else: pa = cells.point_id(c, remap[edges[2 * (entry - 8)]]) pb = cells.point_id(c, remap[edges[2 * (entry - 8) + 1]]) @@ -214,6 +214,16 @@ def _clip_emit(cells, values, value, invert, data, cases, edges, remap, whole_ty at += count +def _scanned_map(used, num_points, odt): + """``(starts, kept)``: the point map's marks (i32, ``num_points + 1`` of them) + scanned into the output's id type, in place where that is i32, and how many + points are marked.""" + if not num_points: + return used, 0 + out = used if odt == tack.i32 else tack.empty((num_points + 1,), odt) + return out, exclusive_scan(used, out, num_points, close=True) + + @tack.kernel def _number_points(used, keys, order, run_offsets, crossings, number, ends, weights): # The output's points are numbered as a sort of (low, high) edge keys over @@ -447,7 +457,9 @@ def clip(data, by, value=0.0, inside=False, fields=None, subdivision=None, toler centroid_cells = tack.empty((n_centroids,), idt) members = tack.empty((n_members,), odt) num_points = data.num_points - used = tack.zeros((num_points + 1,), odt) + # Marked by every cell keeping the point, so atomically (i32, which every + # backend's atomics take); scanned into the output's id type. + used = tack.zeros((num_points + 1,), tack.i32) crossing = -(n_centroids + 1) # below every centroid's placeholder for group, (table, cases, edges, remap, whole_type, whole_order) in groups: _clip_emit(group.view(), values, float(value), invert, table, cases, edges, remap, @@ -458,7 +470,7 @@ def clip(data, by, value=0.0, inside=False, fields=None, subdivision=None, toler _close(offsets, n_cells, n_conn) _close(centroid_offsets, n_centroids, n_members) - kept = exclusive_scan(used, used, num_points, close=True) if num_points else 0 + used, kept = _scanned_map(used, num_points, odt) order = key_order(keys, num_points, odt) point_runs, crossings = run_offsets(keys, order) count = kept + crossings From 3eacd6c245ef75b36b57e725cf8bc34282cfda35 Mon Sep 17 00:00:00 2001 From: Berk Geveci Date: Sun, 15 Nov 2026 18:36:36 -0500 Subject: [PATCH 5/5] clip of polyhedra: the point map marked atomically 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. --- .../src/tack/dataset/algorithms/_clip_polyhedra.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/tack-vis/src/tack/dataset/algorithms/_clip_polyhedra.py b/packages/tack-vis/src/tack/dataset/algorithms/_clip_polyhedra.py index 5a83a9339..0ff9de581 100644 --- a/packages/tack-vis/src/tack/dataset/algorithms/_clip_polyhedra.py +++ b/packages/tack-vis/src/tack/dataset/algorithms/_clip_polyhedra.py @@ -12,7 +12,7 @@ from tack.arrays import size_of from tack.dataset import ids from tack.dataset.algorithms._common import _close, _crossing, _first_end -from tack.dataset.algorithms.clip import _number_points, _renumber +from tack.dataset.algorithms.clip import _number_points, _renumber, _scanned_map from tack.dataset.buckets import key_order, run_offsets from tack.dataset.carry import Interpolate, Take, carry @@ -61,7 +61,7 @@ def _face_keys(face_offsets, face_points, values, iso, sizes, starts, record_sta vb = values[b] if va >= iso: point_of[at] = a - used[a] = 1 + tack.atomic_max(used, a, 1) at += 1 if (va > iso and not vb >= iso) or (not va >= iso and vb > iso): keys[record] = [min(a, b), max(a, b)] @@ -304,7 +304,8 @@ def clip_parts(data, values, iso, inside): total = face_keys + cap_count point_of = tack.empty((total,), odt) num_points = data.num_points - used = tack.zeros((num_points + 1,), odt) + # Marked by every face and cap using the point, so atomically (as `clip`'s). + used = tack.zeros((num_points + 1,), tack.i32) crossing = -1 if face_keys: _face_keys(t.face_offsets, t.face_points, values, iso, sizes, starts, face_records, @@ -313,7 +314,7 @@ def clip_parts(data, values, iso, inside): _cap_names(cap_ends, cap_records, keys, slots, point_of, used, face_keys, face_crossing_count, crossing, cap_count) del face_records, cap_records - kept_points = exclusive_scan(used, used, num_points, close=True) if num_points else 0 + used, kept_points = _scanned_map(used, num_points, odt) order = key_order(keys, num_points, odt) runs, crossings = run_offsets(keys, order) count = kept_points + crossings @@ -402,7 +403,7 @@ def _cap_names(ends, record_starts, keys, slots, point_of, used, offset, first_r e = ends[i] if e[0] == e[1]: point_of[offset + i] = e[0] - used[e[0]] = 1 + tack.atomic_max(used, e[0], 1) else: record = first_record + record_starts[i] keys[record] = [min(e[0], e[1]), max(e[0], e[1])]