Skip to content
Draft
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
Expand Up @@ -53,7 +53,7 @@ RecursiveArrayToolsZygoteExt = "Zygote"
Adapt = "4"
ArrayInterface = "7.17.0"
CUDA = "6.0"
FastBroadcast = "1.3"
FastBroadcast = "1.3.1"
ForwardDiff = "1"
GPUArraysCore = "0.2"
KernelAbstractions = "0.9.36, 0.10"
Expand Down
5 changes: 4 additions & 1 deletion ext/RecursiveArrayToolsFastBroadcastExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ const AbstractVectorOfSArray = AbstractVectorOfArray{
) where {S}
if FastBroadcast.use_fast_broadcast(S)
for i in 1:length(dst.u)
unpacked = RecursiveArrayTools.unpack_voa(bc, i)
inner_bc = RecursiveArrayTools.unpack_voa(bc, i, Val(ndims(dst)))
unpacked = Broadcast.instantiate(
Broadcast.Broadcasted(inner_bc.f, inner_bc.args, axes(dst.u[i]))
)
dst.u[i] = StaticArraysCore.similar_type(dst.u[i])(
unpacked[j]
for j in eachindex(unpacked)
Expand Down
15 changes: 10 additions & 5 deletions ext/RecursiveArrayToolsFastBroadcastPolyesterExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ const AbstractVectorOfSArray = AbstractVectorOfArray{
T, N, <:AbstractVector{<:StaticArraysCore.SArray},
} where {T, N}

# Keep the generator outside @batch so its closure captures only the inner broadcast.
@inline function materialize_inner(inner, bc, i, n)
inner_bc = RecursiveArrayTools.unpack_voa(bc, i, n)
unpacked = Broadcast.instantiate(
Broadcast.Broadcasted(inner_bc.f, inner_bc.args, axes(inner))
)
return StaticArraysCore.similar_type(inner)(unpacked[j] for j in eachindex(unpacked))
end

@inline function _polyester_fast_materialize!(
dst::AbstractVectorOfSArray,
bc::Broadcast.Broadcasted{S}
) where {S}
if FastBroadcast.use_fast_broadcast(S)
@batch for i in 1:length(dst.u)
unpacked = RecursiveArrayTools.unpack_voa(bc, i)
dst.u[i] = StaticArraysCore.similar_type(dst.u[i])(
unpacked[j]
for j in eachindex(unpacked)
)
dst.u[i] = materialize_inner(dst.u[i], bc, i, Val(ndims(dst)))
end
else
Broadcast.materialize!(dst, bc)
Expand Down
9 changes: 6 additions & 3 deletions ext/RecursiveArrayToolsStructArraysExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ for (type, N_expr) in [
dest_i = dest[:, i]
if dest_i isa AbstractArray
if ArrayInterface.ismutable(dest_i)
copyto!(dest_i, unpack_voa(bc, i))
Broadcast.materialize!(dest_i, unpack_voa(bc, i, Val(ndims(dest))))
else
unpacked = unpack_voa(bc, i)
inner_bc = unpack_voa(bc, i, Val(ndims(dest)))
unpacked = Broadcast.instantiate(
Broadcast.Broadcasted(inner_bc.f, inner_bc.args, axes(dest_i))
)
arr_type = StaticArraysCore.similar_type(dest_i)
dest_i = if length(unpacked) == 1 && length(dest_i) == 1
arr_type(unpacked[1])
Expand All @@ -55,7 +58,7 @@ for (type, N_expr) in [
end
end
else
dest_i = copy(unpack_voa(bc, i))
dest_i = copy(unpack_voa(bc, i, Val(ndims(dest))))
end
dest[:, i] = dest_i
end
Expand Down
39 changes: 21 additions & 18 deletions src/vector_of_array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1646,11 +1646,11 @@ end
# this is the default behavior in v3.15.0
N = narrays(bc)
map(1:N) do i
copy(unpack_voa(bc, i))
copy(unpack_voa(bc, i, Val(ndims(bc))))
end
else # if parent isa AbstractArray
map(enumerate(Iterators.product(axes(parent)...))) do (i, _)
copy(unpack_voa(bc, i))
copy(unpack_voa(bc, i, Val(ndims(bc))))
end
end
return VectorOfArray(rewrap(parent, u))
Expand All @@ -1676,9 +1676,12 @@ for (type, N_expr) in [
inner = dest.u[i]
if inner isa AbstractArray
if ArrayInterface.ismutable(inner)
copyto!(inner, unpack_voa(bc, i))
Broadcast.materialize!(inner, unpack_voa(bc, i, Val(ndims(dest))))
else
unpacked = unpack_voa(bc, i)
inner_bc = unpack_voa(bc, i, Val(ndims(dest)))
unpacked = Broadcast.instantiate(
Broadcast.Broadcasted(inner_bc.f, inner_bc.args, axes(inner))
)
arr_type = StaticArraysCore.similar_type(inner)
dest.u[i] = if length(unpacked) == 1 && length(inner) == 1
arr_type(unpacked[1])
Expand All @@ -1689,7 +1692,7 @@ for (type, N_expr) in [
end
end
else
dest.u[i] = copy(unpack_voa(bc, i))
dest.u[i] = copy(unpack_voa(bc, i, Val(ndims(dest))))
end
end
return dest
Expand Down Expand Up @@ -1724,24 +1727,24 @@ _narrays(args::AbstractVectorOfArray) = length(args.u)
_narrays(args::Tuple{Any}) = _narrays(args[1])
_narrays(::Any) = 0

# drop axes because it is easier to recompute
@inline function unpack_voa(bc::Broadcast.Broadcasted{Style}, i) where {Style}
return Broadcast.Broadcasted{Style}(bc.f, unpack_args_voa(i, bc.args))
# Slice along the destination's outer dimension; lower-dimensional arguments
# broadcast unchanged across all inner arrays.
@inline function unpack_voa(bc::Broadcast.Broadcasted, i, n)
return Broadcast.Broadcasted(bc.f, unpack_args_voa(i, n, bc.args))
end
@inline function unpack_voa(bc::Broadcast.Broadcasted{<:VectorOfArrayStyle}, i)
return Broadcast.Broadcasted(bc.f, unpack_args_voa(i, bc.args))
unpack_voa(x, ::Any, ::Any) = x
function unpack_voa(x::AbstractVectorOfArray{T, M}, i, ::Val{N}) where {T, M, N}
return M < N ? x : x.u[length(x.u) == 1 ? 1 : i]
end
unpack_voa(x, ::Any) = x
unpack_voa(x::AbstractVectorOfArray, i) = x.u[i]
function unpack_voa(x::AbstractArray{T, N}, i) where {T, N}
return @view x[ntuple(x -> Colon(), N - 1)..., i]
function unpack_voa(x::AbstractArray{T, M}, i, ::Val{N}) where {T, M, N}
M < N && return x
return @view x[ntuple(_ -> Colon(), Val(N - 1))..., size(x, N) == 1 ? 1 : i]
end

@inline function unpack_args_voa(i, args::Tuple)
return (unpack_voa(args[1], i), unpack_args_voa(i, Base.tail(args))...)
@inline function unpack_args_voa(i, n, args::Tuple)
return (unpack_voa(args[1], i, n), unpack_args_voa(i, n, Base.tail(args))...)
end
unpack_args_voa(i, args::Tuple{Any}) = (unpack_voa(args[1], i),)
unpack_args_voa(::Any, args::Tuple{}) = ()
unpack_args_voa(::Any, ::Any, args::Tuple{}) = ()

"""
VA
Expand Down
58 changes: 58 additions & 0 deletions test/Core/voa_broadcast_dimensions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Test, RecursiveArrayTools, StaticArrays, FastBroadcast, Polyester, StructArrays

@testset "VectorOfArray broadcast dimensions" begin
for (wrap, storage) in (
(identity, identity), (SVector{3}, identity), (MVector{3}, identity),
(SizedVector{3}, StructArray), (SVector{3}, StructArray), (MVector{3}, StructArray),
)
original = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]
make() = VectorOfArray(storage(wrap.(deepcopy(original))))
dense = Array(make())
for x in (
[10.0, 20.0, 30.0], [10.0 20.0], fill(2.0), 2.0, fill(2.0, 1, 1),
reshape(1.0:6.0, 3, 2), reshape([10.0, 20.0, 30.0], 3, 1),
)
for add_destination in (true, false)
v = make()
expected = add_destination ? dense .+ x : x .+ 1
expected = broadcast(+, zeros(size(dense)), expected)
if add_destination
v .= v .+ x
else
v .= x .+ 1
end
@test Array(v) == expected
if wrap === SVector{3} && storage === identity
for threaded in (false, true)
v = make()
if add_destination
@.. thread = threaded v = v + x
else
@.. thread = threaded v = x + 1
end
@test Array(v) == expected
end
end
end
end
v = make()
@test Array(v .+ fill(2.0)) == dense .+ 2
end
for wrap in (identity, SMatrix{2, 3})
make() = VectorOfArray(
[
wrap(reshape(collect(1.0:6.0), 2, 3)),
wrap(reshape(collect(7.0:12.0), 2, 3)),
]
)
for x in (
[10.0, 20.0], 10.0:10.0:20.0, reshape(1.0:6.0, 2, 3),
reshape(1.0:6.0, 2, 3, 1), reshape([10.0, 20.0], 1, 1, 2), fill(2.0),
)
v = make()
expected = Array(v) .+ x
v .= v .+ x
@test Array(v) == expected
end
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ run_tests(;
@time @safetestset "Partitions Tests" include("Core/partitions_test.jl")
@time @safetestset "Partitions and StaticArrays Tests" include("Core/partitions_and_static_arrays.jl")
@time @safetestset "VecOfArr Indexing Tests" include("Core/basic_indexing.jl")
@time @safetestset "VecOfArr Broadcast Dimensions" include("Core/voa_broadcast_dimensions.jl")
@time @safetestset "VecOfArr Interface Tests" include("Core/interface_tests.jl")
@time @safetestset "Public API Tests" include("Core/public_api_test.jl")
@time @safetestset "Table traits" include("Core/tabletraits.jl")
Expand Down
Loading