Skip to content

Commit

Permalink
Use the Julia wrappers in CUTEst.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
amontoison committed Aug 16, 2024
1 parent 9a6ef3a commit 57b7cff
Show file tree
Hide file tree
Showing 4 changed files with 244 additions and 352 deletions.
19 changes: 18 additions & 1 deletion gen/wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,25 @@ function main()
code = replace(code, "Ptr{rpc_}" => "Ptr{Float64}")
code = replace(code, "Ptr{ip_}" => "Ptr{Cint}")
code = replace(code, "Ptr{ipc_}" => "Ptr{Cint}")
write(path, code)
code = code *
"
function cutest_uofg_(status, n, x, f, g, grad)
@ccall cutest_lib_path.cutest_uofg_(status::Ptr{Cint}, n::Ptr{Cint}, x::Ptr{Float64}, f::Ptr{Float64}, g::Ptr{Float64}, grad::Ptr{Bool})::Cvoid
end
function cutest_uofg_s_(status, n, x, f, g, grad)
@ccall cutest_lib_path.cutest_uofg_(status::Ptr{Cint}, n::Ptr{Cint}, x::Ptr{Float32}, f::Ptr{Float32}, g::Ptr{Float32}, grad::Ptr{Bool})::Cvoid
end
function cutest_uhprod_(status, n, goth, x, vector, result)
@ccall cutest_lib_path.cutest_uhprod_(status::Ptr{Cint}, n::Ptr{Cint}, goth::Ptr{Cint}, x::Ptr{Float64}, vector::Ptr{Float64}, result::Ptr{Float64})::Cvoid
end
function cutest_uhprod_s(status, n, goth, x, vector, result)
@ccall cutest_lib_path.cutest_uhprod_(status::Ptr{Cint}, n::Ptr{Cint}, goth::Ptr{Float32}, x::Ptr{Float32}, vector::Ptr{Float32}, result::Ptr{Float32})::Cvoid
end
"
write(path, code)
format_file(path, YASStyle())

code = read(path, String)
Expand Down
114 changes: 53 additions & 61 deletions src/CUTEst.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ end

CUTEstException(info::Integer) = CUTEstException(convert(Int32, info))

macro cutest_error() # Handle nonzero exit codes.
esc(:(io_err[1] > 0 && throw(CUTEstException(io_err[1]))))
function cutest_error(status::Cint) # Handle nonzero exit codes.
(status > 0) && throw(CUTEstException(status))
end

# to allow view inputs with stride one
StrideOneVector{T} =
Union{Vector{T}, SubArray{T, 1, Vector{T}, Tuple{UnitRange{U}}, true} where {U <: Integer}}
Union{Ref{T}, Vector{T}, SubArray{T, 1, Vector{T}, Tuple{UnitRange{U}}, true} where {U <: Integer}}

include("core_interface.jl")
include("julia_interface.jl")
Expand Down Expand Up @@ -287,7 +287,6 @@ function CUTEstModel(
outsdif = "OUTSDIF_$pname.d"
global cutest_instances
cutest_instances > 0 && error("CUTEst: call finalize on current model first")
io_err = Cint[0]
global cutest_lib
cd(cutest_problems_path) do
if !decode
Expand All @@ -298,47 +297,40 @@ function CUTEstModel(
else
sifdecoder(path_sifname, args..., verbose = verbose, outsdif = outsdif)
end
ccall(
dlsym(cutest_lib, :fortran_open_),
Nothing,
(Ref{Int32}, Ptr{UInt8}, Ptr{Int32}),
[funit],
outsdif,
io_err,
)
@cutest_error
status = Ref{Cint}(0)
fortran_open_(Ref{Cint}(funit), outsdif, status)
cutest_error(status[])
end

# Obtain problem size.
nvar = Cint[0]
ncon = Cint[0]

cdimen(io_err, [funit], nvar, ncon)
@cutest_error
nvar = nvar[1]
ncon = ncon[1]

x = Vector{Float64}(undef, nvar)
bl = Vector{Float64}(undef, nvar)
bu = Vector{Float64}(undef, nvar)
v = Vector{Float64}(undef, ncon)
cl = Vector{Float64}(undef, ncon)
cu = Vector{Float64}(undef, ncon)
equatn = Vector{Int32}(undef, ncon)
linear = Vector{Int32}(undef, ncon)

if ncon > 0
e_order = efirst ? Cint[1] : Cint[0]
l_order = lfirst ? Cint[1] : Cint[0]
v_order = lvfirst ? Cint[1] : Cint[0]
status = Ref{Cint}(0)
nvar = Ref{Cint}(0)
ncon = Ref{Cint}(0)

cdimen(status, Ref{Cint}(funit), nvar, ncon)
cutest_error(status[])

x = Vector{Float64}(undef, nvar[])
bl = Vector{Float64}(undef, nvar[])
bu = Vector{Float64}(undef, nvar[])
v = Vector{Float64}(undef, ncon[])
cl = Vector{Float64}(undef, ncon[])
cu = Vector{Float64}(undef, ncon[])
equatn = Vector{Int32}(undef, ncon[])
linear = Vector{Int32}(undef, ncon[])

if ncon[] > 0
e_order = efirst ? Ref{Cint}(1) : Ref{Cint}(0)
l_order = lfirst ? Ref{Cint}(1) : Ref{Cint}(0)
v_order = lvfirst ? Ref{Cint}(1) : Ref{Cint}(0)
# Equality constraints first, linear constraints first, nonlinear variables first.
csetup(
io_err,
[funit],
Cint[0],
Cint[6],
[nvar],
[ncon],
status,
Ref{Cint}(funit),
Ref{Cint}(0),
Ref{Cint}(6),
nvar,
ncon,
x,
bl,
bu,
Expand All @@ -352,9 +344,9 @@ function CUTEstModel(
v_order,
)
else
usetup(io_err, [funit], Cint[0], Cint[6], [nvar], x, bl, bu)
usetup(status, Ref{Cint}(funit), Ref{Cint}(0), Ref{Cint}(6), nvar, x, bl, bu)
end
@cutest_error
cutest_error(status[])

for lim in Any[bl, bu, cl, cu]
I = findall(abs.(lim) .>= 1e20)
Expand All @@ -364,26 +356,26 @@ function CUTEstModel(
lin = findall(linear .!= 0)
nlin = length(lin)

nnzh = Cint[0]
nnzj = Cint[0]
nnzh = Ref{Cint}(0)
nnzj = Ref{Cint}(0)

if ncon > 0
cdimsh(io_err, nnzh)
cdimsj(io_err, nnzj)
nnzj[1] -= nvar # nnzj also counts the nonzeros in the objective gradient.
if ncon[] > 0
cdimsh(status, nnzh)
cdimsj(status, nnzj)
nnzj[] -= nvar # nnzj also counts the nonzeros in the objective gradient.
else
udimsh(io_err, nnzh)
udimsh(status, nnzh)
end
@cutest_error

nnzh = Int(nnzh[1])
nnzj = Int(nnzj[1])
cutest_error(status[])

ccall(dlsym(cutest_lib, :fortran_close_), Nothing, (Ref{Int32}, Ptr{Int32}), funit, io_err)
@cutest_error
fortran_close_(Ref{Cint}(funit), status)
cutest_error(status[])

ncon = Int(ncon)
nvar = Int(nvar)
# Remove references
nvar = nvar[] |> Int
ncon = ncon[] |> Int
nnzj = nnzj[] |> Int
nnzh = nnzh[] |> Int

lin_nnzj = min(nvar * nlin, nnzj)
nln_nnzj = min(nvar * (ncon - nlin), nnzj)
Expand Down Expand Up @@ -450,13 +442,13 @@ function cutest_finalize(nlp::CUTEstModel)
global cutest_instances
cutest_instances == 0 && return
global cutest_lib
io_err = Cint[0]
status = Ref{Cint}(0)
if nlp.meta.ncon > 0
cterminate(io_err)
cterminate(status)
else
uterminate(io_err)
uterminate(status)
end
@cutest_error
cutest_error(status[])
Libdl.dlclose(cutest_lib)
cutest_instances -= 1
cutest_lib = C_NULL
Expand Down
Loading

0 comments on commit 57b7cff

Please sign in to comment.