# Why is this loop type not supported by LoopVectorization?

**URL:** <https://discourse.julialang.org/t/why-is-this-loop-type-not-supported-by-loopvectorization/102886>\
**Category:** New to Julia\
**Created:** [August 16, 2023, 10:06pm UTC](https://discourse.julialang.org/t/why-is-this-loop-type-not-supported-by-loopvectorization/102886 "2023-08-16T22:06:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![james3](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@james3](https://discourse.julialang.org/u/james3)\
**Post date:** [August 16, 2023, 10:06pm UTC](https://discourse.julialang.org/t/why-is-this-loop-type-not-supported-by-loopvectorization/102886/1 "2023-08-16T22:06:21Z")

</div>

I am trying to understand why I can’t use `@turbo` in part of my code. I can reduce the issue to the following example

```julia
using LoopVectorization

f = [15, 13, 12, 10, 9, 7, 5, 4, 2, 1]
y0 = zeros(16)
y1 = zeros(16)

for i = 1:10
    y0[f[i]] += 1.0
    y0[f[i] + 1] += 2.0
end

@turbo for i = 1:10
    y1[f[i]] += 1.0
    y1[f[i] + 1] += 2.0
end

```

I then get

```julia
julia> [y0 y1]
16×2 Matrix{Float64}:
 1.0 1.0
 3.0 2.0
 2.0 2.0
 1.0 1.0
 3.0 2.0
 2.0 2.0
 1.0 1.0
 2.0 2.0
 1.0 1.0
 3.0 2.0
 2.0 2.0
 1.0 1.0
 3.0 2.0
 2.0 2.0
 1.0 1.0
 2.0 2.0

```

So I am not getting the same output when I use `@turbo`. Why is it not appropriate to use it in this case? It was not obvious to me from reading LoopVectorization.jl limitations, so I suspect I may have misunderstood something. Thanks.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 16, 2023, 10:11pm UTC](https://discourse.julialang.org/t/why-is-this-loop-type-not-supported-by-loopvectorization/102886/2 "2023-08-16T22:11:15Z")

</div>

> [@james3](#):
>
> Why is it not appropriate to use it in this case?

From the [`@turbo` documentation](https://juliasimd.github.io/LoopVectorization.jl/stable/api/#LoopVectorization.@turbo):

> It assumes that loop iterations are independent.

Here, the loop iterations are somewhat dependent (though they can be executed in any order) because different iterations modify the same `y1` element, which means that they cannot be executed in parallel (SIMD is instruction-level parallelism). And you can see that it precisely for these elements (which have value `3.0` after the scalar loop) that the results differ.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [August 16, 2023, 10:12pm UTC](https://discourse.julialang.org/t/why-is-this-loop-type-not-supported-by-loopvectorization/102886/3 "2023-08-16T22:12:39Z")

</div>

> [@stevengj](#):
>
> It assumes that loop iterations are independent.

What is the definition of independence?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 16, 2023, 10:13pm UTC](https://discourse.julialang.org/t/why-is-this-loop-type-not-supported-by-loopvectorization/102886/4 "2023-08-16T22:13:31Z")

</div>

> [@jar1](#):
>
> What is the definition of independence?

google [“loop independence”](https://www.cita.utoronto.ca/~merz/intel_c10b/main_cls/mergedProjects/optaps_cls/common/optaps_perf_loop_indep.htm)

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 16, 2023, 10:20pm UTC](https://discourse.julialang.org/t/why-is-this-loop-type-not-supported-by-loopvectorization/102886/5 "2023-08-16T22:20:00Z")

</div>

If you can rearrange the order of the iterations without changing the result of the calculation.

For example, what happens if you run this many times:

```julia
for i = shuffle(1:10) 
    y0[f[i]] += 1.0
    y0[f[i] + 1] += 2.0
end

```

Do you always get the same result?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 16, 2023, 10:49pm UTC](https://discourse.julialang.org/t/why-is-this-loop-type-not-supported-by-loopvectorization/102886/6 "2023-08-16T22:49:19Z")

</div>

I would guess that invariance to shuffling is not enough either. The different iterations should be able to execute simultaneously, so check for possible data races.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 16, 2023, 11:15pm UTC](https://discourse.julialang.org/t/why-is-this-loop-type-not-supported-by-loopvectorization/102886/7 "2023-08-16T23:15:33Z")

</div>

> [@DNF](#):
>
> Do you always get the same result?

In this case yes, because addition is commutative. But the iterations cannot execute arbitrarily in parallel without data races.
