Skip to content

Commit

Permalink
fix passing Ref{Cint} to C interface
Browse files Browse the repository at this point in the history
thanks @vtjnash
closes #88
  • Loading branch information
dpo committed Dec 24, 2019
1 parent 2e36d26 commit 9fba7ec
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "AmplNLReader"
uuid = "77dd3d4c-cb1d-5e09-9340-85030ff7ba66"
version = "0.6.0"
version = "0.6.1"

[deps]
ASL_jll = "ae81ac8f-d209-56e5-92de-9978fef736f9"
Expand All @@ -11,7 +11,7 @@ Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[compat]
ASL_jll = "^0.1.0"
ASL_jll = "^0.1.1"
NLPModels = "0.10.0, 1"
julia = "^1.3.0"

Expand Down
40 changes: 20 additions & 20 deletions src/ampl_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,27 +145,27 @@ function NLPModels.varscale(nlp :: AmplModel, s :: Vector{Cdouble})
@check_ampl_model
length(s) >= nlp.meta.nvar || error("s must have length at least $(nlp.meta.nvar)")

err = Cint(0)
err = Ref{Cint}(0)
@asl_call(:asl_varscale, Nothing, (Ptr{Nothing}, Ptr{Cdouble}, Ref{Cint}), nlp.__asl, s, err)
err == 0 || throw(AmplException("Error while scaling variables"))
err[] == 0 || throw(AmplException("Error while scaling variables"))
end

NLPModels.varscale(nlp :: AmplModel, s :: AbstractVector) = varscale(nlp, Vector{Cdouble}(s))

function NLPModels.lagscale(nlp :: AmplModel, σ :: Float64)
@check_ampl_model
err = Cint(0)
err = Ref{Cint}(0)
@asl_call(:asl_lagscale, Nothing, (Ptr{Nothing}, Cdouble, Ref{Cint}), nlp.__asl, σ, err)
err == 0 || throw(AmplException("Error while scaling Lagrangian"))
err[] == 0 || throw(AmplException("Error while scaling Lagrangian"))
end

function NLPModels.conscale(nlp :: AmplModel, s :: Vector{Cdouble})
@check_ampl_model
length(s) >= nlp.meta.ncon || error("s must have length at least $(nlp.meta.ncon)")

err = Cint(0)
err = Ref{Cint}(0)
@asl_call(:asl_conscale, Nothing, (Ptr{Nothing}, Ptr{Cdouble}, Ref{Cint}), nlp.__asl, s, err)
err == 0 || throw(AmplException("Error while scaling constraints"))
err[] == 0 || throw(AmplException("Error while scaling constraints"))
end

NLPModels.conscale(nlp :: AmplModel, s :: AbstractVector) = conscale(nlp, Vector{Cdouble}(s))
Expand All @@ -176,10 +176,10 @@ function NLPModels.obj(nlp :: AmplModel, x :: Vector{Cdouble})
@check_ampl_model
length(x) >= nlp.meta.nvar || error("x must have length at least $(nlp.meta.nvar)")

err = Cint(0)
err = Ref{Cint}(0)
f = @asl_call(:asl_obj, Float64, (Ptr{Nothing}, Ptr{Cdouble}, Ref{Cint}), nlp.__asl, x, err)
nlp.counters.neval_obj += 1
err == 0 || throw(AmplException("Error while evaluating objective"))
err[] == 0 || throw(AmplException("Error while evaluating objective"))
return f
end

Expand All @@ -189,12 +189,12 @@ function NLPModels.grad!(nlp :: AmplModel, x :: Vector{Cdouble}, g :: Vector{Cdo
@check_ampl_model
length(x) >= nlp.meta.nvar || error("x must have length at least $(nlp.meta.nvar)")

err = Cint(0)
err = Ref{Cint}(0)
@asl_call(:asl_grad, Nothing,
(Ptr{Nothing}, Ptr{Cdouble}, Ptr{Cdouble}, Ref{Cint}),
nlp.__asl, x, g, err)
nlp.counters.neval_grad += 1
err == 0 || throw(AmplException("Error while evaluating objective gradient"))
err[] == 0 || throw(AmplException("Error while evaluating objective gradient"))
return g
end

Expand All @@ -209,12 +209,12 @@ function NLPModels.cons!(nlp :: AmplModel, x :: Vector{Cdouble}, c :: Vector{Cdo
@check_ampl_model
length(x) >= nlp.meta.nvar || error("x must have length at least $(nlp.meta.nvar)")

err = Cint(0)
err = Ref{Cint}(0)
@asl_call(:asl_cons, Nothing,
(Ptr{Nothing}, Ptr{Cdouble}, Ptr{Cdouble}, Ref{Cint}),
nlp.__asl, x, c, err)
nlp.counters.neval_cons += 1
err == 0 || throw(AmplException("Error while evaluating constraints"))
err[] == 0 || throw(AmplException("Error while evaluating constraints"))
return c
end

Expand All @@ -230,12 +230,12 @@ function NLPModels.jth_con(nlp :: AmplModel, x :: Vector{Cdouble}, j :: Int)
(1 <= j <= nlp.meta.ncon) || error("expected 0 ≤ j ≤ $(nlp.meta.ncon)")
length(x) >= nlp.meta.nvar || error("x must have length at least $(nlp.meta.nvar)")

err = Cint(0)
err = Ref{Cint}(0)
cj = @asl_call(:asl_jcon, Float64,
(Ptr{Nothing}, Ptr{Cdouble}, Int32, Ref{Cint}),
nlp.__asl, x, j-1, err)
nlp.counters.neval_jcon += 1
err == 0 || throw(AmplException("Error while evaluating $j-th constraint"))
err[] == 0 || throw(AmplException("Error while evaluating $j-th constraint"))
return cj
end

Expand All @@ -246,12 +246,12 @@ function NLPModels.jth_congrad!(nlp :: AmplModel, x :: Vector{Cdouble}, j :: Int
(1 <= j <= nlp.meta.ncon) || error("expected 0 ≤ j ≤ $(nlp.meta.ncon)")
length(x) >= nlp.meta.nvar || error("x must have length at least $(nlp.meta.nvar)")

err = Cint(0)
err = Ref{Cint}(0)
@asl_call(:asl_jcongrad, Nothing,
(Ptr{Nothing}, Ptr{Cdouble}, Ptr{Cdouble}, Int32, Ref{Cint}),
nlp.__asl, x, g, j-1, err)
nlp.counters.neval_jgrad += 1
err == 0 || throw(AmplException("Error while evaluating $j-th constraint gradient"))
err[] == 0 || throw(AmplException("Error while evaluating $j-th constraint gradient"))
return g
end

Expand All @@ -270,14 +270,14 @@ function NLPModels.jth_sparse_congrad(nlp :: AmplModel, x :: Vector{Cdouble}, j
nnz = @asl_call(:asl_sparse_congrad_nnz, Csize_t,
(Ptr{Nothing}, Cint), nlp.__asl, j-1)

err = Cint(0)
err = Ref{Cint}(0)
inds = Vector{Cint}(undef, nnz)
vals = Vector{Cdouble}(undef, nnz)
@asl_call(:asl_sparse_congrad, Nothing,
(Ptr{Nothing}, Ptr{Cdouble}, Int32, Ptr{Cint}, Ptr{Cdouble}, Ref{Cint}),
nlp.__asl, x, j-1, inds, vals, err)
nlp.counters.neval_jgrad += 1
err == 0 || throw(AmplException("Error while evaluating $j-th sparse constraint gradient"))
err[] == 0 || throw(AmplException("Error while evaluating $j-th sparse constraint gradient"))
# Use 1-based indexing.
@. inds += Cint(1)
return sparsevec(inds, vals, nlp.meta.nvar)
Expand Down Expand Up @@ -310,12 +310,12 @@ function NLPModels.jac_coord!(nlp :: AmplModel, x :: Vector{Cdouble}, vals :: Ve

_ = cons(nlp, x) ; nlp.counters.neval_cons -= 1

err = Cint(0)
err = Ref{Cint}(0)
@asl_call(:asl_jacval, Nothing,
(Ptr{Nothing}, Ptr{Cdouble}, Ptr{Cdouble}, Ref{Cint}),
nlp.__asl, x, vals, err)
nlp.counters.neval_jac += 1
err == 0 || throw(AmplException("Error while evaluating constraints Jacobian"))
err[] == 0 || throw(AmplException("Error while evaluating constraints Jacobian"))
return vals
end

Expand Down

0 comments on commit 9fba7ec

Please sign in to comment.