# Problems with sequential update with @turbo

**URL:** <https://discourse.julialang.org/t/problems-with-sequential-update-with-turbo/118397>\
**Category:** General Usage\
**Tags:** loopvectorization\
**Created:** [August 20, 2024, 12:42am UTC](https://discourse.julialang.org/t/problems-with-sequential-update-with-turbo/118397 "2024-08-20T00:42:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![2xu2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/2xu2/32/211442_2.png) [@2xu2](https://discourse.julialang.org/u/2xu2)\
**Post date:** [August 20, 2024, 12:42am UTC](https://discourse.julialang.org/t/problems-with-sequential-update-with-turbo/118397/1 "2024-08-20T00:42:53Z")

</div>

I have the following minimal working example that contains two weird behaviors of @turbo with sequential update:

```julia
using LoopVectorization

# Incorrect usage of @turbo 
function wrong_example(n)
    x = 0.0
    y = zeros(n)
    
    @turbo for i in 1:n
        x += i * 2 + 1 # the order of updating this matters somehow with both * and +? 
        for j in 1:n
            y[j] += 1
        end
    end
    
    return x, y
end

# Correct usage without @turbo
function correct_example(n)
    x = 0.0
    y = zeros(n)
    
    for i in 1:n
        x += i * 2 + 1 # Sequential update of x
        for j in 1:n
            y[j] += 1
        end
    end
    
    return x, y
end

# Test with a small n
n = 5

x_wrong, y_wrong = wrong_example(n)
x_correct, y_correct = correct_example(n)

println("x_wrong = ", x_wrong)
println("y_wrong = ", y_wrong)
println("x_correct = ", x_correct)
println("y_correct = ", y_correct)

```

The result spits

```julia
x_wrong = 0.0
y_wrong = [1.0, 1.0, 1.0, 1.0, 1.0]
x_correct = 35.0
y_correct = [5.0, 5.0, 5.0, 5.0, 5.0]

```

The reasons why either x or y differ from the correct results seem peculiar to me, as both should not rely on specific execution orders. In particular, either removing `*2` or `+1` in the operations of x can’t generate the difference in x.

I wonder why these are the cases and how I can fix these. Since eventually I want to apply @turbo onto a much more complicated stochastic optimization problem, I will also appreciate some in depth explanation of how @turbo parallelize loops and some general advice on what to avoid when it comes to updating something with either += or \*=.
