Skip to content

Type-generic zeromatrix for arraypartition #444

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 5 additions & 2 deletions src/array_partition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ Base.:(==)(A::ArrayPartition, B::ArrayPartition) = A.x == B.x

Base.map(f, A::ArrayPartition) = ArrayPartition(map(x -> map(f, x), A.x))
function Base.mapreduce(f, op, A::ArrayPartition{T}; kwargs...) where {T}
mapreduce(f, op, (i for i in A); kwargs...)
mapreduce(x->mapreduce(f, op, x; kwargs...), op, (i for i in A.x); kwargs...)
Copy link
Member

Choose a reason for hiding this comment

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

I'm guessing this is probably the inference issue. Our mapreduce framework is rather baroque.

end
Base.filter(f, A::ArrayPartition) = ArrayPartition(map(x -> filter(f, x), A.x))
Base.any(f, A::ArrayPartition) = any((any(f, x) for x in A.x))
Expand Down Expand Up @@ -430,7 +430,10 @@ end

## Linear Algebra

ArrayInterface.zeromatrix(A::ArrayPartition) = ArrayInterface.zeromatrix(Vector(A))
function ArrayInterface.zeromatrix(A::ArrayPartition)
x = reduce(vcat,vec.(A.x))
x .* x' .* false
end

function __get_subtypes_in_module(
mod, supertype; include_supertype = true, all = false, except = [])
Expand Down
7 changes: 7 additions & 0 deletions src/named_array_partition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ end
return dest
end

#Overwrite ArrayInterface zeromatrix to work with NamedArrayPartitions & implicit solvers within OrdinaryDiffEq
function ArrayInterface.zeromatrix(A::NamedArrayPartition)
B = ArrayPartition(A)
x = reduce(vcat,vec.(B.x))
x .* x' .* false
end

# `x = find_NamedArrayPartition(x)` returns the first `NamedArrayPartition` among broadcast arguments.
find_NamedArrayPartition(bc::Base.Broadcast.Broadcasted) = find_NamedArrayPartition(bc.args)
function find_NamedArrayPartition(args::Tuple)
Expand Down
7 changes: 6 additions & 1 deletion test/gpu/arraypartition_gpu.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using RecursiveArrayTools, CUDA, Test
using RecursiveArrayTools, ArrayInterface, CUDA, Test
CUDA.allowscalar(false)

# Test indexing with colon
Expand All @@ -21,3 +21,8 @@ fill!(pA, false)
a = ArrayPartition(([1.0f0] |> cu, [2.0f0] |> cu, [3.0f0] |> cu))
b = ArrayPartition(([0.0f0] |> cu, [0.0f0] |> cu, [0.0f0] |> cu))
@. a + b

x = ArrayPartition((CUDA.zeros(2),CUDA.zeros(2)))
@test ArrayInterface.zeromatrix(x) isa CuMatrix
@test size(ArrayInterface.zeromatrix(x)) == (4,4)
@test maximum(abs, x) == 0f0
3 changes: 3 additions & 0 deletions test/named_array_partition_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ using RecursiveArrayTools, Test
@test x.a ≈ ones(10)
@test typeof(x .+ x[1:end]) <: Vector # test broadcast precedence
@test all(x .== x[1:end])
@test ArrayInterface.zeromatrix(x) isa Matrix
@test size(ArrayInterface.zeromatrix(x)) == (30,30)
y = copy(x)
@test zero(x, (10, 20)) == zero(x) # test that ignoring dims works
@test typeof(zero(x)) <: NamedArrayPartition
@test (y .*= 2).a[1] ≈ 2 # test in-place bcast


@test length(Array(x)) == 30
@test typeof(Array(x)) <: Array
Expand Down
Loading