Skip to content

Commit 0bb99f5

Browse files
MaxenceGollierdpo
authored andcommitted
solve allocation tests
1 parent 66bf1d5 commit 0bb99f5

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

src/R2_alg.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ For advanced usage, first define a solver "R2Solver" to preallocate the memory u
132132
solver = R2Solver(reg_nlp)
133133
solve!(solver, reg_nlp)
134134
135-
stats = GenericExecutionStats(reg_nlp.model)
135+
stats = GenericExecutionStats(reg_nlp)
136136
solver = R2Solver(reg_nlp)
137137
solve!(solver, reg_nlp, stats)
138138
@@ -292,7 +292,7 @@ function R2(reg_nlp::AbstractRegularizedNLPModel; kwargs...)
292292
kwargs_dict = Dict(kwargs...)
293293
max_iter = pop!(kwargs_dict, :max_iter, 10000)
294294
solver = R2Solver(reg_nlp, max_iter = max_iter)
295-
stats = GenericExecutionStats(reg_nlp.model)
295+
stats = GenericExecutionStats(reg_nlp.model) # TODO: change this to `stats = GenericExecutionStats(reg_nlp)` when FHist etc. is ruled out.
296296
cb = pop!(
297297
kwargs_dict,
298298
:callback,

src/utils.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
export GenericExecutionStats
2+
3+
import SolverCore.GenericExecutionStats
4+
15
# use Arpack to obtain largest eigenvalue in magnitude with a minimum of robustness
26
function LinearAlgebra.opnorm(B; kwargs...)
37
_, s, _ = tsvd(B)
@@ -20,3 +24,20 @@ ShiftedProximalOperators.iprox!(
2024

2125
LinearAlgebra.diag(op::AbstractDiagonalQuasiNewtonOperator) = copy(op.d)
2226
LinearAlgebra.diag(op::SpectralGradient{T}) where {T} = zeros(T, op.nrow) .* op.d[1]
27+
28+
"""
29+
GenericExecutionStats(reg_nlp :: AbstractRegularizedNLPModel{T, V})
30+
31+
Construct a GenericExecutionStats object from an AbstractRegularizedNLPModel.
32+
More specifically, construct a GenericExecutionStats on the NLPModel of reg_nlp and add three solver_specific entries namely :smooth_obj, :nonsmooth_obj and :xi.
33+
This is useful for reducing the number of allocations when calling solve!(..., reg_nlp, stats) and should be used by default.
34+
Warning: This should *not* be used when adding other solver_specific entries that do not have the current scalar type.
35+
For instance, when one adds the history of the objective value as a solver_specific entry (which has Vector{T} type), this will cause an error and `GenericExecutionStats(reg_nlp.model)` should be used instead.
36+
"""
37+
function GenericExecutionStats(reg_nlp :: AbstractRegularizedNLPModel{T, V}) where{T, V}
38+
stats = GenericExecutionStats(reg_nlp.model, solver_specific = Dict{Symbol, T}())
39+
set_solver_specific!(stats, :smooth_obj, T(Inf))
40+
set_solver_specific!(stats, :nonsmooth_obj, T(Inf))
41+
set_solver_specific!(stats, :xi, T(Inf))
42+
return stats
43+
end

test/test_allocs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ end
4141
for solver (:R2Solver,)
4242
reg_nlp = RegularizedNLPModel(bpdn, h)
4343
solver = eval(solver)(reg_nlp)
44-
stats = GenericExecutionStats(bpdn, solver_specific = Dict{Symbol, Float64}())
44+
stats = GenericExecutionStats(reg_nlp)
4545
@test @wrappedallocs(solve!(solver, reg_nlp, stats)) == 0
4646
end
4747
end

0 commit comments

Comments
 (0)