Repository navigation
Make operators and assembly scratch element-type generic - #71
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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}Vector{Float64}buffers ofNonlinearOperatorzeros(ne, ne)element scratchcheck_state, which accepted only Float64 states and Duals of Float64A 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
assemble_linear!,assemble_nonlinear!andassemble_sources!takes the element type of the matrix or vector it is assembled into.MefollowsM,KefollowsK,FefollowsF,JefollowsJ, andreandu_elemfollowr.LinearOpandNonlinearOpaccept anyAbstractFloatbuffers, with separate types for the mass and stiffness scratch.NonlinearOperator{…,T},FEMOp{T}andFEMNonlinear{T}carry their element type. The existing constructors still produce Float64, andFEMOp(A)still converts other matrices toSparseMatrixCSC{Float64,Int}.fem_systemdeclaresMop,KopandNopwith the concreteFEMOp{T}andFEMNonlinear{T}types, because MTKParameters rejects a UnionAll type for a non-numeric parameter.check_stateand the nonlinear Jacobian accept anyAbstractFloatstate and first-orderForwardDiff.Duals of one. Nested Duals are still rejected. The error messages change from "Float64" to "AbstractFloat" wording.precisionorbackendkeyword is added; those wait for the Ferrite GPU release.Tests
test/Assembly/float32.jl(new):CellValuesand parameters, and checks them against Float64 to a relative error of 1e-5. The Float64 path keeps its types.assemble_nonlinear!, and through aNonlinearOperatorwith Float32 buffers viaresidual!andjacobian!.Frontend nonlinear testset (rewritten, end to end):
Dual{Float64}andDual{Float32}states against Float64, checking both values and partials. Nested Duals are still rejected with the new message.check_stateand Jacobian check, the Dual path for anyAbstractFloat, and the Float32 time-dependent load throughassemble_sources!.Results:
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_systembuilds the parameter types at run time (FEMOp{eltype(ops.K)},typeof(ops.nonlinear)). TodayFEMOperatorsforcesMopandKopto Float64.remake(prob; p = [sys.Kop => FEMOp(K32)])with a Float32 matrix now gives aFEMOp{Float32}, which does not match the declaredFEMOp{Float64}. Before this change it was silently converted. No test or caller does this.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