For a simple reduction of an array I have code that vectorizes nicely:
function simd_sum{T}(x::Vector{T})
s = zero(T)
@simd for i in eachindex(x)
@inbounds s = s + x[i]
end
return send
By looking at
@code_llvm simd_sum(rand(Float64, 10))
it can be seen that the loop is vectorized to use SIMD.
However, for a similar loop using tuples:
function tuple_simd_sum{T}(x::Vector{NTuple{4, T}})
s = (0.0, 0.0, 0.0, 0.0)
@inbounds @simd for i in eachindex(x)
x_i = x[i]
s = (s[1] + x_i[1], s[2] + x_i[2], s[3] + x_i[3], s[4] + x_i[4])
end
return send
tuple_vec = [(rand(), rand(), rand(), rand()) for i = 1:20]
@code_llvm tuple_simd_sum(tuple_vec)
The loop fails to use and vector instructions.
Does anyone have any more info regarding vectorization of operations
including tuples and if it is possible to somehow write code that
vectorizes with tuples.
Thanks!
// Kristoffer
PS: I've seen https://github.com/eschnett/SIMD.jl but something a bit
higher level would be nice.