Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog.d/flying-edges-points-length.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]])
Expand Down Expand Up @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions packages/tack-dataset/tests/test_structured_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 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

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[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}
Loading