# How to choose vec size in SIMD.jl

**URL:** <https://discourse.julialang.org/t/how-to-choose-vec-size-in-simd-jl/63269>\
**Category:** Performance\
**Tags:** simd\
**Created:** [June 21, 2021, 2:45am UTC](https://discourse.julialang.org/t/how-to-choose-vec-size-in-simd-jl/63269 "2021-06-21T02:45:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [June 21, 2021, 2:45am UTC](https://discourse.julialang.org/t/how-to-choose-vec-size-in-simd-jl/63269/1 "2021-06-21T02:45:14Z")

</div>

Looking at the [SIMD.jl](https://github.com/eschnett/SIMD.jl) example `vadd!(xs, ys, Vec{8,Float64})` there is a specific size `8` being set for the vec size. How do I know whether to use 4 or 8 or more? I assume it is hardware dependent, but probably there is some way to choose this at runtime?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [June 21, 2021, 2:51am UTC](https://discourse.julialang.org/t/how-to-choose-vec-size-in-simd-jl/63269/2 "2021-06-21T02:51:54Z")

</div>

Don’t use `SIMD.jl`. It is a low level library meant for implementing higher level abstractions. Instead you should use `LoopVectorization` which will automatically pick the right defaults for you.

As an answer to the question you asked though, yes it’s hardware dependent, and you can choose the right size by seeing what instructions the CPU supports (specifically SSE/AVX/AVX2/AVX512 for x86 cpus)

---

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [June 21, 2021, 2:58am UTC](https://discourse.julialang.org/t/how-to-choose-vec-size-in-simd-jl/63269/3 "2021-06-21T02:58:42Z")

</div>

What I am trying to do does not really fit LoopVectorizations. I need bit-wise logical operations with complicated intermediate bitwise formulas that LoopVectorization’s @turbo and @simd failed to infere can be vectorized.

Hence going back to the SIMD.jl question I have above.

[Here is the fairly short kernel that I want to vectorize better](https://github.com/Krastanov/QuantumClifford.jl/blob/8ba2ddd84cc79dc0532bdc8c93df1c33dfc3cb55/src/QuantumClifford.jl#L656)

```julia
function mul_left!(r::AbstractVector{T}, l::AbstractVector{T}) where T<:Unsigned
    cnt1 = zero(T)
    cnt2 = zero(T)
    len = length(l)>>1
    @inbounds @simd for i in 1:len
        x1, x2, z1, z2 = l[i], r[i], l[i+len], r[i+len]
        r[i] = newx1 = x1 ⊻ x2
        r[i+len] = newz1 = z1 ⊻ z2
        x1z2 = x1 & z2
        anti_comm = (x2 & z1) ⊻ x1z2
        cnt2 ⊻= (cnt1 ⊻ newx1 ⊻ newz1 ⊻ x1z2) & anti_comm
        cnt1 ⊻= anti_comm
    end
    s = count_ones(cnt1)
    s ⊻= count_ones(cnt2) << 1
    s
end

```

More general advice on how to improve its vectorization would certainly be appreciated of course!

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [June 21, 2021, 4:16am UTC](https://discourse.julialang.org/t/how-to-choose-vec-size-in-simd-jl/63269/4 "2021-06-21T04:16:24Z")

</div>

IMO, this seems like a place where you should file a bug report. Nothing about this code should be that hard for LoopVectorization to vectorize.

---

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [June 21, 2021, 4:19am UTC](https://discourse.julialang.org/t/how-to-choose-vec-size-in-simd-jl/63269/5 "2021-06-21T04:19:32Z")

</div>

Isn’t the fact that `cnt1` and `cnt2` of fixed scalar size, instead of a SIMD vector, making it impossible to perform operations involving them vectorized?

---

<div class="post-metadata">

**Author:** ![YingboMa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yingboma/32/2181_2.png) [@YingboMa](https://discourse.julialang.org/u/YingboMa)\
**Post date:** [June 21, 2021, 4:32am UTC](https://discourse.julialang.org/t/how-to-choose-vec-size-in-simd-jl/63269/6 "2021-06-21T04:32:47Z")

</div>

VectorizationBase.jl has more features than SIMD.jl

```julia
julia> using VectorizationBase

julia> VectorizationBase.pick_vector_width(Float64)
static(8)

```
