run-random.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. #
  4. # AWL simulator - Run random AWL program
  5. #
  6. # Copyright 2019 Michael Buesch <m@bues.ch>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. from __future__ import division, absolute_import, print_function, unicode_literals
  23. import subprocess
  24. import multiprocessing
  25. import sys
  26. import os
  27. import getopt
  28. basedir = os.path.dirname(os.path.abspath(__file__))
  29. awlsim_base = os.path.join(basedir, "..")
  30. class Error(Exception): pass
  31. def error(msg):
  32. print(msg, file=sys.stderr)
  33. def info(msg):
  34. print(msg, file=sys.stdout)
  35. def process(seed):
  36. procGen = None
  37. procAwlsim = None
  38. try:
  39. info("Running with seed=%d ..." % seed)
  40. procGen = subprocess.Popen(
  41. [ sys.executable,
  42. os.path.join(awlsim_base, "maintenance", "benchmark", "generate_benchmark.py"),
  43. "--seed", str(seed), "--one-cycle" ],
  44. stdout=subprocess.PIPE,
  45. shell=False)
  46. procAwlsim = subprocess.Popen(
  47. [ sys.executable,
  48. os.path.join(awlsim_base, "awlsim-test"),
  49. "-D", "-L1", "-4", "-" ],
  50. stdin=procGen.stdout,
  51. stdout=subprocess.PIPE,
  52. shell=False)
  53. procGen.stdout.close()
  54. procAwlsim.communicate()
  55. procGen.terminate()
  56. procGen.wait()
  57. if procGen.returncode or procAwlsim.returncode:
  58. raise Error("Process exited with error.")
  59. except KeyboardInterrupt:
  60. raise Error("Aborted by user.")
  61. finally:
  62. for p in (procGen, procAwlsim):
  63. if p:
  64. p.terminate()
  65. p.wait()
  66. def usage(f=sys.stdout):
  67. print("run-random.py [OPTIONS]", file=f)
  68. print("", file=f)
  69. print(" -j|--jobs JOBS Number of parallel jobs. Default: Number of CPUs", file=f)
  70. print(" -f|--seed-from SEED Start with seed from this value. Default: 0", file=f)
  71. print(" -t|--seed-to SEED End with seed at this value.", file=f)
  72. print(" -s|--seed-step INC Increment seed by this value. Default: 1", file=f)
  73. def main():
  74. opt_jobs = None
  75. opt_rangeMin = 0
  76. opt_rangeMax = 0xFFFFFF
  77. opt_rangeStep = 1
  78. try:
  79. (opts, args) = getopt.getopt(sys.argv[1:],
  80. "hj:f:t:s:",
  81. [ "help", "jobs=", "seed-from=", "seed-to=", "seed-step=", ])
  82. except getopt.GetoptError as e:
  83. error(str(e))
  84. for (o, v) in opts:
  85. if o in ("-h", "--help"):
  86. usage()
  87. return 0
  88. if o in ("-j", "--jobs"):
  89. try:
  90. opt_jobs = int(v)
  91. if opt_jobs < 0:
  92. raise ValueError
  93. if opt_jobs == 0:
  94. opt_jobs = None
  95. except ValueError:
  96. error("Invalid -j|--jobs argument.")
  97. return 1
  98. if o in ("-f", "--seed-from"):
  99. try:
  100. opt_rangeMin = int(v)
  101. if opt_rangeMin < 0 or opt_rangeMin > 0xFFFFFFFF:
  102. raise ValueError
  103. except ValueError:
  104. error("Invalid -f|--seed-from argument.")
  105. return 1
  106. if o in ("-t", "--seed-to"):
  107. try:
  108. opt_rangeMax = int(v)
  109. if opt_rangeMax < 0 or opt_rangeMax > 0xFFFFFFFF:
  110. raise ValueError
  111. except ValueError:
  112. error("Invalid -t|--seed-to argument.")
  113. return 1
  114. if o in ("-s", "--seed-step"):
  115. try:
  116. opt_rangeStep = int(v)
  117. except ValueError:
  118. error("Invalid -s|--seed-step argument.")
  119. return 1
  120. try:
  121. seedRange = range(opt_rangeMin, opt_rangeMax + 1, opt_rangeStep)
  122. if opt_jobs == 1:
  123. for seed in seedRange:
  124. process(seed)
  125. else:
  126. with multiprocessing.Pool(opt_jobs) as pool:
  127. pool.map(process, seedRange)
  128. except KeyboardInterrupt:
  129. return 1
  130. except Exception as e:
  131. error(str(e))
  132. return 1
  133. return 0
  134. if __name__ == "__main__":
  135. sys.exit(main())