JuliaDiff / ForwardDiff.jl

Forward Mode Automatic Differentiation for Julia
Other
884 stars 141 forks source link

Simple reductions of Vectors of ForwardDiff numbers fail to use SIMD #98

Closed KristofferC closed 7 years ago

KristofferC commented 8 years ago

A standrad reduction function like:

function simd_sum{T}(x::Vector{T})
    s = zero(T)
    @simd for i in eachindex(x)
        @inbounds s = s + x[i]
    end
    return s
end

which vectorizes with Vector{Float64} fails to vectorize with ForwardDiff numbers. For example:

@code_llvm simd_sum(rand(Float64, 10))

G = ForwardDiff.GradientNumber{4, Float64, NTuple{4, Float64}}
G_vec = G[rand(G) for i = 1:10]
@code_llvm simd_sum(G_vec)

It is possible that there is nothing that can be done on the ForwardDiff side to solve this but I thought it could be interesting to track nonetheless.