Skip to content

Commit d1115e2

Browse files
Chaluvadisyurkevi
authored andcommitted
added benchmark examples
1 parent dcae000 commit d1115e2

File tree

3 files changed

+153
-11
lines changed

3 files changed

+153
-11
lines changed

examples/benchmarks/bench_blas.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/python
2+
3+
#######################################################
4+
# Copyright (c) 2015, ArrayFire
5+
# All rights reserved.
6+
#
7+
# This file is distributed under 3-clause BSD license.
8+
# The complete license agreement can be obtained at:
9+
# http://arrayfire.com/licenses/BSD-3-Clause
10+
########################################################
11+
12+
13+
import sys
14+
from time import time
15+
16+
import arrayfire as af
17+
18+
# TODO: Remove -1 from sync() after default value has been put in
19+
try:
20+
import numpy as np
21+
except ImportError:
22+
np = None # type: ignore[assignment]
23+
24+
25+
def calc_arrayfire(n):
26+
A = af.randu((n, n))
27+
af.sync(-1)
28+
29+
def run(iters):
30+
for t in range(iters):
31+
B = af.matmul(A, A) # noqa: F841
32+
af.sync(-1)
33+
34+
return run
35+
36+
37+
def calc_numpy(n):
38+
np.random.seed(1)
39+
A = np.random.rand(n, n).astype(np.float32)
40+
41+
def run(iters):
42+
for t in range(iters):
43+
B = np.dot(A, A) # noqa: F841
44+
45+
return run
46+
47+
48+
def bench(calc, iters=100, upto=2048):
49+
_, name = calc.__name__.split("_")
50+
print("Benchmark N x N matrix multiply on %s" % name)
51+
52+
for n in range(128, upto + 128, 128):
53+
run = calc(n)
54+
start = time()
55+
run(iters)
56+
t = (time() - start) / iters
57+
gflops = 2.0 * (n**3) / (t * 1e9)
58+
print("Time taken for %4d x %4d: %0.4f Gflops" % (n, n, gflops))
59+
60+
61+
if __name__ == "__main__":
62+
63+
if len(sys.argv) > 1:
64+
af.set_device(int(sys.argv[1]))
65+
66+
af.info()
67+
68+
bench(calc_arrayfire)
69+
if np:
70+
bench(calc_numpy, upto=512)

examples/benchmarks/bench_fft.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# http://arrayfire.com/licenses/BSD-3-Clause
2+
########################################################
3+
4+
5+
import sys
6+
from time import time
7+
8+
import arrayfire as af
9+
10+
try:
11+
import numpy as np
12+
except ImportError:
13+
np = None # type: ignore[assignment]
14+
15+
16+
def calc_arrayfire(n):
17+
A = af.randu((n, n))
18+
af.sync(-1)
19+
20+
def run(iters):
21+
for t in range(iters):
22+
B = af.fft2(A) # noqa: F841
23+
24+
af.sync(-1)
25+
26+
return run
27+
28+
29+
def calc_numpy(n):
30+
np.random.seed(1)
31+
A = np.random.rand(n, n).astype(np.float32)
32+
33+
def run(iters):
34+
for t in range(iters):
35+
B = np.fft.fft2(A) # noqa: F841
36+
37+
return run
38+
39+
40+
def bench(calc, iters=100, upto=13):
41+
_, name = calc.__name__.split("_")
42+
print("Benchmark N x N 2D fft on %s" % name)
43+
44+
for M in range(7, upto):
45+
N = 1 << M
46+
run = calc(N)
47+
start = time()
48+
run(iters)
49+
t = (time() - start) / iters
50+
gflops = (10.0 * N * N * M) / (t * 1e9)
51+
print("Time taken for %4d x %4d: %0.4f Gflops" % (N, N, gflops))
52+
53+
54+
if __name__ == "__main__":
55+
56+
if len(sys.argv) > 1:
57+
af.set_device(int(sys.argv[1]))
58+
59+
af.info()
60+
61+
bench(calc_arrayfire)
62+
if np:
63+
bench(calc_numpy, upto=10)

examples/benchmarks/monte_carlo_pi.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,53 @@
99
# http://arrayfire.com/licenses/BSD-3-Clause
1010
########################################################
1111

12+
import sys
1213
from random import random
1314
from time import time
15+
1416
import arrayfire as af
15-
import sys
1617

1718
try:
1819
import numpy as np
1920
except ImportError:
20-
np = None
21+
np = None # type: ignore[assignment]
2122

22-
#alias range / xrange because xrange is faster than range in python2
23+
#TODO: Remove Python2 support?
24+
# alias range / xrange because xrange is faster than range in python2
2325
try:
24-
frange = xrange #Python2
26+
frange = xrange # type: ignore[name-defined]
2527
except NameError:
26-
frange = range #Python3
28+
frange = range # Python3
29+
2730

2831
# Having the function outside is faster than the lambda inside
2932
def in_circle(x, y):
30-
return (x*x + y*y) < 1
33+
return (x * x + y * y) < 1
34+
3135

3236
def calc_pi_device(samples):
3337
x = af.randu((samples,))
3438
y = af.randu((samples,))
3539
return 4 * af.sum(in_circle(x, y)) / samples
3640

41+
3742
def calc_pi_numpy(samples):
3843
np.random.seed(1)
3944
x = np.random.rand(samples).astype(np.float32)
4045
y = np.random.rand(samples).astype(np.float32)
41-
return 4. * np.sum(in_circle(x, y)) / samples
46+
return 4.0 * np.sum(in_circle(x, y)) / samples
47+
4248

4349
def calc_pi_host(samples):
4450
count = sum(1 for k in frange(samples) if in_circle(random(), random()))
4551
return 4 * float(count) / samples
4652

53+
4754
def bench(calc_pi, samples=1000000, iters=25):
4855
func_name = calc_pi.__name__[8:]
49-
print("Monte carlo estimate of pi on %s with %d million samples: %f" % \
50-
(func_name, samples/1e6, calc_pi(samples)))
56+
print(
57+
"Monte carlo estimate of pi on %s with %d million samples: %f" % (func_name, samples / 1e6, calc_pi(samples))
58+
)
5159

5260
start = time()
5361
for k in frange(iters):
@@ -56,12 +64,13 @@ def bench(calc_pi, samples=1000000, iters=25):
5664

5765
print("Average time taken: %f ms" % (1000 * (end - start) / iters))
5866

67+
5968
if __name__ == "__main__":
60-
if (len(sys.argv) > 1):
69+
if len(sys.argv) > 1:
6170
af.set_device(int(sys.argv[1]))
6271
af.info()
6372

6473
bench(calc_pi_device)
6574
if np:
6675
bench(calc_pi_numpy)
67-
bench(calc_pi_host)
76+
bench(calc_pi_host)

0 commit comments

Comments
 (0)