Skip to content

Fix to copyto!(::ArrayPartition,::ArrayPartition) and copyto!(::Array,::ArrayPartition) - #69

Merged
ChrisRackauckas merged 3 commits into
SciML:masterfrom
nantonel:master
Aug 7, 2019
Merged

ChrisRackauckas merged 3 commits into
SciML:masterfrom
nantonel:master

Conversation

@nantonel

@nantonel nantonel commented Aug 6, 2019

Copy link
Copy Markdown
Contributor

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!

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

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

Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread src/array_partition.jl
@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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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")

Comment thread src/array_partition.jl
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
Copy Markdown
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
Copy Markdown
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