-
Notifications
You must be signed in to change notification settings - Fork 55
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
Why is reinterpret
prohibited?
#289
Comments
Maybe look at the git blame for that to see if it points to some discussion? I suppose that reinterpret on values would be quite usable. |
Follow up: I tried to write my own specialized method to circumvent the general prohibition, but the method failed because SparseArrays.jl/src/sparsematrix.jl Line 27 in 311b4b4
Perhaps that could be relaxed with suitable checking of 1-based indexing etc. import Base: reinterpret
using SparseArrays
function reinterpret(E::DataType, A::SparseMatrixCSC{<:Number, Int64})
tmp = reinterpret(E, A.nzval)
SparseMatrixCSC(A.m, A.n, A.colptr, A.rowval, tmp)
end I will look into the git history! |
I found a |
Switching to AbstractVector would require a new parameter, and to avoid breakage we didn't want to introduce that in the 1.x series. The parameter isn't so much an issue as downstream packages expecting Vector not AbstractVector We could look into doing that for 1.10, or perhaps as a separate package (I'll have more on that later this week). |
I concocted the following kludgey work-around for now using import Base: reinterpret
using SparseArrays
function reinterpret(T::DataType, A::SparseMatrixCSC{<:Number, Int64})
tmp = reinterpret(T, A.nzval)
v = Base.unsafe_wrap(Array, pointer(tmp), length(tmp))
SparseMatrixCSC(A.m, A.n, A.colptr, A.rowval, v)
end
using Unitful: s
A = spdiagm(Float32.(1:3)*s^2) # unitful sparse array
T = eltype(A / oneunit(A))
B = reinterpret(T, A) I look forward to better options :) |
This is really dangerous I think. A must stay alive otherwise you will segfault. |
If there is a specialized method you want to avoid calling, using Unitful: s
using SparseArrays
A = spdiagm(Float32.(1:3)*s^2) # unitful sparse array
T = eltype(A / oneunit(A))
B = invoke(reinterpret, Tuple{Type{T}, AbstractArray{eltype(A), ndims(A)}}, T, A) While this solves the OP's use case, it does not close the issue of "Why is The git blame on this is hard to find, but here it is: JuliaLang/julia#23750 (comment). I was just looking into it because I came across this issue in a different context. Fundamentally, the issue is this: julia> s = spzeros(3)
3-element SparseVector{Float64, Int64} with 0 stored entries
julia> rs = invoke(reinterpret, Tuple{Type{Int}, AbstractVector{Float64}}, Int, s)
3-element reinterpret(Int64, ::SparseVector{Float64, Int64}):
0
0
0
julia> x = reinterpret(Int, -0.0)
-9223372036854775808
julia> rs[1] = x # Line 4
-9223372036854775808
julia> rs[1] == x
false
julia> rs
3-element reinterpret(Int64, ::SparseVector{Float64, Int64}):
0
0
0 On line 4, we reinterpret x back into a Float64 (-0.0), which iszero ( I think that this total prohibition is excessive and would prefer to throw only when performing a |
Implementing the behavior @StefanKarpinski suggested in #55 (comment) would fix this problem and allow us to use standard |
Hi @LilithHafner thanks so much for explaining about |
IMHO anyone who does this should expect to get weird results! |
reinterpret has well-defined semantics, and so long as we conform to them it allows for certain optimizations. It's useful for, among other things, sorting:
Agreed
Half a loaf is better than no loaf. |
@LilithHafner thank you for your work on this and your persistence in the PR :) |
1.10. we need to get it onto Julia master with other things and let it all get tested out there. |
No, this will be backported to v1.9 for some sorting reason, I believe. |
Yep. It fixes this regression: #304 & JuliaLang/julia#48079 |
I have a Unitful sparse array that I would like to
reinterpret
as a non-unitful sparse array so that I can pass it to a method that sadly will not accept unitful spars data (lldl
in LimitedLDLFactorizations FWIW).But for some (undocumented?) reason this package is (unnecessarily?) completely prohibiting
reinterpret
here:SparseArrays.jl/src/abstractsparse.jl
Line 80 in 311b4b4
Here is my MWE of what I would like to do (which fails at the above line):
My view is that
reinterpret
is something that one uses with some trepidation and this package should let users attempt it at their own risk, instead of prohibiting it. Specifically I would like to allow reinterprets fromSparseMatrixCSC{T1, Int64}
toSparseMatrixCSC{T2, Int64}
or some generalization thereof. Is there openness to some cases ofreinterpret
being allowed? Or is there some huge risk I am overlooking?The text was updated successfully, but these errors were encountered: