Skip to content

Commit df07a88

Browse files
authored
Update allocs_model.jl (#131)
1 parent 97dc214 commit df07a88

File tree

2 files changed

+92
-69
lines changed

2 files changed

+92
-69
lines changed

Project.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1111
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1212

1313
[compat]
14-
NLPModels = "0.21"
14+
LinearAlgebra = "1.10"
15+
NLPModels = "0.21.6"
1516
NLPModelsModifiers = "0.7"
16-
julia = "^1.6"
17+
Printf = "1.10"
18+
SparseArrays = "1.10"
19+
Test = "1.10"
20+
julia = "1.10"
1721

1822
[extras]
1923
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"

src/allocs_model.jl

Lines changed: 86 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ function test_allocs_nlpmodels(nlp::AbstractNLPModel; linear_api = false, exclud
3030
)
3131

3232
test_obj_grad!(nlp_allocations, nlp, exclude)
33-
if !(hess in exclude)
33+
34+
if !(hess in exclude) && nlp.meta.hess_available
3435
rows = Vector{Int}(undef, nlp.meta.nnzh)
3536
cols = Vector{Int}(undef, nlp.meta.nnzh)
3637
hess_structure!(nlp, rows, cols)
@@ -45,7 +46,8 @@ function test_allocs_nlpmodels(nlp::AbstractNLPModel; linear_api = false, exclud
4546
nlp_allocations[:hess_lag_coord!] = @allocated hess_coord!(nlp, x, y, vals)
4647
end
4748
end
48-
if !(hprod in exclude)
49+
50+
if !(hprod in exclude)&& nlp.meta.hprod_available
4951
x = get_x0(nlp)
5052
v = copy(x)
5153
Hv = similar(x)
@@ -57,7 +59,8 @@ function test_allocs_nlpmodels(nlp::AbstractNLPModel; linear_api = false, exclud
5759
nlp_allocations[:hprod_lag!] = @allocated hprod!(nlp, x, y, v, Hv)
5860
end
5961
end
60-
if !(hess_op in exclude)
62+
63+
if !(hess_op in exclude) && nlp.meta.hprod_available
6164
x = get_x0(nlp)
6265
Hv = similar(x)
6366
v = copy(x)
@@ -78,7 +81,8 @@ function test_allocs_nlpmodels(nlp::AbstractNLPModel; linear_api = false, exclud
7881
cons!(nlp, x, c)
7982
nlp_allocations[:cons!] = @allocated cons!(nlp, x, c)
8083
end
81-
if get_ncon(nlp) > 0 && !(jac in exclude)
84+
85+
if get_ncon(nlp) > 0 && !(jac in exclude) && nlp.meta.jac_available
8286
rows = Vector{Int}(undef, nlp.meta.nnzj)
8387
cols = Vector{Int}(undef, nlp.meta.nnzj)
8488
jac_structure!(nlp, rows, cols)
@@ -88,21 +92,24 @@ function test_allocs_nlpmodels(nlp::AbstractNLPModel; linear_api = false, exclud
8892
jac_coord!(nlp, x, vals)
8993
nlp_allocations[:jac_coord!] = @allocated jac_coord!(nlp, x, vals)
9094
end
91-
if get_ncon(nlp) > 0 && !(jprod in exclude)
95+
96+
if get_ncon(nlp) > 0 && !(jprod in exclude) && nlp.meta.jprod_available
9297
x = get_x0(nlp)
9398
v = copy(x)
9499
Jv = Vector{eltype(x)}(undef, get_ncon(nlp))
95100
jprod!(nlp, x, v, Jv)
96101
nlp_allocations[:jprod!] = @allocated jprod!(nlp, x, v, Jv)
97102
end
98-
if get_ncon(nlp) > 0 && !(jtprod in exclude)
103+
104+
if get_ncon(nlp) > 0 && !(jtprod in exclude) && nlp.meta.jtprod_available
99105
x = get_x0(nlp)
100106
v = copy(get_y0(nlp))
101107
Jtv = similar(x)
102108
jtprod!(nlp, x, v, Jtv)
103109
nlp_allocations[:jtprod!] = @allocated jtprod!(nlp, x, v, Jtv)
104110
end
105-
if get_ncon(nlp) > 0 && !(jac_op in exclude)
111+
112+
if get_ncon(nlp) > 0 && !(jac_op in exclude) && nlp.meta.jprod_available && nlp.meta.jtprod_available
106113
x = get_x0(nlp)
107114
Jtv = similar(x)
108115
Jv = Vector{eltype(x)}(undef, get_ncon(nlp))
@@ -130,7 +137,8 @@ function test_allocs_nlpmodels(nlp::AbstractNLPModel; linear_api = false, exclud
130137
eval(fun)(nlp, x, c)
131138
nlp_allocations[fun] = @allocated eval(fun)(nlp, x, c)
132139
end
133-
if !(jac in exclude)
140+
141+
if !(jac in exclude) && nlp.meta.jac_available
134142
rows = Vector{Int}(undef, nnzj)
135143
cols = Vector{Int}(undef, nnzj)
136144
fun = type == :lin ? jac_lin_structure! : jac_nln_structure! # eval(fun) would allocate here
@@ -142,23 +150,26 @@ function test_allocs_nlpmodels(nlp::AbstractNLPModel; linear_api = false, exclud
142150
eval(fun)(nlp, x, vals)
143151
nlp_allocations[fun] = @allocated eval(fun)(nlp, x, vals)
144152
end
145-
if !(jprod in exclude)
153+
154+
if !(jprod in exclude) && nlp.meta.jprod_available
146155
x = get_x0(nlp)
147156
v = copy(x)
148157
Jv = Vector{eltype(x)}(undef, nn)
149158
fun = Symbol(:jprod_, type, :!)
150159
eval(fun)(nlp, x, v, Jv)
151160
nlp_allocations[fun] = @allocated eval(fun)(nlp, x, v, Jv)
152161
end
153-
if !(jtprod in exclude)
162+
163+
if !(jtprod in exclude) && nlp.meta.jtprod_available
154164
x = get_x0(nlp)
155165
v = copy(get_y0(nlp)[1:nn])
156166
Jtv = similar(x)
157167
fun = Symbol(:jtprod_, type, :!)
158168
eval(fun)(nlp, x, v, Jtv)
159169
nlp_allocations[fun] = @allocated eval(fun)(nlp, x, v, Jtv)
160170
end
161-
if !(jac_op in exclude)
171+
172+
if !(jac_op in exclude) && nlp.meta.jprod_available && nlp.meta.jtprod_available
162173
x = get_x0(nlp)
163174
Jtv = similar(x)
164175
Jv = Vector{eltype(x)}(undef, nn)
@@ -199,7 +210,8 @@ function test_obj_grad!(nlp_allocations, nlp::AbstractNLPModel, exclude)
199210
obj(nlp, x)
200211
nlp_allocations[:obj] = @allocated obj(nlp, x)
201212
end
202-
if !(grad in exclude)
213+
214+
if !(grad in exclude) && nlp.meta.grad_available
203215
x = get_x0(nlp)
204216
g = similar(x)
205217
grad!(nlp, x, g)
@@ -215,7 +227,8 @@ function test_obj_grad!(nlp_allocations, nls::AbstractNLSModel, exclude)
215227
obj(nls, x, Fx)
216228
nlp_allocations[:obj] = @allocated obj(nls, x, Fx)
217229
end
218-
if !(grad in exclude)
230+
231+
if !(grad in exclude) && nls_meta(nls).jtprod_residual_available
219232
x = get_x0(nls)
220233
Fx = Vector{eltype(x)}(undef, get_nequ(nls))
221234
g = similar(x)
@@ -226,16 +239,16 @@ function test_obj_grad!(nlp_allocations, nls::AbstractNLSModel, exclude)
226239
end
227240

228241
"""
229-
test_allocs_nlsmodels(nlp::AbstractNLSModel; exclude = [])
242+
test_allocs_nlsmodels(nls::AbstractNLSModel; exclude = [])
230243
231244
Returns a `Dict` containing allocations of the in-place functions specialized to nonlinear least squares of NLPModel API.
232245
233246
The keyword `exclude` takes a Array of Function to be excluded from the tests.
234247
Use `hess_residual` (resp. `jac_residual`) to exclude `hess_residual_coord` and `hess_residual_structure` (resp. `jac_residual_coord` and `jac_residual_structure`).
235248
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.
236249
"""
237-
function test_allocs_nlsmodels(nlp::AbstractNLSModel; exclude = [])
238-
nlp_allocations = Dict(
250+
function test_allocs_nlsmodels(nls::AbstractNLSModel; exclude = [])
251+
nls_allocations = Dict(
239252
:residual! => NaN,
240253
:hess_structure_residual! => NaN,
241254
:hess_coord_residual! => NaN,
@@ -250,79 +263,85 @@ function test_allocs_nlsmodels(nlp::AbstractNLSModel; exclude = [])
250263
)
251264

252265
if !(residual in exclude)
253-
x = get_x0(nlp)
254-
Fx = Vector{eltype(x)}(undef, get_nequ(nlp))
255-
residual!(nlp, x, Fx)
256-
nlp_allocations[:residual!] = @allocated residual!(nlp, x, Fx)
266+
x = get_x0(nls)
267+
Fx = Vector{eltype(x)}(undef, get_nequ(nls))
268+
residual!(nls, x, Fx)
269+
nls_allocations[:residual!] = @allocated residual!(nls, x, Fx)
257270
end
258-
if !(jac_residual in exclude)
259-
rows = Vector{Int}(undef, nlp.nls_meta.nnzj)
260-
cols = Vector{Int}(undef, nlp.nls_meta.nnzj)
261-
jac_structure_residual!(nlp, rows, cols)
262-
nlp_allocations[:jac_structure_residual!] = @allocated jac_structure_residual!(nlp, rows, cols)
263-
x = get_x0(nlp)
264-
vals = Vector{eltype(x)}(undef, nlp.nls_meta.nnzj)
265-
jac_coord_residual!(nlp, x, vals)
266-
nlp_allocations[:jac_coord_residual!] = @allocated jac_coord_residual!(nlp, x, vals)
271+
272+
if !(jac_residual in exclude) && nls_meta(nls).jac_residual_available
273+
rows = Vector{Int}(undef, nls.nls_meta.nnzj)
274+
cols = Vector{Int}(undef, nls.nls_meta.nnzj)
275+
jac_structure_residual!(nls, rows, cols)
276+
nls_allocations[:jac_structure_residual!] = @allocated jac_structure_residual!(nls, rows, cols)
277+
x = get_x0(nls)
278+
vals = Vector{eltype(x)}(undef, nls.nls_meta.nnzj)
279+
jac_coord_residual!(nls, x, vals)
280+
nls_allocations[:jac_coord_residual!] = @allocated jac_coord_residual!(nls, x, vals)
267281
end
268-
if !(jprod_residual in exclude)
269-
x = get_x0(nlp)
282+
283+
if !(jprod_residual in exclude) && nls_meta(nls).jprod_residual_available
284+
x = get_x0(nls)
270285
v = copy(x)
271-
Jv = Vector{eltype(x)}(undef, get_nequ(nlp))
272-
jprod_residual!(nlp, x, v, Jv)
273-
nlp_allocations[:jprod_residual!] = @allocated jprod_residual!(nlp, x, v, Jv)
286+
Jv = Vector{eltype(x)}(undef, get_nequ(nls))
287+
jprod_residual!(nls, x, v, Jv)
288+
nls_allocations[:jprod_residual!] = @allocated jprod_residual!(nls, x, v, Jv)
274289
end
275-
if !(jtprod_residual in exclude)
276-
x = get_x0(nlp)
277-
w = zeros(eltype(x), get_nequ(nlp))
290+
291+
if !(jtprod_residual in exclude) && nls_meta(nls).jtprod_residual_available
292+
x = get_x0(nls)
293+
w = zeros(eltype(x), get_nequ(nls))
278294
Jtv = similar(x)
279-
jtprod_residual!(nlp, x, w, Jtv)
280-
nlp_allocations[:jtprod_residual!] = @allocated jtprod_residual!(nlp, x, w, Jtv)
295+
jtprod_residual!(nls, x, w, Jtv)
296+
nls_allocations[:jtprod_residual!] = @allocated jtprod_residual!(nls, x, w, Jtv)
281297
end
282-
if !(jac_op_residual in exclude)
283-
x = get_x0(nlp)
298+
299+
if !(jac_op_residual in exclude) && nls_meta(nls).jprod_residual_available && nls_meta(nls).jtprod_residual_available
300+
x = get_x0(nls)
284301
Jtv = similar(x)
285-
Jv = Vector{eltype(x)}(undef, get_nequ(nlp))
302+
Jv = Vector{eltype(x)}(undef, get_nequ(nls))
286303

287304
v = copy(x)
288-
w = zeros(eltype(x), get_nequ(nlp))
289-
J = jac_op_residual!(nlp, x, Jv, Jtv)
305+
w = zeros(eltype(x), get_nequ(nls))
306+
J = jac_op_residual!(nls, x, Jv, Jtv)
290307
mul!(Jv, J, v)
291-
nlp_allocations[:jac_op_residual_prod!] = @allocated mul!(Jv, J, v)
308+
nls_allocations[:jac_op_residual_prod!] = @allocated mul!(Jv, J, v)
292309
Jt = J'
293310
mul!(Jtv, Jt, w)
294-
nlp_allocations[:jac_op_residual_transpose_prod!] = @allocated mul!(Jtv, Jt, w)
311+
nls_allocations[:jac_op_residual_transpose_prod!] = @allocated mul!(Jtv, Jt, w)
295312
end
296-
if !(hess_residual in exclude)
297-
rows = Vector{Int}(undef, nlp.nls_meta.nnzh)
298-
cols = Vector{Int}(undef, nlp.nls_meta.nnzh)
299-
hess_structure_residual!(nlp, rows, cols)
300-
nlp_allocations[:hess_structure_residual!] =
301-
@allocated hess_structure_residual!(nlp, rows, cols)
302-
x = get_x0(nlp)
303-
v = ones(eltype(x), get_nequ(nlp))
304-
vals = Vector{eltype(x)}(undef, nlp.nls_meta.nnzh)
305-
hess_coord_residual!(nlp, x, v, vals)
306-
nlp_allocations[:hess_coord_residual!] = @allocated hess_coord_residual!(nlp, x, v, vals)
313+
314+
if !(hess_residual in exclude) && nls_meta(nls).hess_residual_available
315+
rows = Vector{Int}(undef, nls.nls_meta.nnzh)
316+
cols = Vector{Int}(undef, nls.nls_meta.nnzh)
317+
hess_structure_residual!(nls, rows, cols)
318+
nls_allocations[:hess_structure_residual!] =
319+
@allocated hess_structure_residual!(nls, rows, cols)
320+
x = get_x0(nls)
321+
v = ones(eltype(x), get_nequ(nls))
322+
vals = Vector{eltype(x)}(undef, nls.nls_meta.nnzh)
323+
hess_coord_residual!(nls, x, v, vals)
324+
nls_allocations[:hess_coord_residual!] = @allocated hess_coord_residual!(nls, x, v, vals)
307325
end
308-
for i = 1:get_nequ(nlp)
309-
if !(hprod_residual in exclude)
310-
x = get_x0(nlp)
326+
327+
for i = 1:get_nequ(nls)
328+
if !(hprod_residual in exclude) && nls_meta(nls).hprod_residual_available
329+
x = get_x0(nls)
311330
v = copy(x)
312331
Hv = similar(x)
313-
hprod_residual!(nlp, x, i, v, Hv)
314-
nlp_allocations[:hprod_residual!] = @allocated hprod_residual!(nlp, x, i, v, Hv)
332+
hprod_residual!(nls, x, i, v, Hv)
333+
nls_allocations[:hprod_residual!] = @allocated hprod_residual!(nls, x, i, v, Hv)
315334
end
316-
if !(hess_op_residual in exclude)
317-
x = get_x0(nlp)
335+
if !(hess_op_residual in exclude) && nls_meta(nls).jprod_residual_available
336+
x = get_x0(nls)
318337
Hv = similar(x)
319338
v = copy(x)
320-
H = hess_op_residual!(nlp, x, i, Hv)
339+
H = hess_op_residual!(nls, x, i, Hv)
321340
mul!(Hv, H, v)
322-
nlp_allocations[:hess_op_residual_prod!] = @allocated mul!(Hv, H, v)
341+
nls_allocations[:hess_op_residual_prod!] = @allocated mul!(Hv, H, v)
323342
end
324343
end
325-
return nlp_allocations
344+
return nls_allocations
326345
end
327346

328347
function NLPModels.histline(s::String, v::Integer, maxv::Integer)

0 commit comments

Comments
 (0)