Skip to content

Add spatially varying boundary data to DenseDistributed (builds on #78) - #79

Draft
rpep wants to merge 10 commits into
Autodesk:carbonfrom
rpep:feature/dense-distributed-bc-varying
Draft

rpep wants to merge 10 commits into
Autodesk:carbonfrom
rpep:feature/dense-distributed-bc-varying

Conversation

@rpep

@rpep rpep commented Nov 10, 2026

Copy link
Copy Markdown

Based on #78 (#78, per-face constant Dirichlet and Neumann conditions). This branch contains #78's commits as well, so until #78 merges the diff shows both. The commits that belong to this PR are the last five:

  • 4b88755 Add boundary views, host store and a view-aware neighbour_bc
  • c3c5ba2 Add the BoundaryData handle
  • e7c3d5e Document spatially varying boundary data
  • 7bd3366 Add the BoundaryData device test
  • 980bddb Add the offset_current_terminals example

Summary

#78 gives each face of a DenseDistributed grid one boundary condition: one kind and one constant. This adds BoundaryData<Components>, a separate object holding the condition (Dirichlet or Neumann, and its value) of every cell of every exterior face, per component. A kernel takes bnd.view(loader.relative_gpu_id()) in its loading lambda and calls neighbour_bc(span, view, idx, offset, comp) in the device lambda. The formulas are #78's (Dirichlet 2g - u_m, Neumann u_m + 2khg), but the condition is read at the face cell under the target of the read. It is opt-in: the loader, access tokens, Skeleton and #78's code are untouched.

What it enables

  • Non-zero flux over part of a face, for example current injected through a terminal on an otherwise insulating face. One constant per face cannot say this at any stencil radius.
  • Different conditions per component or per field on one grid: free-slip walls (Dirichlet normal velocity, Neumann tangential), or a potential and a conductivity that need different conditions.
  • Mixed Dirichlet and Neumann on one face (an electrode in an insulator, an inlet in a wall), declared once instead of hand-coded. BoundaryView::dirichlet_at tells a solver which nodes to pin.

One thing it does not add: for a radius-1 stencil, a Dirichlet disc on an otherwise zero-flux face could already be done with #78 by setting the face to Neumann and pinning the nodes under the disc. I checked this on a pole-piece problem and the two results are identical (difference 0.0). The gain there is declaring it once.

Example: offset current terminals

examples/offset_current_terminals.cu solves steady current flow in a conducting box. Current enters through a disc terminal on the top face and leaves through an offset one on the bottom face, with a resistive sphere between; everything else is insulating. The flux differs from cell to cell on one face, so it is declared with BoundaryData and read through neighbour_bc in a Jacobi sweep run by a Skeleton. The conductivity field gets its own BoundaryData (zero flux on every face), so two fields on one grid carry different conditions.

Offset current terminals

The example also solves the same total current spread evenly over each face, which is all one constant per face can say, and compares the two. All 45 units of injected current cross the plane midway between the terminals (44.98 computed) while the even spread carries none sideways, and the potential swing is about 4x larger (3.9 against 0.9). The program exits non-zero unless the iteration has converged and the current matches to 1%; flipping the Neumann sign reverses the current and fails it. The figure is plotted from the example's --raw output.

Status

Tested: a new CPU-only test (21.4M checks, emulated slab/pencil/brick ranks, radius 1 and 2) covers constant data matching #78, Dirichlet rectangle and disc patches, per-component kinds, diagonal reads across rank boundaries, stored ranges and coverage, and the pinning helper; four deliberate mutations each make it fail. A device test running a Skeleton against an independent oracle passes (1,728 cells), and the example passes its own checks (25^3 box, 4000 sweeps), both on the SYCL backend with the OpenMP host as device; each fails when the Neumann sign is flipped. The #78 test and the existing host test give identical results.

Not yet verified: the CUDA build, real GPUs, multi-GPU and MPI runs.

Limits: conditions must stay constant while a Skeleton using them runs. Reads across two faces at once (edges, corners) stay invalid. fill runs on the host.

rpep added 10 commits November 10, 2026 10:52
New carbon/boundary_condition_distributed.h: BoundaryConditions (six faces,
each BcKind::{None, Dirichlet, Neumann} plus one constant scalar; Neumann is
the outward-normal derivative). Trivially copyable and constexpr so it can
ride inside the by-value kernel Span. Header only and not yet used anywhere,
so behaviour is unchanged.
Span carries a BoundaryConditions block (default none) and gains
neighbour_idx_bc, a sibling of neighbour_idx that checks the global domain
before the buffer, because a single-rank open axis has no halo storage. A read
across a face with a condition resolves to the in-domain mirror cell, and
Partition::neighbour_bc turns it into a virtual boundary value (node-centred:
Dirichlet 2g - u_m, Neumann u_m + 2*k*h*g). Edge and corner reads and faces
without a condition stay invalid.

Opt-in: neighbour, read_pull and Cursor are unchanged. Span now static_asserts
that it stays trivially copyable.

test_dense_distributed_bc_host (no GPU, no MPI) checks that neighbour_bc equals
neighbour when no conditions are set, matches an independent per-cell oracle on
emulated slab / pencil / brick worlds at radius 1 and 2 including mixed
periodic axes, reproduces linear fields exactly, and keeps the documented limits.
Appends a defaulted BoundaryConditions parameter after block_override on all
three constructors (source compatible), stores it, exposes
boundary_conditions(), and passes it into every per-GPU Span. init_common_
rejects a condition on a periodic axis, on an axis whose extent is not larger
than the stencil radius there, and a non-finite value. The grid stays
immutable after construction. halo_extent, the layouts and HaloDistributed
are not touched.
AGENTS.md layout list, a dated changelog entry, and a doxygen pointer from
BoundaryModeDistributed to the new type.
BoundaryView holds, per GPU and per global face, the condition (kind and value, per
component) of every stored face cell as raw pointers, so one trivially copyable struct
serves host and device. resolve() mirrors neighbour_idx_bc but looks the condition up at
the face cell under the target of the read; dirichlet_at() is the pinning query.
HostBoundaryStore holds the arrays on the host and decides which part of each face a GPU
stores: its owned tangential range widened by the stencil radius, clipped to the domain,
nothing for faces it does not touch or on periodic axes. Partition::neighbour_bc gains a
templated overload taking a view; the existing overload is unchanged.

test_boundary_data_host (no GPU, no MPI) checks equivalence with the constants path, patches
and per-component kinds against an independent oracle including diagonal reads, stored
ranges and coverage, the pinning helper and validation.
BoundaryData<Components> owns the per-GPU storage for spatially varying face conditions:
fill() from a deterministic function of global position (or from the per-face constants),
upload() through Transfer, view(gpu) for device kernels and host_view(gpu) for host use. It
needs no access token and no loader change; the data must stay constant while a Skeleton
using it runs.
Runs a six-point neighbour-sum kernel through a Skeleton, reading every neighbour with
neighbour_bc and a BoundaryData view, over a grid with a Dirichlet rectangle, a Dirichlet disc,
Neumann regions and unconditioned faces, and compares every owned cell with an independent
host oracle. It requires boundary reads and invalid reads to both occur, so it cannot pass
by taking only one path.
Current enters a conducting box through a disc terminal on the top face and leaves through an
offset one on the bottom face, insulating elsewhere, with a resistive sphere between. The
flux differs from cell to cell on one face, so it is declared with BoundaryData and read
through neighbour_bc with a view in a Jacobi sweep run by a Skeleton; a second BoundaryData
gives the conductivity zero flux on every face.

It solves the declared terminals and the same current spread evenly over each face, writes a
two-panel PPM of the potential, and fails unless the iteration has converged and the current
crossing the plane midway between the terminals matches the injected current to 1%.
@rpep rpep changed the title Spatially varying boundary data for DenseDistributed (based on #78) Add spatially varying boundary data to DenseDistributed (builds on #78) Nov 10, 2026

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.

1 participant