Gavin Howard | 36d3534 | 2018-10-15 20:25:31 -0600 | [diff] [blame] | 1 | #! /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 | |
| 17 | import os |
| 18 | import sys |
| 19 | import subprocess |
| 20 | import time |
| 21 | |
| 22 | def usage(): |
Gavin Howard | dcaf230 | 2018-10-17 16:30:38 -0600 | [diff] [blame] | 23 | print("usage: {} [test_num exe]".format(script)) |
| 24 | 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] | 25 | sys.exit(1) |
| 26 | |
| 27 | script = sys.argv[0] |
| 28 | testdir = os.path.dirname(script) |
| 29 | |
Gavin Howard | efc2eb4 | 2018-10-17 16:37:43 -0600 | [diff] [blame] | 30 | print("\nWARNING: This script is for distro and package maintainers.") |
| 31 | print("It is for finding the optimal Karatsuba number.") |
| 32 | print("It takes forever to run.") |
| 33 | print("You have been warned.\n") |
| 34 | |
Gavin Howard | 36d3534 | 2018-10-15 20:25:31 -0600 | [diff] [blame] | 35 | if __name__ != "__main__": |
| 36 | usage() |
| 37 | |
Gavin Howard | dcaf230 | 2018-10-17 16:30:38 -0600 | [diff] [blame] | 38 | mx = 200 |
| 39 | mx2 = mx // 2 |
| 40 | mn = 2 |
Gavin Howard | 36d3534 | 2018-10-15 20:25:31 -0600 | [diff] [blame] | 41 | |
| 42 | num = "9" * mx |
| 43 | |
| 44 | if len(sys.argv) >= 2: |
Gavin Howard | dcaf230 | 2018-10-17 16:30:38 -0600 | [diff] [blame] | 45 | test_num = int(sys.argv[1]) |
Gavin Howard | 36d3534 | 2018-10-15 20:25:31 -0600 | [diff] [blame] | 46 | else: |
Gavin Howard | dcaf230 | 2018-10-17 16:30:38 -0600 | [diff] [blame] | 47 | test_num = 0 |
Gavin Howard | 36d3534 | 2018-10-15 20:25:31 -0600 | [diff] [blame] | 48 | |
Gavin Howard | dcaf230 | 2018-10-17 16:30:38 -0600 | [diff] [blame] | 49 | if len(sys.argv) >= 3: |
| 50 | exe = sys.argv[2] |
| 51 | else: |
| 52 | exe = testdir + "/bin/bc" |
Gavin Howard | 36d3534 | 2018-10-15 20:25:31 -0600 | [diff] [blame] | 53 | |
Gavin Howard | dcaf230 | 2018-10-17 16:30:38 -0600 | [diff] [blame] | 54 | exedir = os.path.dirname(exe) |
Gavin Howard | 36d3534 | 2018-10-15 20:25:31 -0600 | [diff] [blame] | 55 | |
Gavin Howard | dcaf230 | 2018-10-17 16:30:38 -0600 | [diff] [blame] | 56 | indata = "for (i = 0; i < 100; ++i) {} * {}\nhalt".format(num, num) |
Gavin Howard | 36d3534 | 2018-10-15 20:25:31 -0600 | [diff] [blame] | 57 | |
Gavin Howard | dcaf230 | 2018-10-17 16:30:38 -0600 | [diff] [blame] | 58 | times = [] |
| 59 | nums = [] |
| 60 | |
| 61 | tests = [ "multiply", "modulus", "power", "sqrt" ] |
| 62 | |
| 63 | for i in range(mn, mx2 + 1): |
Gavin Howard | 36d3534 | 2018-10-15 20:25:31 -0600 | [diff] [blame] | 64 | |
Gavin Howard | 93b0e5f | 2019-01-08 01:18:31 -0700 | [diff] [blame] | 65 | print("\nCompiling...\n") |
| 66 | |
Gavin Howard | 79f605c | 2019-01-08 13:08:21 -0700 | [diff] [blame] | 67 | makecmd = [ "./configure.sh", "-O3", "-k{}".format(i) ] |
Gavin Howard | 93b0e5f | 2019-01-08 01:18:31 -0700 | [diff] [blame] | 68 | p = subprocess.run(makecmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 69 | |
| 70 | if p.returncode != 0: |
Gavin Howard | 79f605c | 2019-01-08 13:08:21 -0700 | [diff] [blame] | 71 | print("configure.sh returned an error ({}); exiting...".format(p.returncode)) |
Gavin Howard | 93b0e5f | 2019-01-08 01:18:31 -0700 | [diff] [blame] | 72 | sys.exit(p.returncode) |
| 73 | |
| 74 | makecmd = [ "make" ] |
Gavin Howard | 36d3534 | 2018-10-15 20:25:31 -0600 | [diff] [blame] | 75 | 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 Howard | dcaf230 | 2018-10-17 16:30:38 -0600 | [diff] [blame] | 81 | if (test_num >= i): |
| 82 | |
Gavin Howard | 93b0e5f | 2019-01-08 01:18:31 -0700 | [diff] [blame] | 83 | print("Running tests...\n") |
Gavin Howard | dcaf230 | 2018-10-17 16:30:38 -0600 | [diff] [blame] | 84 | |
| 85 | for test in tests: |
| 86 | |
Gavin Howard | 93b0e5f | 2019-01-08 01:18:31 -0700 | [diff] [blame] | 87 | cmd = [ "{}/tests/test.sh".format(testdir), "bc", test, "0", exe ] |
Gavin Howard | dcaf230 | 2018-10-17 16:30:38 -0600 | [diff] [blame] | 88 | |
| 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 Howard | 36d3534 | 2018-10-15 20:25:31 -0600 | [diff] [blame] | 106 | |
| 107 | if p.returncode != 0: |
| 108 | print("bc returned an error; exiting...") |
| 109 | sys.exit(p.returncode) |
| 110 | |
Gavin Howard | dcaf230 | 2018-10-17 16:30:38 -0600 | [diff] [blame] | 111 | nums.append(i) |
Gavin Howard | 36d3534 | 2018-10-15 20:25:31 -0600 | [diff] [blame] | 112 | times.append(end - start) |
Gavin Howard | dcaf230 | 2018-10-17 16:30:38 -0600 | [diff] [blame] | 113 | print(", Time: {}".format(times[i - mn])) |
Gavin Howard | 36d3534 | 2018-10-15 20:25:31 -0600 | [diff] [blame] | 114 | |
Gavin Howard | 4995d20 | 2018-10-24 15:24:57 -0600 | [diff] [blame] | 115 | opt = nums[times.index(min(times))] |
Gavin Howard | efc2eb4 | 2018-10-17 16:37:43 -0600 | [diff] [blame] | 116 | |
| 117 | print("\nOptimal Karatsuba Num (for this machine): {}".format(opt)) |
| 118 | print("Run the following:\n") |
Gavin Howard | 79f605c | 2019-01-08 13:08:21 -0700 | [diff] [blame] | 119 | print("./configure.sh -O3 -k {}".format(opt)) |
Gavin Howard | 93b0e5f | 2019-01-08 01:18:31 -0700 | [diff] [blame] | 120 | print("make") |