ECF 1.7
run_parallel.py
1import subprocess
2from concurrent.futures import ThreadPoolExecutor, as_completed
3from pathlib import Path
4import time
5
6# input parameters
7executable = "./GAOneMax.exe"
8exe_args = ["parameters.txt"]
9log_prefix = "log"
10n_runs = 10
11n_procs = 4
12
13pad_width = len(str(n_runs))
14
15
16def run_instance(run_id: int):
17 log_name = f"{log_prefix}_{run_id:0{pad_width}d}.txt"
18 #print(f"Starting instance {run_id}")
19 with open(log_name, "w") as log_file:
20 result = subprocess.run(
21 [executable, *exe_args],
22 stdout=log_file,
23 stderr=subprocess.STDOUT
24 )
25 return run_id, result.returncode
26
27
28def main():
29 print(f"Running {n_runs} runs with {n_procs} parallel instances")
30
31 with ThreadPoolExecutor(max_workers=n_procs) as executor:
32 futures = []
33
34 for run_id in range(1, n_runs + 1):
35 futures.append(executor.submit(run_instance, run_id))
36 time.sleep(1.1) # delay before next start
37
38 for future in as_completed(futures):
39 run_id, rc = future.result()
40 print(f"Run {run_id:0{pad_width}d} finished (code {rc})")
41
42
43if __name__ == "__main__":
44 main()