mcell3_runner.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/usr/bin/env python
  2. """
  3. This is free and unencumbered software released into the public domain.
  4. Anyone is free to copy, modify, publish, use, compile, sell, or
  5. distribute this software, either in source code form or as a compiled
  6. binary, for any purpose, commercial or non-commercial, and by any
  7. means.
  8. In jurisdictions that recognize copyright laws, the author or authors
  9. of this software dedicate any and all copyright interest in the
  10. software to the public domain. We make this dedication for the benefit
  11. of the public at large and to the detriment of our heirs and
  12. successors. We intend this dedication to be an overt act of
  13. relinquishment in perpetuity of all present and future rights to this
  14. software under copyright law.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. For more information, please refer to [http://unlicense.org]
  23. """
  24. import os
  25. import sys
  26. import shutil
  27. import glob
  28. import pathlib
  29. import argparse
  30. import itertools
  31. import multiprocessing
  32. import re
  33. import subprocess
  34. from mcell4_runner import *
  35. def run_mcell3(args_str_w_opts):
  36. args_str = args_str_w_opts[0]
  37. opts = args_str_w_opts[1]
  38. if os.name == 'nt':
  39. ext = '.exe'
  40. else:
  41. ext = ''
  42. mcell_path = os.environ.get('MCELL_PATH', '')
  43. cmd_str = os.path.join(mcell_path, 'mcell' + ext) + ' ' + opts.main_model_file + ' '
  44. cmd_str += args_str
  45. print("Running " + cmd_str)
  46. args_log = args_str.replace(' ', '_').replace('-', '_').replace('\\', '_').replace('/', '_')
  47. log_name = os.path.join(LOGS_DIR, os.path.splitext(opts.main_model_file)[0] + '_' + args_log + '.mcell3.log')
  48. exit_code = 1
  49. with open(log_name, "w") as f:
  50. proc = subprocess.Popen(cmd_str, shell=True, cwd=os.getcwd(), stdout=f, stderr=subprocess.STDOUT)
  51. proc.communicate()
  52. exit_code = proc.returncode
  53. with open(log_name, "a") as f:
  54. f.write("DIR:" + os.getcwd() + "\n")
  55. f.write("CMD:" + cmd_str + "\n")
  56. if exit_code != 0:
  57. print("MCell3 failed, see '" + os.path.join(os.getcwd(), log_name) + "'.")
  58. return exit_code
  59. else:
  60. return 0
  61. def run_mcell3_parallel(opts, args):
  62. # set up the parallel task pool to use all available processors ot the
  63. # maximum specified
  64. if opts.max_cores:
  65. cpu_count = int(opts.max_cores)
  66. else:
  67. cpu_count = multiprocessing.cpu_count()
  68. # create logs directory
  69. if not os.path.exists(LOGS_DIR):
  70. os.mkdir(LOGS_DIR)
  71. # run the jobs
  72. pool = multiprocessing.Pool(processes=cpu_count)
  73. args_str_w_opts = zip(args, itertools.repeat(opts))
  74. res_codes = pool.map(run_mcell3, args_str_w_opts, 1)
  75. num_total = 0
  76. num_failed = 0
  77. for i in range(len(res_codes)):
  78. c = res_codes[i]
  79. if c != 0:
  80. print("MCell run with args '" + args[i] + "' failed with exit code " + str(c) + ".")
  81. num_failed += 1
  82. num_total += 1
  83. if num_failed == 0:
  84. print("Finished, all runs passed.")
  85. return 0
  86. else:
  87. print("Finished with errors, " + str(num_failed) + "/" + str(num_total) + " runs failed.")
  88. return 1
  89. if __name__ == '__main__':
  90. file_markers_start()
  91. check_prerequisites()
  92. opts = process_opts('MCell3')
  93. args = prepare_args(opts)
  94. exit_code = run_mcell3_parallel(opts, args)
  95. file_markers_finish()
  96. sys.exit(exit_code)