# Method Error in simple LoopVectorization call

**URL:** <https://discourse.julialang.org/t/method-error-in-simple-loopvectorization-call/98444>\
**Category:** General Usage\
**Tags:** loopvectorization\
**Created:** [May 7, 2023, 7:21pm UTC](https://discourse.julialang.org/t/method-error-in-simple-loopvectorization-call/98444 "2023-05-07T19:21:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alec\_Loudenback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alec_loudenback/32/278_2.png) [@Alec\_Loudenback](https://discourse.julialang.org/u/Alec_Loudenback)\
**Post date:** [May 7, 2023, 7:21pm UTC](https://discourse.julialang.org/t/method-error-in-simple-loopvectorization-call/98444/1 "2023-05-07T19:21:01Z")

</div>

With this simple example, I get an error where I do not expect it when looping over a `Vector{Int}` with length 2.

```julia
using LoopVectorization
function f(v)
    x = zero(eltype(v))
    @turbo for i ∈ eachindex(v)
        amt = v[i] * 0.1
        x += amt
    end
    x
end

```

These work:

```julia
f(rand(Int,3)) # Integer length 3 
f(rand(2)) # Float length 2

```

But integers with a vector length of 2 do not:

```julia-repl
julia> f(rand(Int,2))
ERROR: MethodError: no method matching VectorizationBase.VecUnroll(::Tuple{VectorizationBase.Vec{2, Float64}, VectorizationBase.Vec{2, Int64}})

```

Am I doing something wrong? If I removed the `* 0.1` from the `amt = v[i] * 0.1` line then an integer vector of length two works.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [May 7, 2023, 7:27pm UTC](https://discourse.julialang.org/t/method-error-in-simple-loopvectorization-call/98444/2 "2023-05-07T19:27:08Z")

</div>

When I run your code the example with `f(rand(Int, 3))` also fails. I don’t think this is too surprising since your code is type unstable. `x` is changing from an `Int` to a `Float64` here. If we initialize `x` in a type stable manner, everything is fine:

```julia
julia> function f(v)
           x = zero(promote_type(Float64, eltype(v)))
           @turbo for i ∈ eachindex(v)
               amt = v[i] * 0.1
               x += amt
           end
           x
       end
f (generic function with 1 method)

julia> f(rand(Int, 2))
2.6610511336745555e17

julia> f([1, 2])
0.30000000000000004

julia> f([1, 2, 3])
0.6000000000000001

```

LoopVectorization.jl is pretty brittle around things like this.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [May 7, 2023, 7:29pm UTC](https://discourse.julialang.org/t/method-error-in-simple-loopvectorization-call/98444/3 "2023-05-07T19:29:43Z")

</div>

> [@Mason](#):
>
> LoopVectorization.jl is pretty brittle around things like this.

Yes, it generally assumes code is type stable.  
This is a case where a better error message would be preferable over getting the example to work.  
Unfortunately, an error message would probably be more difficult.
