Obviously, the explicit use of views does not need to allocate copies of the subarrays I want to operate on. However, I was very surprised that the broadcasting does not use views automatically.
I was hoping someone can help me build a more “julia philosophy coherent” mental image of why broadcast does not automatically use views. Is this something that might be automated in future versions of Julia?
Broadcast uses whatever you give it — you made the decision to use a view (or not) before you invoked the broadcast.
That is, when you write c .= a[10,:] .& b[:,10], you are essentially writing broadcast!(&, c, a[10,:], b[:,10]). So a[10,:] is the argument to the broadcast, and it is nonsensical to ask why broadcast! didn’t use a view, because the choice was made before it was called.
The other part was that it wasn’t a clear performance win in all cases.
Note that ideally, we’d use neither views nor allocating indexing — we’d fuse the indexing operation directly into the kernel. There’s a bit of an impedance mismatch, though, making that a tough thing to do, but you can manually do it:
Thank you both for the explanations! I guess the root of my confusion was that I was expecting the broadcast syntactic sugar to go one step beyond what it is doing currently and provide indexing information to the broadcast function, instead of just evaluating the indexing operation. I guess this would be too restrictive though…
Theoretically yes, but in practice there are differences. Take the example above — that’s allocating for its views but still faster than the fused indexing! The difference is actually real and meaningful: the views pre-compute a fast linear indexing transformation and use linear indexing instead of cartesian indexing (I’m guessing that’s the difference without looking, anyways — edit: the bigger difference is bounds checking, see below!).
Fusion will be much more likely to win if you can also fuse the whole way into the computation of an index vector, whereas views would require allocating the index vector before doing the operation. There may be other differences, too.