# Is this a valid use of simd?

**URL:** <https://discourse.julialang.org/t/is-this-a-valid-use-of-simd/100463>\
**Category:** General Usage\
**Tags:** simd\
**Created:** [June 16, 2023, 6:17pm UTC](https://discourse.julialang.org/t/is-this-a-valid-use-of-simd/100463 "2023-06-16T18:17:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)\
**Post date:** [June 16, 2023, 6:17pm UTC](https://discourse.julialang.org/t/is-this-a-valid-use-of-simd/100463/1 "2023-06-16T18:17:38Z")

</div>

I am trying to calculate the sum of the absolute differences between two vectors without allocation. I have no experience with `@simd` but was wondering if this is a correct use:

```julia
function sum_absdiff(x::Vector{T}, y::Vector{T}) where {T}
    s = zero(T)
    @simd for i in eachindex(x)
        s += abs(x[i] - y[i])
    end
    return s
end

```

---

<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 16, 2023, 6:19pm UTC](https://discourse.julialang.org/t/is-this-a-valid-use-of-simd/100463/2 "2023-06-16T18:19:05Z")

</div>

you probably need `eachindex(x,y)` so the compiler knows you aren’t indexing out of bounds on `y`, but other than that, this looks right.

---

<div class="post-metadata">

**Author:** ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)\
**Post date:** [June 16, 2023, 6:26pm UTC](https://discourse.julialang.org/t/is-this-a-valid-use-of-simd/100463/3 "2023-06-16T18:26:57Z")

</div>

Thank you!
