# Unsure why I'm getting errors with LoopVectorization

**URL:** <https://discourse.julialang.org/t/unsure-why-im-getting-errors-with-loopvectorization/98766>\
**Category:** New to Julia\
**Tags:** loopvectorization\
**Created:** [May 12, 2023, 9:40pm UTC](https://discourse.julialang.org/t/unsure-why-im-getting-errors-with-loopvectorization/98766 "2023-05-12T21:40:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dominic\_Pazzula](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dominic_pazzula/32/12783_2.png) [@Dominic\_Pazzula](https://discourse.julialang.org/u/Dominic_Pazzula)\
**Post date:** [May 12, 2023, 9:40pm UTC](https://discourse.julialang.org/t/unsure-why-im-getting-errors-with-loopvectorization/98766/1 "2023-05-12T21:40:03Z")

</div>

Julia v1.8.5  
LoopVectorization v0.12.159

Working on trying to maximize performance of some functions and I’m getting errors. Here is a minimum code example of one:

```julia
function test(a,n)
    rows = size(a,1)
    cols = size(a,2)

    println(rows,":",cols)
    # a[n,:] = mean.(eachcol(a[1:n,:]))
    @turbo for j in 1:cols
        _s = 0.0
        for i in 1:n
            _s += a[i,j]
        end
        a[n,j] = _s / n
    end

end

N = 100_000
x = rand(N,2)
test(x,10)

y = rand(N)
test(y,10)

```

The second call gives: `ERROR: MethodError: reducing over an empty collection is not allowed; consider supplying `init` to the reducer`

I realize the first passes a Matrix and the second passes a Vector.

Function works as expected without the `@turbo`.

Why is this error being generated and how can I modify the code for this to work with both Matrix and Vector types?

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [May 12, 2023, 9:56pm UTC](https://discourse.julialang.org/t/unsure-why-im-getting-errors-with-loopvectorization/98766/2 "2023-05-12T21:56:34Z")

</div>

At first glance it seems this code is explicitly designed for a two-dimensional array, for instance because it mentions rows and cols. What do you expect to achieve when giving it a vector?

---

<div class="post-metadata">

**Author:** ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)\
**Post date:** [May 13, 2023, 9:49am UTC](https://discourse.julialang.org/t/unsure-why-im-getting-errors-with-loopvectorization/98766/3 "2023-05-13T09:49:15Z")

</div>

Redefine the existing method as `test(a::AbstractaMatrix, n)`, and add a dispatch for `test(a::AbstractVector, n)`, which would have only a single loop.

You can define different methods for the different combinations of input parameter types: [https://docs.julialang.org/en/v1/manual/methods/](https://docs.julialang.org/en/v1/manual/methods/)

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [May 15, 2023, 7:58am UTC](https://discourse.julialang.org/t/unsure-why-im-getting-errors-with-loopvectorization/98766/4 "2023-05-15T07:58:37Z")

</div>

If you don’t want, as others suggest, to define a vector-specific method, try moving @turbo to the inner loop, which is the more demanding one.

```julia
function test(a,n)
    rows = size(a,1)
    cols = size(a,2)

    println(rows,":",cols)
    # a[n,:] = mean.(eachcol(a[1:n,:]))
    for j in 1:cols
        _s = 0.0
        @turbo for i in 1:n
            _s += a[i,j]
        end
        a[n,j] = _s / n
    end

end

```

Or reshape the vector as a column matrix. (although, it doesn’t seem like a nice solution to me)

```julia

function test1(a,n)
    rows = size(a,1)
    cols = size(a,2)
    a=reshape(a,:,cols)
    println(rows,":",cols)
    # a[n,:] = mean.(eachcol(a[1:n,:]))
    @turbo for j in 1:cols
        _s = 0.0
        for i in 1:n
            _s += a[i,j]
        end
        a[n,j] = _s / n
    end
end

```

PS  
I don’t know the terms and applicability limits of the macro, but the highlighted problem deserves further investigation by those who know the macro well
