Skip to content

Commit c9070a0

Browse files
committed
Fix and test allocations in Rosenbrock
1 parent b80bd3b commit c9070a0

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,7 @@ end
12401240
linsolve_tmp = @.. du + dtd[1] * dT
12411241
k1 = _reshape(W \ -_vec(linsolve_tmp), axes(uprev))
12421242
# constant number for type stability make sure this is greater than num_stages
1243-
ks = ntuple(Returns(k1), 20)
1243+
ks = ntuple(Returns(k1), Val(20))
12441244
# Loop for stages
12451245
for stage in 2:num_stages
12461246
u = uprev

lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function _ode_addsteps!(k, t, uprev, u, dt, f, p, cache::RosenbrockCombinedConst
8585
linsolve_tmp = @.. du + dtd[1] * dT
8686
k1 = _reshape(W \ _vec(linsolve_tmp), axes(uprev))
8787
# constant number for type stability make sure this is greater than num_stages
88-
ks = ntuple(Returns(k1), 20)
88+
ks = ntuple(Returns(k1), Val(20))
8989
# Last stage affect's ks for Rodas5,5P,6P
9090
for stage in 2:num_stages
9191
u = uprev

lib/OrdinaryDiffEqRosenbrock/test/allocation_tests.jl

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,39 @@ Currently, Rosenbrock solvers are allocating and marked with @test_broken.
1212
@testset "Rosenbrock Allocation Tests" begin
1313
# Test problem - use a simple linear problem for stiff solvers
1414
linear_prob = ODEProblem((u, p, t) -> -u, 1.0, (0.0, 1.0))
15-
15+
1616
# Vector problem
1717
function simple_system!(du, u, p, t)
1818
du[1] = -0.5 * u[1]
1919
du[2] = -1.5 * u[2]
2020
end
2121
vector_prob = ODEProblem(simple_system!, [1.0, 1.0], (0.0, 1.0))
22-
23-
# Test all exported Rosenbrock solvers for allocation-free behavior
22+
23+
# Test all exported Rosenbrock solvers for allocation-free behavior
2424
rosenbrock_solvers = [Rosenbrock23(), Rosenbrock32(), RosShamp4(), Veldd4(), Velds4(), GRK4T(), GRK4A(),
2525
Rodas3(), Rodas23W(), Rodas3P(), Rodas4(), Rodas42(), Rodas4P(), Rodas4P2(), Rodas5(),
2626
Rodas5P(), Rodas5Pe(), Rodas5Pr(), Rodas6P()]
27-
27+
2828
@testset "Rosenbrock Solver Allocation Analysis" begin
2929
for solver in rosenbrock_solvers
3030
@testset "$(typeof(solver)) allocation check" begin
3131
integrator = init(linear_prob, solver, dt=0.1, save_everystep=false, abstol=1e-6, reltol=1e-6)
3232
step!(integrator) # Setup step may allocate
33-
33+
3434
# Use AllocCheck for accurate allocation detection
3535
allocs = check_allocs(step!, (typeof(integrator),))
36-
36+
3737
# These solvers should be allocation-free, but mark as broken for now
3838
# to verify with AllocCheck (more accurate than @allocated)
3939
@test_broken length(allocs) == 0
40-
40+
41+
# However, we don't want to allow any dynamic dispatch from this module
42+
@test count(
43+
t -> t isa AllocCheck.DynamicDispatch &&
44+
any(s -> contains(string(s.file), "Rosenbrock"), t.backtrace),
45+
allocs
46+
) == 0
47+
4148
if length(allocs) > 0
4249
println("AllocCheck found $(length(allocs)) allocation sites in $(typeof(solver)) step!:")
4350
for (i, alloc) in enumerate(allocs[1:min(3, end)]) # Show first 3

0 commit comments

Comments
 (0)