Skip to content
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

fix: rework broadcasting copyto! #311

Merged
merged 1 commit into from
Dec 22, 2023
Merged
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
65 changes: 37 additions & 28 deletions src/vector_of_array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -658,40 +658,49 @@
end)
end

@inline function Base.copyto!(dest::AbstractVectorOfArray,
bc::Broadcast.Broadcasted{<:VectorOfArrayStyle})
bc = Broadcast.flatten(bc)
N = narrays(bc)
@inbounds for i in 1:N
if dest[:, i] isa AbstractArray && ArrayInterface.ismutable(dest[:, i])
copyto!(dest[:, i], unpack_voa(bc, i))
else
unpacked = unpack_voa(bc, i)
dest[:, i] = unpacked.f(unpacked.args...)
end
end
dest
end

@inline function Base.copyto!(dest::AbstractVectorOfArray,
bc::Broadcast.Broadcasted{<:Broadcast.DefaultArrayStyle})
bc = Broadcast.flatten(bc)
@inbounds for i in 1:length(dest.u)
if dest[:, i] isa AbstractArray && ArrayInterface.ismutable(dest[:, i])
copyto!(dest[:, i], unpack_voa(bc, i))
else
unpacked = unpack_voa(bc, i)
value = unpacked.f(unpacked.args...)
dest[:, i] = if value isa Number && dest[:, i] isa AbstractArray
fill(value, StaticArraysCore.similar_type(dest[:, i]))
for (type, N_expr) in [
(Broadcast.Broadcasted{<:VectorOfArrayStyle}, :(narrays(bc))),
(Broadcast.Broadcasted{<:Broadcast.DefaultArrayStyle}, :(length(dest.u)))
]
@eval @inline function Base.copyto!(dest::AbstractVectorOfArray,

Check warning on line 665 in src/vector_of_array.jl

View check run for this annotation

Codecov / codecov/patch

src/vector_of_array.jl#L665

Added line #L665 was not covered by tests
bc::$type)
bc = Broadcast.flatten(bc)
N = $N_expr
@inbounds for i in 1:N
if dest[:, i] isa AbstractArray
if ArrayInterface.ismutable(dest[:, i])
copyto!(dest[:, i], unpack_voa(bc, i))

Check warning on line 672 in src/vector_of_array.jl

View check run for this annotation

Codecov / codecov/patch

src/vector_of_array.jl#L667-L672

Added lines #L667 - L672 were not covered by tests
else
unpacked = unpack_voa(bc, i)
arr_type = StaticArraysCore.similar_type(dest[:, i])
dest[:, i] = if length(unpacked) == 1
fill(copy(unpacked), arr_type)

Check warning on line 677 in src/vector_of_array.jl

View check run for this annotation

Codecov / codecov/patch

src/vector_of_array.jl#L674-L677

Added lines #L674 - L677 were not covered by tests
else
arr_type(unpacked[j] for j in eachindex(unpacked))

Check warning on line 679 in src/vector_of_array.jl

View check run for this annotation

Codecov / codecov/patch

src/vector_of_array.jl#L679

Added line #L679 was not covered by tests
end
end
else
value
dest[:, i] = copy(unpack_voa(bc, i))

Check warning on line 683 in src/vector_of_array.jl

View check run for this annotation

Codecov / codecov/patch

src/vector_of_array.jl#L683

Added line #L683 was not covered by tests
end
end
dest

Check warning on line 686 in src/vector_of_array.jl

View check run for this annotation

Codecov / codecov/patch

src/vector_of_array.jl#L686

Added line #L686 was not covered by tests
end
dest
end

# @inline function Base.copyto!(dest::AbstractVectorOfArray,
# bc::Broadcast.Broadcasted{<:Broadcast.DefaultArrayStyle})
# bc = Broadcast.flatten(bc)
# @inbounds for i in 1:length(dest.u)
# if dest[:, i] isa AbstractArray && ArrayInterface.ismutable(dest[:, i])
# copyto!(dest[:, i], unpack_voa(bc, i))
# else
# unpacked = unpack_voa(bc, i)
# dest[:, i] = StaticArraysCore.similar_type(dest[:, i])(unpacked[j] for j in eachindex(unpacked))
# end
# end
# dest
# end

## broadcasting utils

"""
Expand Down
Loading