Skip to content
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
8 changes: 6 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
NLPModels = "0.21"
LinearAlgebra = "1.10"
NLPModels = "0.21.6"
NLPModelsModifiers = "0.7"
julia = "^1.6"
Printf = "1.10"
SparseArrays = "1.10"
Test = "1.10"
julia = "1.10"

[extras]
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
Expand Down
153 changes: 86 additions & 67 deletions src/allocs_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ function test_allocs_nlpmodels(nlp::AbstractNLPModel; linear_api = false, exclud
)

test_obj_grad!(nlp_allocations, nlp, exclude)
if !(hess in exclude)

if !(hess in exclude) && nlp.meta.hess_available
rows = Vector{Int}(undef, nlp.meta.nnzh)
cols = Vector{Int}(undef, nlp.meta.nnzh)
hess_structure!(nlp, rows, cols)
Expand All @@ -45,7 +46,8 @@ function test_allocs_nlpmodels(nlp::AbstractNLPModel; linear_api = false, exclud
nlp_allocations[:hess_lag_coord!] = @allocated hess_coord!(nlp, x, y, vals)
end
end
if !(hprod in exclude)

if !(hprod in exclude)&& nlp.meta.hprod_available
x = get_x0(nlp)
v = copy(x)
Hv = similar(x)
Expand All @@ -57,7 +59,8 @@ function test_allocs_nlpmodels(nlp::AbstractNLPModel; linear_api = false, exclud
nlp_allocations[:hprod_lag!] = @allocated hprod!(nlp, x, y, v, Hv)
end
end
if !(hess_op in exclude)

if !(hess_op in exclude) && nlp.meta.hprod_available
x = get_x0(nlp)
Hv = similar(x)
v = copy(x)
Expand All @@ -78,7 +81,8 @@ function test_allocs_nlpmodels(nlp::AbstractNLPModel; linear_api = false, exclud
cons!(nlp, x, c)
nlp_allocations[:cons!] = @allocated cons!(nlp, x, c)
end
if get_ncon(nlp) > 0 && !(jac in exclude)

if get_ncon(nlp) > 0 && !(jac in exclude) && nlp.meta.jac_available
rows = Vector{Int}(undef, nlp.meta.nnzj)
cols = Vector{Int}(undef, nlp.meta.nnzj)
jac_structure!(nlp, rows, cols)
Expand All @@ -88,21 +92,24 @@ function test_allocs_nlpmodels(nlp::AbstractNLPModel; linear_api = false, exclud
jac_coord!(nlp, x, vals)
nlp_allocations[:jac_coord!] = @allocated jac_coord!(nlp, x, vals)
end
if get_ncon(nlp) > 0 && !(jprod in exclude)

if get_ncon(nlp) > 0 && !(jprod in exclude) && nlp.meta.jprod_available
x = get_x0(nlp)
v = copy(x)
Jv = Vector{eltype(x)}(undef, get_ncon(nlp))
jprod!(nlp, x, v, Jv)
nlp_allocations[:jprod!] = @allocated jprod!(nlp, x, v, Jv)
end
if get_ncon(nlp) > 0 && !(jtprod in exclude)

if get_ncon(nlp) > 0 && !(jtprod in exclude) && nlp.meta.jtprod_available
x = get_x0(nlp)
v = copy(get_y0(nlp))
Jtv = similar(x)
jtprod!(nlp, x, v, Jtv)
nlp_allocations[:jtprod!] = @allocated jtprod!(nlp, x, v, Jtv)
end
if get_ncon(nlp) > 0 && !(jac_op in exclude)

if get_ncon(nlp) > 0 && !(jac_op in exclude) && nlp.meta.jprod_available && nlp.meta.jtprod_available
x = get_x0(nlp)
Jtv = similar(x)
Jv = Vector{eltype(x)}(undef, get_ncon(nlp))
Expand Down Expand Up @@ -130,7 +137,8 @@ function test_allocs_nlpmodels(nlp::AbstractNLPModel; linear_api = false, exclud
eval(fun)(nlp, x, c)
nlp_allocations[fun] = @allocated eval(fun)(nlp, x, c)
end
if !(jac in exclude)

if !(jac in exclude) && nlp.meta.jac_available
rows = Vector{Int}(undef, nnzj)
cols = Vector{Int}(undef, nnzj)
fun = type == :lin ? jac_lin_structure! : jac_nln_structure! # eval(fun) would allocate here
Expand All @@ -142,23 +150,26 @@ function test_allocs_nlpmodels(nlp::AbstractNLPModel; linear_api = false, exclud
eval(fun)(nlp, x, vals)
nlp_allocations[fun] = @allocated eval(fun)(nlp, x, vals)
end
if !(jprod in exclude)

if !(jprod in exclude) && nlp.meta.jprod_available
x = get_x0(nlp)
v = copy(x)
Jv = Vector{eltype(x)}(undef, nn)
fun = Symbol(:jprod_, type, :!)
eval(fun)(nlp, x, v, Jv)
nlp_allocations[fun] = @allocated eval(fun)(nlp, x, v, Jv)
end
if !(jtprod in exclude)

if !(jtprod in exclude) && nlp.meta.jtprod_available
x = get_x0(nlp)
v = copy(get_y0(nlp)[1:nn])
Jtv = similar(x)
fun = Symbol(:jtprod_, type, :!)
eval(fun)(nlp, x, v, Jtv)
nlp_allocations[fun] = @allocated eval(fun)(nlp, x, v, Jtv)
end
if !(jac_op in exclude)

if !(jac_op in exclude) && nlp.meta.jprod_available && nlp.meta.jtprod_available
x = get_x0(nlp)
Jtv = similar(x)
Jv = Vector{eltype(x)}(undef, nn)
Expand Down Expand Up @@ -199,7 +210,8 @@ function test_obj_grad!(nlp_allocations, nlp::AbstractNLPModel, exclude)
obj(nlp, x)
nlp_allocations[:obj] = @allocated obj(nlp, x)
end
if !(grad in exclude)

if !(grad in exclude) && nlp.meta.grad_available
x = get_x0(nlp)
g = similar(x)
grad!(nlp, x, g)
Expand All @@ -215,7 +227,8 @@ function test_obj_grad!(nlp_allocations, nls::AbstractNLSModel, exclude)
obj(nls, x, Fx)
nlp_allocations[:obj] = @allocated obj(nls, x, Fx)
end
if !(grad in exclude)

if !(grad in exclude) && nls_meta(nls).jtprod_residual_available
x = get_x0(nls)
Fx = Vector{eltype(x)}(undef, get_nequ(nls))
g = similar(x)
Expand All @@ -226,16 +239,16 @@ function test_obj_grad!(nlp_allocations, nls::AbstractNLSModel, exclude)
end

"""
test_allocs_nlsmodels(nlp::AbstractNLSModel; exclude = [])
test_allocs_nlsmodels(nls::AbstractNLSModel; exclude = [])

Returns a `Dict` containing allocations of the in-place functions specialized to nonlinear least squares of NLPModel API.

The keyword `exclude` takes a Array of Function to be excluded from the tests.
Use `hess_residual` (resp. `jac_residual`) to exclude `hess_residual_coord` and `hess_residual_structure` (resp. `jac_residual_coord` and `jac_residual_structure`).
The hessian-vector product is tested for all the component of the residual function, so exclude `hprod_residual` and `hess_op_residual` if you want to avoid this.
"""
function test_allocs_nlsmodels(nlp::AbstractNLSModel; exclude = [])
nlp_allocations = Dict(
function test_allocs_nlsmodels(nls::AbstractNLSModel; exclude = [])
nls_allocations = Dict(
:residual! => NaN,
:hess_structure_residual! => NaN,
:hess_coord_residual! => NaN,
Expand All @@ -250,79 +263,85 @@ function test_allocs_nlsmodels(nlp::AbstractNLSModel; exclude = [])
)

if !(residual in exclude)
x = get_x0(nlp)
Fx = Vector{eltype(x)}(undef, get_nequ(nlp))
residual!(nlp, x, Fx)
nlp_allocations[:residual!] = @allocated residual!(nlp, x, Fx)
x = get_x0(nls)
Fx = Vector{eltype(x)}(undef, get_nequ(nls))
residual!(nls, x, Fx)
nls_allocations[:residual!] = @allocated residual!(nls, x, Fx)
end
if !(jac_residual in exclude)
rows = Vector{Int}(undef, nlp.nls_meta.nnzj)
cols = Vector{Int}(undef, nlp.nls_meta.nnzj)
jac_structure_residual!(nlp, rows, cols)
nlp_allocations[:jac_structure_residual!] = @allocated jac_structure_residual!(nlp, rows, cols)
x = get_x0(nlp)
vals = Vector{eltype(x)}(undef, nlp.nls_meta.nnzj)
jac_coord_residual!(nlp, x, vals)
nlp_allocations[:jac_coord_residual!] = @allocated jac_coord_residual!(nlp, x, vals)

if !(jac_residual in exclude) && nls_meta(nls).jac_residual_available
rows = Vector{Int}(undef, nls.nls_meta.nnzj)
cols = Vector{Int}(undef, nls.nls_meta.nnzj)
jac_structure_residual!(nls, rows, cols)
nls_allocations[:jac_structure_residual!] = @allocated jac_structure_residual!(nls, rows, cols)
x = get_x0(nls)
vals = Vector{eltype(x)}(undef, nls.nls_meta.nnzj)
jac_coord_residual!(nls, x, vals)
nls_allocations[:jac_coord_residual!] = @allocated jac_coord_residual!(nls, x, vals)
end
if !(jprod_residual in exclude)
x = get_x0(nlp)

if !(jprod_residual in exclude) && nls_meta(nls).jprod_residual_available
x = get_x0(nls)
v = copy(x)
Jv = Vector{eltype(x)}(undef, get_nequ(nlp))
jprod_residual!(nlp, x, v, Jv)
nlp_allocations[:jprod_residual!] = @allocated jprod_residual!(nlp, x, v, Jv)
Jv = Vector{eltype(x)}(undef, get_nequ(nls))
jprod_residual!(nls, x, v, Jv)
nls_allocations[:jprod_residual!] = @allocated jprod_residual!(nls, x, v, Jv)
end
if !(jtprod_residual in exclude)
x = get_x0(nlp)
w = zeros(eltype(x), get_nequ(nlp))

if !(jtprod_residual in exclude) && nls_meta(nls).jtprod_residual_available
x = get_x0(nls)
w = zeros(eltype(x), get_nequ(nls))
Jtv = similar(x)
jtprod_residual!(nlp, x, w, Jtv)
nlp_allocations[:jtprod_residual!] = @allocated jtprod_residual!(nlp, x, w, Jtv)
jtprod_residual!(nls, x, w, Jtv)
nls_allocations[:jtprod_residual!] = @allocated jtprod_residual!(nls, x, w, Jtv)
end
if !(jac_op_residual in exclude)
x = get_x0(nlp)

if !(jac_op_residual in exclude) && nls_meta(nls).jprod_residual_available && nls_meta(nls).jtprod_residual_available
x = get_x0(nls)
Jtv = similar(x)
Jv = Vector{eltype(x)}(undef, get_nequ(nlp))
Jv = Vector{eltype(x)}(undef, get_nequ(nls))

v = copy(x)
w = zeros(eltype(x), get_nequ(nlp))
J = jac_op_residual!(nlp, x, Jv, Jtv)
w = zeros(eltype(x), get_nequ(nls))
J = jac_op_residual!(nls, x, Jv, Jtv)
mul!(Jv, J, v)
nlp_allocations[:jac_op_residual_prod!] = @allocated mul!(Jv, J, v)
nls_allocations[:jac_op_residual_prod!] = @allocated mul!(Jv, J, v)
Jt = J'
mul!(Jtv, Jt, w)
nlp_allocations[:jac_op_residual_transpose_prod!] = @allocated mul!(Jtv, Jt, w)
nls_allocations[:jac_op_residual_transpose_prod!] = @allocated mul!(Jtv, Jt, w)
end
if !(hess_residual in exclude)
rows = Vector{Int}(undef, nlp.nls_meta.nnzh)
cols = Vector{Int}(undef, nlp.nls_meta.nnzh)
hess_structure_residual!(nlp, rows, cols)
nlp_allocations[:hess_structure_residual!] =
@allocated hess_structure_residual!(nlp, rows, cols)
x = get_x0(nlp)
v = ones(eltype(x), get_nequ(nlp))
vals = Vector{eltype(x)}(undef, nlp.nls_meta.nnzh)
hess_coord_residual!(nlp, x, v, vals)
nlp_allocations[:hess_coord_residual!] = @allocated hess_coord_residual!(nlp, x, v, vals)

if !(hess_residual in exclude) && nls_meta(nls).hess_residual_available
rows = Vector{Int}(undef, nls.nls_meta.nnzh)
cols = Vector{Int}(undef, nls.nls_meta.nnzh)
hess_structure_residual!(nls, rows, cols)
nls_allocations[:hess_structure_residual!] =
@allocated hess_structure_residual!(nls, rows, cols)
x = get_x0(nls)
v = ones(eltype(x), get_nequ(nls))
vals = Vector{eltype(x)}(undef, nls.nls_meta.nnzh)
hess_coord_residual!(nls, x, v, vals)
nls_allocations[:hess_coord_residual!] = @allocated hess_coord_residual!(nls, x, v, vals)
end
for i = 1:get_nequ(nlp)
if !(hprod_residual in exclude)
x = get_x0(nlp)

for i = 1:get_nequ(nls)
if !(hprod_residual in exclude) && nls_meta(nls).hprod_residual_available
x = get_x0(nls)
v = copy(x)
Hv = similar(x)
hprod_residual!(nlp, x, i, v, Hv)
nlp_allocations[:hprod_residual!] = @allocated hprod_residual!(nlp, x, i, v, Hv)
hprod_residual!(nls, x, i, v, Hv)
nls_allocations[:hprod_residual!] = @allocated hprod_residual!(nls, x, i, v, Hv)
end
if !(hess_op_residual in exclude)
x = get_x0(nlp)
if !(hess_op_residual in exclude) && nls_meta(nls).jprod_residual_available
x = get_x0(nls)
Hv = similar(x)
v = copy(x)
H = hess_op_residual!(nlp, x, i, Hv)
H = hess_op_residual!(nls, x, i, Hv)
mul!(Hv, H, v)
nlp_allocations[:hess_op_residual_prod!] = @allocated mul!(Hv, H, v)
nls_allocations[:hess_op_residual_prod!] = @allocated mul!(Hv, H, v)
end
end
return nlp_allocations
return nls_allocations
end

function NLPModels.histline(s::String, v::Integer, maxv::Integer)
Expand Down
Loading