Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unsafe_convert method for CbcProblem. Resolve #100. #101

Merged
merged 5 commits into from
Jan 31, 2019

Conversation

tkoolen
Copy link
Contributor

@tkoolen tkoolen commented Jan 31, 2019

No description provided.

@mlubin
Copy link
Member

mlubin commented Jan 31, 2019

Thanks for the fix. I'd like a decisive answer about whether function arguments need to be preserved because if that's the case we need to make a lot of changes to wrappers. I wasn't able to follow the post you linked at #100 (comment).

src/CbcCInterface.jl Outdated Show resolved Hide resolved
src/CbcCInterface.jl Outdated Show resolved Hide resolved
@tkoolen
Copy link
Contributor Author

tkoolen commented Jan 31, 2019

I used GC.@preserve on any arguments that were ::Integer (because they could be e.g. BigInt), but on second thought, that might not be necessary, because they're passed as Cint, and isbitstype(typeof(Base.cconvert(Cint, big"1"))).

end

function writeMps(prob::CbcModel, filename::String)
check_problem(prob)
@assert isascii(filename)
@cbc_ccall writeMps Cint (Ptr{Cvoid}, Ptr{UInt8}) prob.p filename
GC.@preserve prob filename begin
Copy link
Member

Choose a reason for hiding this comment

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

filename is a direct argument, no preserve needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So this one (and the one you commented on below) I'm not sure of. I think it could currently still be an issue if writeMps happens to get inlined, and given Stefan's comment, there don't seem to be future guarantees regarding input arguments anyway. Again, I'm not sure of the cost of GC.@preserve, but I'd assume it'd be negligible compared to the ccall.

Copy link
Member

Choose a reason for hiding this comment

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

The documentation makes a strong statement about ccall arguments: https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/#Garbage-Collection-Safety-1

ccall automatically arranges that all of its arguments will be preserved from garbage collection until the call returns.

I interpret that to mean that if this behavior changes it can only do so In a new major Julia release. I agree that the extra preserve is unlikely to have a performance impact. I'm nit picking so that we apply consistent rules and don't confuse someone who looks at this code and wonders why we preserved this particular argument when it's not needed.

Choose a reason for hiding this comment

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

Would it be cleaner to define a convert(::Type{Ptr{Cvoid}},::CbcModel) method and just pass prob to ccall instead? See https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/#Auto-conversion:-1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm fully onboard with the nitpicking here since this has ramifications for many other packages. I think @andreasnoack's idea is a good one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, according to https://docs.julialang.org/en/v1/base/c/#Base.cconvert,

Neither convert nor cconvert should take a Julia object and turn it into a Ptr

So I think this should be a Base.unsafe_convert method.

src/CbcCInterface.jl Outdated Show resolved Hide resolved
src/CbcCInterface.jl Outdated Show resolved Hide resolved
@tkoolen tkoolen changed the title Use GC.@preserve where appropriate. Resolve #100. Add unsafe_convert method for CbcProblem. Resolve #100. Jan 31, 2019
@mlubin mlubin merged commit 489db06 into jump-dev:master Jan 31, 2019
@mlubin
Copy link
Member

mlubin commented Jan 31, 2019

Thanks for the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants