blob: 81c82b52fe5a33544fb28763d6d6b1029bfe6382 [file] [log] [blame]
Gavin Howard36d35342018-10-15 20:25:31 -06001#! /usr/bin/python3 -B
2#
3# Copyright 2018 Gavin D. Howard
4#
5# Permission to use, copy, modify, and/or distribute this software for any
6# purpose with or without fee is hereby granted.
7#
8# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
9# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
11# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
13# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14# PERFORMANCE OF THIS SOFTWARE.
15#
16
17import os
18import sys
19import subprocess
20import time
21
22def usage():
Gavin Howarddcaf2302018-10-17 16:30:38 -060023 print("usage: {} [test_num exe]".format(script))
24 print("\n test_num is the last Karatsuba number to run through tests")
Gavin Howard36d35342018-10-15 20:25:31 -060025 sys.exit(1)
26
27script = sys.argv[0]
28testdir = os.path.dirname(script)
29
Gavin Howardefc2eb42018-10-17 16:37:43 -060030print("\nWARNING: This script is for distro and package maintainers.")
31print("It is for finding the optimal Karatsuba number.")
32print("It takes forever to run.")
33print("You have been warned.\n")
34
Gavin Howard36d35342018-10-15 20:25:31 -060035if __name__ != "__main__":
36 usage()
37
Gavin Howarddcaf2302018-10-17 16:30:38 -060038mx = 200
39mx2 = mx // 2
40mn = 2
Gavin Howard36d35342018-10-15 20:25:31 -060041
42num = "9" * mx
43
44if len(sys.argv) >= 2:
Gavin Howarddcaf2302018-10-17 16:30:38 -060045 test_num = int(sys.argv[1])
Gavin Howard36d35342018-10-15 20:25:31 -060046else:
Gavin Howarddcaf2302018-10-17 16:30:38 -060047 test_num = 0
Gavin Howard36d35342018-10-15 20:25:31 -060048
Gavin Howarddcaf2302018-10-17 16:30:38 -060049if len(sys.argv) >= 3:
50 exe = sys.argv[2]
51else:
52 exe = testdir + "/bin/bc"
Gavin Howard36d35342018-10-15 20:25:31 -060053
Gavin Howarddcaf2302018-10-17 16:30:38 -060054exedir = os.path.dirname(exe)
Gavin Howard36d35342018-10-15 20:25:31 -060055
Gavin Howarddcaf2302018-10-17 16:30:38 -060056indata = "for (i = 0; i < 100; ++i) {} * {}\nhalt".format(num, num)
Gavin Howard36d35342018-10-15 20:25:31 -060057
Gavin Howarddcaf2302018-10-17 16:30:38 -060058times = []
59nums = []
60
61tests = [ "multiply", "modulus", "power", "sqrt" ]
62
63for i in range(mn, mx2 + 1):
Gavin Howard36d35342018-10-15 20:25:31 -060064
Gavin Howard93b0e5f2019-01-08 01:18:31 -070065 print("\nCompiling...\n")
66
Gavin Howard79f605c2019-01-08 13:08:21 -070067 makecmd = [ "./configure.sh", "-O3", "-k{}".format(i) ]
Gavin Howard93b0e5f2019-01-08 01:18:31 -070068 p = subprocess.run(makecmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
69
70 if p.returncode != 0:
Gavin Howard79f605c2019-01-08 13:08:21 -070071 print("configure.sh returned an error ({}); exiting...".format(p.returncode))
Gavin Howard93b0e5f2019-01-08 01:18:31 -070072 sys.exit(p.returncode)
73
74 makecmd = [ "make" ]
Gavin Howard36d35342018-10-15 20:25:31 -060075 p = subprocess.run(makecmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
76
77 if p.returncode != 0:
78 print("make returned an error ({}); exiting...".format(p.returncode))
79 sys.exit(p.returncode)
80
Gavin Howarddcaf2302018-10-17 16:30:38 -060081 if (test_num >= i):
82
Gavin Howard93b0e5f2019-01-08 01:18:31 -070083 print("Running tests...\n")
Gavin Howarddcaf2302018-10-17 16:30:38 -060084
85 for test in tests:
86
Gavin Howard93b0e5f2019-01-08 01:18:31 -070087 cmd = [ "{}/tests/test.sh".format(testdir), "bc", test, "0", exe ]
Gavin Howarddcaf2302018-10-17 16:30:38 -060088
89 p = subprocess.run(cmd + sys.argv[3:], stderr=subprocess.PIPE)
90
91 if p.returncode != 0:
92 print("{} test failed:\n".format(test, p.returncode))
93 print(p.stderr.decode())
94 print("\nexiting...")
95 sys.exit(p.returncode)
96
97 print("")
98
99 print("Timing Karatsuba Num: {}".format(i), end='', flush=True)
100
101 cmd = [ exe, "{}/tests/bc/power.txt".format(testdir) ]
102
103 start = time.perf_counter()
104 p = subprocess.run(cmd, input=indata.encode(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
105 end = time.perf_counter()
Gavin Howard36d35342018-10-15 20:25:31 -0600106
107 if p.returncode != 0:
108 print("bc returned an error; exiting...")
109 sys.exit(p.returncode)
110
Gavin Howarddcaf2302018-10-17 16:30:38 -0600111 nums.append(i)
Gavin Howard36d35342018-10-15 20:25:31 -0600112 times.append(end - start)
Gavin Howarddcaf2302018-10-17 16:30:38 -0600113 print(", Time: {}".format(times[i - mn]))
Gavin Howard36d35342018-10-15 20:25:31 -0600114
Gavin Howard4995d202018-10-24 15:24:57 -0600115opt = nums[times.index(min(times))]
Gavin Howardefc2eb42018-10-17 16:37:43 -0600116
117print("\nOptimal Karatsuba Num (for this machine): {}".format(opt))
118print("Run the following:\n")
Gavin Howard79f605c2019-01-08 13:08:21 -0700119print("./configure.sh -O3 -k {}".format(opt))
Gavin Howard93b0e5f2019-01-08 01:18:31 -0700120print("make")