Skip to content

Commit 1b4a1fe

Browse files
MaxenceGollieramontoison
authored andcommitted
preallocate v = Hx for NLP.obj
1 parent bd4e1cd commit 1b4a1fe

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/qpmodel.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ mutable struct QPData{
88
}
99
c0::T # constant term in objective
1010
c::S # linear term
11+
v::S # vector that stores products with the hessian v = H*
1112
H::M1
1213
A::M2
1314
end
1415

16+
@inline QPData(c0, c, H, A) = QPData(c0, c, similar(c), H, A)
1517
isdense(data::QPData{T, S, M1, M2}) where {T, S, M1, M2} = M1 <: DenseMatrix || M2 <: DenseMatrix
1618

1719
function Base.convert(
@@ -20,7 +22,7 @@ function Base.convert(
2022
) where {T, S, M1 <: AbstractMatrix, M2 <: AbstractMatrix, MCOO <: SparseMatrixCOO{T}}
2123
HCOO = (M1 <: SparseMatrixCOO) ? data.H : SparseMatrixCOO(data.H)
2224
ACOO = (M2 <: SparseMatrixCOO) ? data.A : SparseMatrixCOO(data.A)
23-
return QPData(data.c0, data.c, HCOO, ACOO)
25+
return QPData(data.c0, data.c, data.v, HCOO, ACOO)
2426
end
2527
Base.convert(
2628
::Type{QPData{T, S, MCOO, MCOO}},
@@ -256,9 +258,8 @@ end
256258

257259
function NLPModels.obj(qp::AbstractQuadraticModel{T, S}, x::AbstractVector) where {T, S}
258260
NLPModels.increment!(qp, :neval_obj)
259-
Hx = fill!(S(undef, qp.meta.nvar), zero(T))
260-
mul!(Hx, Symmetric(qp.data.H, :L), x)
261-
return qp.data.c0 + dot(qp.data.c, x) + dot(Hx, x) / 2
261+
mul!(qp.data.v, Symmetric(qp.data.H, :L), x)
262+
return qp.data.c0 + dot(qp.data.c, x) + dot(qp.data.v, x) / 2
262263
end
263264

264265
function NLPModels.grad!(qp::AbstractQuadraticModel, x::AbstractVector, g::AbstractVector)

0 commit comments

Comments
 (0)