-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathnumpy_benchmarks.py
129 lines (111 loc) · 2.91 KB
/
numpy_benchmarks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from tqdm import trange
from time import time, sleep
import numpy as np
import os
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--nproc', type=int, default=1)
parser.add_argument('--runs', type=int, default=10)
parser.add_argument('--size', type=int, default=4096)
args = parser.parse_args()
os.environ["OMP_NUM_THREADS"] = str(args.nproc)
os.environ["OPENBLAS_NUM_THREADS"] = str(args.nproc)
os.environ["MKL_NUM_THREADS"] = str(args.nproc)
os.environ["VECLIB_MAXIMUM_THREADS"] = str(args.nproc)
os.environ["NUMEXPR_NUM_THREADS"] = str(args.nproc)
print("\nUsing", args.nproc, "threads for benchmark\n")
timings = {
"datagen": 0,
"special": 0,
"stats": 0,
"matmul": 0,
"vecmul": 0,
"svd": 0,
"cholesky": 0,
"eigendecomp": 0,
}
print("Running benchmarks...\n")
for i in trange(args.runs):
t = time()
size = args.size
print("size =", size)
A, B = np.random.random((size, size)), np.random.random((size, size))
C, D = np.random.random((size * size,)), np.random.random((size * size,))
E = np.random.random((int(size / 2), int(size / 4)))
F = np.random.random((int(size / 2), int(size / 2)))
F = np.dot(F, F.T)
G = np.random.random((int(size / 2), int(size / 2)))
delta = time() - t
timings["datagen"] += delta
def run_special_funcs(nparray):
np.exp(nparray)
np.sqrt(nparray)
np.sin(nparray)
np.log(nparray)
N = 3
t = time()
for i in range(N):
run_special_funcs(A)
run_special_funcs(C)
delta = time() - t
timings["special"] += delta/N
sleep(2.0)
def run_stats(nparray):
nparray.sum()
nparray.min()
nparray.max()
nparray.cumsum()
nparray.mean()
np.median(nparray)
np.corrcoef(nparray)
np.std(nparray)
t = time()
for i in range(N):
run_stats(A)
run_stats(C)
delta = time() - t
timings["stats"] += delta/N
sleep(2.0)
t = time()
for i in range(N):
np.dot(A, B)
delta = time() - t
del A, B
timings["matmul"] += delta/N
sleep(2.0)
t = time()
for i in range(N):
np.dot(C, D)
delta = time() - t
del C, D
timings["vecmul"] += delta/N
sleep(2.0)
t = time()
for i in range(N):
np.linalg.svd(E, full_matrices=False)
delta = time() - t
del E
timings["svd"] += delta/N
sleep(2.0)
t = time()
for i in range(N):
np.linalg.cholesky(F)
delta = time() - t
del F
timings["cholesky"] += delta/N
sleep(2.0)
# eigendecomp is slow, set max runs to 3
t = time()
for i in range(N):
np.linalg.eig(G)
delta = time() - t
del G
timings["eigendecomp"] += delta/N
sleep(2.0)
print("\nDone!\n")
print("Results")
print("=======")
for key in timings.keys():
timing = round(timings[key]/args.runs, 3)
print("| "+key+" |", timing, "|")
print("")