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 25, 2019
1 parent 2e36d26 commit 3fa270e
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

2 comments on commit 3fa270e

@dpo
Copy link
Member Author

@dpo dpo commented on 3fa270e Dec 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/7156

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.1 -m "<description of version>" 3fa270e06fd688358f208337c130ceb38e0ffd1e
git push origin v0.6.1

Please sign in to comment.