-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
185 lines (164 loc) · 6.38 KB
/
Copy pathscript.py
File metadata and controls
185 lines (164 loc) · 6.38 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import subprocess
import time
import re
import os
import json
import sys
from pathlib import Path
RUN_RS_PATH = Path("src/run.rs")
STATE_PATH = Path("latest_bounds.json")
LOWER_BOUND = 1
UPPER_BOUND = 4294967296
RETRIES = 2
MAX_RESTARTS = 5
RESTART_DELAY = 5 # seconds
EXECUTABLE = "./target/release/bf_opt"
CONST_PATTERN = re.compile(r"const SHRINK_TO_SIZE: usize = \d+;")
def log(msg, log_path):
timestamp = time.strftime("[%Y-%m-%d %H:%M:%S]")
with open(log_path, "a") as f:
f.write(f"{timestamp} {msg}\n")
print(f"{timestamp} {msg}")
def update_const(value):
code = RUN_RS_PATH.read_text()
updated = CONST_PATTERN.sub(f"const SHRINK_TO_SIZE: usize = {value};", code)
RUN_RS_PATH.write_text(updated)
def build_project(log_path):
try:
result = subprocess.run(["cargo", "build", "--release"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
timeout=300)
if result.returncode != 0:
log(f"Build stderr: {result.stderr.decode()}", log_path)
return result.returncode == 0
except Exception as e:
log(f"Build exception: {e}", log_path)
return False
def run_and_time(log_path):
times = []
for attempt in range(3):
try:
start = time.time()
result = subprocess.run([EXECUTABLE], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=800)
end = time.time()
if result.returncode == 0:
times.append(end - start)
else:
log(f"Run failed (attempt {attempt + 1}) with return code {result.returncode}. stderr: {result.stderr.decode()}", log_path)
except subprocess.TimeoutExpired:
log(f"Run timed out on attempt {attempt + 1}.", log_path)
except Exception as e:
log(f"Run exception on attempt {attempt + 1}: {e}", log_path)
if times:
return min(times)
return None
def save_state(low, high, best_val, best_time):
state = {
"low": low,
"high": high,
"best_val": best_val,
"best_time": best_time
}
with open(STATE_PATH, "w") as f:
json.dump(state, f)
def load_state():
if STATE_PATH.exists():
try:
with open(STATE_PATH) as f:
state = json.load(f)
return (
state.get("low", LOWER_BOUND),
state.get("high", UPPER_BOUND),
state.get("best_val"),
state.get("best_time", float("inf"))
)
except Exception as e:
log(f"Error reading state file: {e}")
return LOWER_BOUND, UPPER_BOUND, None, float("inf")
def stepwise_search(log_path):
low, high, best_val, best_time = load_state()
if best_val is None:
best_val = (low + high) // 2
best_time = float("inf")
step = max(1, (high - low) // 4)
current = best_val
while step >= 1:
candidates = [current - step, current, current + step]
candidates = [c for c in candidates if low <= c <= high]
times = {}
for val in candidates:
log(f"Testing SHRINK_TO_SIZE = {val}", log_path)
update_const(val)
if not build_project(log_path):
log("Build failed. Skipping value.", log_path)
continue
runtime = run_and_time(log_path)
if runtime is not None:
times[val] = runtime
log(f"Success: SHRINK_TO_SIZE = {val}, Time = {runtime:.2f} seconds", log_path)
else:
log("Execution failed. Skipping value.", log_path)
if not times:
log("No successful runs in this step. Halving step size.", log_path)
step //= 2
continue
min_val = min(times, key=times.get)
min_time = times[min_val]
# Find all candidates within 2% of the best time
close_vals = [v for v, t in times.items() if abs(t - min_time) / min_time < 0.02]
if len(close_vals) > 1:
# Focus search between the min and max of these close candidates
new_low = max(low, min(close_vals))
new_high = min(high, max(close_vals))
log(f"Plateau detected between {new_low} and {new_high}, reducing step.", log_path)
low, high = new_low, new_high
current = (low + high) // 2
step = max(1, step // 2)
elif min_time < best_time:
best_time = min_time
best_val = min_val
log(f"New best: SHRINK_TO_SIZE = {best_val}, Time = {best_time:.2f} seconds", log_path)
current = best_val
# keep step the same to keep exploring in this direction
else:
# No improvement, reduce step size
step //= 2
save_state(low, high, best_val, best_time)
# Local sweep around best_val
sweep_range = range(max(low, best_val - 2), min(high, best_val + 3))
for val in sweep_range:
log(f"Local sweep: Testing SHRINK_TO_SIZE = {val}", log_path)
update_const(val)
if not build_project(log_path):
log("Build failed. Skipping value.", log_path)
continue
runtime = run_and_time(log_path)
if runtime is not None and runtime < best_time:
best_time = runtime
best_val = val
log(f"Local sweep new best: SHRINK_TO_SIZE = {best_val}, Time = {best_time:.2f} seconds", log_path)
save_state(low, high, best_val, best_time)
log(f"Search complete. Best SHRINK_TO_SIZE = {best_val} with time = {best_time:.2f}s", log_path)
def main():
attempt = 0
while True:
log_path = Path(f"stepwise_search_log_{attempt + 1}.txt")
log(f"===== Stepwise Search Attempt #{attempt + 1} Started =====", log_path)
try:
stepwise_search(log_path)
except Exception as e:
log(f"Fatal error: {e}", log_path)
if attempt < MAX_RESTARTS:
log(f"Restarting in {RESTART_DELAY} seconds...", log_path)
time.sleep(RESTART_DELAY)
attempt += 1
continue
else:
log("Max restarts reached. Exiting.", log_path)
break
log("===== Stepwise Search Finished =====", log_path)
attempt += 1
# Optionally, sleep or reset state here if needed
if __name__ == "__main__":
main()