Is this a valid use of simd?

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:

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

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.

2 Likes

Thank you!