-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
98 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,27 @@ | ||
using NLPModels, Stopping, AdaptiveRegularization | ||
using NLPModelsTest, Stopping, AdaptiveRegularization | ||
|
||
mutable struct EmptyNLPModel{T, S} <: AbstractNLPModel{T, S} | ||
meta::NLPModelMeta{T, S} | ||
counters::Counters | ||
end | ||
|
||
NLPModels.obj(::EmptyNLPModel{T, S}, x) where {T, S} = one(T) | ||
function NLPModels.grad!(::EmptyNLPModel{T, S}, x, gx::S) where {T, S} | ||
gx .= one(T) | ||
return gx | ||
end | ||
function NLPModels.hprod!( | ||
::EmptyNLPModel, | ||
x::AbstractVector{T}, | ||
v::AbstractVector{T}, | ||
Hv::AbstractVector{T}; | ||
obj_weight = one(T), | ||
) where {T} | ||
Hv .= zero(T) | ||
return Hv | ||
end | ||
function NLPModels.hess_structure!( | ||
::EmptyNLPModel{T, S}, | ||
rows::AbstractVector, | ||
cols::AbstractVector, | ||
) where {T, S} | ||
rows .= one(T) | ||
cols .= one(T) | ||
return rows, cols | ||
end | ||
function NLPModels.hess_coord!( | ||
::EmptyNLPModel{T, S}, | ||
x::AbstractVector{T}, | ||
vals::AbstractVector; | ||
obj_weight = one(T), | ||
) where {T, S} | ||
vals .= zero(T) | ||
return vals | ||
end | ||
|
||
import NLPModels: hess, hess_coord!, hess_coord, hess_op, hess_op!, hprod, hprod! | ||
nlp = BROWNDEN() | ||
n, x0 = nlp.meta.nvar, nlp.meta.x0 | ||
|
||
mutable struct NothingPData{T} <: AdaptiveRegularization.TPData{T} | ||
OK::Any | ||
d::Any | ||
λ::Any | ||
end | ||
|
||
n = 100 | ||
TRnothing = TrustRegion(10.0, max_unsuccinarow = 2) | ||
|
||
function NothingPData(::Type{S}, ::Type{T}, n; kwargs...) where {T, S} | ||
return NothingPData{T}(true, rand(T, n), one(T)) | ||
end | ||
S, T = typeof(x0), eltype(x0) | ||
PData = PDataKARC(S, T, n) | ||
|
||
function solve_nothing(X::NothingPData, H, g, ng, calls, max_calls, α::T) where {T} | ||
X.d .= g | ||
X.λ = zero(T) | ||
return X.d, X.λ | ||
function alloc_AdaptiveRegularization(stp, PData, workspace, TRnothing) | ||
TRARC(stp, PData, workspace, TRnothing, solve_model = AdaptiveRegularization.solve_modelKARC) | ||
return nothing | ||
end | ||
|
||
x0 = rand(n) | ||
nlp = EmptyNLPModel{eltype(x0), typeof(x0)}(NLPModelMeta(n), Counters()) | ||
|
||
for (Workspace, limit) in ( | ||
(HessDense, 286624), | ||
(HessSparse, 84016), | ||
(HessSparseCOO, 0), # independent of `n` | ||
(HessOp, 2304), # independent of `n` | ||
@testset "Test TRARC allocations using $Hess" for (Hess, limit) in ( | ||
(HessOp, 1904), | ||
(HessSparseCOO, 944), | ||
) | ||
who = Workspace(nlp, n) | ||
alloc_hessian(who, nlp, x0) = @allocated AdaptiveRegularization.hessian!(who, nlp, x0) | ||
alloc_hessian(who, nlp, x0) | ||
@test (alloc_hessian(who, nlp, x0)) <= limit | ||
@show alloc_hessian(who, nlp, x0) | ||
stp = NLPStopping(nlp) | ||
stp.meta.max_iter = 5 | ||
workspace = AdaptiveRegularization.TRARCWorkspace(nlp, Hess) | ||
alloc_AdaptiveRegularization(stp, PData, workspace, TRnothing) | ||
al = @allocated alloc_AdaptiveRegularization(stp, PData, workspace, TRnothing) | ||
println("Allocations of TRARC using $Hess is $al .") | ||
@show al | ||
end | ||
|
||
stp = NLPStopping(nlp) | ||
stp.meta.max_iter = 5 | ||
TRnothing = TrustRegion(10.0, max_unsuccinarow = 2) | ||
|
||
nlp = stp.pb | ||
S, T = typeof(x0), eltype(x0) | ||
PData = NothingPData(S, T, nlp.meta.nvar) | ||
workspace = AdaptiveRegularization.TRARCWorkspace(nlp, HessOp) | ||
|
||
alloc_AdaptiveRegularization(stp, PData, workspace, TRnothing, solve_nothing) = | ||
@allocated TRARC(stp, PData, workspace, TRnothing, solve_model = solve_nothing) | ||
alloc_AdaptiveRegularization(stp, PData, workspace, TRnothing, solve_nothing) | ||
alloc_AdaptiveRegularization(stp, PData, workspace, TRnothing, solve_nothing) | ||
@show alloc_AdaptiveRegularization(stp, PData, workspace, TRnothing, solve_nothing) # 11192 | ||
|
||
workspace = AdaptiveRegularization.TRARCWorkspace(nlp, HessSparseCOO) | ||
|
||
alloc_AdaptiveRegularization_COO(stp, PData, workspace, TRnothing, solve_nothing) = | ||
@allocated TRARC(stp, PData, workspace, TRnothing, solve_model = solve_nothing) | ||
alloc_AdaptiveRegularization_COO(stp, PData, workspace, TRnothing, solve_nothing) | ||
alloc_AdaptiveRegularization_COO(stp, PData, workspace, TRnothing, solve_nothing) | ||
@show alloc_AdaptiveRegularization_COO(stp, PData, workspace, TRnothing, solve_nothing) # 11192 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using AdaptiveRegularization, LinearAlgebra, Test | ||
|
||
T = Float64 | ||
S = Vector{T} | ||
n = 1000 | ||
|
||
TR = TrustRegion(T(10)) | ||
α = T(100) | ||
|
||
# Test increase / decrease | ||
|
||
function alloc_decrease(XData, α, TR) | ||
AdaptiveRegularization.decrease(XData, α, TR) | ||
return nothing | ||
end | ||
|
||
function alloc_increase(XData, α, TR) | ||
AdaptiveRegularization.increase(XData, α, TR) | ||
return nothing | ||
end | ||
|
||
for XData in (PDataKARC(S, T, n), PDataTRK(S, T, n), PDataST(S, T, n)) | ||
@testset "Allocation test in AdaptiveRegularization.decrease for $(typeof(XData))" begin | ||
alloc_decrease(XData, α, TR) | ||
al = @allocated alloc_decrease(XData, α, TR) | ||
@test al == 0 | ||
end | ||
@testset "Allocation test in AdaptiveRegularization.decrease for $(typeof(XData))" begin | ||
alloc_increase(XData, α, TR) | ||
al = @allocated alloc_increase(XData, α, TR) | ||
@test al == 0 | ||
end | ||
end | ||
|
||
# Test hessian evaluation in-place | ||
|
||
using NLPModelsTest | ||
nlp = NLPModelsTest.BROWNDEN() | ||
n = nlp.meta.nvar | ||
x0 = nlp.meta.x0 | ||
|
||
function alloc_hessian(who, nlp, x0) | ||
AdaptiveRegularization.hessian!(who, nlp, x0) | ||
return nothing | ||
end | ||
|
||
@testset "Test in-place hessian allocations" for (Workspace, limit) in ( | ||
(HessDense, 1952), | ||
(HessSparse, 944), | ||
(HessSparseCOO, 0), | ||
(HessOp, 960), | ||
) | ||
who = Workspace(nlp, n) | ||
alloc_hessian(who, nlp, x0) | ||
al = @allocated alloc_hessian(who, nlp, x0) | ||
@test al <= limit | ||
println("Allocations for $Workspace is $al.") | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters