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

reinterpret-like behavior on a SubArray? #16652

Closed
quinnj opened this issue May 29, 2016 · 5 comments
Closed

reinterpret-like behavior on a SubArray? #16652

quinnj opened this issue May 29, 2016 · 5 comments

Comments

@quinnj
Copy link
Member

quinnj commented May 29, 2016

I've been playing with sub a lot lately with a use-case around flipping between a "memory buffer" (Vector{UInt8}) and arrays, and find myself running into cases where I essentially want to do something like:

A = Vector{UInt8}(x)  # initial buffer

x = reinterpret(Int32, A)[offset] # get an Int32 at a given offset, currently possible, works fine

subset = sub(A, off1:off2)

y = reinterpret(Int32, subset)[offset2] # not currently possible to reinterpret a SubArray

in the second case, it being easier to just deal with a specific sub-region.

I wonder if we could possibly have a SubArray constructor like:

sub{T}(::Type{T}, arr, dims)

but maybe that would be weird because you're not sure if dims would apply to the parent array or as it would be interpreted by T.

Maybe we could just allow reinterpret on a SubArray?

I also realize that I may have just not thought long enough about this and am over-thinking it here.

@vtjnash
Copy link
Sponsor Member

vtjnash commented May 30, 2016

I doubt that it's usually valid to attempt to reinterpret a SubArray, as it is likely assuming quite a bit about how the underlying array is arranged in memory.

@timholy
Copy link
Sponsor Member

timholy commented May 31, 2016

You'd need arr to have contiguous layout. In that case it might be reasonable.

@quinnj quinnj closed this as completed Aug 7, 2017
@quinnj
Copy link
Member Author

quinnj commented Aug 7, 2017

I don't think this is really that important.

@timholy
Copy link
Sponsor Member

timholy commented Aug 7, 2017

Also worth noting that MappedArrays.jl makes it possible to have reinterpret-like functionality (and more) for arbitrary AbstractArrays. I use it all the time that way, see e.g. the rawview and normedview functions in ImageCore. colorview and channelview are more complex examples where the number of elements "apparently" changes.

@JaredCrean2
Copy link
Contributor

The following script fails due to lack of reinterpret for SubArray:

A = rand(Complex128, 3, 3, 2)
Av = view(A, :, :, 1)

B = rand(3,3)
C = zeros(Complex128, 3,3)

A_mul_B!(C, Av, B)

with error

ERROR: LoadError: MethodError: no method matching reinterpret(::Type{Float64}, ::SubArray{Complex{Float64},2,Array{Complex{Float64},3},Tuple{Base.Slice{Base.OneTo{Int64}},Base.Slice{Base.OneTo{Int64}},Int64},true}, ::Tuple{Int64,Int64})
Closest candidates are:
  reinterpret(::Type{T}, !Matched::Array{S,N} where N, ::Tuple{Vararg{Int64,N}}) where {N, S, T} at array.jl:143
  reinterpret(::Type{T}, !Matched::Base.ReshapedArray, ::Tuple{Vararg{Int64,N}} where N) where T at reshapedarray.jl:171
  reinterpret(::Type{T}, !Matched::SparseMatrixCSC{Tv,Ti}, ::Tuple{Vararg{Int64,N}}) where {T, Tv, Ti, N} at sparse/sparsematrix.jl:210
  ...
Stacktrace:
 [1] A_mul_B!(::Array{Complex{Float64},2}, ::SubArray{Complex{Float64},2,Array{Complex{Float64},3},Tuple{Base.Slice{Base.OneTo{Int64}},Base.Slice{Base.OneTo{Int64}},Int64},true}, ::Array{Float64,2}) at ./linalg/matmul.jl:154
 [2] include_from_node1(::String) at ./loading.jl:552
 [3] include(::String) at ./sysimg.jl:14
 [4] process_options(::Base.JLOptions) at ./client.jl:305
 [5] _start() at ./client.jl:371
while loading /tmp/tmp.jl, in expression starting on line 7

Some of the Blas wrappers try to reinterpret a complex array as real to do complex time real multiplicataion.

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

No branches or pull requests

4 participants