Skip to content

Separate the per-cell element routines from the op structs - #72

Merged
xtalax merged 2 commits into
masterfrom
refactor/element-routine-buffers
Nov 6, 2026
Merged

xtalax merged 2 commits into
masterfrom
refactor/element-routine-buffers

Conversation

@xtalax-droid

Copy link
Copy Markdown
Collaborator

Motivation

This is phase 0 of GPU support: a CPU-only refactor that merges on its own, with no Ferrite dependency. GPU assembly will run the per-cell arithmetic inside a KernelAbstractions kernel, with each worker's element buffers as views into 3D device arrays. For the CPU driver and the device kernel to share the ~150 lines of contraction code, the element routine must not read or write LinearOp/NonlinearOp fields.

Change

  • _linear_element!(Me, Ke, Fe, k, Val(E), vals, xs, ranges, p, t) (for both LinearBlock and KernelSpec) and _nonlinear_element!(Je, re, u_elem, k, Val(E), vals, xs, ranges, p, t) take the element buffers and element state as arguments. These can be arrays or views.
  • Je = nothing assembles the residual only.
  • _kernel! is now a one-line adapter that passes the op's existing buffers. Volume and facet terms both go through it, so the driver, _start_cell!/_finish_cell! and the op structs are unchanged.
  • No behaviour change.

Tests

  • New test/Assembly/element.jl uses two coupled fields with cross-diffusion and a nonlinear reaction, on both the coeffs (LinearBlock) path and the f path.
  • Gathering the routines over every cell with caller-owned buffers reproduces assemble_linear and assemble_nonlinear exactly (==). This holds with one worker and with three workers using views into zeros(ne, ne, nw) slabs, for both the Jacobian and the residual-only path.
  • Calls on views into 3D buffers are allocation-free, checked statically with AllocCheck and at run time with @allocated == 0.
  • Groups: Perf 28/28 (including the no-per-cell-allocation gate), Assembly 113/113, Kernels 345/345, DAE 91/91, Frontend 771/771. The existing Frontend and Assembly end-to-end tests cover the driver path.

Performance

benchmarks/assembly_cost.jl cases, 100×100 P1, BenchmarkTools medians of fresh single-process runs alternating master and branch.

case runs per side master branch
(i) volume 2 4.656, 4.639 ms 4.594, 4.837 ms
(i) with facets 2 4.783, 4.951 ms 4.666, 4.850 ms
(iii) residual + Jacobian 5 3.288, 3.080, 3.075, 3.142, 3.112 ms (median 3.112) 3.059, 3.148, 3.055, 3.179, 3.073 ms (median 3.073)

Allocations per pass are identical: 129, 268 and 180. Fresh-process noise on this machine is about ±7%. The generated code of the hot nonlinear routine matches master's apart from where three buffer loads happen: same calls, no boxing or dynamic dispatch, and Je === nothing folds away. Marking the element routines @inline made no measurable difference (median 3.081 ms), so this PR does not add it.

This PR merges cleanly with #71 (element-type-generic operators), which also touches src/assembly.jl.


🤖 Posted by an AI agent · harness: Claude Code 2.1.318 · model: claude-opus-5-5
Review: unreviewed
Session: f7b46bdf-7cc3-44dd-b8a4-9205616ff6ac

t and others added 2 commits November 5, 2026 19:56
_linear_element! and _nonlinear_element! write the Me/Ke/Fe or Je/re buffers and read the element state they are passed, instead of the LinearOp/NonlinearOp fields, so the CPU driver and a future device kernel (with per-worker views into 3D arrays) call the same contraction code. _kernel! keeps its signature and forwards the op's buffers; residual-only assembly passes Je = nothing. No behaviour change.

Agent-Harness: Claude Code 2.1.318
Agent-Model: claude-opus-5-5
Agent-Session: f7b46bdf-7cc3-44dd-b8a4-9205616ff6ac
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
Gathering _linear_element! and _nonlinear_element! over every cell, with plain arrays and with views into 3D per-worker buffers, reproduces assemble_linear and assemble_nonlinear exactly for the coeffs and f paths, and the calls on views are allocation-free (AllocCheck and @allocated).

Agent-Harness: Claude Code 2.1.318
Agent-Model: claude-opus-5-5
Agent-Session: f7b46bdf-7cc3-44dd-b8a4-9205616ff6ac
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Benchmark Results (Julia v1)

Time benchmarks
master 4717e17... master / 4717e17...
construction/heat P1 10x10 3.77 ± 0.27 ms 3.62 ± 0.13 ms 1.04 ± 0.085
construction/heat P1 20x20 5.34 ± 0.33 ms 5.03 ± 0.097 ms 1.06 ± 0.069
time_to_load 3.6 ± 0.033 s 3.42 ± 0.017 s 1.05 ± 0.011
Memory benchmarks
master 4717e17... master / 4717e17...
construction/heat P1 10x10 0.0393 M allocs: 2.12 MB 0.0393 M allocs: 2.12 MB 1
construction/heat P1 20x20 0.0702 M allocs: 4.73 MB 0.0702 M allocs: 4.73 MB 1
time_to_load 0.2 k allocs: 11.8 kB 0.2 k allocs: 11.8 kB 1

@xtalax
xtalax merged commit e8e9757 into master Nov 6, 2026
51 checks passed
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.

2 participants