Skip to content

Commit 74e4928

Browse files
authored
Don't use generated function in _generic_blockbroadcast_copyto (#352)
1 parent 1abbad4 commit 74e4928

File tree

1 file changed

+19
-32
lines changed

1 file changed

+19
-32
lines changed

src/blockbroadcast.jl

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -144,45 +144,32 @@ end
144144
return copyto!(dest, Base.Broadcast.instantiate(Base.Broadcast.Broadcasted{BS}(bc.f, bc.args, combine_blockaxes.(axes(dest),axes(bc)))))
145145
end
146146

147-
@generated function _generic_blockbroadcast_copyto!(dest::AbstractArray,
147+
function _generic_blockbroadcast_copyto!(dest::AbstractArray,
148148
bc::Broadcasted{<:AbstractBlockStyle{NDims}, <:Any, <:Any, Args}) where {NDims, Args <: Tuple}
149149

150-
NArgs = length(Args.parameters)
151-
152-
# `bvar(0, dim)` is a variable for BlockIndexRange of `dim`-th dimension
153-
# of `dest` array. `bvar(i, dim)` is a similar variable of `i`-th
154-
# argument in `bc.args`.
155-
bvar(i, dim) = Symbol("blockindexrange_", i, "_", dim)
156-
157-
function forloop(dim)
158-
if dim > 0
159-
quote
160-
for ($(bvar(0, dim)), $(bvar.(1:NArgs, dim)...),) in zip(
161-
subblocks(dest, bs, $dim),
162-
subblocks.(bc.args, Ref(bs), Ref($dim))...)
163-
$(forloop(dim - 1))
164-
end
165-
end
166-
else
167-
bview(a, i) = :(_bview($a, $([bvar(i, d) for d in 1:NDims]...)))
168-
destview = bview(:dest, 0)
169-
argblocks = [bview(:(bc.args[$i]), i) for i in 1:NArgs]
170-
quote
171-
broadcast!(bc.f, $destview, $(argblocks...))
172-
end
173-
end
150+
NArgs = fieldcount(Args)
151+
152+
bs = axes(bc)
153+
if !blockisequal(axes(dest), bs)
154+
copyto!(PseudoBlockArray(dest, bs), bc)
155+
return dest
174156
end
175157

176-
quote
177-
bs = axes(bc)
178-
if !blockisequal(axes(dest), bs)
179-
copyto!(PseudoBlockArray(dest, bs), bc)
180-
return dest
158+
t = ntuple(NDims) do dim
159+
zip(
160+
subblocks(dest, bs, dim),
161+
map(x -> subblocks(x, bs, dim), bc.args)...
162+
)
181163
end
182164

183-
$(forloop(NDims))
184-
return dest
165+
for inds in Iterators.product(t...)
166+
destinds, bcinds = map(first, inds), ntuple(i -> map(x->x[i+1], inds), NArgs)
167+
destview = _bview(dest, destinds...)
168+
argblocks = ntuple(i -> _bview(bc.args[i], bcinds[i]...), NArgs)
169+
broadcast!(bc.f, destview, argblocks...)
185170
end
171+
172+
return dest
186173
end
187174

188175
copyto!(dest::AbstractArray,

0 commit comments

Comments
 (0)