Skip to content

Commit 0165653

Browse files
committed
Benchmark produce calls.
1 parent 318064d commit 0165653

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

perf/benchmark.jl

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ using BenchmarkTools
55
####################################################################
66

77
function benchmark_driver!(f, x...; f_displayname=string(f))
8+
x = (x..., nothing)
89
println("benchmarking $(f_displayname)...")
9-
tf = Libtask.TapedFunction(f, x)
10+
tf = Libtask.TapedFunction(f, x...);
1011

1112
print(" Run Original Function:")
1213
@btime $f($(x)...)
@@ -20,30 +21,43 @@ function benchmark_driver!(f, x...; f_displayname=string(f))
2021
print(" Run TapedFunction (compiled):")
2122
@btime $ctf($(x)...)
2223
GC.gc()
24+
25+
print(" Run TapedTask:")
26+
x = (x[1:end-1]..., produce)
27+
tf = Libtask.TapedFunction(f, x...);
28+
@btime begin tt = TapedTask($tf, $x...); while consume(tt)!==nothing end; end
29+
# show the number of produce calls inside `f`
30+
tt = TapedTask(tf, x...);
31+
c = 0; while consume(tt)!==nothing c+=1 end;
32+
println(f_displayname, ": #produce=", c);
33+
GC.gc()
2334
end
2435

2536
####################################################################
2637

2738

28-
function rosenbrock(x)
39+
function rosenbrock(x, callback=nothing)
2940
i = x[2:end]
3041
j = x[1:end-1]
31-
return sum((1 .- j).^2 + 100*(i - j.^2).^2)
42+
ret = sum((1 .- j).^2 + 100*(i - j.^2).^2)
43+
callback === nothing || callback(ret);
44+
return ret
3245
end
3346

3447
x = rand(100000)
3548
benchmark_driver!(rosenbrock, x)
3649

3750
####################################################################
3851

39-
function ackley(x::AbstractVector)
52+
function ackley(x::AbstractVector, callback=nothing)
4053
a, b, c = 20.0, -0.2, 2.0*π
4154
len_recip = inv(length(x))
4255
sum_sqrs = zero(eltype(x))
4356
sum_cos = sum_sqrs
4457
for i in x
4558
sum_cos += cos(c*i)
4659
sum_sqrs += i^2
60+
callback === nothing || callback(sum_sqrs);
4761
end
4862
return (-a * exp(b * sqrt(len_recip*sum_sqrs)) -
4963
exp(len_recip*sum_cos) + a + MathConstants.e)
@@ -54,11 +68,13 @@ benchmark_driver!(ackley, x)
5468

5569
####################################################################
5670
function generate_matrix_test(n)
57-
return x -> begin
71+
return (x, callback=nothing) -> begin
5872
# @assert length(x) == 2n^2 + n
5973
a = reshape(x[1:n^2], n, n)
6074
b = reshape(x[n^2 + 1:2n^2], n, n)
61-
return log.((a * b) + a - b)
75+
ret = log.((a * b) + a - b)
76+
callback === nothing || callback(ret);
77+
return ret
6278
end
6379
end
6480

@@ -71,10 +87,12 @@ benchmark_driver!(matrix_test, x; f_displayname="matrix_test")
7187
relu(x) = log.(1.0 .+ exp.(x))
7288
sigmoid(n) = 1. / (1. + exp(-n))
7389

74-
function neural_net(w1, w2, w3, x1)
90+
function neural_net(w1, w2, w3, x1, callback=nothing)
7591
x2 = relu(w1 * x1)
7692
x3 = relu(w2 * x2)
77-
return sigmoid(LinearAlgebra.dot(w3, x3))
93+
ret = sigmoid(LinearAlgebra.dot(w3, x3))
94+
callback === nothing || callback(ret);
95+
return ret
7896
end
7997

8098
xs = (randn(10,10), randn(10,10), randn(10), rand(10))

0 commit comments

Comments
 (0)