Skip to content

Use broadcast expressions instead of some loops. #351

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 2 commits into from
Aug 27, 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: 6 additions & 14 deletions src/apiutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 2 additions & 3 deletions src/gradient.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/partials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down