Skip to content

Make operators and assembly scratch element-type generic - #71

Merged
xtalax merged 3 commits into
masterfrom
feat/generic-eltype-operators
Nov 6, 2026
Merged

xtalax merged 3 commits into
masterfrom
feat/generic-eltype-operators

Conversation

@xtalax-droid

Copy link
Copy Markdown
Collaborator

Motivation

This is phase 0a of GPU support: CPU-only refactors that merge on their own, with no dependency on unreleased Ferrite. The operator and assembly structures were hard-wired to Float64:

  • FEMOp.A::SparseMatrixCSC{Float64,Int}
  • FEMNonlinear.p::Vector{Float64}
  • the Vector{Float64} buffers of NonlinearOperator
  • the zeros(ne, ne) element scratch
  • check_state, which accepted only Float64 states and Duals of Float64

A device assembly path needs to reuse these structures with other element types. On its own, this change also makes Float32 assembly and Float32 residual evaluation work on the CPU. Float64 behaviour does not change.

Change

  • The scratch in assemble_linear!, assemble_nonlinear! and assemble_sources! takes the element type of the matrix or vector it is assembled into. Me follows M, Ke follows K, Fe follows F, Je follows J, and re and u_elem follow r. LinearOp and NonlinearOp accept any AbstractFloat buffers, with separate types for the mass and stiffness scratch.
  • NonlinearOperator{…,T}, FEMOp{T} and FEMNonlinear{T} carry their element type. The existing constructors still produce Float64, and FEMOp(A) still converts other matrices to SparseMatrixCSC{Float64,Int}.
  • fem_system declares Mop, Kop and Nop with the concrete FEMOp{T} and FEMNonlinear{T} types, because MTKParameters rejects a UnionAll type for a non-numeric parameter.
  • check_state and the nonlinear Jacobian accept any AbstractFloat state and first-order ForwardDiff.Duals of one. Nested Duals are still rejected. The error messages change from "Float64" to "AbstractFloat" wording.
  • No precision or backend keyword is added; those wait for the Ferrite GPU release.

Tests

test/Assembly/float32.jl (new):

  • It assembles M, K and F from generated heat kernels into Float32 storage, on a Float32 grid with Float32 CellValues and parameters, and checks them against Float64 to a relative error of 1e-5. The Float64 path keeps its types.
  • It assembles M and K with different element types (Float64/Float32 and the reverse), using a mass coefficient that overflows Float32. Each matrix is accumulated in its own type.
  • It evaluates the Float32 nonlinear residual and Jacobian through assemble_nonlinear!, and through a NonlinearOperator with Float32 buffers via residual! and jacobian!.

Frontend nonlinear testset (rewritten, end to end):

  • It discretizes a PDESystem and evaluates the DAE residual and Jacobian at Float32, BigFloat, Dual{Float64} and Dual{Float32} states against Float64, checking both values and partials. Nested Duals are still rejected with the new message.
  • It covers the relaxed check_state and Jacobian check, the Dual path for any AbstractFloat, and the Float32 time-dependent load through assemble_sources!.

Results:

  • Groups: Perf 28/28, Assembly 92/92, DAE 91/91, Kernels 345/345, and all Frontend files pass.
  • A linear and a nonlinear problem were compared against master. The hashes of M, K, u0, du0, the residual, the Jacobian and the final solution are identical, and so are the step counts.
  • The docs build (doctests, checkdocs) passes, and Runic 1.13 is clean.

Performance

The Perf group's per-cell allocation gates pass. Allocations per pass do not change with mesh size: 16128 bytes linear and 10144 bytes nonlinear, at 20 and at 100 cells.

For reviewers

  • fem_system builds the parameter types at run time (FEMOp{eltype(ops.K)}, typeof(ops.nonlinear)). Today FEMOperators forces Mop and Kop to Float64.
  • remake(prob; p = [sys.Kop => FEMOp(K32)]) with a Float32 matrix now gives a FEMOp{Float32}, which does not match the declared FEMOp{Float64}. Before this change it was silently converted. No test or caller does this.
  • A sibling PR separates the per-cell element routines in src/assembly.jl. The overlap is small; whichever merges second merges master.

🤖 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 3 commits November 5, 2026 20:07
The element scratch of assemble_linear!, assemble_nonlinear! and assemble_sources! now takes the element type of K, J, F and r instead of Float64, LinearOp and NonlinearOp accept any AbstractFloat buffers, and NonlinearOperator carries its buffer element type as a parameter. Float64 callers get the same types and values as before. This lets Float32 assembly run on the CPU and is the first enabler of GPU assembly (design phase 0a). test/Assembly/float32.jl assembles M, K, F and the nonlinear residual and Jacobian of generated heat kernels in Float32 against Float64.

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>
FEMOp and FEMNonlinear take their element type as a parameter, check_state and the Jacobian accept any AbstractFloat state and first-order Duals of one, and fem_system declares the concrete FEMOp{T} and FEMNonlinear{T} as parameter types, since MTKParameters rejects a UnionAll symtype for a non-numeric parameter. Float64 problems are unchanged bit for bit. The frontend nonlinear test now evaluates the discretized residual and Jacobian at Float32, BigFloat and Dual states against Float64, and still rejects nested Duals.

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>
assemble_linear! allocated both element matrices in K's element type, so a Float64 M assembled next to a Float32 K was accumulated in Float32 and could overflow. LinearOp now has separate type parameters for Me and Ke, and Me takes eltype(M). The new testset assembles Float64/Float32 and Float32/Float64 pairs of M and K with a mass coefficient that overflows Float32, and checks that each matrix is accumulated in its own type.

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 705cd1b... master / 705cd1b...
construction/heat P1 10x10 7.44 ± 0.39 ms 7.6 ± 0.51 ms 0.979 ± 0.083
construction/heat P1 20x20 9.87 ± 0.3 ms 10.6 ± 0.35 ms 0.93 ± 0.041
time_to_load 5.43 ± 0.018 s 5.56 ± 0.003 s 0.976 ± 0.0033
Memory benchmarks
master 705cd1b... master / 705cd1b...
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 d3836b9 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