blob: 59644c42126e6202a21cdd87e7101297096efec1 [file] [log] [blame]
Gavin Howard36d35342018-10-15 20:25:31 -06001#! /usr/bin/python3 -B
2#
Gavin Howard7345cb92019-04-08 14:13:43 -06003# Copyright (c) 2018-2019 Gavin D. Howard and contributors.
Gavin Howard36d35342018-10-15 20:25:31 -06004#
Gavin Howard7345cb92019-04-08 14:13:43 -06005# All rights reserved.
Gavin Howard36d35342018-10-15 20:25:31 -06006#
Gavin Howard7345cb92019-04-08 14:13:43 -06007# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions are met:
9#
10# * Redistributions of source code must retain the above copyright notice, this
11# list of conditions and the following disclaimer.
12#
13# * Redistributions in binary form must reproduce the above copyright notice,
14# this list of conditions and the following disclaimer in the documentation
15# and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27# POSSIBILITY OF SUCH DAMAGE.
Gavin Howard36d35342018-10-15 20:25:31 -060028#
29
30import os
31import sys
32import subprocess
33import time
34
35def usage():
Gavin Howarddcaf2302018-10-17 16:30:38 -060036 print("usage: {} [test_num exe]".format(script))
37 print("\n test_num is the last Karatsuba number to run through tests")
Gavin Howard36d35342018-10-15 20:25:31 -060038 sys.exit(1)
39
Gavin Howardab6ba682019-05-10 08:42:15 -060040script = sys.argv[0]
41testdir = os.path.dirname(script)
42
43print("\nWARNING: This script is for distro and package maintainers.")
44print("It is for finding the optimal Karatsuba number.")
Gavin Howard8c9288d2019-05-10 12:31:42 -060045print("Though it only needs to be run once per release/platform,")
46print("it takes forever to run.")
Gavin Howardab6ba682019-05-10 08:42:15 -060047print("You have been warned.\n")
Gavin Howard2a659882019-05-10 12:47:33 -060048print("Note: If you send an interrupt, it will report the current best number.\n")
Gavin Howardab6ba682019-05-10 08:42:15 -060049
Gavin Howard36d35342018-10-15 20:25:31 -060050if __name__ != "__main__":
51 usage()
52
Gavin Howard977767f2019-04-24 10:46:23 -060053mx = 520
Gavin Howardab6ba682019-05-10 08:42:15 -060054mx2 = mx // 2
Gavin Howard8c9288d2019-05-10 12:31:42 -060055mn = 16
Gavin Howard36d35342018-10-15 20:25:31 -060056
57num = "9" * mx
58
Gavin Howardab6ba682019-05-10 08:42:15 -060059if len(sys.argv) >= 2:
60 test_num = int(sys.argv[1])
61else:
62 test_num = 0
63
64if len(sys.argv) >= 3:
65 exe = sys.argv[2]
66else:
67 exe = testdir + "/bin/bc"
68
69exedir = os.path.dirname(exe)
Gavin Howard36d35342018-10-15 20:25:31 -060070
Gavin Howard8c9288d2019-05-10 12:31:42 -060071indata = "for (i = 0; i < 100; ++i) {} * {}\n"
72indata += "1.23456789^100000\n1.23456789^100000\nhalt"
73indata = indata.format(num, num)
Gavin Howard36d35342018-10-15 20:25:31 -060074
Gavin Howardab6ba682019-05-10 08:42:15 -060075times = []
76nums = []
77runs = []
78nruns = 5
Gavin Howard977767f2019-04-24 10:46:23 -060079
Gavin Howardab6ba682019-05-10 08:42:15 -060080for i in range(0, nruns):
81 runs.append(0)
82
83tests = [ "multiply", "modulus", "power", "sqrt" ]
Gavin Howardbc332b82019-05-15 11:19:58 -060084scripts = [ "multiply" ]
Gavin Howardab6ba682019-05-10 08:42:15 -060085
Gavin Howard030ef052019-05-10 10:59:24 -060086if test_num != 0:
87 mx2 = test_num
88
Gavin Howard2a659882019-05-10 12:47:33 -060089try:
Gavin Howardab6ba682019-05-10 08:42:15 -060090
Gavin Howard2a659882019-05-10 12:47:33 -060091 for i in range(mn, mx2 + 1):
Gavin Howardab6ba682019-05-10 08:42:15 -060092
Gavin Howard2a659882019-05-10 12:47:33 -060093 print("\nCompiling...\n")
Gavin Howardab6ba682019-05-10 08:42:15 -060094
Gavin Howard2a659882019-05-10 12:47:33 -060095 makecmd = [ "./configure.sh", "-O3", "-k{}".format(i) ]
96 p = subprocess.run(makecmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Gavin Howardab6ba682019-05-10 08:42:15 -060097
Gavin Howard2a659882019-05-10 12:47:33 -060098 if p.returncode != 0:
99 print("configure.sh returned an error ({}); exiting...".format(p.returncode))
100 sys.exit(p.returncode)
Gavin Howardab6ba682019-05-10 08:42:15 -0600101
Gavin Howardaa8fbc52019-05-10 20:39:12 -0600102 makecmd = [ "make" ]
Gavin Howard2a659882019-05-10 12:47:33 -0600103 p = subprocess.run(makecmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Gavin Howardab6ba682019-05-10 08:42:15 -0600104
Gavin Howard2a659882019-05-10 12:47:33 -0600105 if p.returncode != 0:
106 print("make returned an error ({}); exiting...".format(p.returncode))
107 sys.exit(p.returncode)
Gavin Howardab6ba682019-05-10 08:42:15 -0600108
Gavin Howard2a659882019-05-10 12:47:33 -0600109 if (test_num >= i):
Gavin Howardab6ba682019-05-10 08:42:15 -0600110
Gavin Howard2a659882019-05-10 12:47:33 -0600111 print("Running tests for Karatsuba Num: {}\n".format(i))
Gavin Howardab6ba682019-05-10 08:42:15 -0600112
Gavin Howard2a659882019-05-10 12:47:33 -0600113 for test in tests:
Gavin Howardab6ba682019-05-10 08:42:15 -0600114
Gavin Howard2a659882019-05-10 12:47:33 -0600115 cmd = [ "{}/tests/test.sh".format(testdir), "bc", test, "0", exe ]
Gavin Howardab6ba682019-05-10 08:42:15 -0600116
Gavin Howard2a659882019-05-10 12:47:33 -0600117 p = subprocess.run(cmd + sys.argv[3:], stderr=subprocess.PIPE)
Gavin Howardab6ba682019-05-10 08:42:15 -0600118
Gavin Howard2a659882019-05-10 12:47:33 -0600119 if p.returncode != 0:
120 print("{} test failed:\n".format(test, p.returncode))
121 print(p.stderr.decode())
122 print("\nexiting...")
123 sys.exit(p.returncode)
Gavin Howardab6ba682019-05-10 08:42:15 -0600124
Gavin Howard2a659882019-05-10 12:47:33 -0600125 print("")
Gavin Howardab6ba682019-05-10 08:42:15 -0600126
Gavin Howardbc332b82019-05-15 11:19:58 -0600127 for script in scripts:
128
129 cmd = [ "{}/tests/script.sh".format(testdir), "bc", script + ".bc",
130 "0", "1", "0", exe ]
131
132 p = subprocess.run(cmd + sys.argv[3:], stderr=subprocess.PIPE)
133
134 if p.returncode != 0:
135 print("{} test failed:\n".format(test, p.returncode))
136 print(p.stderr.decode())
137 print("\nexiting...")
138 sys.exit(p.returncode)
139
140 print("")
141
Gavin Howard2a659882019-05-10 12:47:33 -0600142 elif test_num == 0:
Gavin Howardab6ba682019-05-10 08:42:15 -0600143
Gavin Howard2a659882019-05-10 12:47:33 -0600144 print("Timing Karatsuba Num: {}".format(i), end='', flush=True)
Gavin Howardab6ba682019-05-10 08:42:15 -0600145
Gavin Howard2a659882019-05-10 12:47:33 -0600146 for j in range(0, nruns):
Gavin Howardab6ba682019-05-10 08:42:15 -0600147
Gavin Howard2a659882019-05-10 12:47:33 -0600148 cmd = [ exe, "{}/tests/bc/power.txt".format(testdir) ]
Gavin Howardab6ba682019-05-10 08:42:15 -0600149
Gavin Howard2a659882019-05-10 12:47:33 -0600150 start = time.perf_counter()
151 p = subprocess.run(cmd, input=indata.encode(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
152 end = time.perf_counter()
Gavin Howardab6ba682019-05-10 08:42:15 -0600153
Gavin Howard2a659882019-05-10 12:47:33 -0600154 if p.returncode != 0:
155 print("bc returned an error; exiting...")
156 sys.exit(p.returncode)
Gavin Howardab6ba682019-05-10 08:42:15 -0600157
Gavin Howard2a659882019-05-10 12:47:33 -0600158 runs[j] = end - start
Gavin Howardab6ba682019-05-10 08:42:15 -0600159
Gavin Howard2a659882019-05-10 12:47:33 -0600160 run_times = runs[1:]
161 avg = sum(run_times) / len(run_times)
162
163 times.append(avg)
164 nums.append(i)
165 print(", Time: {}".format(times[i - mn]))
166
167except KeyboardInterrupt:
168 nums = nums[0:i]
169 times = times[0:i]
Gavin Howardab6ba682019-05-10 08:42:15 -0600170
Gavin Howard030ef052019-05-10 10:59:24 -0600171if test_num == 0:
Gavin Howardab6ba682019-05-10 08:42:15 -0600172
Gavin Howard030ef052019-05-10 10:59:24 -0600173 opt = nums[times.index(min(times))]
174
Gavin Howard2a659882019-05-10 12:47:33 -0600175 print("\n\nOptimal Karatsuba Num (for this machine): {}".format(opt))
Gavin Howard030ef052019-05-10 10:59:24 -0600176 print("Run the following:\n")
177 print("./configure.sh -O3 -k {}".format(opt))
178 print("make")