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

Mark SubArrays as invalid outputs for IDAIntegrator(out, t) #434

Merged
merged 6 commits into from
Dec 10, 2023

Conversation

staticfloat
Copy link
Contributor

Let's imagine we have a matrix that we are filling out, column by column with integrator(t), but we realize we want to use the non-allocating version. We try to use integrator(out[:, idx], t), and then realize that of course we need to make that a view because otherwise slicing just creates a copy. Imagine our surprise when the view doesn't work either. Indeed:

julia> out = zeros(3)
       outv = @view(out[:])
       nv = convert(Sundials.NVector, outv)
       nv[1] = 1.0
       out
3-element Vector{Float64}:
 0.0
 0.0
 0.0

The issue, as it turns out, is due to the convert(NVector, x) overload automatically making a copy of non-plain Vector types. I understand the reasoning behind this, as the backing memory store needs to be contiguous memory. It may be possible to write a correct dispatch for SubArrays that are contiguous vectors, but I'm not sure how to do that, and so have opted instead to just make this an error rather than silently return untouched memory.

Let's imagine we have a matrix that we are filling out, column by column with
`integrator(t)`, but we realize we want to use the non-allocating version.
We try to use `integrator(out[:, idx], t)`, and then realize that of course we
need to make that a view because otherwise slicing just creates a copy.
Imagine our surprise when the view doesn't work either.  Indeed:

```julia
julia> out = zeros(3)
       outv = @view(out[:])
       nv = convert(Sundials.NVector, outv)
       nv[1] = 1.0
       out
3-element Vector{Float64}:
 0.0
 0.0
 0.0
```

The issue, as it turns out, is due to the `convert(NVector, x)` overload
automatically making a copy of non-plain `Vector` types.  I understand
the reasoning behind this, as the backing memory store needs to be
contiguous memory.  It may be possible to write a correct dispatch for
`SubArray`s that are contiguous vectors, but I'm not sure how to do
that, and so have opted instead to just make this an error rather than
silently return untouched memory.
Copy link

codecov bot commented Nov 18, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (81fb59a) 81.80% compared to head (e71b24d) 81.33%.
Report is 8 commits behind head on master.

❗ Current head e71b24d differs from pull request most recent head 23d7738. Consider uploading reports for the commit 23d7738 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #434      +/-   ##
==========================================
- Coverage   81.80%   81.33%   -0.48%     
==========================================
  Files          11       11              
  Lines        1440     1441       +1     
==========================================
- Hits         1178     1172       -6     
- Misses        262      269       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ChrisRackauckas ChrisRackauckas merged commit 51d98f9 into SciML:master Dec 10, 2023
1 of 2 checks passed
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