-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgen_solve.py
117 lines (97 loc) · 3.6 KB
/
gen_solve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import argparse
import itertools
import multiprocessing
import os
import shutil
import signal
import subprocess
import sys
import time
import tqdm
def run_process(args):
fname, problem_folder, solution_folder, tmp_folder, solver_base, solvers, timeout = (
args
)
problem_file = os.path.join(problem_folder, fname)
output_file = os.path.join(solution_folder, fname)
if os.path.exists(output_file):
return
processes = []
files = []
for solver in solvers:
tmp_proof = os.path.join(tmp_folder, fname, solver)
os.makedirs(tmp_proof, exist_ok=True)
tmp_solution = os.path.join(tmp_folder, fname, solver, "cnf.out")
tmp_solution = open(tmp_solution, "w")
files.append(tmp_solution)
solver_path = os.path.join(solver_base, solver, "bin", "starexec_run_default")
solver_cwd = os.path.join(solver_base, solver, "bin")
process = subprocess.Popen(
[solver_path, problem_file, tmp_proof],
cwd=solver_cwd,
stdout=tmp_solution,
stderr=tmp_solution,
)
process._myname = solver
processes.append(process)
starttime = time.time() + timeout
should_run = True
finish_name = None
while should_run:
for process in processes:
try:
process.communicate(timeout=1)
finish_name = process._myname
should_run = False
except subprocess.TimeoutExpired:
pass
if time.time() > starttime:
should_run = False
for process in processes:
process.send_signal(signal.SIGTERM)
process.wait()
for f in files:
f.close()
# print(fname, finish_name)
if finish_name:
tmp_solution = os.path.join(tmp_folder, fname, finish_name, "cnf.out")
shutil.copy(tmp_solution, output_file)
def main(problem_folder, solution_folder, tmp_folder, solver_base, solvers, timeout, nprocess):
os.makedirs(problem_folder, exist_ok=True)
os.makedirs(solution_folder, exist_ok=True)
os.makedirs(tmp_folder, exist_ok=True)
problems = [fname for fname in os.listdir(problem_folder) if fname.endswith(".cnf")]
problems = sorted(problems)
problems = [
(
fname,
problem_folder,
solution_folder,
tmp_folder,
solver_base,
solvers,
timeout,
)
for fname in problems
]
pool = multiprocessing.Pool(nprocess)
for _ in tqdm.tqdm(pool.imap_unordered(run_process, problems), total=len(problems), ascii=True):
pass
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input_folder", required=True, type=str)
parser.add_argument("-o", "--output_folder", default=None, type=str)
parser.add_argument("-m", "--tmp_folder", default=None, type=str)
parser.add_argument("-b", "--solver_base", required=True, type=str)
parser.add_argument("-s", "--solver_names", required=True, type=str, nargs="+")
parser.add_argument("-t", "--timeout", type=int, default=600)
parser.add_argument("-N", '--nprocess', type=int, default=10)
args = parser.parse_args()
problem_folder = args.input_folder.rstrip("/")
solution_folder = args.output_folder or problem_folder + "-solution"
tmp_folder = args.tmp_folder or problem_folder + "-tmp"
solver_base = args.solver_base
solvers = list(args.solver_names)
timeout = args.timeout
nprocess = args.nprocess
main(problem_folder, solution_folder, tmp_folder, solver_base, solvers, timeout, nprocess)