From 79b0501d13e72f2929c58124a0bb5aa765882fd7 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Fri, 24 Aug 2018 14:44:48 +0200 Subject: [PATCH 1/2] Use broadcast expressions instead of some loops. --- src/apiutils.jl | 20 ++++++-------------- src/gradient.jl | 5 ++--- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/src/apiutils.jl b/src/apiutils.jl index eec39653..44b931df 100644 --- a/src/apiutils.jl +++ b/src/apiutils.jl @@ -53,36 +53,28 @@ end function seed!(duals::AbstractArray{Dual{T,V,N}}, x, seed::Partials{N,V} = zero(Partials{N,V})) where {T,V,N} - for i in eachindex(duals) - duals[i] = Dual{T,V,N}(x[i], seed) - end + duals .= Dual{T,V,N}.(x, Base.RefValue(seed)) return duals end function seed!(duals::AbstractArray{Dual{T,V,N}}, x, seeds::NTuple{N,Partials{N,V}}) where {T,V,N} - for i in 1:N - duals[i] = Dual{T,V,N}(x[i], seeds[i]) - end + duals[1:N] .= Dual{T,V,N}.(x[1:N], seeds[1:N]) return duals end function seed!(duals::AbstractArray{Dual{T,V,N}}, x, index, seed::Partials{N,V} = zero(Partials{N,V})) where {T,V,N} offset = index - 1 - for i in 1:N - j = i + offset - duals[j] = Dual{T,V,N}(x[j], seed) - end + chunk = (1:N) .+ offset + duals[chunk] .= Dual{T,V,N}.(x[chunk], Base.RefValue(seed)) return duals end function seed!(duals::AbstractArray{Dual{T,V,N}}, x, index, seeds::NTuple{N,Partials{N,V}}, chunksize = N) where {T,V,N} offset = index - 1 - for i in 1:chunksize - j = i + offset - duals[j] = Dual{T,V,N}(x[j], seeds[i]) - end + chunk = (1:chunksize) .+ offset + duals[chunk] .= Dual{T,V,N}.(x[chunk], seeds[1:chunksize]) return duals end diff --git a/src/gradient.jl b/src/gradient.jl index 8721af6d..771bffa7 100644 --- a/src/gradient.jl +++ b/src/gradient.jl @@ -77,9 +77,8 @@ extract_gradient!(::Type{T}, result::AbstractArray, dual::Dual) where {T}= copyt function extract_gradient_chunk!(::Type{T}, result, dual, index, chunksize) where {T} offset = index - 1 - for i in 1:chunksize - result[i + offset] = partials(T, dual, i) - end + chunk = (1:chunksize) .+ offset + result[chunk] .= ForwardDiff.partials(T, dual, 1:chunksize) return result end From 9d21d51b1629de2dffe4624b29e212aa756c8a81 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Mon, 27 Aug 2018 14:21:12 +0200 Subject: [PATCH 2/2] Forward all calls to getindex(::Partials) to the inner container. Otherwise we always return an Array, where a Tuple would have sufficed (eg. when indexing with ranges). --- src/partials.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/partials.jl b/src/partials.jl index 5734a434..e8af95e4 100644 --- a/src/partials.jl +++ b/src/partials.jl @@ -20,7 +20,7 @@ end @inline Base.length(::Partials{N}) where {N} = N @inline Base.size(::Partials{N}) where {N} = (N,) -@inline Base.@propagate_inbounds Base.getindex(partials::Partials, i::Int) = partials.values[i] +@inline Base.@propagate_inbounds Base.getindex(partials::Partials, I...) = partials.values[I...] Base.iterate(partials::Partials) = iterate(partials.values) Base.iterate(partials::Partials, i) = iterate(partials.values, i)