Skip to content

Commit

Permalink
Use broadcast expressions instead of some loops.
Browse files Browse the repository at this point in the history
  • Loading branch information
maleadt authored and jrevels committed Aug 27, 2018
1 parent 579f6a7 commit 19003af
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
20 changes: 6 additions & 14 deletions src/apiutils.jl
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])

This comment has been minimized.

Copy link
@jrevels

jrevels Aug 31, 2018

Member

@maleadt ref JuliaLang/METADATA.jl#17515 (comment), also x[chunk] will allocate for normal arrays, yeah?

Is there an easy way we could revert/alter this but still preserve the goal of #351 to make this code CuArray-compatible?

Sorry, this is something I should've caught in the original review of the PR 😛

return duals
end
5 changes: 2 additions & 3 deletions src/gradient.jl
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

0 comments on commit 19003af

Please sign in to comment.