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

Concrete return type of BMI.get_value_ptr #2018

Merged
merged 2 commits into from
Jan 27, 2025
Merged

Concrete return type of BMI.get_value_ptr #2018

merged 2 commits into from
Jan 27, 2025

Conversation

visr
Copy link
Member

@visr visr commented Jan 24, 2025

This aligns better with the updated expectations in https://github.com/Deltares/BasicModelInterface.jl/releases/tag/v0.1.1.

This means we don't return SubArrays or similar anymore, but always a simple Vector{Float64}.

This aligns better with the updated expectations in https://github.com/Deltares/BasicModelInterface.jl/releases/tag/v0.1.1.

This means we don't return `SubArrays` or similar anymore, but always a simple `Vector{Float64}`.
Copy link
Member

@evetion evetion left a comment

Choose a reason for hiding this comment

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

Can you comment/link on the actual issue solved here? As in, I get that returning a SubArray is bad implicitly here, but why?

core/src/util.jl Outdated Show resolved Hide resolved
core/src/util.jl Outdated
function unsafe_array(
A::SubArray{Float64, 1, Vector{Float64}, Tuple{UnitRange{Int64}}, true},
)::Vector{Float64}
unsafe_wrap(Array, pointer(A), length(A))
Copy link
Member

Choose a reason for hiding this comment

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

This probably requires a GC.@preserve A?

Copy link
Member Author

Choose a reason for hiding this comment

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

That would only be needed if A is at risk of GC right? In which case this shouldn't be called anyway.

Copy link
Member

Choose a reason for hiding this comment

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

Well I rather program defensively than making those assumptions. Worst case you don't get a crash, but silent corruption. I don't think the macro has downsides for us here.

Copy link
Member Author

Choose a reason for hiding this comment

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

It doesn't have downsides, but I don't think it helps us here. The docs mention:

Mark the objects x1, x2, ... as being in use during the evaluation of the expression expr.

So if we return from this function this won't apply anymore I think.
I confirmed this with a little experiment:

f(a) = unsafe_wrap(Array, pointer(a), length(a))
g(a) = GC.@preserve a unsafe_wrap(Array, pointer(a), length(a))

a = collect(1:9)
b = f(a)  # f or g give the same results
a = 2  # deref the original a
GC.gc()  # a bunch of times
b  # the data changes after a while

core/src/bmi.jl Outdated Show resolved Hide resolved
core/src/parameter.jl Show resolved Hide resolved
@visr
Copy link
Member Author

visr commented Jan 27, 2025

Can you comment/link on the actual issue solved here?

The type stability was indeed not directly giving us problems that I know of. But I want to keep those functions as simple as possible, and when trying out juliac this was the first thing it would fail on as well.

@visr visr merged commit be5cf52 into main Jan 27, 2025
23 of 25 checks passed
@visr visr deleted the bmi-ptr branch January 27, 2025 20:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants