forked from SciML/RecursiveArrayTools.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursiveArrayToolsFastBroadcastExt.jl
More file actions
58 lines (52 loc) · 1.74 KB
/
RecursiveArrayToolsFastBroadcastExt.jl
File metadata and controls
58 lines (52 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
module RecursiveArrayToolsFastBroadcastExt
using RecursiveArrayTools
using FastBroadcast
using FastBroadcast: Serial, Threaded
using StaticArraysCore
const AbstractVectorOfSArray = AbstractVectorOfArray{
T, N, <:AbstractVector{<:StaticArraysCore.SArray},
} where {T, N}
@inline function FastBroadcast.fast_materialize!(
::Serial, dst::AbstractVectorOfSArray,
bc::Broadcast.Broadcasted{S}
) where {S}
if FastBroadcast.use_fast_broadcast(S)
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)
)
end
else
Broadcast.materialize!(dst, bc)
end
return dst
end
@inline function FastBroadcast.fast_materialize!(
::Threaded, dst::AbstractVectorOfSArray,
bc::Broadcast.Broadcasted{S}
) where {S}
if FastBroadcast.use_fast_broadcast(S)
Threads.@threads 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)
)
end
else
Broadcast.materialize!(dst, bc)
end
return dst
end
# Fallback for non-SArray VectorOfArray: the generic threaded path splits
# along the last axis via views, which does not correctly partition work for
# VectorOfArray. Fall back to serial broadcasting.
@inline function FastBroadcast.fast_materialize!(
::Threaded, dst::AbstractVectorOfArray,
bc::Broadcast.Broadcasted
)
return FastBroadcast.fast_materialize!(Serial(), dst, bc)
end
end # module