Skip to content

Commit 2380a8d

Browse files
committed
fix type instability in workspace and use smart_update
1 parent b83c0ed commit 2380a8d

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

src/PreProcess/PreProcessKARC.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function preprocess(PData::PDataKARC, Hop, g, gNorm2, calls, max_calls, α)
3434
PData.indmin = 0
3535
PData.positives .= solver.converged
3636
for i = 1:nshifts
37-
@. PData.xShift[i] = -solver.x[i]
37+
PData.xShift[i] .= .-solver.x[i]
3838
PData.norm_dirs[i] = norm(solver.x[i]) # Get it from cg_lanczos?
3939
end
4040
PData.shifts .= shifts

src/main.jl

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,28 @@ struct TRARCWorkspace{T,S,Hess}
1515
Fx::S
1616
function TRARCWorkspace(nlp::AbstractNLPModel{T,S}, ::Type{Hess}) where {T,S,Hess}
1717
n = nlp.meta.nvar
18-
return new{T,S,Hess}(
18+
Hstruct, Htype = init(Hess, nlp, n)
19+
return new{T,S,Htype}(
1920
S(undef, n), # xt
2021
S(undef, n), # xtnext
2122
S(undef, n), # d
2223
S(undef, n), # ∇f
2324
S(undef, n), # ∇fnext
24-
Hess(nlp, n),
25+
Hstruct,
2526
S(undef, n), # H * d
2627
S(undef, 0),
2728
)
2829
end
2930
function TRARCWorkspace(nlp::AbstractNLSModel{T,S}, ::Type{Hess}) where {T,S,Hess}
3031
n = nlp.meta.nvar
31-
return new{T,S,Hess}(
32+
Hstruct, Htype = init(Hess, nlp, n)
33+
return new{T,S,Htype}(
3234
S(undef, n), # xt
3335
S(undef, n), # xtnext
3436
S(undef, n), # d
3537
S(undef, n), # ∇f
3638
S(undef, n), # ∇fnext
37-
Hess(nlp, n),
39+
Hstruct,
3840
S(undef, n), # H * d
3941
S(undef, nlp.nls_meta.nequ),
4042
)
@@ -193,8 +195,9 @@ function TRARC(
193195
norm_∇f = norm(∇f)
194196
nlp_stop.meta.optimality0 = norm_∇f
195197

196-
OK = update_and_start!(nlp_stop, x = xt, fx = ft, gx = ∇f)
197-
!OK && Stopping.update!(nlp_at_x, Hx = hessian!(workspace, nlp, xt), convert = true)
198+
Stopping._smart_update!(nlp_at_x, x = xt, fx = ft, gx = ∇f)
199+
OK = start!(nlp_stop)
200+
!OK && Stopping._smart_update!(nlp_at_x, Hx = hessian!(workspace, nlp, xt))
198201

199202
iter = 0 # counter different than stop count
200203
succ, unsucc, verysucc, unsuccinarow = 0, 0, 0, 0
@@ -271,8 +274,9 @@ function TRARC(
271274
end # while !success
272275

273276
nlp_stop.meta.nb_of_stop = iter
274-
OK = update_and_stop!(nlp_stop, x = xt, fx = ft, gx = ∇f)
275-
success && Stopping.update!(nlp_at_x, Hx = hessian!(workspace, nlp, xt))
277+
Stopping._smart_update!(nlp_at_x, x = xt, fx = ft, gx = ∇f)
278+
OK = stop!(nlp_stop)
279+
success && Stopping._smart_update!(nlp_at_x, Hx = hessian!(workspace, nlp, xt))
276280
end # while !OK
277281

278282
return nlp_stop

src/utils/hessian_rep.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ end
6363

6464
export HessDense, HessSparse, HessSparseCOO, HessOp, HessGaussNewtonOp
6565

66+
"""
67+
init(::Type{Hess}, nlp::AbstractNLPModel{T,S}, n)
68+
69+
Return the hessian structure `Hess` and its composite type.
70+
"""
71+
function init(::Type{Hess}, nlp::AbstractNLPModel{T,S}, n) where {Hess,T,S}
72+
Hstruct = Hess(nlp, n)
73+
return Hstruct, typeof(Hstruct)
74+
end
75+
6676
"""
6777
hessian!(workspace::HessDense, nlp, x)
6878

0 commit comments

Comments
 (0)