Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify set_solver_specific! #63

Merged
merged 1 commit into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/stats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export AbstractExecutionStats,
set_bounds_multipliers!,
set_iter!,
set_time!,
broadcast_solver_specific!,
set_solver_specific!,
statsgetfield,
statshead,
Expand Down Expand Up @@ -317,23 +318,28 @@ function set_time!(stats::GenericExecutionStats, time::Float64)
stats
end

"""
broadcast_solver_specific!(stats::GenericExecutionStats, field::Symbol, value)

Broadcast `value` as a solver-specific value identified by `field` in `stats`
and mark it as reliable.
"""
function broadcast_solver_specific!(stats::GenericExecutionStats, field::Symbol, value)
if field ∈ keys(stats.solver_specific)
stats.solver_specific[field] .= value
end
stats.solver_specific_reliable = true
stats
end

"""
set_solver_specific!(stats::GenericExecutionStats, field::Symbol, value)

Register `value` as a solver-specific value identified by `field` in `stats`
and mark it as reliable.
"""
function set_solver_specific!(stats::GenericExecutionStats, field::Symbol, value)
if field ∈ keys(stats.solver_specific)
try
# will fail if typeof(solver_specific[field]) does not support broadcast
stats.solver_specific[field] .= value
catch
stats.solver_specific[field] = value
end
else
stats.solver_specific[field] = value
end
stats.solver_specific[field] = value
stats.solver_specific_reliable = true
stats
end
Expand Down
15 changes: 15 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ using ADNLPModels, NLPModels
# stdlib
using LinearAlgebra, Logging, Test


if VERSION ≥ v"1.6"
@testset "Test allocations of solver specific" begin
nlp = ADNLPModel(x -> sum(x), ones(2))
stats = GenericExecutionStats(nlp) # stats = GenericExecutionStats(nlp, solver_specific = Dict{Symbol, Bool}())
function fake_solver(stats)
set_solver_specific!(stats, :test, true)
return stats
end
@allocated fake_solver(stats)
a = @allocated fake_solver(stats)
@test a == 0
end
end

include("test_logging.jl")
include("test_stats.jl")
include("test_callback.jl")
Expand Down
1 change: 1 addition & 0 deletions test/test_stats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function test_stats()
set_solver_specific!(stats, :matvec, 10)
set_solver_specific!(stats, :dot, 25)
set_solver_specific!(stats, :empty_vec, [])
broadcast_solver_specific!(stats, :empty_vec, [])
set_solver_specific!(stats, :axpy, 20)
set_solver_specific!(stats, :ray, -1 ./ (1:100))

Expand Down