Skip to content

Revert "Use broadcast expressions instead of some loops." #354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/apiutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,36 @@ end

function seed!(duals::AbstractArray{Dual{T,V,N}}, x,
seed::Partials{N,V} = zero(Partials{N,V})) where {T,V,N}
duals .= Dual{T,V,N}.(x, Base.RefValue(seed))
for i in eachindex(duals)
duals[i] = Dual{T,V,N}(x[i], seed)
end
return duals
end

function seed!(duals::AbstractArray{Dual{T,V,N}}, x,
seeds::NTuple{N,Partials{N,V}}) where {T,V,N}
duals[1:N] .= Dual{T,V,N}.(x[1:N], seeds[1:N])
for i in 1:N
duals[i] = Dual{T,V,N}(x[i], seeds[i])
end
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
chunk = (1:N) .+ offset
duals[chunk] .= Dual{T,V,N}.(x[chunk], Base.RefValue(seed))
for i in 1:N
j = i + offset
duals[j] = Dual{T,V,N}(x[j], seed)
end
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
chunk = (1:chunksize) .+ offset
duals[chunk] .= Dual{T,V,N}.(x[chunk], seeds[1:chunksize])
for i in 1:chunksize
j = i + offset
duals[j] = Dual{T,V,N}(x[j], seeds[i])
end
return duals
end
5 changes: 3 additions & 2 deletions src/gradient.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ 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
chunk = (1:chunksize) .+ offset
result[chunk] .= ForwardDiff.partials(T, dual, 1:chunksize)
for i in 1:chunksize
result[i + offset] = partials(T, dual, i)
end
return result
end

Expand Down