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
50 lines (45 loc) · 1.81 KB
/
RecursiveArrayToolsFastBroadcastExt.jl
File metadata and controls
50 lines (45 loc) · 1.81 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
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
# 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.
# For SArray VectorOfArray, throw an informative error telling the user to
# load Polyester.jl for threaded broadcasting.
@inline function FastBroadcast.fast_materialize!(
::Threaded, dst::AbstractVectorOfArray,
bc::Broadcast.Broadcasted
)
# When Polyester is loaded, RecursiveArrayToolsFastBroadcastPolyesterExt
# defines more-specific methods for AbstractVectorOfSArray, so reaching
# this method with an SArray VoA means Polyester is not loaded.
if dst isa AbstractVectorOfSArray
error("Threaded FastBroadcast on VectorOfArray{SArray} requires Polyester.jl. " *
"Add `using Polyester` to enable threaded broadcasting, or use " *
"`@.. thread=false` for serial broadcasting.")
end
return FastBroadcast.fast_materialize!(Serial(), dst, bc)
end
end # module