-
-
Notifications
You must be signed in to change notification settings - Fork 68
Fix to copyto!(::ArrayPartition,::ArrayPartition) and copyto!(::Array,::ArrayPartition) #69
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
Conversation
test.jl
Outdated
@@ -0,0 +1,23 @@ | |||
using RecursiveArrayTools, BenchmarkTools | |||
|
|||
function Base.copyto!(A::ArrayPartition,src::ArrayPartition) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's this file for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry just used for benchmarking, will remove.
@inbounds for i in 1:length(A.x) | ||
A.x[i] .= @view(src[cur:(cur+length(A.x[i])-1)]) | ||
cur += length(A.x[i]) | ||
if size.(A.x) == size.(src.x) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this need if all(...)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to be working without all
... I guess when using all
you would need .==
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems we have to use size.(A.x) == size.(src.x)
. If partition don't agree this can happen:
julia> x = ArrayPartition(randn(3,2),randn(2)); y = ArrayPartition(randn(3),randn(3),randn(2));
julia> all(size.(x.x) .== size.(y.x))
ERROR: DimensionMismatch("arrays could not be broadcast to a common size")
@@ -103,22 +103,29 @@ end | |||
Base.mapreduce(f,op,A::ArrayPartition) = mapreduce(f,op,(mapreduce(f,op,x) for x in A.x)) | |||
Base.any(f,A::ArrayPartition) = any(f,(any(f,x) for x in A.x)) | |||
Base.any(f::Function,A::ArrayPartition) = any(f,(any(f,x) for x in A.x)) | |||
function Base.copyto!(dest::Array,A::ArrayPartition) | |||
function Base.copyto!(dest::AbstractArray,A::ArrayPartition) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems more general
Thanks! |
Addressing #68
I've also added a fix to the method
copyto!(::Array,::ArrayPartition)
and added some tests.Concerning the method
copyto!(::ArrayPartition,::ArrayPartition)
I finally went for a "hybrid solution".Since
x.=y
is way faster that the implementation I've proposed in #68 and most of the time it is expected forArrayPartition
s to be partitioned equally I'm calling thex.=y
when this is the case.It is not so elegant but still it allows for different partitioning and adds only little overhead when
ArrayPartition
s have the same partitioning.Let me know if you think there is room for improvement!