Skip to content

Commit

Permalink
Remove a few allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
amontoison committed Aug 19, 2024
1 parent 5e58b6f commit ad586c0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
31 changes: 17 additions & 14 deletions src/julia_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function NLPModels.objcons!(nlp::CUTEstModel, x::AbstractVector, c::AbstractVect
@lencheck nlp.meta.nvar x
@lencheck ncon c
if ncon > 0
cc = zeros(Float64, ncon)
cc = nlp.workspace_ncon
f, _ = objcons!(nlp, convert(Vector{Float64}, x), cc)
c .= cc
return f, c
Expand Down Expand Up @@ -86,15 +86,15 @@ end

function NLPModels.objgrad!(nlp::CUTEstModel, x::AbstractVector, g::AbstractVector)
@lencheck nlp.meta.nvar x g
gc = Vector{Float64}(undef, nlp.meta.nvar)
gc = nlp.workspace_nvar
f, _ = objgrad!(nlp, convert(Vector{Float64}, x), gc)
g .= gc
return f, g
end

function NLPModels.obj(nlp::CUTEstModel, x::AbstractVector)
@lencheck nlp.meta.nvar x
f, _ = objcons!(nlp, x, nlp.work)
f, _ = objcons!(nlp, x, nlp.workspace_ncon)
if nlp.meta.ncon > 0
decrement!(nlp, :neval_cons) # does not really count as a constraint eval
end
Expand Down Expand Up @@ -301,7 +301,7 @@ end
function NLPModels.jac_coord!(nlp::CUTEstModel, x::AbstractVector, vals::AbstractVector)
@lencheck nlp.meta.nvar x
@lencheck nlp.meta.nnzj vals
cons_coord!(nlp, x, nlp.work, nlp.jrows, nlp.jcols, vals)
cons_coord!(nlp, x, nlp.workspace_ncon, nlp.jrows, nlp.jcols, vals)
decrement!(nlp, :neval_cons) # does not really count as a constraint eval
return vals
end
Expand Down Expand Up @@ -372,7 +372,7 @@ function NLPModels.jprod!(
)
@lencheck nlp.meta.nvar x v
@lencheck nlp.meta.ncon jv
jvc = nlp.work
jvc = nlp.workspace_ncon
jprod!(nlp, convert(Vector{Float64}, x), convert(Vector{Float64}, v), jvc)
jv .= jvc
end
Expand All @@ -385,11 +385,11 @@ function NLPModels.jprod_nln!(
)
@lencheck nlp.meta.nvar x v
@lencheck nlp.meta.nnln jv
jvc = nlp.work
jvc = nlp.workspace_ncon
jprod!(nlp, x, v, jvc)
decrement!(nlp, :neval_jprod)
increment!(nlp, :neval_jprod_nln)
jv .= jvc[nlp.meta.nln]
jv .= view(jvc, nlp.meta.nln)
end

function NLPModels.jprod_lin!(
Expand Down Expand Up @@ -443,7 +443,7 @@ function NLPModels.jtprod!(
)
@lencheck nlp.meta.nvar x jtv
@lencheck nlp.meta.ncon v
jtvc = zeros(Float64, nlp.meta.nvar)
jtvc = nlp.workspace_nvar
jtprod!(nlp, convert(Vector{Float64}, x), convert(Vector{Float64}, v), jtvc)
jtv .= jtvc
end
Expand All @@ -456,7 +456,7 @@ function NLPModels.jtprod_nln!(
)
@lencheck nlp.meta.nvar x jtv
@lencheck nlp.meta.nnln v
_v = nlp.work
_v = nlp.workspace_ncon
_v[nlp.meta.lin] .= 0.0
_v[nlp.meta.nln] = v
jtprod!(nlp, x, _v, jtv)
Expand Down Expand Up @@ -557,7 +557,9 @@ function NLPModels.hess_coord!(
)
@lencheck nlp.meta.nvar x
@lencheck nlp.meta.nnzh vals
hess_coord!(nlp, x, zeros(Float64, nlp.meta.ncon), vals; obj_weight = obj_weight)
λ = nlp.workspace_ncon
λ .= 0.0
hess_coord!(nlp, x, λ, vals; obj_weight = obj_weight)
end

function NLPModels.hprod!(
Expand Down Expand Up @@ -626,7 +628,7 @@ function NLPModels.hprod!(
)
@lencheck nlp.meta.nvar x v hv
@lencheck nlp.meta.ncon y
hvc = zeros(Float64, nlp.meta.nvar)
hvc = nlp.workspace_nvar
hprod!(
nlp,
convert(Vector{Float64}, x),
Expand All @@ -646,11 +648,12 @@ function NLPModels.hprod!(
obj_weight::Float64 = 1.0,
)
@lencheck nlp.meta.nvar x v hv
nlp.work .= 0.0 # Lagrange multipliers
λ = nlp.workspace_ncon
λ .= 0.0 # Lagrange multipliers
hprod!(
nlp,
convert(Vector{Float64}, x),
nlp.work,
λ,
convert(Vector{Float64}, v),
hv,
obj_weight = obj_weight,
Expand All @@ -665,7 +668,7 @@ function NLPModels.hprod!(
obj_weight::Float64 = 1.0,
)
@lencheck nlp.meta.nvar x v hv
hvc = zeros(Float64, nlp.meta.nvar)
hvc = nlp.workspace_nvar
hprod!(
nlp,
convert(Vector{Float64}, x),
Expand Down
10 changes: 7 additions & 3 deletions src/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ mutable struct CUTEstModel{T} <: AbstractNLPModel{T, Vector{T}}
clincols::Vector{Int32}
clinvals::Vector{T}

work::Vector{T}
workspace_nvar::Vector{T}
workspace_ncon::Vector{T}

Jval::Vector{T}
Jvar::Vector{Cint}
end
Expand Down Expand Up @@ -227,7 +229,8 @@ function CUTEstModel(
nnzj = nnzj[] |> Int
nnzh = nnzh[] |> Int

work = Vector{Float64}(undef, ncon)
workspace_nvar = Vector{Float64}(undef, nvar)
workspace_ncon = Vector{Float64}(undef, ncon)

fortran_close_(Ref{Cint}(funit), status)
cutest_error(status[])
Expand Down Expand Up @@ -260,7 +263,8 @@ function CUTEstModel(
clinrows,
clincols,
clinvals,
work,
workspace_nvar,
workspace_ncon,
Jval,
Jvar,
)
Expand Down

0 comments on commit ad586c0

Please sign in to comment.