From 02253ad016114bb5d9f53eac6318cf842fed5da4 Mon Sep 17 00:00:00 2001 From: Berk Geveci Date: Sat, 5 Dec 2026 13:50:07 -0500 Subject: [PATCH 1/2] Flying edges: _points takes its windows' count, so a new grid size compiles nothing new _points compared q + 1 with windows.shape[0] inside its loop, which bakes the length into the code: every grid of another size compiled it anew. The count is an argument now. A test contours solids and sheets and slices on three grid sizes and checks that no flying-edges kernel gains a variant after the first. --- changelog.d/flying-edges-points-length.md | 6 ++++ .../tack/dataset/algorithms/_flying_edges.py | 12 ++++---- .../tests/test_structured_contour.py | 29 +++++++++++++++++++ 3 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 changelog.d/flying-edges-points-length.md diff --git a/changelog.d/flying-edges-points-length.md b/changelog.d/flying-edges-points-length.md new file mode 100644 index 000000000..8c6cad209 --- /dev/null +++ b/changelog.d/flying-edges-points-length.md @@ -0,0 +1,6 @@ +### Fixed + +- `contour` of a structured grid by flying edges compiled one of its kernels + (`_points`) anew for every grid size, since it read its rows' count inside the + loop, which bakes the length into the code. It takes the count as an argument + now, so a grid of a new size compiles nothing new. diff --git a/packages/tack-dataset/src/tack/dataset/algorithms/_flying_edges.py b/packages/tack-dataset/src/tack/dataset/algorithms/_flying_edges.py index e187647a9..2d562244a 100644 --- a/packages/tack-dataset/src/tack/dataset/algorithms/_flying_edges.py +++ b/packages/tack-dataset/src/tack/dataset/algorithms/_flying_edges.py @@ -413,10 +413,12 @@ def _triangle_counts(cells, u, hexa, flags, windows, point_counts, levels, nleve @tack.kernel -def _points(grid, flags, windows, levels, nlevels, nx, ny, starts, total, positions, - edges, weights): +def _points(grid, flags, windows, levels, nlevels, nx, ny, starts, total, nwindows, + positions, edges, weights): # Each (row of points, value)'s points, in its order: a point a crossing lands - # on, then its edges' crossings between their ends along x, y and z. + # on, then its edges' crossings between their ends along x, y and z. The + # windows' count comes as a scalar: read from windows.shape[0] in the body, it + # was baked into the code, and every grid of another size compiled anew. for q in range(windows.shape[0]): w = windows[q] r = q // nlevels @@ -425,7 +427,7 @@ def _points(grid, flags, windows, levels, nlevels, nx, ny, starts, total, positi k = r // ny level = levels[v] at = starts[q] - end = starts[q + 1] if q + 1 < windows.shape[0] else total + end = starts[q + 1] if q + 1 < nwindows else total for i in range(w[0], w[1] + 1 if end > at else w[0]): p = i + nx * r f = tack.i32(flags[w[2] + i - w[0]]) @@ -1057,7 +1059,7 @@ def _surface(data, values, u, levels, nlevels, weight_dtype, windows, nflags, co weights = tack.empty((count,), weight_dtype) if count: _points(grid, flags, windows, levels, nlevels, nx, ny, point_counts, count, - positions, edges, weights) + nrows * nlevels, positions, edges, weights) rows = tack.empty((width * triangles,), odt) sources = tack.empty((triangles,), idt) which = tack.empty((triangles,), tack.i32) diff --git a/packages/tack-dataset/tests/test_structured_contour.py b/packages/tack-dataset/tests/test_structured_contour.py index 55a2e41c9..b1be99d8c 100644 --- a/packages/tack-dataset/tests/test_structured_contour.py +++ b/packages/tack-dataset/tests/test_structured_contour.py @@ -381,3 +381,32 @@ def test_a_sheets_values_on_points_nans_and_hidden_cells(backend, monkeypatch): fast = _check(monkeypatch, hidden, [0.0, 0.5]) assert _same(_cell_marks(fast), _cell_marks(_general(monkeypatch, hidden, "s", [0.0, 0.5]))) + + +def test_a_new_grid_size_compiles_nothing_new(backend): + """No flying-edges kernel bakes a grid's size into its code: contours of solids + and sheets, and slices, on grids of three sizes compile each kernel once (after + the first size). ``_points`` read its windows' count in its body, and compiled + anew for every size.""" + from tack.lang.kernel import Kernel + from tack.runtime.dispatch import get_backend + + kernels = [k for k in vars(_flying_edges).values() if isinstance(k, Kernel)] + + def counts(): + cache = get_backend()._cache + return {k.name: len(cache.get(k) or ()) for k in kernels} + + seen = [] + for n in (12, 17, 23): + solid = td.uniform_grid((n, n + 2, n - 3), spacing=(0.5, 0.5, 0.5)) + x = solid.geometry.values.numpy().astype(float) + solid = _with(solid, np.linalg.norm(x - x.mean(axis=0), axis=1), tack.f32) + td.contour(solid, "s", [1.0, 2.0]) + td.slice(solid, td.Plane(origin=x.mean(axis=0), normal=(0.2, 0.3, 1.0))) + sheet = td.uniform_grid((n, n + 4), spacing=(0.5, 0.5)) + y = sheet.geometry.values.numpy().astype(float) + td.contour(_with(sheet, np.sin(y[:, 0]) + np.cos(y[:, 1]), tack.f32), "s", 0.5) + seen.append(counts()) + assert seen[1] == seen[2], {k: (seen[1][k], seen[2][k]) for k in seen[1] + if seen[1][k] != seen[2][k]} From 47593745c259a2c4a2a78f9a2c00ce061125202f Mon Sep 17 00:00:00 2001 From: Berk Geveci Date: Sat, 5 Dec 2026 14:04:03 -0500 Subject: [PATCH 2/2] Flying edges' size test checks every size, the second as well as the third The test compared only the second and third grid sizes' compile counts, so a kernel compiled once more at the second size alone would pass (the review's point). It asserts the three are equal; against the old _points it fails with {'_points': (3, 6, 9)}. --- packages/tack-dataset/tests/test_structured_contour.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/tack-dataset/tests/test_structured_contour.py b/packages/tack-dataset/tests/test_structured_contour.py index b1be99d8c..0369fc837 100644 --- a/packages/tack-dataset/tests/test_structured_contour.py +++ b/packages/tack-dataset/tests/test_structured_contour.py @@ -385,9 +385,9 @@ def test_a_sheets_values_on_points_nans_and_hidden_cells(backend, monkeypatch): def test_a_new_grid_size_compiles_nothing_new(backend): """No flying-edges kernel bakes a grid's size into its code: contours of solids - and sheets, and slices, on grids of three sizes compile each kernel once (after - the first size). ``_points`` read its windows' count in its body, and compiled - anew for every size.""" + and sheets, and slices, on grids of three sizes compile each kernel at the first + size only. ``_points`` read its windows' count in its body, and compiled anew for + every size.""" from tack.lang.kernel import Kernel from tack.runtime.dispatch import get_backend @@ -408,5 +408,5 @@ def counts(): y = sheet.geometry.values.numpy().astype(float) td.contour(_with(sheet, np.sin(y[:, 0]) + np.cos(y[:, 1]), tack.f32), "s", 0.5) seen.append(counts()) - assert seen[1] == seen[2], {k: (seen[1][k], seen[2][k]) for k in seen[1] - if seen[1][k] != seen[2][k]} + assert seen[0] == seen[1] == seen[2], { + k: tuple(s[k] for s in seen) for k in seen[0] if len({s[k] for s in seen}) > 1}