Skip to content

Commit 6e2d92b

Browse files
Simplify tolerance checking by modifying err in-place
Much cleaner approach: instead of complex temp array management, simply modify the error vector in-place for vector abstol: @inline function check_dae_tolerance(integrator, err, abstol, t) if abstol isa Number return integrator.opts.internalnorm(err, t) / abstol <= 1 else @. err = err / abstol # In-place, zero allocation return integrator.opts.internalnorm(err, t) <= 1 end end This is much simpler and cleaner than the previous complex optimizations while still achieving zero allocations since err is typically a temp array. Benefits: - Zero allocations for both scalar and vector abstol - Much simpler, more maintainable code - Consistent interface across all usage sites - Leverages the fact that err arrays are temporary 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 7b6701e commit 6e2d92b

File tree

1 file changed

+5
-16
lines changed

1 file changed

+5
-16
lines changed

lib/OrdinaryDiffEqNonlinearSolve/src/initialize_dae.jl

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
# Optimized tolerance checking that avoids allocations for scalar abstol
1+
# Optimized tolerance checking that avoids allocations
22
@inline function check_dae_tolerance(integrator, err, abstol, t)
33
if abstol isa Number
44
return integrator.opts.internalnorm(err, t) / abstol <= 1
55
else
6-
return integrator.opts.internalnorm(err ./ abstol, t) <= 1
6+
@. err = err / abstol
7+
return integrator.opts.internalnorm(err, t) <= 1
78
end
89
end
910

@@ -66,13 +67,7 @@ function _initialize_dae!(integrator, prob::ODEProblem, alg::ShampineCollocation
6667
f(tmp, u0, p, t)
6768
tmp .= ArrayInterface.restructure(tmp, algebraic_eqs .* _vec(tmp))
6869

69-
# Zero-allocation tolerance check reusing tmp
70-
if integrator.opts.abstol isa Number
71-
integrator.opts.internalnorm(tmp, t) / integrator.opts.abstol <= 1 && return
72-
else
73-
@. tmp = tmp / integrator.opts.abstol
74-
integrator.opts.internalnorm(tmp, t) <= 1 && return
75-
end
70+
check_dae_tolerance(integrator, tmp, integrator.opts.abstol, t) && return
7671

7772
if isdefined(integrator.cache, :nlsolver) && !isnothing(alg.nlsolve)
7873
# backward Euler
@@ -400,13 +395,7 @@ function _initialize_dae!(integrator, prob::ODEProblem,
400395

401396
tmp .= ArrayInterface.restructure(tmp, algebraic_eqs .* _vec(tmp))
402397

403-
# Zero-allocation tolerance check reusing tmp
404-
if alg.abstol isa Number
405-
integrator.opts.internalnorm(tmp, t) / alg.abstol <= 1 && return
406-
else
407-
@. tmp = tmp / alg.abstol
408-
integrator.opts.internalnorm(tmp, t) <= 1 && return
409-
end
398+
check_dae_tolerance(integrator, tmp, alg.abstol, t) && return
410399
alg_u = @view u[algebraic_vars]
411400

412401
# These non-dual values are thus used to make the caches

0 commit comments

Comments
 (0)