Skip to content

AMR: levels in one array each, marks on the device, boxes in bulk (#336) - #458

Open
berkgeveci wants to merge 23 commits into
Kitware:mainfrom
berkgeveci:vis/amr-level-buffers
Open

berkgeveci wants to merge 23 commits into
Kitware:mainfrom
berkgeveci:vis/amr-level-buffers

Conversation

@berkgeveci

@berkgeveci berkgeveci commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #336, closes #314.

An AMR hierarchy's per-box Python and host work was the cost of every AMR path once a hierarchy has thousands of boxes. This keeps each level in one array per device with the boxes as views of it, makes the marks on the device, and builds boxes without re-running constructor checks on parts already known to be consistent.

What changes

  • Levels in one array each. The marks AMRDataSet makes are uploaded once per level per device, each box's a view (array[i:j]). in_level_buffers puts each level's fields in one array the same way. vtk_to_dataset (when it copies) and amr_gaussian_pulse use it, so every AMR algorithm runs on that form. Array._row_views makes many views of one array in one pass.
  • Marks on the device. Box pairs are found by binning boxes (_box_pairs.pairs), with one launch per kind of mark. The overlap and nesting checks use the same pairs.
  • Boxes in bulk. StructuredTopology, UniformCoordinates, Field, Ghosts and DataSet each have an _assembled that skips the constructor's checks. ghost_cells' grown boxes, the constructor's marks and vtk_to_dataset's AMR boxes use them. vtk_to_dataset takes a fast path for vtkImageData boxes and reads VTK information keys through the key's own Has/Get.
  • ghost_cells. The grown boxes' marks are made on the device, the fills' rows stay on the device, each device's values are placed in one launch, and sources are found one row of a shell at a time.
  • statistics. It reduces each level's boxes on a device as one array: one pass a level, as AMR volume-weighted statistics: batch the boxes #314 asks. On a GPU, an f32 array's sums are partial sums of 256 values, each compensated, added in f64 on the host (_sum). Array.sum() there adds with f32 partials, and a level's volume summed as one array came out 3e-4 off on Metal; plain partial sums still drifted, since a level's cells have one volume and adding equal f32 values in order rounds the same way every time.
  • VTK, without copies. dataset_to_vtk(amr, copy=False) shares the boxes' memory, and vtk_to_dataset(amr, copy=False) gives blocks lying end to end as one shared array.

No host NumPy is left in the AMR marks.

Measured

AMR-Wind, five levels (15,598 boxes), CPU, VTK master, with marks and values bit for bit the same as before:

before now
AMRDataSet constructor 4.0 s 0.40 s
vtk_to_dataset, copying 4.5 s 1.25 s
vtk_to_dataset(copy=False) (a NumPy reader's arrays) 0.90 s
ghost_cells 11.5 s 2.0 s

statistics(amr, "temperature", weights="volume") on the same plotfile (warm; the first call, which compiles, in brackets):

main this PR
CPU, five levels 5.04 s (12.9) 0.54 s (1.30)
CPU, levels 0-1 1.65 s 0.066 s
Metal, five levels 111 to 272 s, growing each call 0.31 s (0.61)
Metal, levels 0-1 37.9 s 0.07 s

The results agree: the same count, minimum and maximum; the mean 291.40631 on the CPU (both), 291.40634 (main) and 291.40633 (this PR) on Metal. The total weight, the domain's volume (2677917480.47), is off by 146 (main) and 246 (this PR) on the CPU, and by 172 (main) and 56 (this PR) on Metal: f32 cell volumes.

Most of what a box costs now is Python's garbage collector (0.90 s with it, 0.64 s without). Switching it off inside a filter only moves the work to the next collections, and it is process-wide, so it stays on.

Tests

  • test_amr.py:
    • each assembled part held to a constructed one, attribute for attribute, in 1D, 2D and 3D;
    • staged boxes held to boxes converted one at a time, copying and sharing, over three hierarchies;
    • the level-array form, shared VTK arrays both ways.
    • statistics of 4M f32 values against NumPy's f64 moments, on every device (fails on Metal without _sum), and _partial_sums on equal values (fails without the compensation, on the CPU too).
  • test_array_views.py: row views.
  • On Metal, one process: test_amr.py (77 passed with the CPU's), and the AMR-Wind runs above.
  • Locally, on this branch merged with today's main: test_amr.py, test_amr_probe.py, test_units.py, test_vtk_interop.py, test_array_views.py and test_array_views_overlap.py pass (273 passed, 19 skipped). ruff and the changelog check are clean.

Merging main

The branch dates from 11-07, so main is merged in (181992b). Three conflicts:

  • amr.py: main's refusal of DUPLICATECELL boxes on every rank (agree), beside this branch's nesting check on the device;
  • units.py: main's pure-number shortcut ahead of this branch's cached spellings;
  • the VTK interop: main's owned= beside this branch's staged boxes.

🤖 Generated with Claude Code

@berkgeveci

Copy link
Copy Markdown
Collaborator Author

@spyridon97, could you review this one? It is the AMR data-layout step: each level's fields in one array per device with the boxes as views of it, the blanking marks made on the device, and boxes assembled in bulk. It is what the batched AMR filters build on (prototypes on berkgeveci/tack-fork, branch scratch/fe-plus-level-buffers). CI is green and it merges cleanly with today's main.

@spyridon97

Copy link
Copy Markdown

@spyridon97, could you review this one? It is the AMR data-layout step: each level's fields in one array per device with the boxes as views of it, the blanking marks made on the device, and boxes assembled in bulk. It is what the batched AMR filters build on (prototypes on berkgeveci/tack-fork, branch scratch/fe-plus-level-buffers). CI is green and it merges cleanly with today's main.

I will take today (Friday) and Monday to familiarize myself a bit with the repo, and then start the reviewing here, i hope that's okay!

@spyridon97

Copy link
Copy Markdown

I read the whole diff, including the new amr_ghosts.py and the VTK span code. The structure makes sense to me:

I found one real bug, one thing that will bite at scale, and one thing that is unlikely but would corrupt data silently. Then some smaller questions.

@spyridon97

Copy link
Copy Markdown
  1. Bug: average_down reads past the end of its arrays on several devices

_average_cells_in_place allocates weighted[g] and sums[g] with n + m entries (n own values, m staged sources), then calls

_scaled(joined[g], measures, weighted[g])
_copy_offset(measures, sums[g])

Both kernels loop over out.shape[0], which is n + m, but joined[g] and measures only have n entries. So whenever m > 0 (boxes on more than one device) they read m entries past the end.

The results come out right because fill.plan.run overwrites that tail afterwards. That is why the tests pass. But debug mode catches it. With bounds checks on, this fails:

TACK_DEBUG=1 uv run pytest packages/tack-vis/tests/test_amr.py::test_ghost_cells_and_averages_on_two_devices_are_one_devices
IndexError: Kernel '_scaled': values[i] at amr_ghosts.py:1715, column 18:
index 80 is outside [0, 80). The access was skipped (a load gives 0).
15 more out-of-bounds accesses in the launch were skipped too.

Without TACK_DEBUG the same test passes. (I stopped at the first failure, so I only saw the image case. I did not run curvilinear in debug mode.) CLAUDE.md says TACK_DEBUG=1 pytest passes for the whole suite, so this should be fixed before merge.

I expect _copy_offset has the same problem, since it is the same shape, but the run stopped at _scaled, so I have not confirmed that one.

Fix: pass the first n entries as the output, weighted[g][:n] and sums[g][:n], or give the kernels the input length. On a GPU, reading past the end of a fresh allocation may also fault.

@spyridon97

Copy link
Copy Markdown
  1. Id type in _level_moments is too small for a big level

_level_moments calls

ids = owned_ids(marks, skipped, boxes[0].id_dtype)

on marks, which is the joined marks of a whole level. A small box has i32 ids, so on a level with more than 2^31 entries the scan and the _take would wrap. AMR-Wind's 160M cells are fine, so nothing fails today. A level at exascale is not fine.

ids.at_least(dtype, extent) exists for exactly this ("the type of an array derived from a topology that may hold more than its ids do"). Could the id type be ids.at_least(boxes[0].id_dtype, marks.shape[0])? In the old code owned_values used the dataset's own id_dtype, which always covers the dataset. A level made from many boxes is the new case.

@spyridon97

Copy link
Copy Markdown
  1. _joined_view(parts, extra) decides who owns the tail by a size check

_Fill.run asks for the group's values as one view with room for the staged sources after them. _joined_view accepts the tail as free when

at + extra * width == capacity

That is a numerical check, not a statement of who owns the memory. In average_down the groups are only levels (l, l+1), but their values are views into the hierarchy's per-device array, which holds all levels in order. If the levels after l+1 happen to total exactly extra entries, the staged sources are written over their data, and nothing complains.

It needs several devices (m > 0) and an exact size match, so it is unlikely. But it is silent corruption if it happens. Could the buffers made for this (_GroupArrays.buffer) carry an explicit flag saying "this tail is staging room", and _joined_view require it?

@spyridon97

Copy link
Copy Markdown

Smaller questions

  • CPU total weight. On the CPU the total weight went from 146 off to 246 off. _sum skips the CPU, and the per-box f64 merge is gone. Why not compensate on the CPU as well? np.sum(values.numpy(), dtype=np.float64) copies nothing there. The mean is the same either way (291.40631), so this is not a regression in the results people use, but the number moved the wrong way.
  • Bin size in _box_pairs. The lattice cell is the median box of set B. For a single large root box over small coarsened children, the root gets (root size / median size) bins per axis multiplied together. Has a skewed hierarchy been measured? _LevelBoxes uses the largest box instead, so the two heuristics differ. This is performance only.
  • Space cache. The new _plain cache in spaces.__new__ is global. It is safe today: only H1 and L2 override _defaults, and with no arguments the order is 1, so the basis does not depend on the mesh. A later _defaults that reads the topology would be cached from the first topology. There is a comment, but a small test would pin it.
  • PR text. The description says in_level_buffers and "levels in one array each". The code and the changelog say in_device_buffers, one array per device for the whole hierarchy. "No host NumPy is left in the AMR marks" is true for the marks, but _LevelBoxes (used by ghost_cells) still builds its bins in NumPy on the host.

@spyridon97

Copy link
Copy Markdown

a second note, nothing here blocks the merge. I read the PR through four patterns from my dissertation:

  • Batching: split the input into fixed batches (a row of voxels, as in Flying Edges). Each batch counts its output, and a prefix sum gives it its slot, so nothing allocates during the run.
  • Stream compaction: flag what to keep, scan, write only those.
  • Reduce by key: group items by key with an atomic count, a scan and an atomic placement, instead of a sort.
  • Horizontal fusion: scan the data once and let each element find only the queries that touch it, through a query index.

Already in the PR

  • Batching: the mark and ghost kernels use one thread per x-row, with count, scan and fill. The "hole" skip in _row_of is edge trimming.
  • Query index: the bin lattice in _box_pairs and _LevelBoxes.
  • Compaction: _one_source, _counted, owned_ids.

Suggestion: statistics in one pass

For a weighted level, _level_moments makes about ten level-sized arrays: weights, flags, scan slots, ids, taken values and weights, wx, w2, p2, p3 and p4. Each has its own launch. A kernel could instead give each thread a chunk of 256, read value, weight and mark once, skip marked entries, and write (weight, mean, M2, M3, M4). The host merges the chunks in f64 with the existing _merged.

That removes the temporaries and the compaction, and gives the same code and precision on CPU and GPU. Two things to check first: f32 on Metal needs each chunk's own mean so M3 and M4 do not cancel, and the per-box volume weight needs a way to find the box inside a chunk.

Smaller: fuse fields

ghost_cells and update_ghosts run one exchange and one _apply launch per field. Fields of the same kind and type could share one launch. It matters most for update_ghosts, which runs every step. I would measure a multi-field run first.

@berkgeveci

Copy link
Copy Markdown
Collaborator Author

@spyridon97, thank you for such a careful review. It's a big diff, and you found exactly the problems that tests don't catch.

I checked the three main points, and you're right on all of them:

  1. _scaled reproduces here with TACK_DEBUG=1, in both the image and curvilinear cases. _copy_offset is fine, though: it loops over source.shape[0], which is n.
  2. owned_ids on a joined level should use ids.at_least(boxes[0].id_dtype, marks.shape[0]), as you say.
  3. _joined_view should require buffers that were made with staging room, rather than inferring it from a size match. in_device_buffers leaves no room after the group's values, so in average_down that check can only pass by coincidence.

The smaller points are fair too, and the PR description is out of date. The one-pass statistics and the fused fields would make good follow-up issues for 0.9.

Would you like to fix these yourself, or should I? Either works for me. If you'd like to do it, it could be a good way into the code, and you can push to the branch or open a PR against it. Otherwise I'll fix them and ask you to look again.

@spyridon97

Copy link
Copy Markdown

@spyridon97, thank you for such a careful review. It's a big diff, and you found exactly the problems that tests don't catch.

I checked the three main points, and you're right on all of them:

  1. _scaled reproduces here with TACK_DEBUG=1, in both the image and curvilinear cases. _copy_offset is fine, though: it loops over source.shape[0], which is n.
  2. owned_ids on a joined level should use ids.at_least(boxes[0].id_dtype, marks.shape[0]), as you say.
  3. _joined_view should require buffers that were made with staging room, rather than inferring it from a size match. in_device_buffers leaves no room after the group's values, so in average_down that check can only pass by coincidence.

The smaller points are fair too, and the PR description is out of date. The one-pass statistics and the fused fields would make good follow-up issues for 0.9.

Would you like to fix these yourself, or should I? Either works for me. If you'd like to do it, it could be a good way into the code, and you can push to the branch or open a PR against it. Otherwise I'll fix them and ask you to look again.

I will all three, initially as commits on top of yours, and then i will let you squash

@spyridon97
spyridon97 force-pushed the vis/amr-level-buffers branch from 1af71a7 to 2d0d1d7 Compare November 25, 2026 15:34
@berkgeveci

Copy link
Copy Markdown
Collaborator Author

@spyridon97, thanks for the three fixes. They look right to me. With them, test_amr.py, test_array_views.py and test_ghost_cells.py pass here, both plain and with TACK_DEBUG=1.

The red CI isn't from your commits. The branch was last merged with main on 11-15, and CI tests it merged with today's main. Since then, #471 (field kinds) added a _kind attribute that Field.__init__ sets. This branch has a bulk constructor, Field._assembled, which skips __init__, so the fields it makes have no _kind. Every failing job I checked shows AttributeError: 'Field' object has no attribute '_kind'. The branch's own test that compares _assembled with __init__ (test_amr.py, assert list(vars(assembled)) == list(vars(made))) catches it too.

Two jobs also fail test_vtk_algorithms.py::test_a_filter_that_makes_a_dataset_gives_a_partitioned_dataset_per_level (assert 0 == 2). That is most likely the same error surfacing further downstream, but I'll confirm once main is merged.

The fix is to merge main into the branch and have _assembled set _kind the way __init__ does.

The marks AMRDataSet makes are uploaded once a level per device, each box's
a view (array[i:j]); in_level_buffers puts each level's fields in one array
the same way, and vtk_to_dataset (when it copies) and amr_gaussian_pulse
call it, so every AMR algorithm runs on that form. Writes through a box land
in its level's array; copy=False still shares VTK's arrays box by box. The
AMR tests build their hierarchies through it (amr_of), and check the form.
statistics of an AMRDataSet reduces each level's boxes on a device as one
array: values, marks and weights joined by tack.arrays._joined (the level's
own array where the boxes are its views, else a copy), the owned entries
taken once (ghosts.owned_ids, factored out of owned_values), one
_array_moments, the parts gathered and merged as pieces' are. An image
level's volumes are each box's spacing product spread by segment_broadcast.
A box's device is read from its marks, since DataSet.device searches the
parts. On 2,592 boxes (10.6M cells, CPU): 0.68 to 0.05 s with volume weights,
0.37 to 0.03 s without.
A hierarchy's marks and fields are one array per device, levels in order
and each box's a view (_with_marks, in_device_buffers, renamed from
in_level_buffers), so a level's boxes and a device's boxes of any run of
levels are one stretch of it. _Fill.run fills such a group in place
(tack.arrays._joined_view, which grants room for staged sources only at
the buffer's end) instead of concatenating and copying back, and uploads
its index tables once. ghost_cells allocates each group's grown arrays as
one buffer with that room (_GroupArrays), so update_ghosts fills in place;
average_down weighs and divides a group's cells in one launch each. On
2,592 boxes (CPU): average_down 0.58 to 0.37 s, update_ghosts 0.01 s.
The constructor's marks were made on the host box by box and uploaded. The
pairs of boxes that meet are now found on the device (tack.dataset._box_pairs:
bins of a lattice, each pair kept in the bin of its meeting's low corner), each
pair's part a row of a table, and one launch a kind of mark and level sets the
bit. The overlap and nesting checks use the same pairs. Boxes are copied by
DataSet._with_ghosts, which checks only the marks.

AMR-Wind's five levels (15,598 boxes, CPU): 4.0 s to 0.8 s, the marks bit for
bit the same.
vtk_to_dataset of a vtkOverlappingAMR uploaded each box's fields and ghost
marks as arrays of their own, then concatenated them (in_device_buffers) and
copied the marks again box by box (AMRDataSet's _kept). A _Staging now
allocates one array a field and a kind of mark and copies each box's VTK array
into its view once.

Also per box: AMR boxes read no direction matrix; an unoriented image shares one
identity matrix on its device (kept on the backend) instead of uploading one;
DataSet._device reads its fields' entries instead of looking each up by name;
Space.__new__ interns through a plain dict of weak references instead of making
a WeakValueDictionary (once a call through setdefault, 20 us each).

AMR-Wind's five levels (15,598 boxes, CPU): 4.5 s to 2.9 s, the marks bit for
bit the same.
The views of consecutive row ranges of one array (an AMR hierarchy's boxes'
fields and marks) made in one pass, the view class and the root's fields worked
out once: 2.2 us a view against 2.7 slicing one at a time (CPU). Used by the
AMR marks, in_device_buffers and the VTK AMR staging. Tested against slicing,
attribute for attribute, on the CPU and Warp.
ghost_cells of an AMRDataSet made each grown box's marks on the host:
its own marks placed into the grown extent, the ghosts marked duplicate,
refined where _refined_boxes (NumPy bins, _meeting) found a finer box,
hidden where the fills found no source. Now _grown_ghosts makes them on
the device, one array a kind and group with each box a view, as the
constructor lays them out: _grown_own_marks (a thread a row) places the
own marks and marks the ghosts, the refined bits come from the
constructor's box pairs and _mark against the grown extents, and
_set_bits sets the hidden ones. _refined, _refined_boxes, _meeting,
_bin_entries, _cut and _slices are gone.

AMR-Wind, five levels, CPU: the marks 2.2 to 0.47 s, ghost_cells 11.5
to 9.0 s, the marks bit for bit the same.
ghost_cells and average_down found each ghost's sources with kernels,
then read them back, masked, split and assembled them in NumPy, and
_Fill uploaded them again. Now each level's sources go into views of
arrays for the whole group and are compacted there (_one_source,
_counted: flags, scans, a scatter), the hidden entries as group places.
_Fill keeps the rows on the device (_Part): picks by _heaviest, each
floating type's weights by _weights_of from integer numerators over one
denominator a group, so they round as the f64 host weights did. Only
the sources other groups hold come to the host, to make the plan.

AMR-Wind, five levels, CPU: _ghost_parts 4.9 to 2.7 s, _Fill 1.6 to
0.07 s, ghost_cells 9.0 to 5.8 s, values and marks bit for bit the same.
A new test runs ghost_cells, update_ghosts and average_down with every
other box on a second device against one device.
ghost_cells placed each box's own values in its grown array by one
launch a box (_placed). Where a device's boxes hold a field as views of
one array, as vtk_to_dataset and in_device_buffers lay them out,
_GroupArrays.placed copies every box's own rows into the group's grown
buffer at once (_place_rows, a thread a row) and hands out its views;
anything else goes box by box as before. The point source kernel's
counting pass no longer looks up the coarse corners' owners, which only
the writing pass reads.

AMR-Wind, five levels, CPU: placing 1.3 to 0.09 s, ghost_cells 5.8 to
4.4 s, values and marks bit for bit the same.
_ghost_cells_of and _ghost_points_of took a thread an entry of the boxes'
shells: each decoded its box by a binary search and its index by
divisions, then searched the bins for the lowest sibling holding it.
Now a thread takes a row along x (_row_of; rows sized by _shell_rows and
scanned into each row's first entry, _shell_layout, so the entries keep
their order), finds the siblings meeting the row once (row_boxes, in id
order, into a local array) and gives each entry the first holding it
(pick). A row meeting more boxes than it keeps (_row_most, a launch
argument) searches entry by entry, as before; a test keeps none and
compares.

AMR-Wind, five levels, CPU: the kernels 2.3 to 0.7 s, ghost_cells 4.4
to 2.9 s; the sources the same entry for entry (637M), the grown values
and marks bit for bit.
…e#336)

An AMR hierarchy's boxes are made by the thousand, and each paid the
DataSet constructor's checks of parts its maker had just made
consistent, about 12 us a box. DataSet._assembled sets what the
constructor sets and checks nothing; ghost_cells' grown boxes and
vtk_to_dataset's staged AMR boxes use it. A test holds an assembled
dataset to a constructed one attribute for attribute, another the
interop's staged boxes to boxes converted alone (copy=False).

What every dataset, field and image pays is less too: units.canonical
keeps each unit's spelling (unyt printed it through sympy every time,
about 5 us a field); as_origin and UniformCoordinates' origin and
spacing take plain numbers without NumPy; unoriented images share one
read-only identity matrix; StructuredTopology counts with math.prod.
_grown_box works in plain numbers.

AMR-Wind, five levels, CPU, VTK master: vtk_to_dataset 2.1 to 1.9 s,
ghost_cells 2.8 to 2.2 s (its boxes 0.98 to 0.53 s), values and marks
bit for bit the same. The garbage collector is about 0.4 s of the
conversion, left as it is.
_amr_to_vtk exported each box with dataset_to_vtk(box), the default,
so every field was copied however copy was given; a dict of datasets,
and the collection of levels a hierarchy VTK's AMR cannot hold becomes,
dropped it too. copy is passed through all three now, so each block's
arrays point at the box's own values: views of one array a field, at
their offsets in it. A test holds the blocks' pointers to the views'
places for a vtkOverlappingAMR and a collection, and copy=True to copies.
A vtkOverlappingAMR whose blocks' arrays lie end to end in memory -- a
field read into one array, each block a view of it, as
dataset_to_vtk(amr, copy=False) and a NumPy reader lay them out -- was
converted box by box, each box's values wrapped alone. _SharedSpans,
copy=False's counterpart of _Staging, finds such a field from each
block's address (about 1 us a block) and shares it as one CPU array
over all of it, each box's values a view: the single-array form, with
no copy. _Span holds every block's VTK array, so abutting arrays keep
every byte alive however they were allocated. Anything else, and the
ghost marks, is shared box by box as before.

AMR-Wind through the Python AMReX reader: every box's temperature a
view of the reader's own array; converting 1.5 s sharing against 1.9 s
copying. The test helper _is_view_of_one returns False for arrays that
are not views instead of raising.
…are#336)

DataSet._assembled skipped the dataset's checks, but every box still
built its parts through their constructors: the topology, the uniform
coordinates, each field, the ghost marks, each checking what its
maker had just made consistent. StructuredTopology, UniformCoordinates,
Field and Ghosts have an _assembled each now, setting their attributes
in their constructors' order (a template's attribute layout is learned
per order of names); _grown_box, _grown_ghosts and _with_marks use them,
and _with_ghosts copies without copy.copy and can skip the marks' check.
A space made without parameters caches its parameters on its class
(Space._plain), since at order 1 its defaults never depend on the mesh.

vtk_to_dataset's staged AMR boxes take a fast path for vtkImageData
(_ImageBoxes.box), falling back to the box-by-box conversion for
anything else. Information keys are read through the key's own
Has/Get, about 0.1 us against vtkInformation's 3 us, which resolves
its overloads over every kind of key; the time key is found once.

A test holds each assembled part to a constructed one attribute for
attribute in 1D, 2D and 3D; the staged-boxes test now runs copying and
sharing over three hierarchies and checks every box took the fast path.

AMR-Wind, 15,598 boxes, CPU, VTK master: vtk_to_dataset(copy=False)
1.65 to 0.90 s, copying 1.9 to 1.25 s, ghost_cells 2.2 to 2.0 s. The
garbage collector is now most of what a box costs (0.90 against 0.64 s
with it off); turning it off during a filter only defers that cost to
the next collections, so it stays on.
_Groups looked up each box's device through DataSet.device, which
searches every part of the box, computed each box's sizes with NumPy
on arrays of three, and found each group's members and each level's
boxes by scanning lists: 123 ms of AMR-Wind's 15,598 boxes. A box's
device now comes from its marks (_box_device, factored out of
amr._by_device), sizes are computed a level at a time, and members and
levels come from index arrays: 10 ms. ghost_cells 2.04 to 1.98 s;
average_down, which groups each pair of levels, gains the same.
Grown values and marks bit for bit the same.
berkgeveci and others added 7 commits November 25, 2026 11:47
…n f64

A level reduced as one array summed its values with Array.sum(), which on a
GPU adds an f32 array with f32 partial sums. On AMR-Wind's 15,598 boxes
(183M cells) on Metal, the volume came out 771,425 off (3e-4), the mean
291.3669 against 291.4063 and the variance 0.7% off; box by box, the boxes'
moments had been merged in f64. Now every sum the moments take (_sum) is,
on a GPU, one thread's compensated sum of 256 values each, added in f64 on
the host: 56 off the exact volume, the mean and variance the CPU's, in
0.31 s warm (111 s box by box on Metal; the CPU 5.0 to 0.54 s). Kahan's
compensation matters: the cells of a level have one volume, and adding
equal f32 values in order rounds the same way every time (5,880 off).

test_amr: statistics of 4M f32 values on every device against NumPy's f64
(fails on Metal before), and _partial_sums on equal values (fails without
the compensation, on the CPU too).
@spyridon97

Copy link
Copy Markdown

@spyridon97, thanks for the three fixes. They look right to me. With them, test_amr.py, test_array_views.py and test_ghost_cells.py pass here, both plain and with TACK_DEBUG=1.

The red CI isn't from your commits. The branch was last merged with main on 11-15, and CI tests it merged with today's main. Since then, #471 (field kinds) added a _kind attribute that Field.__init__ sets. This branch has a bulk constructor, Field._assembled, which skips __init__, so the fields it makes have no _kind. Every failing job I checked shows AttributeError: 'Field' object has no attribute '_kind'. The branch's own test that compares _assembled with __init__ (test_amr.py, assert list(vars(assembled)) == list(vars(made))) catches it too.

Two jobs also fail test_vtk_algorithms.py::test_a_filter_that_makes_a_dataset_gives_a_partitioned_dataset_per_level (assert 0 == 2). That is most likely the same error surfacing further downstream, but I'll confirm once main is merged.

The fix is to merge main into the branch and have _assembled set _kind the way __init__ does.

i am on it, i am handling the rebase

@spyridon97

Copy link
Copy Markdown

Issue for further improvements

#487

Ci is green.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AMR marks on the device AMR volume-weighted statistics: batch the boxes

2 participants