Skip to content

Instantly share code, notes, and snippets.

@huanglangwen
Last active September 22, 2019 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huanglangwen/36d39800e5f68b585193a74d6f0a4975 to your computer and use it in GitHub Desktop.
Save huanglangwen/36d39800e5f68b585193a74d6f0a4975 to your computer and use it in GitHub Desktop.
Benchmarking the performance of `forward_color_jacobian!` and `ForwardDiff.jacobian!`
using ForwardDiff, SparseDiffTools, DiffTests, DiffResults
using BenchmarkTools
######################Test/utils.jl
using ForwardDiff: DEFAULT_CHUNK_THRESHOLD
using Test
using Random
# seed RNG, thus making result inaccuracies deterministic
# so we don't have to retune EPS for arbitrary inputs
Random.seed!(1)
const XLEN = DEFAULT_CHUNK_THRESHOLD + 1
const YLEN = div(DEFAULT_CHUNK_THRESHOLD, 2) + 1
const X, Y = rand(XLEN), rand(YLEN)
const CHUNK_SIZES = (1, div(DEFAULT_CHUNK_THRESHOLD, 3), div(DEFAULT_CHUNK_THRESHOLD, 2), DEFAULT_CHUNK_THRESHOLD, DEFAULT_CHUNK_THRESHOLD + 1)
const HESSIAN_CHUNK_SIZES = (1, 2, 3)
const FINITEDIFF_ERROR = 3e-5
#############################################################
name(f) = last(split(string(f), '.'))
const SUITE = BenchmarkGroup()
const mats = map(n -> rand(MersenneTwister(1), n, n), (5, 16, 32))
const jacobian_group = addgroup!(SUITE, "jacobian")
const jacobiansp_group = addgroup!(SUITE, "sparsejacobian")
for f in DiffTests.INPLACE_ARRAY_TO_ARRAY_FUNCS
fjac = addgroup!(jacobian_group, name(f))
for x in mats
y = similar(x)
out = DiffResults.JacobianResult(y, x)
cfg = ForwardDiff.JacobianConfig(nothing, y, x)
fjac[length(x)] = @benchmarkable ForwardDiff.jacobian!($out, $f, $y, $x, $cfg)
end
end
for f in DiffTests.INPLACE_ARRAY_TO_ARRAY_FUNCS
fjac = addgroup!(jacobiansp_group, name(f))
for x in mats
y = similar(x)
#out = DiffResults.JacobianResult(y, x)
out = reshape(y,:) .* reshape(x,:)'
cfg = SparseDiffTools.ForwardColorJacCache(f, x)
fjac[length(x)] = @benchmarkable SparseDiffTools.forwarddiff_color_jacobian!($out, $f, $x, $cfg)
end
end
#sparseAD problem: DiffResults.JacobianResult
println("Tuning...")
tune!(SUITE)
println("Running Benchmarks...")
results=run(SUITE, verbose = true)
show(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment