Skip to content

Commit 0e9d91d

Browse files
Avoid FIRK collocation views in AD residuals
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
1 parent fcc08ec commit 0e9d91d

1 file changed

Lines changed: 32 additions & 6 deletions

File tree

  • lib/BoundaryValueDiffEqFIRK/src

lib/BoundaryValueDiffEqFIRK/src/firk.jl

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,7 @@ end
14111411
cache, eval_sol, trait::NoDiffCacheNeeded, constraint
14121412
) where {BC}
14131413
y_ = recursive_unflatten!(y, u)
1414-
resids = [r for r in residual]
1414+
resids = residual
14151415
Φ!(resids[2:end], cache, y_, u, trait, constraint)
14161416
eval_sol.u[1:end] .= y_
14171417
eval_bc_residual!(resids[1], pt, bc!, eval_sol, p, mesh)
@@ -1508,23 +1508,49 @@ end
15081508
return eval_bc_residual(pt, bc!, eval_sol, p, mesh)
15091509
end
15101510

1511+
function __firk_recursive_unflatten!(y::Vector{<:AbstractArray}, x::AbstractVector)
1512+
i = firstindex(x)
1513+
for yᵢ in y
1514+
for j in eachindex(yᵢ)
1515+
yᵢ[j] = x[i]
1516+
i += 1
1517+
end
1518+
end
1519+
return y
1520+
end
1521+
1522+
function __firk_recursive_unflatten!(y::Vector{<:DiffCache}, x::AbstractVector)
1523+
return __firk_recursive_unflatten!(get_tmp.(y, (x,)), x)
1524+
end
1525+
1526+
function __firk_recursive_flatten!(y::AbstractVector, x)
1527+
i = firstindex(y)
1528+
for xᵢ in x
1529+
for j in eachindex(xᵢ)
1530+
y[i] = xᵢ[j]
1531+
i += 1
1532+
end
1533+
end
1534+
return y
1535+
end
1536+
15111537
@views function __firk_loss_collocation!(
15121538
resid, u, p, y, mesh, residual, cache, trait::DiffCacheNeeded, constraint
15131539
)
1514-
y_ = recursive_unflatten!(y, u)
1540+
y_ = __firk_recursive_unflatten!(y, u)
15151541
resids = [get_tmp(r, u) for r in residual[2:end]]
15161542
Φ!(resids, cache, y_, u, trait, constraint)
1517-
recursive_flatten!(resid, resids)
1543+
__firk_recursive_flatten!(resid, resids)
15181544
return nothing
15191545
end
15201546

15211547
@views function __firk_loss_collocation!(
15221548
resid, u, p, y, mesh, residual, cache, trait::NoDiffCacheNeeded, constraint
15231549
)
1524-
y_ = recursive_unflatten!(y, u)
1525-
resids = [r for r in residual[2:end]]
1550+
y_ = __firk_recursive_unflatten!(y, u)
1551+
resids = residual[2:end]
15261552
Φ!(resids, cache, y_, u, trait, constraint)
1527-
recursive_flatten!(resid, resids)
1553+
__firk_recursive_flatten!(resid, resids)
15281554
return nothing
15291555
end
15301556

0 commit comments

Comments
 (0)