# Trying to understand what LoopVectorization does/doesn’t like.If I broadcast-add

**URL:** <https://discourse.julialang.org/t/trying-to-understand-what-loopvectorization-does-doesn-t-like-if-i-broadcast-add/54468>\
**Category:** General Usage\
**Created:** [February 2, 2021, 4:17pm UTC](https://discourse.julialang.org/t/trying-to-understand-what-loopvectorization-does-doesn-t-like-if-i-broadcast-add/54468 "2021-02-02T16:17:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BridgeBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bridgebot/32/21491_2.png) [@BridgeBot](https://discourse.julialang.org/u/BridgeBot)\
**Post date:** [February 2, 2021, 4:17pm UTC](https://discourse.julialang.org/t/trying-to-understand-what-loopvectorization-does-doesn-t-like-if-i-broadcast-add/54468/1 "2021-02-02T16:17:39Z")

</div>

Trying to understand what LoopVectorization does/doesn’t like.

If I broadcast-add elements of tuples

```julia
function bcast_add(x::NTuple{N,T},y::NTuple{N,T}) where {N,T}
    @avx for i = 1:length(first(x))
        x_i = getindex.(x,i) .+ getindex.(y,i)
        setindex!.(x,x_i,i)
    end
end

```

using `@avx` gives an `Expression not recognized` error which disappears if I remove `getindex.(...)` or `setindex.(...)`. Are these operations not supported?

Note that the original poster on Slack cannot see your response here on Discourse. Consider _transcribing the appropriate answer back to Slack_, or pinging the poster here on Discourse so they can _follow this thread_.  
[(Original message :slack:)](https://julialang.slack.com/archives/C6A044SQH/p1612281702414200?thread_ts=1612281702.414200&cid=C6A044SQH) [(More Info)](https://github.com/JuliaCommunity/SlackBridge)

---

<div class="post-metadata">

**Author:** ![jlchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlchan/32/10958_2.png) [@jlchan](https://discourse.julialang.org/u/jlchan)\
**Post date:** [February 2, 2021, 4:17pm UTC](https://discourse.julialang.org/t/trying-to-understand-what-loopvectorization-does-doesn-t-like-if-i-broadcast-add/54468/2 "2021-02-02T16:17:59Z")

</div>

from @Mason on Slack:

> That’s correct, `@avx` doesn’t know how to deal with broadcasting, though this would be a conceivable feature to add. Perhaps open an issue in LoopVectorization.jl?

---

<div class="post-metadata">

**Author:** ![jlchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlchan/32/10958_2.png) [@jlchan](https://discourse.julialang.org/u/jlchan)\
**Post date:** [February 2, 2021, 5:30pm UTC](https://discourse.julialang.org/t/trying-to-understand-what-loopvectorization-does-doesn-t-like-if-i-broadcast-add/54468/3 "2021-02-02T17:30:53Z")

</div>

More in-detail response to a related Github issue [Broadcasted indexing · Issue #197 · JuliaSIMD/LoopVectorization.jl · GitHub](https://github.com/chriselrod/LoopVectorization.jl/issues/197)

Solution using code generation

```julia
using LoopVectorization, Base.Cartesian
@generated function foo!(x::Tuple{Vararg{Array,N}}) where {N}
    quote
        @nextract $N x x
        @avx for i = 1:length(first(x))
            @nexprs $N n -> x_n[i] = exp(x_n[i])
        end
    end
end
using Random
x = (rand(100), randn(100), randexp(100))
foo!(x)
x

```

---

<div class="post-metadata">

**Author:** ![jlchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlchan/32/10958_2.png) [@jlchan](https://discourse.julialang.org/u/jlchan)\
**Post date:** [February 2, 2021, 5:32pm UTC](https://discourse.julialang.org/t/trying-to-understand-what-loopvectorization-does-doesn-t-like-if-i-broadcast-add/54468/4 "2021-02-02T17:32:18Z")

</div>

Helpful comments from @mcabbott on Slack:

> I could be wrong but don’t think any kind of two-deep indexing is going to work […] I think things like `x[n][i]` also won’t work, it needs to know what arrays it’s dealing with. You could generate code though, for each `N` build the unrolled expression with all N loops visible. Either just a loop `for N in 1:10 @eval fun!(x::NTuple{$N,T}, y::...)` or a generated function.
