-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbroadcast.jl
389 lines (334 loc) · 12.7 KB
/
broadcast.jl
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import MultiBroadcastFusion as MBF
import MultiBroadcastFusion: fused_direct
# Make a MultiBroadcastFusion type, `FusedMultiBroadcast`, and macro, `@fused`:
# via https://github.com/CliMA/MultiBroadcastFusion.jl
MBF.@make_type FusedMultiBroadcast
MBF.@make_fused fused_direct FusedMultiBroadcast fused_direct
# Broadcasting of AbstractData objects
# https://docs.julialang.org/en/v1/manual/interfaces/#Broadcast-Styles
abstract type DataStyle <: Base.BroadcastStyle end
abstract type Data0DStyle <: DataStyle end
struct DataFStyle{A} <: Data0DStyle end
DataStyle(::Type{DataF{S, A}}) where {S, A} = DataFStyle{parent_array_type(A)}()
Data0DStyle(::Type{DataFStyle{A}}) where {A} = DataFStyle{A}
abstract type DataColumnStyle <: DataStyle end
struct VFStyle{Nv, A} <: DataColumnStyle end
DataStyle(::Type{VF{S, Nv, A}}) where {S, Nv, A} =
VFStyle{Nv, parent_array_type(A)}()
DataColumnStyle(::Type{VFStyle{Nv, A}}) where {Nv, A} = VFStyle{Nv, A}
abstract type Data1DStyle{Ni} <: DataStyle end
struct IFHStyle{Ni, Nh, A} <: Data1DStyle{Ni} end
DataStyle(::Type{IFH{S, Ni, Nh, A}}) where {S, Ni, Nh, A} =
IFHStyle{Ni, Nh, parent_array_type(A)}()
abstract type DataSlab1DStyle{Ni} <: DataStyle end
DataSlab1DStyle(::Type{IFHStyle{Ni, Nh, A}}) where {Ni, Nh, A} = IFStyle{Ni, A}
struct IFStyle{Ni, A} <: DataSlab1DStyle{Ni} end
DataStyle(::Type{IF{S, Ni, A}}) where {S, Ni, A} =
IFStyle{Ni, parent_array_type(A)}()
abstract type DataSlab2DStyle{Nij} <: DataStyle end
struct IJFStyle{Nij, A} <: DataSlab2DStyle{Nij} end
DataStyle(::Type{IJF{S, Nij, A}}) where {S, Nij, A} =
IJFStyle{Nij, parent_array_type(A)}()
abstract type Data2DStyle{Nij} <: DataStyle end
struct IJFHStyle{Nij, Nh, A} <: Data2DStyle{Nij} end
DataStyle(::Type{IJFH{S, Nij, Nh, A}}) where {S, Nij, Nh, A} =
IJFHStyle{Nij, Nh, parent_array_type(A)}()
DataSlab2DStyle(::Type{IJFHStyle{Nij, Nh, A}}) where {Nij, Nh, A} =
IJFStyle{Nij, A}
abstract type Data1DXStyle{Nv, Ni} <: DataStyle end
struct VIFHStyle{Nv, Ni, Nh, A} <: Data1DXStyle{Nv, Ni} end
DataStyle(::Type{VIFH{S, Nv, Ni, Nh, A}}) where {S, Nv, Ni, Nh, A} =
VIFHStyle{Nv, Ni, Nh, parent_array_type(A)}()
Data1DXStyle(::Type{VIFHStyle{Nv, Ni, Nh, A}}) where {Ni, Nv, Nh, A} =
VIFHStyle{Nv, Ni, Nh, A}
DataColumnStyle(::Type{VIFHStyle{Nv, Ni, Nh, A}}) where {Ni, Nv, Nh, A} =
VFStyle{Nv, A}
DataSlab1DStyle(::Type{VIFHStyle{Nv, Ni, Nh, A}}) where {Ni, Nv, Nh, A} =
IFStyle{Ni, A}
abstract type Data2DXStyle{Nv, Nij} <: DataStyle end
struct VIJFHStyle{Nv, Nij, Nh, A} <: Data2DXStyle{Nv, Nij} end
DataStyle(::Type{VIJFH{S, Nv, Nij, Nh, A}}) where {S, Nv, Nij, Nh, A} =
VIJFHStyle{Nv, Nij, Nh, parent_array_type(A)}()
Data2DXStyle(::Type{VIJFHStyle{Nv, Nij, Nh, A}}) where {Nv, Nij, Nh, A} =
VIJFHStyle{Nv, Nij, Nh, A}
DataColumnStyle(::Type{VIJFHStyle{Nv, Nij, Nh, A}}) where {Nv, Nij, Nh, A} =
VFStyle{Nv, A}
DataSlab2DStyle(::Type{VIJFHStyle{Nv, Nij, Nh, A}}) where {Nv, Nij, Nh, A} =
IJFStyle{Nij, A}
#####
##### Union styles
#####
#! format: off
const BroadcastedUnionData = Union{Base.Broadcast.Broadcasted{<:DataStyle}, AbstractData}
const BroadcastedUnionIJFH{S, Nij, Nh, A} = Union{Base.Broadcast.Broadcasted{IJFHStyle{Nij, Nh, A}}, IJFH{S, Nij, Nh, A}}
const BroadcastedUnionIFH{S, Ni, Nh, A} = Union{Base.Broadcast.Broadcasted{IFHStyle{Ni, Nh, A}}, IFH{S, Ni, Nh, A}}
const BroadcastedUnionIJF{S, Nij, A} = Union{Base.Broadcast.Broadcasted{IJFStyle{Nij, A}}, IJF{S, Nij, A}}
const BroadcastedUnionIF{S, Ni, A} = Union{Base.Broadcast.Broadcasted{IFStyle{Ni, A}}, IF{S, Ni, A}}
const BroadcastedUnionVIFH{S, Nv, Ni, Nh, A} = Union{Base.Broadcast.Broadcasted{VIFHStyle{Nv, Ni, Nh, A}}, VIFH{S, Nv, Ni, Nh, A}}
const BroadcastedUnionVIJFH{S, Nv, Nij, Nh, A} = Union{Base.Broadcast.Broadcasted{VIJFHStyle{Nv, Nij, Nh, A}}, VIJFH{S, Nv, Nij, Nh, A}}
const BroadcastedUnionVF{S, Nv, A} = Union{Base.Broadcast.Broadcasted{VFStyle{Nv, A}}, VF{S, Nv, A}}
const BroadcastedUnionDataF{S, A} = Union{Base.Broadcast.Broadcasted{DataFStyle{A}}, DataF{S, A}}
#! format: on
abstract type Data3DStyle <: DataStyle end
Base.Broadcast.BroadcastStyle(::Type{D}) where {D <: AbstractData} =
DataStyle(D)
# precedence rules
# scalars are broadcast over the data object
Base.Broadcast.BroadcastStyle(
::Base.Broadcast.AbstractArrayStyle{0},
ds::DataStyle,
) = ds
Base.Broadcast.BroadcastStyle(::Base.Broadcast.Style{Tuple}, ds::DataStyle) = ds
Base.Broadcast.BroadcastStyle(
::DataFStyle{A1},
::DataFStyle{A2},
) where {A1, A2} = DataFStyle{promote_parent_array_type(A1, A2)}()
Base.Broadcast.BroadcastStyle(
::VFStyle{Nv, A1},
::VFStyle{Nv, A2},
) where {Nv, A1, A2} = VFStyle{Nv, promote_parent_array_type(A1, A2)}()
Base.Broadcast.BroadcastStyle(
::IFStyle{Ni, A1},
::IFStyle{Ni, A2},
) where {Ni, A1, A2} = IFStyle{Ni, promote_parent_array_type(A1, A2)}()
Base.Broadcast.BroadcastStyle(
::IFHStyle{Ni, Nh, A1},
::IFHStyle{Ni, Nh, A2},
) where {Ni, Nh, A1, A2} = IFHStyle{Ni, Nh, promote_parent_array_type(A1, A2)}()
Base.Broadcast.BroadcastStyle(
::VIFHStyle{Nv, Ni, Nh, A1},
::VIFHStyle{Nv, Ni, Nh, A2},
) where {Nv, Ni, Nh, A1, A2} =
VIFHStyle{Nv, Ni, Nh, promote_parent_array_type(A1, A2)}()
Base.Broadcast.BroadcastStyle(
::IJFStyle{Nij, A1},
::IJFStyle{Nij, A2},
) where {Nij, A1, A2} = IJFStyle{Nij, promote_parent_array_type(A1, A2)}()
Base.Broadcast.BroadcastStyle(
::IJFHStyle{Nij, Nh, A1},
::IJFHStyle{Nij, Nh, A2},
) where {Nij, Nh, A1, A2} =
IJFHStyle{Nij, Nh, promote_parent_array_type(A1, A2)}()
Base.Broadcast.BroadcastStyle(
::VIJFHStyle{Nv, Nij, Nh, A1},
::VIJFHStyle{Nv, Nij, Nh, A2},
) where {Nv, Nij, Nh, A1, A2} =
VIJFHStyle{Nv, Nij, Nh, promote_parent_array_type(A1, A2)}()
Base.Broadcast.BroadcastStyle(
::DataFStyle{A1},
::IFStyle{Ni, A2},
) where {Ni, A1, A2} = IFStyle{Ni, promote_parent_array_type(A1, A2)}()
Base.Broadcast.BroadcastStyle(
::DataFStyle{A1},
::IJFStyle{Nij, A2},
) where {Nij, A1, A2} = IJFStyle{Nij, promote_parent_array_type(A1, A2)}()
Base.Broadcast.BroadcastStyle(
::DataFStyle{A1},
::VFStyle{Nv, A2},
) where {A1, Nv, A2} = VFStyle{Nv, promote_parent_array_type(A1, A2)}()
Base.Broadcast.BroadcastStyle(
::DataFStyle{A1},
::IFHStyle{Ni, Nh, A2},
) where {Ni, Nh, A1, A2} = IFHStyle{Ni, Nh, promote_parent_array_type(A1, A2)}()
Base.Broadcast.BroadcastStyle(
::DataFStyle{A1},
::IJFHStyle{Nij, Nh, A2},
) where {Nij, Nh, A1, A2} =
IJFHStyle{Nij, Nh, promote_parent_array_type(A1, A2)}()
Base.Broadcast.BroadcastStyle(
::DataFStyle{A1},
::VIFHStyle{Nv, Ni, Nh, A2},
) where {Nv, Ni, Nh, A1, A2} =
VIFHStyle{Nv, Ni, Nh, promote_parent_array_type(A1, A2)}()
Base.Broadcast.BroadcastStyle(
::DataFStyle{A1},
::VIJFHStyle{Nv, Nij, Nh, A2},
) where {Nv, Nij, Nh, A1, A2} =
VIJFHStyle{Nv, Nij, Nh, promote_parent_array_type(A1, A2)}()
Base.Broadcast.BroadcastStyle(
::VFStyle{Nv, A1},
::IFHStyle{Ni, Nh, A2},
) where {Nv, Ni, Nh, A1, A2} =
VIFHStyle{Nv, Ni, Nh, promote_parent_array_type(A1, A2)}()
Base.Broadcast.BroadcastStyle(
::VFStyle{Nv, A1},
::IJFHStyle{Nij, Nh, A2},
) where {Nv, Nij, Nh, A1, A2} =
VIJFHStyle{Nv, Nij, Nh, promote_parent_array_type(A1, A2)}()
Base.Broadcast.BroadcastStyle(
::VFStyle{Nv, A1},
::VIFHStyle{Nv, Ni, Nh, A2},
) where {Nv, Ni, Nh, A1, A2} =
VIFHStyle{Nv, Ni, Nh, promote_parent_array_type(A1, A2)}()
Base.Broadcast.BroadcastStyle(
::VFStyle{Nv, A1},
::VIJFHStyle{Nv, Nij, Nh, A2},
) where {Nv, Nij, Nh, A1, A2} =
VIJFHStyle{Nv, Nij, Nh, promote_parent_array_type(A1, A2)}()
Base.Broadcast.BroadcastStyle(
::IFHStyle{Ni, Nh, A1},
::VIFHStyle{Nv, Ni, Nh, A2},
) where {Nv, Ni, Nh, A1, A2} =
VIFHStyle{Nv, Ni, Nh, promote_parent_array_type(A1, A2)}()
Base.Broadcast.BroadcastStyle(
::IJFHStyle{Nij, Nh, A1},
::VIJFHStyle{Nv, Nij, Nh, A2},
) where {Nv, Nij, Nh, A1, A2} =
VIJFHStyle{Nv, Nij, Nh, promote_parent_array_type(A1, A2)}()
Base.Broadcast.broadcastable(data::AbstractData) = data
Base.@propagate_inbounds function slab(
bc::Base.Broadcast.Broadcasted{DS},
inds...,
) where {Ni, DS <: Data1DStyle{Ni}}
_args = slab_args(bc.args, inds...)
_axes = (SOneTo(Ni),)
Base.Broadcast.Broadcasted{DataSlab1DStyle(DS)}(bc.f, _args, _axes)
end
Base.@propagate_inbounds function slab(
bc::Base.Broadcast.Broadcasted{DS},
inds...,
) where {Nv, Ni, DS <: Data1DXStyle{Nv, Ni}}
_args = slab_args(bc.args, inds...)
_axes = (SOneTo(Ni),)
Base.Broadcast.Broadcasted{DataSlab1DStyle(DS)}(bc.f, _args, _axes)
end
Base.@propagate_inbounds function slab(
bc::Base.Broadcast.Broadcasted{DS},
inds...,
) where {Nij, DS <: Data2DStyle{Nij}}
_args = slab_args(bc.args, inds...)
_axes = (SOneTo(Nij), SOneTo(Nij))
Base.Broadcast.Broadcasted{DataSlab2DStyle(DS)}(bc.f, _args, _axes)
end
Base.@propagate_inbounds function slab(
bc::Base.Broadcast.Broadcasted{DS},
inds...,
) where {Nv, Nij, DS <: Data2DXStyle{Nv, Nij}}
_args = slab_args(bc.args, inds...)
_axes = (SOneTo(Nij), SOneTo(Nij))
Base.Broadcast.Broadcasted{DataSlab2DStyle(DS)}(bc.f, _args, _axes)
end
Base.@propagate_inbounds function column(
bc::Base.Broadcast.Broadcasted{DS},
inds...,
) where {Nv, N, DS <: Union{Data1DXStyle{Nv, N}, Data2DXStyle{Nv, N}}}
_args = column_args(bc.args, inds...)
_axes = nothing
Base.Broadcast.Broadcasted{DataColumnStyle(DS)}(bc.f, _args, _axes)
end
@inline function column(
bc::Base.Broadcast.Broadcasted{DS},
inds...,
) where {DS <: DataColumnStyle}
bc
end
Base.@propagate_inbounds function column(
bc::Union{Data1D, Base.Broadcast.Broadcasted{<:Data1D}},
i,
h,
)
slab(bc, h)[i]
end
Base.@propagate_inbounds function column(
bc::Union{Data1D, Base.Broadcast.Broadcasted{<:Data1D}},
i,
j,
h,
)
slab(bc, h)[i]
end
Base.@propagate_inbounds function column(
bc::Union{Data2D, Base.Broadcast.Broadcasted{<:Data2D}},
i,
j,
h,
)
slab(bc, h)[i, j]
end
function Base.similar(
bc::BroadcastedUnionDataF{<:Any, A},
::Type{Eltype},
) where {A, Eltype}
PA = parent_array_type(A)
array = similar(PA, (typesize(eltype(A), Eltype)))
return DataF{Eltype}(array)
end
function Base.similar(
bc::BroadcastedUnionIJFH{<:Any, Nij, Nh, A},
::Type{Eltype},
) where {Nij, Nh, A, Eltype}
PA = parent_array_type(A)
array = similar(PA, (Nij, Nij, typesize(eltype(A), Eltype), Nh))
return IJFH{Eltype, Nij, Nh}(array)
end
function Base.similar(
bc::BroadcastedUnionIFH{<:Any, Ni, Nh, A},
::Type{Eltype},
) where {Ni, Nh, A, Eltype}
PA = parent_array_type(A)
array = similar(PA, (Ni, typesize(eltype(A), Eltype), Nh))
return IFH{Eltype, Ni, Nh}(array)
end
function Base.similar(
::BroadcastedUnionIJF{<:Any, Nij, A},
::Type{Eltype},
) where {Nij, A, Eltype}
Nf = typesize(eltype(A), Eltype)
array = MArray{Tuple{Nij, Nij, Nf}, eltype(A), 3, Nij * Nij * Nf}(undef)
return IJF{Eltype, Nij}(array)
end
function Base.similar(
::BroadcastedUnionIF{<:Any, Ni, A},
::Type{Eltype},
) where {Ni, A, Eltype}
Nf = typesize(eltype(A), Eltype)
array = MArray{Tuple{Ni, Nf}, eltype(A), 2, Ni * Nf}(undef)
return IF{Eltype, Ni}(array)
end
Base.similar(
bc::BroadcastedUnionVF{<:Any, Nv},
::Type{Eltype},
) where {Nv, Eltype} = Base.similar(bc, Eltype, Val(Nv))
function Base.similar(
bc::BroadcastedUnionVF{<:Any, Nv, A},
::Type{Eltype},
::Val{newNv},
) where {Nv, A, Eltype, newNv}
PA = parent_array_type(A)
array = similar(PA, (newNv, typesize(eltype(A), Eltype)))
return VF{Eltype, newNv}(array)
end
Base.similar(
bc::BroadcastedUnionVIFH{<:Any, Nv},
::Type{Eltype},
) where {Nv, Eltype} = Base.similar(bc, Eltype, Val(Nv))
function Base.similar(
bc::BroadcastedUnionVIFH{<:Any, Nv, Ni, Nh, A},
::Type{Eltype},
::Val{newNv},
) where {Nv, Ni, Nh, A, Eltype, newNv}
PA = parent_array_type(A)
array = similar(PA, (newNv, Ni, typesize(eltype(A), Eltype), Nh))
return VIFH{Eltype, newNv, Ni, Nh}(array)
end
Base.similar(
bc::BroadcastedUnionVIJFH{<:Any, Nv, Nij, Nh, A},
::Type{Eltype},
) where {Nv, Nij, Nh, A, Eltype} = similar(bc, Eltype, Val(Nv))
function Base.similar(
bc::BroadcastedUnionVIJFH{<:Any, Nv, Nij, Nh, A},
::Type{Eltype},
::Val{newNv},
) where {Nv, Nij, Nh, A, Eltype, newNv}
PA = parent_array_type(A)
array = similar(PA, (newNv, Nij, Nij, typesize(eltype(A), Eltype), Nh))
return VIJFH{Eltype, newNv, Nij, Nh}(array)
end
# ============= FusedMultiBroadcast
isascalar(
bc::Base.Broadcast.Broadcasted{Style},
) where {
Style <:
Union{Base.Broadcast.AbstractArrayStyle{0}, Base.Broadcast.Style{Tuple}},
} = true
isascalar(bc) = false