Gavin Howard | 36d3534 | 2018-10-15 20:25:31 -0600 | [diff] [blame] | 1 | #! /usr/bin/python3 -B |
| 2 | # |
Gavin Howard | 7345cb9 | 2019-04-08 14:13:43 -0600 | [diff] [blame] | 3 | # Copyright (c) 2018-2019 Gavin D. Howard and contributors. |
Gavin Howard | 36d3534 | 2018-10-15 20:25:31 -0600 | [diff] [blame] | 4 | # |
Gavin Howard | 7345cb9 | 2019-04-08 14:13:43 -0600 | [diff] [blame] | 5 | # All rights reserved. |
Gavin Howard | 36d3534 | 2018-10-15 20:25:31 -0600 | [diff] [blame] | 6 | # |
Gavin Howard | 7345cb9 | 2019-04-08 14:13:43 -0600 | [diff] [blame] | 7 | # 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 Howard | 36d3534 | 2018-10-15 20:25:31 -0600 | [diff] [blame] | 28 | # |
| 29 | |
| 30 | import os |
| 31 | import sys |
| 32 | import subprocess |
| 33 | import time |
| 34 | |
| 35 | def usage(): |
Gavin Howard | dcaf230 | 2018-10-17 16:30:38 -0600 | [diff] [blame] | 36 | print("usage: {} [test_num exe]".format(script)) |
| 37 | print("\n test_num is the last Karatsuba number to run through tests") |
Gavin Howard | 36d3534 | 2018-10-15 20:25:31 -0600 | [diff] [blame] | 38 | sys.exit(1) |
| 39 | |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 40 | script = sys.argv[0] |
| 41 | testdir = os.path.dirname(script) |
| 42 | |
| 43 | print("\nWARNING: This script is for distro and package maintainers.") |
| 44 | print("It is for finding the optimal Karatsuba number.") |
Gavin Howard | 8c9288d | 2019-05-10 12:31:42 -0600 | [diff] [blame] | 45 | print("Though it only needs to be run once per release/platform,") |
| 46 | print("it takes forever to run.") |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 47 | print("You have been warned.\n") |
Gavin Howard | 2a65988 | 2019-05-10 12:47:33 -0600 | [diff] [blame] | 48 | print("Note: If you send an interrupt, it will report the current best number.\n") |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 49 | |
Gavin Howard | 36d3534 | 2018-10-15 20:25:31 -0600 | [diff] [blame] | 50 | if __name__ != "__main__": |
| 51 | usage() |
| 52 | |
Gavin Howard | 977767f | 2019-04-24 10:46:23 -0600 | [diff] [blame] | 53 | mx = 520 |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 54 | mx2 = mx // 2 |
Gavin Howard | 8c9288d | 2019-05-10 12:31:42 -0600 | [diff] [blame] | 55 | mn = 16 |
Gavin Howard | 36d3534 | 2018-10-15 20:25:31 -0600 | [diff] [blame] | 56 | |
| 57 | num = "9" * mx |
| 58 | |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 59 | if len(sys.argv) >= 2: |
| 60 | test_num = int(sys.argv[1]) |
| 61 | else: |
| 62 | test_num = 0 |
| 63 | |
| 64 | if len(sys.argv) >= 3: |
| 65 | exe = sys.argv[2] |
| 66 | else: |
| 67 | exe = testdir + "/bin/bc" |
| 68 | |
| 69 | exedir = os.path.dirname(exe) |
Gavin Howard | 36d3534 | 2018-10-15 20:25:31 -0600 | [diff] [blame] | 70 | |
Gavin Howard | 8c9288d | 2019-05-10 12:31:42 -0600 | [diff] [blame] | 71 | indata = "for (i = 0; i < 100; ++i) {} * {}\n" |
| 72 | indata += "1.23456789^100000\n1.23456789^100000\nhalt" |
| 73 | indata = indata.format(num, num) |
Gavin Howard | 36d3534 | 2018-10-15 20:25:31 -0600 | [diff] [blame] | 74 | |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 75 | times = [] |
| 76 | nums = [] |
| 77 | runs = [] |
| 78 | nruns = 5 |
Gavin Howard | 977767f | 2019-04-24 10:46:23 -0600 | [diff] [blame] | 79 | |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 80 | for i in range(0, nruns): |
| 81 | runs.append(0) |
| 82 | |
| 83 | tests = [ "multiply", "modulus", "power", "sqrt" ] |
Gavin Howard | bc332b8 | 2019-05-15 11:19:58 -0600 | [diff] [blame] | 84 | scripts = [ "multiply" ] |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 85 | |
Gavin Howard | 030ef05 | 2019-05-10 10:59:24 -0600 | [diff] [blame] | 86 | if test_num != 0: |
| 87 | mx2 = test_num |
| 88 | |
Gavin Howard | 2a65988 | 2019-05-10 12:47:33 -0600 | [diff] [blame] | 89 | try: |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 90 | |
Gavin Howard | 2a65988 | 2019-05-10 12:47:33 -0600 | [diff] [blame] | 91 | for i in range(mn, mx2 + 1): |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 92 | |
Gavin Howard | 2a65988 | 2019-05-10 12:47:33 -0600 | [diff] [blame] | 93 | print("\nCompiling...\n") |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 94 | |
Gavin Howard | 2a65988 | 2019-05-10 12:47:33 -0600 | [diff] [blame] | 95 | makecmd = [ "./configure.sh", "-O3", "-k{}".format(i) ] |
| 96 | p = subprocess.run(makecmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 97 | |
Gavin Howard | 2a65988 | 2019-05-10 12:47:33 -0600 | [diff] [blame] | 98 | if p.returncode != 0: |
| 99 | print("configure.sh returned an error ({}); exiting...".format(p.returncode)) |
| 100 | sys.exit(p.returncode) |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 101 | |
Gavin Howard | aa8fbc5 | 2019-05-10 20:39:12 -0600 | [diff] [blame] | 102 | makecmd = [ "make" ] |
Gavin Howard | 2a65988 | 2019-05-10 12:47:33 -0600 | [diff] [blame] | 103 | p = subprocess.run(makecmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 104 | |
Gavin Howard | 2a65988 | 2019-05-10 12:47:33 -0600 | [diff] [blame] | 105 | if p.returncode != 0: |
| 106 | print("make returned an error ({}); exiting...".format(p.returncode)) |
| 107 | sys.exit(p.returncode) |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 108 | |
Gavin Howard | 2a65988 | 2019-05-10 12:47:33 -0600 | [diff] [blame] | 109 | if (test_num >= i): |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 110 | |
Gavin Howard | 2a65988 | 2019-05-10 12:47:33 -0600 | [diff] [blame] | 111 | print("Running tests for Karatsuba Num: {}\n".format(i)) |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 112 | |
Gavin Howard | 2a65988 | 2019-05-10 12:47:33 -0600 | [diff] [blame] | 113 | for test in tests: |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 114 | |
Gavin Howard | 2a65988 | 2019-05-10 12:47:33 -0600 | [diff] [blame] | 115 | cmd = [ "{}/tests/test.sh".format(testdir), "bc", test, "0", exe ] |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 116 | |
Gavin Howard | 2a65988 | 2019-05-10 12:47:33 -0600 | [diff] [blame] | 117 | p = subprocess.run(cmd + sys.argv[3:], stderr=subprocess.PIPE) |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 118 | |
Gavin Howard | 2a65988 | 2019-05-10 12:47:33 -0600 | [diff] [blame] | 119 | 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 Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 124 | |
Gavin Howard | 2a65988 | 2019-05-10 12:47:33 -0600 | [diff] [blame] | 125 | print("") |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 126 | |
Gavin Howard | bc332b8 | 2019-05-15 11:19:58 -0600 | [diff] [blame] | 127 | 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 Howard | 2a65988 | 2019-05-10 12:47:33 -0600 | [diff] [blame] | 142 | elif test_num == 0: |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 143 | |
Gavin Howard | 2a65988 | 2019-05-10 12:47:33 -0600 | [diff] [blame] | 144 | print("Timing Karatsuba Num: {}".format(i), end='', flush=True) |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 145 | |
Gavin Howard | 2a65988 | 2019-05-10 12:47:33 -0600 | [diff] [blame] | 146 | for j in range(0, nruns): |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 147 | |
Gavin Howard | 2a65988 | 2019-05-10 12:47:33 -0600 | [diff] [blame] | 148 | cmd = [ exe, "{}/tests/bc/power.txt".format(testdir) ] |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 149 | |
Gavin Howard | 2a65988 | 2019-05-10 12:47:33 -0600 | [diff] [blame] | 150 | start = time.perf_counter() |
| 151 | p = subprocess.run(cmd, input=indata.encode(), stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 152 | end = time.perf_counter() |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 153 | |
Gavin Howard | 2a65988 | 2019-05-10 12:47:33 -0600 | [diff] [blame] | 154 | if p.returncode != 0: |
| 155 | print("bc returned an error; exiting...") |
| 156 | sys.exit(p.returncode) |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 157 | |
Gavin Howard | 2a65988 | 2019-05-10 12:47:33 -0600 | [diff] [blame] | 158 | runs[j] = end - start |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 159 | |
Gavin Howard | 2a65988 | 2019-05-10 12:47:33 -0600 | [diff] [blame] | 160 | 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 | |
| 167 | except KeyboardInterrupt: |
| 168 | nums = nums[0:i] |
| 169 | times = times[0:i] |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 170 | |
Gavin Howard | 030ef05 | 2019-05-10 10:59:24 -0600 | [diff] [blame] | 171 | if test_num == 0: |
Gavin Howard | ab6ba68 | 2019-05-10 08:42:15 -0600 | [diff] [blame] | 172 | |
Gavin Howard | 030ef05 | 2019-05-10 10:59:24 -0600 | [diff] [blame] | 173 | opt = nums[times.index(min(times))] |
| 174 | |
Gavin Howard | 2a65988 | 2019-05-10 12:47:33 -0600 | [diff] [blame] | 175 | print("\n\nOptimal Karatsuba Num (for this machine): {}".format(opt)) |
Gavin Howard | 030ef05 | 2019-05-10 10:59:24 -0600 | [diff] [blame] | 176 | print("Run the following:\n") |
| 177 | print("./configure.sh -O3 -k {}".format(opt)) |
| 178 | print("make") |