From 4232aae963365f2a7edeaa33d216cf6e77c5320a Mon Sep 17 00:00:00 2001 From: Berk Geveci Date: Sat, 5 Dec 2026 20:22:34 -0500 Subject: [PATCH] vtk_compare: check the points cells use, point to cell as a mean, cell volume's warm time computed Found by the 0.8 round on Windows, against 0.6 (step 11): - external faces failed every check: vtkGeometryFilter(merging=False) keeps every input point, and since 0.7 extract_surface keeps only the points its faces use. summary() counts the points the cells use, on both sides. - point to cell timed project's integral average (a quadrature, the default since 0.7) against vtkPointDataToCellData's mean of a cell's points: average="nodes" is the same work. - cell volume's warm runs read cell_geometry back from the mesh (mesh_kept), 1000x "faster" than 0.6: a pair whose result is kept with the mesh (Pair.kept_with_mesh) is timed warm on a new dataset over the same topology. run.py also says TACK_CPU_THREADS changes nothing there: --config cpuN sets Tack's threads. --- benchmarks/vtk_compare/pairs.py | 35 +++++++++++++++++++++++++++------ benchmarks/vtk_compare/run.py | 16 +++++++++++---- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/benchmarks/vtk_compare/pairs.py b/benchmarks/vtk_compare/pairs.py index 7b1ac6571..dd4a18228 100644 --- a/benchmarks/vtk_compare/pairs.py +++ b/benchmarks/vtk_compare/pairs.py @@ -10,8 +10,8 @@ normals, no cached search structures. ``check`` compares the two outputs before any timing is believed: counts -and integrated area or volume for filters that make a dataset, values for -those that make an array. +(cells, and the points the cells use) and integrated area or volume for +filters that make a dataset, values for those that make an array. """ from dataclasses import dataclass, field @@ -43,6 +43,10 @@ class Pair: vtk_values: Callable = None # VTK output -> host array tack_values: Callable = None # Tack result -> host array others: dict = field(default_factory=dict) # other VTK implementations + # The Tack result is kept with the mesh (``mesh_kept``: ``cell_geometry``), so a + # warm run on the same dataset would read it back: warm runs get a new dataset + # on the same topology, which keeps what the topology derived but not this. + kept_with_mesh: bool = False def _center(mesh): @@ -115,6 +119,8 @@ def _table_clip(grid, mesh, form): def _geometry_filter(grid, mesh, form): + # Keeps every input point (merging off is the fast path); since 0.7 Tack keeps + # only the points its faces use, so ``check`` counts the points cells use. from vtkmodules.vtkFiltersGeometry import vtkGeometryFilter return vtkGeometryFilter(input_data=grid, merging=False, pass_through_cell_ids=False, @@ -232,11 +238,16 @@ def _tack(): point_fields=("p",), output="surface"), Pair("cell volume", vtk=_cell_size, vtk_values=_cell_array("Volume"), - tack=lambda d, m: _tack()[1].cell_geometry(d)[0], tack_values=_host), + tack=lambda d, m: _tack()[1].cell_geometry(d)[0], tack_values=_host, + kept_with_mesh=True), + # VTK's vtkPointDataToCellData takes the mean of a cell's points; Tack's + # ``project`` into Constant is the integral average by default (since 0.7), a + # quadrature over each cell: ``average="nodes"`` is the mean, like for like. Pair("point to cell", vtk=_point_to_cell, vtk_values=_cell_array("p"), - tack=lambda d, m: _tack()[1].project(d, d.fields["p"], _tack()[0].Constant(d)), tack_values=_host, - point_fields=("p",)), + tack=lambda d, m: _tack()[1].project(d, d.fields["p"], _tack()[0].Constant(d), + average="nodes"), + tack_values=_host, point_fields=("p",)), Pair("cell to point", vtk=_cell_to_point, vtk_values=_point_array("c"), tack=lambda d, m: _tack()[1].project(d, d.fields["c"], _tack()[0].H1(d)), tack_values=_host, @@ -267,6 +278,18 @@ def _integrated(dataset, what): return float(cell_data[what][0]) if what in cell_data.keys() else 0.0 +def _used_points(dataset): + """How many distinct points ``dataset``'s cells use: VTK's geometry filter + keeps unused points, Tack's ``extract_surface`` drops them.""" + if hasattr(dataset, "polys"): + ids = [np.asarray(c.connectivity_array) for c in + (dataset.verts, dataset.lines, dataset.polys, dataset.strips) + if c.number_of_cells] + else: + ids = [np.asarray(dataset.cells.connectivity_array)] if dataset.number_of_cells else [] + return int(np.unique(np.concatenate(ids)).size) if ids else 0 + + def summary(pair, vtk_output=None, tack_result=None): """What the check compares, from one side's output.""" if pair.output == "array": @@ -276,7 +299,7 @@ def summary(pair, vtk_output=None, tack_result=None): vtk_output = dataset_to_vtk(tack_result) what = "Area" if pair.output == "surface" else "Volume" - return {"cells": vtk_output.number_of_cells, "points": vtk_output.number_of_points, + return {"cells": vtk_output.number_of_cells, "points": _used_points(vtk_output), what.lower(): _integrated(vtk_output, what)} diff --git a/benchmarks/vtk_compare/run.py b/benchmarks/vtk_compare/run.py index 40b6c9503..b755c5f71 100644 --- a/benchmarks/vtk_compare/run.py +++ b/benchmarks/vtk_compare/run.py @@ -26,11 +26,15 @@ 3. VTK is timed as ``Modified(); Update()``, repeated; 4. Tack is timed -- through ``tack.sync()``, since Metal queues launches -- two ways: ``warm`` -- the same dataset each run, so what it derived on earlier runs (faces, edges, launch groups) is kept, as in a - pipeline -- and ``fresh`` -- a new topology each run, built from the same + pipeline, except a result kept with the mesh itself (``Pair.kept_with_mesh``: + cell volume), timed on a new dataset over the same topology each run -- and ``fresh`` -- a new topology each run, built from the same arrays outside the timing, holding only what VTK's grid holds. Each measurement repeats for ``--budget`` seconds (at least 3 and at most 30 -runs) and records every time; reports use the median. Converting a VTK grid +runs) and records every time; reports use the median. + +Tack's threads come from the configuration (``cpuN`` makes a device of N +threads), so ``TACK_CPU_THREADS`` changes nothing here: use ``--config cpu1``. Converting a VTK grid to Tack, and shape cells to polyhedra, are timed as ``import``. Usage: @@ -271,8 +275,12 @@ def update(_, f=f): f.Update() _record(records, **key, impl=impl, mode="warm", comparable=ok, times=_measure(update, budget=budget)) - _record(records, **key, impl="tack", mode="warm", comparable=ok, - times=_measure(lambda _: run_tack(data), budget=budget)) + if pair.kept_with_mesh: + warm = _measure(run_tack, setup=lambda: source.dataset(fields), + budget=budget) + else: + warm = _measure(lambda _: run_tack(data), budget=budget) + _record(records, **key, impl="tack", mode="warm", comparable=ok, times=warm) _record(records, **key, impl="tack", mode="fresh", comparable=ok, times=_measure(run_tack, setup=lambda: source.dataset(fields, fresh=True),