Skip to content

Commit

Permalink
Add unit tests for jth_hess_coord
Browse files Browse the repository at this point in the history
  • Loading branch information
amontoison committed Aug 27, 2024
1 parent f11c19d commit 461bbdc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
11 changes: 7 additions & 4 deletions src/julia_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -751,24 +751,27 @@ end

function NLPModels.jth_hess_coord!(
nlp::CUTEstModel{T},
x::AbstractVector,
x::StrideOneVector{T},
j::Integer,
vals::AbstractVector,
vals::StrideOneVector{T},
) where {T}
increment!(nlp, :neval_jhess)
@rangecheck 1 nlp.meta.ncon j
ref_j = nlp.index
ref_j[] = j
nnzh_output = Ref{Cint}()
cish(
T,
nlp.libsif,
nlp.status,
nlp.nvar,
x,
ref_j,
nlp.nnzh,
nnzh_output,
nlp.nnzh,
vals,
nlp.hrows,
nlp.hcols,
nlp.hrows,
)
return vals
end
18 changes: 11 additions & 7 deletions test/multiple_precision.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
function multiple_precision()
@testset "Test multiple precision models" begin
for (T, precision) in [(Float32, :single), (Float64, :double)]
for T in (Float32, Float64, Float128)
for p in ["ROSENBR", "HS35", "ALLINITA"]
nlp = CUTEstModel(p, precision = precision)
nlp = CUTEstModel{T}(p)
x = T.(nlp.meta.x0)
fx = obj(nlp, x)
@test typeof(obj(nlp, x)) == T
@test eltype(grad(nlp, x)) == T
@test eltype(hess(nlp, x)) == T
@test eltype(cons(nlp, x)) == T
@test eltype(jac(nlp, x)) == T
gx = grad(nlp, x)
Hx = hess(nlp, x)
cx = cons(nlp, x)
Jx = jac(nlp, x)
@test typeof(fx) == T
@test eltype(gx) == T
@test eltype(Hx) == T
@test eltype(cx) == T
@test eltype(Jx) == T
finalize(nlp)
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/nlpmodelstest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ for problem in problems
nlp_manual = eval(Symbol(problem))()
nlps = [nlp_cutest, nlp_manual]

consistent_nlps(nlps, linear_api = true, reimplemented = ["cons", "jac", "jprod", "jtprod"])
consistent_nlps(nlps, linear_api = true, reimplemented = ["cons", "jac", "jprod", "jtprod"], exclude = [jth_hprod, ghjvprod])
view_subarray_nlp(nlp_cutest)
coord_memory_nlp(nlp_cutest, linear_api = true)

Expand Down
28 changes: 13 additions & 15 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ set_mastsif()
@test ispath(ENV["MASTSIF"])
@test isfile(joinpath(ENV["MASTSIF"], "CYCLOOCT.SIF"))
problems = ["BROWNDEN", "HS5", "HS6", "HS10", "HS11", "HS13", "HS14"]
dict_precision = Dict(Float32 => :single, Float64 => :double, Float128 => :quadruple)

# test sifdecoder
for pb in problems
Expand Down Expand Up @@ -38,9 +39,9 @@ include("coverage.jl")
include("multiple_precision.jl")

for problem in problems
for (T, precision) in [(Float32, :single), (Float64, :double), (Float128, :quadruple)]
println("Testing interfaces on problem $problem in $precision precision")
nlp = CUTEstModel(problem, precision = precision)
for T in (Float32, Float64, Float128)
println("Testing interfaces on problem $problem in $(dict_precision[T]) precision")
nlp = CUTEstModel{T}(problem)
nlp_man = eval(Symbol(problem))(T)

test_nlpinterface(nlp, nlp_man)
Expand All @@ -60,23 +61,20 @@ problems = CUTEst.select(max_var = 2, max_con = 2)
problems = randsubseq(problems, 0.1)

for p in problems
for (T, precision) in [(Float32, :single), (Float64, :double), (Float128, :quadruple)]
nlp = CUTEstModel(p, precision = precision)
for T in (Float32, Float64, Float128)
nlp = CUTEstModel{T}(p)
x0 = nlp.meta.x0
nvar, ncon = nlp.meta.nvar, nlp.meta.ncon

println("$p: julia interface: f(x₀) = $(obj(nlp, x0))")

status = Cint[0]
fval = T[0.0]
fval = Ref{T}()
if ncon > 0
cx = zeros(T, ncon)
CUTEst.cfn(T, nlp.libsif, status, Cint[nvar], Cint[ncon], x0, fval, cx)
CUTEst.cfn(T, nlp.libsif, nlp.status, nlp.nvar, nlp.ncon, x0, fval, cx)
else
CUTEst.ufn(T, nlp.libsif, status, Cint[nvar], x0, fval)
CUTEst.ufn(T, nlp.libsif, nlp.status, nlp.nvar, x0, fval)
end
println("$p: core interface: f(x₀) = $(fval[1])")

println("$p: core interface: f(x₀) = $(fval[])")
finalize(nlp)
end
end
Expand All @@ -92,15 +90,15 @@ finalize(nlp)

@testset "Test decoding separately (issue #239)" begin
problems = ["AKIVA", "ROSENBR", "ZANGWIL2"]
for (T, precision) in [(Float32, :single), (Float64, :double), (Float128, :quadruple)]
for T in (Float32, Float64, Float128)
# Decoding
for p in problems
nlp = CUTEstModel(p, precision = precision)
nlp = CUTEstModel{T}(p)
finalize(nlp)
end
# No decode
for p in problems
nlp = CUTEstModel(p, precision = precision, decode = false)
nlp = CUTEstModel{T}(p, decode = false)
@test nlp.meta.nvar == 2
finalize(nlp)
end
Expand Down

0 comments on commit 461bbdc

Please sign in to comment.