Skip to content

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

Merged
merged 3 commits into from
Aug 7, 2019

Conversation

nantonel
Copy link
Contributor

@nantonel nantonel commented Aug 6, 2019

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 for ArrayPartitions to be partitioned equally I'm calling the x.=y when this is the case.

It is not so elegant but still it allows for different partitioning and adds only little overhead when ArrayPartitions have the same partitioning.

using RecursiveArrayTools, BenchmarkTools
x = ArrayPartition(randn(1000,10),randn(1000),randn(3)); 
y =zero(x); 
@btime copyto!($y,$x);
@btime $y .= $x;
  2.954 μs (11 allocations: 192 bytes)
  2.923 μs (9 allocations: 160 bytes)

Let me know if you think there is room for improvement!

test.jl Outdated
@@ -0,0 +1,23 @@
using RecursiveArrayTools, BenchmarkTools

function Base.copyto!(A::ArrayPartition,src::ArrayPartition)
Copy link
Member

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?

Copy link
Contributor Author

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)
Copy link
Member

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(...)?

Copy link
Contributor Author

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 .==?

Copy link
Contributor Author

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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems more general

@ChrisRackauckas ChrisRackauckas merged commit d42c552 into SciML:master Aug 7, 2019
@ChrisRackauckas
Copy link
Member

Thanks!

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