blob: 0a63cd58b08beab2308e059babc69cd02af4cb31 [file] [log] [blame]
Gavin Howard36d35342018-10-15 20:25:31 -06001#! /usr/bin/python3 -B
2#
Gavin Howard29e00ba2020-06-30 09:25:21 -06003# SPDX-License-Identifier: BSD-2-Clause
4#
Gavin Howard4feb7082021-01-26 01:19:25 -07005# Copyright (c) 2018-2021 Gavin D. Howard and contributors.
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 Howard3efeb912020-05-14 12:38:59 -060036 print("usage: {} [num_iterations test_num exe]".format(script))
37 print("\n num_iterations is the number of times to run each karatsuba number; default is 4")
Gavin Howarddcaf2302018-10-17 16:30:38 -060038 print("\n test_num is the last Karatsuba number to run through tests")
Gavin Howard36d35342018-10-15 20:25:31 -060039 sys.exit(1)
40
Gavin Howard3efeb912020-05-14 12:38:59 -060041def run(cmd, env=None):
42 return subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
43
Gavin Howardab6ba682019-05-10 08:42:15 -060044script = sys.argv[0]
45testdir = os.path.dirname(script)
46
Gavin Howard63ce8b02020-07-03 16:12:24 -060047if testdir == "":
48 testdir = os.getcwd()
49
Gavin Howardab6ba682019-05-10 08:42:15 -060050print("\nWARNING: This script is for distro and package maintainers.")
51print("It is for finding the optimal Karatsuba number.")
Gavin Howard8c9288d2019-05-10 12:31:42 -060052print("Though it only needs to be run once per release/platform,")
53print("it takes forever to run.")
Gavin Howardab6ba682019-05-10 08:42:15 -060054print("You have been warned.\n")
Gavin Howard2a659882019-05-10 12:47:33 -060055print("Note: If you send an interrupt, it will report the current best number.\n")
Gavin Howardab6ba682019-05-10 08:42:15 -060056
Gavin Howard36d35342018-10-15 20:25:31 -060057if __name__ != "__main__":
58 usage()
59
Gavin Howard977767f2019-04-24 10:46:23 -060060mx = 520
Gavin Howardab6ba682019-05-10 08:42:15 -060061mx2 = mx // 2
Gavin Howard8c9288d2019-05-10 12:31:42 -060062mn = 16
Gavin Howard36d35342018-10-15 20:25:31 -060063
64num = "9" * mx
65
Gavin Howardba0605b2020-05-14 13:40:28 -060066args_idx = 4
67
Gavin Howardab6ba682019-05-10 08:42:15 -060068if len(sys.argv) >= 2:
Gavin Howard3efeb912020-05-14 12:38:59 -060069 num_iterations = int(sys.argv[1])
70else:
71 num_iterations = 4
72
73if len(sys.argv) >= 3:
74 test_num = int(sys.argv[2])
Gavin Howardab6ba682019-05-10 08:42:15 -060075else:
76 test_num = 0
77
Gavin Howardba0605b2020-05-14 13:40:28 -060078if len(sys.argv) >= args_idx:
Gavin Howard3efeb912020-05-14 12:38:59 -060079 exe = sys.argv[3]
Gavin Howardab6ba682019-05-10 08:42:15 -060080else:
81 exe = testdir + "/bin/bc"
82
83exedir = os.path.dirname(exe)
Gavin Howard36d35342018-10-15 20:25:31 -060084
Gavin Howard8c9288d2019-05-10 12:31:42 -060085indata = "for (i = 0; i < 100; ++i) {} * {}\n"
86indata += "1.23456789^100000\n1.23456789^100000\nhalt"
Gavin Howard3efeb912020-05-14 12:38:59 -060087indata = indata.format(num, num).encode()
Gavin Howard36d35342018-10-15 20:25:31 -060088
Gavin Howardab6ba682019-05-10 08:42:15 -060089times = []
90nums = []
91runs = []
Gavin Howard3efeb912020-05-14 12:38:59 -060092nruns = num_iterations + 1
Gavin Howard977767f2019-04-24 10:46:23 -060093
Gavin Howardab6ba682019-05-10 08:42:15 -060094for i in range(0, nruns):
95 runs.append(0)
96
97tests = [ "multiply", "modulus", "power", "sqrt" ]
Gavin Howardbc332b82019-05-15 11:19:58 -060098scripts = [ "multiply" ]
Gavin Howardab6ba682019-05-10 08:42:15 -060099
Gavin Howard3efeb912020-05-14 12:38:59 -0600100print("Testing CFLAGS=\"-flto\"...")
101
102flags = dict(os.environ)
103try:
104 flags["CFLAGS"] = flags["CFLAGS"] + " " + "-flto"
105except KeyError:
106 flags["CFLAGS"] = "-flto"
107
108p = run([ "./configure.sh", "-O3" ], flags)
109if p.returncode != 0:
110 print("configure.sh returned an error ({}); exiting...".format(p.returncode))
111 sys.exit(p.returncode)
112
113p = run([ "make" ])
114
115if p.returncode == 0:
116 config_env = flags
117 print("Using CFLAGS=\"-flto\"")
118else:
119 config_env = os.environ
120 print("Not using CFLAGS=\"-flto\"")
121
122p = run([ "make", "clean" ])
123
Gavin Howard160490c2021-03-31 21:46:11 -0600124print("Testing \"make -j16\"")
Gavin Howard3efeb912020-05-14 12:38:59 -0600125
126if p.returncode != 0:
127 print("make returned an error ({}); exiting...".format(p.returncode))
128 sys.exit(p.returncode)
129
Gavin Howard160490c2021-03-31 21:46:11 -0600130p = run([ "make", "-j16" ])
Gavin Howard3efeb912020-05-14 12:38:59 -0600131
132if p.returncode == 0:
Gavin Howard160490c2021-03-31 21:46:11 -0600133 makecmd = [ "make", "-j16" ]
134 print("Using \"make -j16\"")
Gavin Howard3efeb912020-05-14 12:38:59 -0600135else:
136 makecmd = [ "make" ]
Gavin Howard160490c2021-03-31 21:46:11 -0600137 print("Not using \"make -j16\"")
Gavin Howard3efeb912020-05-14 12:38:59 -0600138
Gavin Howard030ef052019-05-10 10:59:24 -0600139if test_num != 0:
140 mx2 = test_num
141
Gavin Howard2a659882019-05-10 12:47:33 -0600142try:
Gavin Howardab6ba682019-05-10 08:42:15 -0600143
Gavin Howard2a659882019-05-10 12:47:33 -0600144 for i in range(mn, mx2 + 1):
Gavin Howardab6ba682019-05-10 08:42:15 -0600145
Gavin Howard2a659882019-05-10 12:47:33 -0600146 print("\nCompiling...\n")
Gavin Howardab6ba682019-05-10 08:42:15 -0600147
Gavin Howard3efeb912020-05-14 12:38:59 -0600148 p = run([ "./configure.sh", "-O3", "-k{}".format(i) ], config_env)
Gavin Howardab6ba682019-05-10 08:42:15 -0600149
Gavin Howard2a659882019-05-10 12:47:33 -0600150 if p.returncode != 0:
151 print("configure.sh returned an error ({}); exiting...".format(p.returncode))
152 sys.exit(p.returncode)
Gavin Howardab6ba682019-05-10 08:42:15 -0600153
Gavin Howard3efeb912020-05-14 12:38:59 -0600154 p = run(makecmd)
Gavin Howardab6ba682019-05-10 08:42:15 -0600155
Gavin Howard2a659882019-05-10 12:47:33 -0600156 if p.returncode != 0:
157 print("make returned an error ({}); exiting...".format(p.returncode))
158 sys.exit(p.returncode)
Gavin Howardab6ba682019-05-10 08:42:15 -0600159
Gavin Howard2a659882019-05-10 12:47:33 -0600160 if (test_num >= i):
Gavin Howardab6ba682019-05-10 08:42:15 -0600161
Gavin Howard2a659882019-05-10 12:47:33 -0600162 print("Running tests for Karatsuba Num: {}\n".format(i))
Gavin Howardab6ba682019-05-10 08:42:15 -0600163
Gavin Howard2a659882019-05-10 12:47:33 -0600164 for test in tests:
Gavin Howardab6ba682019-05-10 08:42:15 -0600165
Gavin Howardf2c997b2020-05-14 08:02:44 -0600166 cmd = [ "{}/tests/test.sh".format(testdir), "bc", test, "0", "0", exe ]
Gavin Howardab6ba682019-05-10 08:42:15 -0600167
Gavin Howardba0605b2020-05-14 13:40:28 -0600168 p = subprocess.run(cmd + sys.argv[args_idx:], stderr=subprocess.PIPE)
Gavin Howardab6ba682019-05-10 08:42:15 -0600169
Gavin Howard2a659882019-05-10 12:47:33 -0600170 if p.returncode != 0:
171 print("{} test failed:\n".format(test, p.returncode))
172 print(p.stderr.decode())
173 print("\nexiting...")
174 sys.exit(p.returncode)
Gavin Howardab6ba682019-05-10 08:42:15 -0600175
Gavin Howard2a659882019-05-10 12:47:33 -0600176 print("")
Gavin Howardab6ba682019-05-10 08:42:15 -0600177
Gavin Howardbc332b82019-05-15 11:19:58 -0600178 for script in scripts:
179
180 cmd = [ "{}/tests/script.sh".format(testdir), "bc", script + ".bc",
Gavin Howardba0605b2020-05-14 13:40:28 -0600181 "0", "1", "1", "0", exe ]
Gavin Howardbc332b82019-05-15 11:19:58 -0600182
Gavin Howardba0605b2020-05-14 13:40:28 -0600183 p = subprocess.run(cmd + sys.argv[args_idx:], stderr=subprocess.PIPE)
Gavin Howardbc332b82019-05-15 11:19:58 -0600184
185 if p.returncode != 0:
186 print("{} test failed:\n".format(test, p.returncode))
187 print(p.stderr.decode())
188 print("\nexiting...")
189 sys.exit(p.returncode)
190
191 print("")
192
Gavin Howard2a659882019-05-10 12:47:33 -0600193 elif test_num == 0:
Gavin Howardab6ba682019-05-10 08:42:15 -0600194
Gavin Howard2a659882019-05-10 12:47:33 -0600195 print("Timing Karatsuba Num: {}".format(i), end='', flush=True)
Gavin Howardab6ba682019-05-10 08:42:15 -0600196
Gavin Howard2a659882019-05-10 12:47:33 -0600197 for j in range(0, nruns):
Gavin Howardab6ba682019-05-10 08:42:15 -0600198
Gavin Howard2a659882019-05-10 12:47:33 -0600199 cmd = [ exe, "{}/tests/bc/power.txt".format(testdir) ]
Gavin Howardab6ba682019-05-10 08:42:15 -0600200
Gavin Howard2a659882019-05-10 12:47:33 -0600201 start = time.perf_counter()
Gavin Howard3efeb912020-05-14 12:38:59 -0600202 p = subprocess.run(cmd, input=indata, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Gavin Howard2a659882019-05-10 12:47:33 -0600203 end = time.perf_counter()
Gavin Howardab6ba682019-05-10 08:42:15 -0600204
Gavin Howard2a659882019-05-10 12:47:33 -0600205 if p.returncode != 0:
206 print("bc returned an error; exiting...")
207 sys.exit(p.returncode)
Gavin Howardab6ba682019-05-10 08:42:15 -0600208
Gavin Howard2a659882019-05-10 12:47:33 -0600209 runs[j] = end - start
Gavin Howardab6ba682019-05-10 08:42:15 -0600210
Gavin Howard2a659882019-05-10 12:47:33 -0600211 run_times = runs[1:]
212 avg = sum(run_times) / len(run_times)
213
214 times.append(avg)
215 nums.append(i)
216 print(", Time: {}".format(times[i - mn]))
217
218except KeyboardInterrupt:
219 nums = nums[0:i]
220 times = times[0:i]
Gavin Howardab6ba682019-05-10 08:42:15 -0600221
Gavin Howard030ef052019-05-10 10:59:24 -0600222if test_num == 0:
Gavin Howardab6ba682019-05-10 08:42:15 -0600223
Gavin Howard030ef052019-05-10 10:59:24 -0600224 opt = nums[times.index(min(times))]
225
Gavin Howard2a659882019-05-10 12:47:33 -0600226 print("\n\nOptimal Karatsuba Num (for this machine): {}".format(opt))
Gavin Howard030ef052019-05-10 10:59:24 -0600227 print("Run the following:\n")
Gavin Howard3efeb912020-05-14 12:38:59 -0600228 if "-flto" in config_env["CFLAGS"]:
229 print("CFLAGS=\"-flto\" ./configure.sh -O3 -k {}".format(opt))
230 else:
231 print("./configure.sh -O3 -k {}".format(opt))
Gavin Howard030ef052019-05-10 10:59:24 -0600232 print("make")