Skip to content

Commit ebe407f

Browse files
Preserve FIRK EvalSol cache for compatible states
Keep the cached EvalSol fast path when the unflattened state can be copied safely, and rebuild it only for incompatible element types such as vector-of-vector ForwardDiff states. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
1 parent 4c603f4 commit ebe407f

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

  • lib/BoundaryValueDiffEqFIRK/src

lib/BoundaryValueDiffEqFIRK/src/firk.jl

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,14 @@ function __construct_problem(
705705
return __construct_problem(cache, y, loss_bc, loss_collocation, loss, pt, constraint)
706706
end
707707

708+
@inline function __firk_eval_sol!(eval_sol, y, mesh, cache)
709+
if eltype(first(y)) <: eltype(first(eval_sol.u))
710+
eval_sol.u[1:end] .= y
711+
return eval_sol
712+
end
713+
return EvalSol(__restructure_sol(y, cache.in_size), mesh, cache)
714+
end
715+
708716
function __construct_problem(
709717
cache::FIRKCacheExpand{iip, T, DC, tune_parameters}, y, loss_bc::BC, loss_collocation::C,
710718
loss::LF, ::StandardBVProblem, ::Val{true}
@@ -1395,25 +1403,25 @@ end
13951403

13961404
@views function __firk_loss!(
13971405
resid, u, p, y, pt::StandardBVProblem, bc!::BC, residual, mesh,
1398-
cache, _, trait::DiffCacheNeeded, constraint
1406+
cache, eval_sol, trait::DiffCacheNeeded, constraint
13991407
) where {BC}
14001408
y_ = recursive_unflatten!(y, u)
14011409
resids = [get_tmp(r, u) for r in residual]
14021410
Φ!(resids[2:end], cache, y_, u, trait, constraint)
1403-
eval_sol = EvalSol(__restructure_sol(y_, cache.in_size), mesh, cache)
1411+
eval_sol = __firk_eval_sol!(eval_sol, y_, mesh, cache)
14041412
eval_bc_residual!(resids[1], pt, bc!, eval_sol, p, mesh)
14051413
recursive_flatten!(resid, resids)
14061414
return nothing
14071415
end
14081416

14091417
@views function __firk_loss!(
14101418
resid, u, p, y, pt::StandardBVProblem, bc!::BC, residual, mesh,
1411-
cache, _, trait::NoDiffCacheNeeded, constraint
1419+
cache, eval_sol, trait::NoDiffCacheNeeded, constraint
14121420
) where {BC}
14131421
y_ = recursive_unflatten!(y, u)
14141422
resids = [r for r in residual]
14151423
Φ!(resids[2:end], cache, y_, u, trait, constraint)
1416-
eval_sol = EvalSol(__restructure_sol(y_, cache.in_size), mesh, cache)
1424+
eval_sol = __firk_eval_sol!(eval_sol, y_, mesh, cache)
14171425
eval_bc_residual!(resids[1], pt, bc!, eval_sol, p, mesh)
14181426
recursive_flatten!(resid, resids)
14191427
return nothing
@@ -1470,10 +1478,10 @@ end
14701478
end
14711479

14721480
@views function __firk_loss(
1473-
u, p, y, pt::StandardBVProblem, bc::BC, mesh, cache, _, trait
1481+
u, p, y, pt::StandardBVProblem, bc::BC, mesh, cache, eval_sol, trait
14741482
) where {BC}
14751483
y_ = recursive_unflatten!(y, u)
1476-
eval_sol = EvalSol(__restructure_sol(y_, cache.in_size), mesh, cache)
1484+
eval_sol = __firk_eval_sol!(eval_sol, y_, mesh, cache)
14771485
resid_bc = eval_bc_residual(pt, bc, eval_sol, p, mesh)
14781486
resid_co = Φ(cache, y_, u, trait)
14791487
return vcat(resid_bc, mapreduce(vec, vcat, resid_co))

0 commit comments

Comments
 (0)