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

"shares memory with another argument" error from reinterpreted view #29545

Closed
tkf opened this issue Oct 6, 2018 · 2 comments · Fixed by #30296 · May be fixed by #29546
Closed

"shares memory with another argument" error from reinterpreted view #29545

tkf opened this issue Oct 6, 2018 · 2 comments · Fixed by #30296 · May be fixed by #29546

Comments

@tkf
Copy link
Member

tkf commented Oct 6, 2018

Executing

buffer = zeros(UInt8, 4 * sizeof(Int))
mid = length(buffer) ÷ 2
x1 = reinterpret(Int, @view buffer[1:mid])
x2 = reinterpret(Int, @view buffer[mid+1:end])
x1 .= x2

throws:

ERROR: ArgumentError: an array of type Base.ReinterpretArray shares memory with another argument and must make a preventative copy of itself in order to maintain consistent semantics, but copy(A) returns a new array of type Array{Int64,1}. To fix, implement: Base.unaliascopy(A::Base.ReinterpretArray)::typeof(A)

I found that I can workaround it by unsafe_wrap:

buffer = zeros(UInt8, 4 * sizeof(Int))
mid = length(buffer) ÷ 2
y1 = reinterpret(Int, unsafe_wrap(Array, pointer(buffer, 1), mid))
y2 = reinterpret(Int, unsafe_wrap(Array, pointer(buffer, mid + 1),
                                  length(buffer) - mid))
y1 .= y2

But is there a less unsafe way to do it?

Note that a similar code using only view works:

z0 = collect(1:4)
z1 = @view z0[1:2]
z2 = @view z0[3:end]
z2 .= z1

so I suppose aliasing analysis can be done for reinterpreted view? If there is no safe way to do it, does it makes sense to add such feature?

@martinholters
Copy link
Member

While figuring out that x1 and x2 actually don't alias would clearly be nice, doing what the error message suggests (implement Base.unaliascopy(A::Base.ReinterpretArray)::typeof(A)) should also be an option, no?

@tkf
Copy link
Member Author

tkf commented Oct 8, 2018

Yes, I agree. I just dug into the direction I was interested in.

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