Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ChainRulesCore"
uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
version = "0.9.41"
version = "0.9.42"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Expand Down
2 changes: 2 additions & 0 deletions src/accumulation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ function add!!(x, t::InplaceableThunk)
end
end

add!!(x::AbstractArray, y::Thunk) = add!!(x, unthunk(y))
Copy link
Member

Choose a reason for hiding this comment

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

if this method didn't exist, add!!(abstract_array, thunk) would just dispatch to the fallback. Now that it does exist, it dispatches to the method below, which saves one accumulation. It's late and my brain hurts and I had to write this down to convince myself that it makes sense.


function add!!(x::AbstractArray{<:Any, N}, y::AbstractArray{<:Any, N}) where N
return if is_inplaceable_destination(x)
x .+= y
Expand Down
14 changes: 6 additions & 8 deletions test/accumulation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,21 @@
end
end

@testset "InplaceableThunk" begin
ithunk = InplaceableThunk(
@thunk(-1.0*ones(2, 2)),
x -> x .-= ones(2, 2)
)

@testset "AbstractThunk $(typeof(thunk))" for thunk in (
@thunk(-1.0*ones(2, 2)),
InplaceableThunk(@thunk(-1.0*ones(2, 2)), x -> x .-= ones(2, 2)),
)
@testset "in place" begin
accumuland = [1.0 2.0; 3.0 4.0]
ret = add!!(accumuland, ithunk)
ret = add!!(accumuland, thunk)
@test ret == [0.0 1.0; 2.0 3.0] # must return right answer
@test ret === accumuland # must be same object
end

@testset "out of place" begin
accumuland = @SMatrix [1.0 2.0; 3.0 4.0]

ret = add!!(accumuland, ithunk)
ret = add!!(accumuland, thunk)
@test ret == [0.0 1.0; 2.0 3.0] # must return right answer
@test ret !== accumuland # must not be same object
@test accumuland == [1.0 2.0; 3.0 4.0] # must not have mutated
Expand Down