|
1 | | -#!/usr/bin/env python3 |
2 | | -import numpy as np |
3 | | -from sys import argv |
4 | | -import subprocess |
5 | | -from time import time |
6 | | -import math |
7 | | - |
8 | | -from matplotlib import pyplot as plt |
9 | | - |
10 | | -MAKE_PLOT = False |
11 | | - |
12 | | -def linear_regression_with_std(x, y): |
13 | | - x = np.array(x) |
14 | | - y = np.array(y) |
15 | | - x_mean = np.mean(x) |
16 | | - y_mean = np.mean(y) |
17 | | - numerator = np.sum((x - x_mean) * (y - y_mean)) |
18 | | - denominator = np.sum((x - x_mean) ** 2) |
19 | | - slope = numerator / denominator |
20 | | - intercept = y_mean - slope * x_mean |
21 | | - y_pred = slope * x + intercept |
22 | | - residuals = y - y_pred |
23 | | - std_dev = np.std(residuals) |
24 | | - return slope, intercept, std_dev |
25 | | - |
26 | | -def do_bench(cliargs, iters): |
27 | | - print([cliargs[1], str(iters)] + cliargs[2:]) |
28 | | - out = str(subprocess.check_output([cliargs[1], str(iters)] + cliargs[2:])) |
29 | | - s1 = out[out.find("SELFTIMED")+11:] |
30 | | - s2 = float(s1[:s1.find("\n")-4]) |
31 | | - selftimed = s2 |
32 | | - |
33 | | - b1 = out[out.find("BATCHTIME")+11:] |
34 | | - b2 = float(b1[:b1.find("SELFTIMED")-2]) |
35 | | - batchtime = b2 |
36 | | - |
37 | | - print(f"ITERS: {iters}, BATCHTIME: {batchtime}, SELFTIMED: {selftimed}") |
38 | | - return batchtime |
39 | | - |
40 | | -def converge(cliargs): |
41 | | - xs = [] |
42 | | - ys = [] |
43 | | - iters = 1 |
44 | | - t = time() |
45 | | - while len(xs) == 0: |
46 | | - st = do_bench(cliargs, iters) |
47 | | - if st * iters < 0.65: |
48 | | - iters *= 2 |
49 | | - continue |
50 | | - xs.append(iters) |
51 | | - ys.append(st) |
52 | | - for _ in range(2): |
53 | | - if time() - t < 3.5: |
54 | | - iters = int(math.trunc(float(iters) * 1.2) + 1) |
55 | | - else: |
56 | | - iters += 1 + iters // 20 |
57 | | - st = do_bench(cliargs, iters) |
58 | | - xs.append(iters) |
59 | | - ys.append(st) |
60 | | - while time() - t < 3.5: |
61 | | - if time() - t < 3.5: |
62 | | - iters = int(math.trunc(float(iters) * 1.2) + 1) |
63 | | - else: |
64 | | - iters += 1 + iters // 20 |
65 | | - st = do_bench(cliargs, iters) |
66 | | - xs.append(iters) |
67 | | - ys.append(st) |
68 | | - m, b, sigma = linear_regression_with_std(xs, ys) |
69 | | - print(f"Slope (Mean): {m}, Intercept (Overhead): {b}, Stdev: {sigma}") |
70 | | - p, lnc, lngsd = linear_regression_with_std([math.log(x) for x in xs], [math.log(y) for y in ys]) |
71 | | - c, gsd = math.exp(lnc), math.exp(lngsd) |
72 | | - print(f"Power (Distortion): {p}, Factor (Geomean) {c}, GeoStdev {gsd}") |
73 | | - if MAKE_PLOT: |
74 | | - plt.plot(xs, ys, 'rx') |
75 | | - plt.plot([xs[0], xs[-1]], [m*xs[0]+b, m*xs[-1]+b], color="blue") |
76 | | - plt.plot(xs, [c*x**p for x in xs], color="green") |
77 | | - plt.savefig("plot.png") |
78 | | - return m, sigma, c, gsd |
79 | | - |
80 | | -if __name__ == "__main__": |
81 | | - print(converge(argv)) |
| 1 | +#!/usr/bin/env python |
| 2 | +import numpy as np |
| 3 | +from sys import argv |
| 4 | +import subprocess |
| 5 | +from time import time |
| 6 | +import math |
| 7 | + |
| 8 | +from matplotlib import pyplot as plt |
| 9 | + |
| 10 | +MAKE_PLOT = False |
| 11 | + |
| 12 | +def linear_regression_with_std(x, y): |
| 13 | + x = np.array(x) |
| 14 | + y = np.array(y) |
| 15 | + x_mean = np.mean(x) |
| 16 | + y_mean = np.mean(y) |
| 17 | + numerator = np.sum((x - x_mean) * (y - y_mean)) |
| 18 | + denominator = np.sum((x - x_mean) ** 2) |
| 19 | + slope = numerator / denominator |
| 20 | + intercept = y_mean - slope * x_mean |
| 21 | + y_pred = slope * x + intercept |
| 22 | + residuals = y - y_pred |
| 23 | + std_dev = np.std(residuals) |
| 24 | + return slope, intercept, std_dev |
| 25 | + |
| 26 | +def do_bench(cliargs, iters): |
| 27 | + print([cliargs[1], str(iters)] + cliargs[2:]) |
| 28 | + out = str(subprocess.check_output([cliargs[1], str(iters)] + cliargs[2:])) |
| 29 | + s1 = out[out.find("SELFTIMED")+11:] |
| 30 | + s2 = float(s1[:s1.find("\n")-4]) |
| 31 | + selftimed = s2 |
| 32 | + |
| 33 | + b1 = out[out.find("BATCHTIME")+11:] |
| 34 | + b2 = float(b1[:b1.find("SELFTIMED")-2]) |
| 35 | + batchtime = b2 |
| 36 | + |
| 37 | + print(f"ITERS: {iters}, BATCHTIME: {batchtime}, SELFTIMED: {selftimed}") |
| 38 | + return batchtime |
| 39 | + |
| 40 | +def converge(cliargs): |
| 41 | + xs = [] |
| 42 | + ys = [] |
| 43 | + iters = 1 |
| 44 | + t = time() |
| 45 | + while len(xs) == 0: |
| 46 | + st = do_bench(cliargs, iters) |
| 47 | + if st * iters < 0.65: |
| 48 | + iters *= 2 |
| 49 | + continue |
| 50 | + xs.append(iters) |
| 51 | + ys.append(st) |
| 52 | + for _ in range(2): |
| 53 | + if time() - t < 3.5: |
| 54 | + iters = int(math.trunc(float(iters) * 1.2) + 1) |
| 55 | + else: |
| 56 | + iters += 1 + iters // 20 |
| 57 | + st = do_bench(cliargs, iters) |
| 58 | + xs.append(iters) |
| 59 | + ys.append(st) |
| 60 | + while time() - t < 3.5: |
| 61 | + if time() - t < 3.5: |
| 62 | + iters = int(math.trunc(float(iters) * 1.2) + 1) |
| 63 | + else: |
| 64 | + iters += 1 + iters // 20 |
| 65 | + st = do_bench(cliargs, iters) |
| 66 | + xs.append(iters) |
| 67 | + ys.append(st) |
| 68 | + m, b, sigma = linear_regression_with_std(xs, ys) |
| 69 | + print(f"Slope (Mean): {m}, Intercept (Overhead): {b}, Stdev: {sigma}") |
| 70 | + p, lnc, lngsd = linear_regression_with_std([math.log(x) for x in xs], [math.log(y) for y in ys]) |
| 71 | + c, gsd = math.exp(lnc), math.exp(lngsd) |
| 72 | + print(f"Power (Distortion): {p}, Factor (Geomean) {c}, GeoStdev {gsd}") |
| 73 | + if MAKE_PLOT: |
| 74 | + plt.plot(xs, ys, 'rx') |
| 75 | + plt.plot([xs[0], xs[-1]], [m*xs[0]+b, m*xs[-1]+b], color="blue") |
| 76 | + plt.plot(xs, [c*x**p for x in xs], color="green") |
| 77 | + plt.savefig("plot.png") |
| 78 | + return m, sigma, c, gsd |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + print(converge(argv)) |
0 commit comments