Skip to content

Instantly share code, notes, and snippets.

@abelsiqueira
Created December 20, 2016 18:24
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 abelsiqueira/e1a593a7e763b201e915b9aa02e2cc1f to your computer and use it in GitHub Desktop.
Save abelsiqueira/e1a593a7e763b201e915b9aa02e2cc1f to your computer and use it in GitHub Desktop.
using BenchmarkTools
using CUTEst
using NLPModels
function nlpsolver(nlp; itmax=100000, eta=1e-4, eps=1e-6, sigma=0.66)
if !unconstrained(nlp)
error("Problem is not unconstrained")
end
f(x) = obj(nlp, x)
∇f(x) = grad(nlp, x)
x = nlp.meta.x0
fx = f(x)
∇fx = ∇f(x)
slope = dot(∇fx, ∇fx)
∇f_norm = sqrt(slope)
iter = 0
while ∇f_norm > eps && iter < itmax
t = 1.0
x_trial = x - t * ∇fx
f_trial = f(x_trial)
while f_trial > fx - eta * t * slope
t *= sigma
x_trial = x - t * ∇fx
f_trial = f(x_trial)
end
x = x_trial
fx = f_trial
∇fx = ∇f(x)
slope = dot(∇fx, ∇fx)
∇f_norm = sqrt(slope)
iter += 1
end
optimal = ∇f_norm <= eps
return x, fx, ∇f_norm, optimal, iter
end
function nlpsolver!(nlp; itmax=100000, eta=1e-4, eps=1e-6, sigma=0.66)
if !unconstrained(nlp)
error("Problem is not unconstrained")
end
f(x) = obj(nlp, x)
∇f!(x,g) = grad!(nlp, x, g)
x = copy(nlp.meta.x0)
x_trial = zeros(nlp.meta.nvar)
fx = f(x)
∇fx = zeros(nlp.meta.nvar)
∇f!(x, ∇fx)
slope = dot(∇fx, ∇fx)
∇f_norm = sqrt(slope)
iter = 0
while ∇f_norm > eps && iter < itmax
t = 1.0
copy!(x_trial, x)
BLAS.axpy!(-t, ∇fx, x_trial)
f_trial = f(x_trial)
while f_trial > fx - eta * t * slope
t *= sigma
copy!(x_trial, x)
BLAS.axpy!(-t, ∇fx, x_trial)
f_trial = f(x_trial)
end
copy!(x, x_trial)
fx = f_trial
∇f!(x, ∇fx)
slope = dot(∇fx, ∇fx)
∇f_norm = sqrt(slope)
iter += 1
end
optimal = ∇f_norm <= eps
return x, fx, ∇f_norm, optimal, iter
end
function specsolver(nlp; itmax=100000, eta=1e-4, eps=1e-6, sigma=0.66)
if !unconstrained(nlp)
error("Problem is not unconstrained")
end
x = nlp.meta.x0
f(x) = ufn(nlp.meta.nvar, x)
∇f(x) = ugr(nlp.meta.nvar, x)
fx = f(x)
∇fx = ∇f(x)
slope = dot(∇fx, ∇fx)
∇f_norm = sqrt(slope)
iter = 0
while ∇f_norm > eps && iter < itmax
t = 1.0
x_trial = x - t * ∇fx
f_trial = f(x_trial)
while f_trial > fx - eta * t * slope
t *= sigma
x_trial = x - t * ∇fx
f_trial = f(x_trial)
end
x = x_trial
fx = f_trial
∇fx = ∇f(x)
slope = dot(∇fx, ∇fx)
∇f_norm = sqrt(slope)
iter += 1
end
optimal = ∇f_norm <= eps
return x, fx, ∇f_norm, optimal, iter
end
function specsolver!(nlp; itmax=100000, eta=1e-4, eps=1e-6, sigma=0.66)
if !unconstrained(nlp)
error("Problem is not unconstrained")
end
f(x) = ufn(nlp.meta.nvar, x)
∇f!(x,g) = ugr!(nlp.meta.nvar, x, g)
x = copy(nlp.meta.x0)
x_trial = zeros(nlp.meta.nvar)
fx = f(x)
∇fx = zeros(nlp.meta.nvar)
∇f!(x, ∇fx)
slope = dot(∇fx, ∇fx)
∇f_norm = sqrt(slope)
iter = 0
while ∇f_norm > eps && iter < itmax
t = 1.0
copy!(x_trial, x)
BLAS.axpy!(-t, ∇fx, x_trial)
f_trial = f(x_trial)
while f_trial > fx - eta * t * slope
t *= sigma
copy!(x_trial, x)
BLAS.axpy!(-t, ∇fx, x_trial)
f_trial = f(x_trial)
end
copy!(x, x_trial)
fx = f_trial
∇f!(x, ∇fx)
slope = dot(∇fx, ∇fx)
∇f_norm = sqrt(slope)
iter += 1
end
optimal = ∇f_norm <= eps
return x, fx, ∇f_norm, optimal, iter
end
function foo()
log = open("interface_bench.log", "w")
for p in ["ROSENBR", ["DIXMAANJ","-param","M=30"]]
if isa(p, String)
println(log, "Problem $p")
nlp = CUTEstModel(p)
else
println(log, "Problem $(p[1])")
nlp = CUTEstModel(p...)
end
try
nlpsolver(nlp)
specsolver(nlp)
nlpsolver!(nlp)
specsolver!(nlp)
t1 = @benchmark obj($nlp, $(nlp.meta.x0))
t2 = @benchmark ufn($(nlp.meta.nvar), $(nlp.meta.x0))
println(log, "# obj vs ufn\n")
println(log, "obj: $t1\n")
println(log, "ufn: $t2\n\n")
t1 = @benchmark grad($nlp, $(nlp.meta.x0))
t2 = @benchmark ugr($(nlp.meta.nvar), $(nlp.meta.x0))
println(log, "# grad vs ugr\n")
println(log, "grad: $t1\n")
println(log, "ugr: $t2\n\n")
t1 = @benchmark nlpsolver($nlp)
t2 = @benchmark specsolver($nlp)
t3 = @benchmark nlpsolver!($nlp)
t4 = @benchmark specsolver!($nlp)
println(log, "Different solvers\n")
println(log, "nlpsolver: $t1\n")
println(log, "specsolver: $t2\n")
println(log, "nlpsolver!: $t3\n")
println(log, "specsolver!: $t4\n")
finally
finalize(nlp)
end
end
close(log)
end
foo()
Problem ROSENBR
# obj vs ufn
obj: BenchmarkTools.Trial:
memory estimate: 304.00 bytes
allocs estimate: 5
--------------
minimum time: 360.394 ns (0.00% GC)
median time: 389.459 ns (0.00% GC)
mean time: 433.279 ns (8.61% GC)
maximum time: 14.973 μs (96.83% GC)
--------------
samples: 10000
evals/sample: 208
time tolerance: 5.00%
memory tolerance: 1.00%
ufn: BenchmarkTools.Trial:
memory estimate: 304.00 bytes
allocs estimate: 4
--------------
minimum time: 329.572 ns (0.00% GC)
median time: 356.568 ns (0.00% GC)
mean time: 400.092 ns (7.96% GC)
maximum time: 13.692 μs (94.79% GC)
--------------
samples: 10000
evals/sample: 222
time tolerance: 5.00%
memory tolerance: 1.00%
# grad vs ugr
grad: BenchmarkTools.Trial:
memory estimate: 304.00 bytes
allocs estimate: 4
--------------
minimum time: 473.898 ns (0.00% GC)
median time: 495.143 ns (0.00% GC)
mean time: 545.081 ns (5.83% GC)
maximum time: 14.033 μs (94.55% GC)
--------------
samples: 10000
evals/sample: 196
time tolerance: 5.00%
memory tolerance: 1.00%
ugr: BenchmarkTools.Trial:
memory estimate: 304.00 bytes
allocs estimate: 4
--------------
minimum time: 414.869 ns (0.00% GC)
median time: 463.608 ns (0.00% GC)
mean time: 502.902 ns (6.55% GC)
maximum time: 16.472 μs (96.09% GC)
--------------
samples: 10000
evals/sample: 199
time tolerance: 5.00%
memory tolerance: 1.00%
Different solvers
nlpsolver: BenchmarkTools.Trial:
memory estimate: 159.54 mb
allocs estimate: 3250662
--------------
minimum time: 190.423 ms (10.62% GC)
median time: 197.683 ms (11.02% GC)
mean time: 200.775 ms (11.05% GC)
maximum time: 219.748 ms (10.12% GC)
--------------
samples: 25
evals/sample: 1
time tolerance: 5.00%
memory tolerance: 1.00%
specsolver: BenchmarkTools.Trial:
memory estimate: 150.72 mb
allocs estimate: 2383716
--------------
minimum time: 167.377 ms (9.09% GC)
median time: 178.970 ms (10.33% GC)
mean time: 180.058 ms (10.22% GC)
maximum time: 204.153 ms (9.96% GC)
--------------
samples: 28
evals/sample: 1
time tolerance: 5.00%
memory tolerance: 1.00%
nlpsolver!: BenchmarkTools.Trial:
memory estimate: 100.57 mb
allocs estimate: 2365756
--------------
minimum time: 168.193 ms (7.25% GC)
median time: 186.184 ms (8.33% GC)
mean time: 193.679 ms (7.87% GC)
maximum time: 268.656 ms (7.34% GC)
--------------
samples: 26
evals/sample: 1
time tolerance: 5.00%
memory tolerance: 1.00%
specsolver!: BenchmarkTools.Trial:
memory estimate: 87.34 mb
allocs estimate: 1209825
--------------
minimum time: 133.121 ms (7.37% GC)
median time: 143.275 ms (7.24% GC)
mean time: 143.585 ms (7.19% GC)
maximum time: 162.090 ms (7.04% GC)
--------------
samples: 35
evals/sample: 1
time tolerance: 5.00%
memory tolerance: 1.00%
Problem DIXMAANJ
# obj vs ufn
obj: BenchmarkTools.Trial:
memory estimate: 304.00 bytes
allocs estimate: 5
--------------
minimum time: 4.073 μs (0.00% GC)
median time: 4.248 μs (0.00% GC)
mean time: 4.389 μs (0.89% GC)
maximum time: 397.942 μs (98.34% GC)
--------------
samples: 10000
evals/sample: 8
time tolerance: 5.00%
memory tolerance: 1.00%
ufn: BenchmarkTools.Trial:
memory estimate: 304.00 bytes
allocs estimate: 4
--------------
minimum time: 3.773 μs (0.00% GC)
median time: 4.073 μs (0.00% GC)
mean time: 4.144 μs (0.80% GC)
maximum time: 339.338 μs (98.08% GC)
--------------
samples: 10000
evals/sample: 8
time tolerance: 5.00%
memory tolerance: 1.00%
# grad vs ugr
grad: BenchmarkTools.Trial:
memory estimate: 1.00 kb
allocs estimate: 4
--------------
minimum time: 10.755 μs (0.00% GC)
median time: 11.260 μs (0.00% GC)
mean time: 11.635 μs (0.00% GC)
maximum time: 26.580 μs (0.00% GC)
--------------
samples: 10000
evals/sample: 1
time tolerance: 5.00%
memory tolerance: 1.00%
ugr: BenchmarkTools.Trial:
memory estimate: 1.00 kb
allocs estimate: 4
--------------
minimum time: 11.122 μs (0.00% GC)
median time: 11.521 μs (0.00% GC)
mean time: 12.186 μs (0.00% GC)
maximum time: 50.900 μs (0.00% GC)
--------------
samples: 10000
evals/sample: 1
time tolerance: 5.00%
memory tolerance: 1.00%
Different solvers
nlpsolver: BenchmarkTools.Trial:
memory estimate: 55.37 mb
allocs estimate: 288012
--------------
minimum time: 380.757 ms (1.21% GC)
median time: 417.060 ms (1.03% GC)
mean time: 418.088 ms (1.02% GC)
maximum time: 475.961 ms (0.80% GC)
--------------
samples: 12
evals/sample: 1
time tolerance: 5.00%
memory tolerance: 1.00%
specsolver: BenchmarkTools.Trial:
memory estimate: 54.78 mb
allocs estimate: 230388
--------------
minimum time: 385.180 ms (0.75% GC)
median time: 389.809 ms (0.96% GC)
mean time: 396.108 ms (0.94% GC)
maximum time: 421.077 ms (0.70% GC)
--------------
samples: 13
evals/sample: 1
time tolerance: 5.00%
memory tolerance: 1.00%
nlpsolver!: BenchmarkTools.Trial:
memory estimate: 10.26 mb
allocs estimate: 211212
--------------
minimum time: 380.829 ms (0.00% GC)
median time: 390.238 ms (0.00% GC)
mean time: 391.400 ms (0.36% GC)
maximum time: 402.276 ms (0.82% GC)
--------------
samples: 13
evals/sample: 1
time tolerance: 5.00%
memory tolerance: 1.00%
specsolver!: BenchmarkTools.Trial:
memory estimate: 9.38 mb
allocs estimate: 134377
--------------
minimum time: 369.951 ms (0.00% GC)
median time: 372.423 ms (0.00% GC)
mean time: 386.405 ms (0.29% GC)
maximum time: 466.496 ms (0.00% GC)
--------------
samples: 14
evals/sample: 1
time tolerance: 5.00%
memory tolerance: 1.00%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment