forked from JuliaDiff/SparseDiffTools.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request JuliaDiff#285 from avik-pal/ap/speed
More extensive use of Polyester
- Loading branch information
Showing
5 changed files
with
141 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name = "SparseDiffTools" | ||
uuid = "47a9eef4-7e08-11e9-0b38-333d64bd3804" | ||
authors = ["Pankaj Mishra <[email protected]>", "Chris Rackauckas <[email protected]>"] | ||
version = "2.16.0" | ||
version = "2.17.0" | ||
|
||
[deps] | ||
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" | ||
|
@@ -27,12 +27,14 @@ VertexSafeGraphs = "19fa3120-7c27-5ec5-8db8-b0b0aa330d6f" | |
|
||
[weakdeps] | ||
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9" | ||
Polyester = "f517fe37-dbe3-4b94-8317-1923a5111588" | ||
PolyesterForwardDiff = "98d1487c-24ca-40b6-b7ab-df2af84e126b" | ||
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7" | ||
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" | ||
|
||
[extensions] | ||
SparseDiffToolsEnzymeExt = "Enzyme" | ||
SparseDiffToolsPolyesterExt = "Polyester" | ||
SparseDiffToolsPolyesterForwardDiffExt = "PolyesterForwardDiff" | ||
SparseDiffToolsSymbolicsExt = "Symbolics" | ||
SparseDiffToolsZygoteExt = "Zygote" | ||
|
@@ -49,6 +51,7 @@ ForwardDiff = "0.10" | |
Graphs = "1" | ||
LinearAlgebra = "<0.0.1, 1" | ||
PackageExtensionCompat = "1" | ||
Polyester = "0.7.9" | ||
PolyesterForwardDiff = "0.1.1" | ||
Random = "1.6" | ||
Reexport = "1" | ||
|
@@ -70,6 +73,7 @@ BlockBandedMatrices = "ffab5731-97b5-5995-9138-79e8c1846df0" | |
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9" | ||
IterativeSolvers = "42fd0dbc-a981-5370-80f2-aaf504508153" | ||
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" | ||
Polyester = "f517fe37-dbe3-4b94-8317-1923a5111588" | ||
PolyesterForwardDiff = "98d1487c-24ca-40b6-b7ab-df2af84e126b" | ||
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" | ||
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
module SparseDiffToolsPolyesterExt | ||
|
||
using Adapt, ArrayInterface, ForwardDiff, FiniteDiff, Polyester, SparseDiffTools, | ||
SparseArrays | ||
import SparseDiffTools: polyesterforwarddiff_color_jacobian, ForwardColorJacCache, | ||
__parameterless_type | ||
|
||
function cld_fast(a::A, b::B) where {A, B} | ||
T = promote_type(A, B) | ||
return cld_fast(a % T, b % T) | ||
end | ||
function cld_fast(n::T, d::T) where {T} | ||
x = Base.udiv_int(n, d) | ||
x += n != d * x | ||
return x | ||
end | ||
|
||
function polyesterforwarddiff_color_jacobian(J::AbstractMatrix{<:Number}, f::F, | ||
x::AbstractArray{<:Number}, jac_cache::ForwardColorJacCache) where {F} | ||
t = jac_cache.t | ||
dx = jac_cache.dx | ||
p = jac_cache.p | ||
colorvec = jac_cache.colorvec | ||
sparsity = jac_cache.sparsity | ||
chunksize = jac_cache.chunksize | ||
maxcolor = maximum(colorvec) | ||
|
||
vecx = vec(x) | ||
|
||
nrows, ncols = size(J) | ||
|
||
if !(sparsity isa Nothing) | ||
rows_index, cols_index = ArrayInterface.findstructralnz(sparsity) | ||
rows_index = [rows_index[i] for i in 1:length(rows_index)] | ||
cols_index = [cols_index[i] for i in 1:length(cols_index)] | ||
else | ||
rows_index = 1:nrows | ||
cols_index = 1:ncols | ||
end | ||
|
||
if J isa AbstractSparseMatrix | ||
fill!(nonzeros(J), zero(eltype(J))) | ||
else | ||
fill!(J, zero(eltype(J))) | ||
end | ||
|
||
batch((length(p), min(length(p), Threads.nthreads()))) do _, start, stop | ||
color_i = (start - 1) * chunksize + 1 | ||
for i in start:stop | ||
partial_i = p[i] | ||
t_ = reshape(eltype(t).(vecx, ForwardDiff.Partials.(partial_i)), size(t)) | ||
fx = f(t_) | ||
for j in 1:chunksize | ||
dx = vec(ForwardDiff.partials.(fx, j)) | ||
pick_inds = [idx | ||
for idx in 1:length(rows_index) | ||
if colorvec[cols_index[idx]] == color_i] | ||
rows_index_c = rows_index[pick_inds] | ||
cols_index_c = cols_index[pick_inds] | ||
@inbounds @simd for i in 1:length(rows_index_c) | ||
J[rows_index_c[i], cols_index_c[i]] = dx[rows_index_c[i]] | ||
end | ||
color_i += 1 | ||
end | ||
end | ||
end | ||
return J | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters