Skip to content

Commit d5f757c

Browse files
committed
reduce allocations in the cost function by avoiding vectors of vectors
1 parent 91e5591 commit d5f757c

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

lib/BoundaryValueDiffEqCore/src/internal_problems.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
l_params = length(tunable_part)
1010
length_u = M - l_params
1111
cost_fun = @views function (u, p_orig)
12-
newy = [u[i:(i + M - 1)] for i in 1:M:(length(u) - M + 1)]
12+
newy = eachcol(reshape(u, M, :))
1313
# Extract tunable params from first mesh point (same at all points)
1414
params_from_u = u[(length_u + 1):M]
1515
new_p = SciMLStructures.replace(SciMLStructures.Tunable(), p_orig, params_from_u)
@@ -20,15 +20,15 @@
2020
length_u = M - length(p)
2121
cost_fun = @views function (u, p)
2222
# When fit_parameters=true, the state vector is augmented with tunable params
23-
newy = [u[i:(i + M - 1)] for i in 1:M:(length(u) - M + 1)]
23+
newy = eachcol(reshape(u, M, :))
2424
params_from_u = u[(length_u + 1):M]
2525
eval_sol = EvalSol(newy, mesh, cache)
2626
return fun(eval_sol, params_from_u)
2727
end
2828
else
2929
cost_fun = @views function (u, p)
3030
# simple recursive unflatten
31-
newy = [u[i:(i + M - 1)] for i in 1:M:(length(u) - M + 1)]
31+
newy = eachcol(reshape(u, M, :))
3232
eval_sol = EvalSol(newy, mesh, cache)
3333
return fun(eval_sol, p)
3434
end

0 commit comments

Comments
 (0)