Non-allocating reduced broadcast?

Suppose A and B are two long arrays of numbers of same length. Then sum(A .* B) allocates the intermediary array A .* B, and then sums it (the reduction).

I know I can write this as a loop or using generators, but at the price of losing the terseness of the dotted broadcast syntax. So is there a way to combine dotted calls with a reducing operation without intermediate allocations?

3 Likes

There is

https://github.com/JuliaLang/julia/pull/31020

which is blocked by

https://github.com/JuliaLang/julia/issues/19198

2 Likes

For this specific case you can also use dot(A,B) in the LinearAlgebra stdlib.

1 Like

This was just an example. I have a more complex scenario in mind where dot doesn’t help.

This is very clever: Is there something like broadcast_mapreduce? - #15 by fabiangans. Perhaps a solution can come out of the ArrayReduce struct suggested there.

Actually, you can do non-allocating reduction by sum(@~ A .* B) using @~ macro from LazyArrays.jl. See:
https://github.com/JuliaArrays/LazyArrays.jl#broadcasting

(But note that I’m actually suggesting to modify API a bit: https://github.com/JuliaArrays/LazyArrays.jl/pull/31#issuecomment-487468147)

3 Likes