forked from RustamC/MacroPlacement
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sa_multicore.py
161 lines (134 loc) · 5.8 KB
/
sa_multicore.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
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
# ---------------------------------------------------------------------------
# Run Simulated Annealing in parallel with multiple cores
# You must set up the environment correctly before you run this script
# Please check Circuit Training for environment setup
# --------------------------------------------------------------------------
import sys
import os
import subprocess
import multiprocessing
import time
from datetime import datetime
import json
import glob
from SA import SimulatedAnnealing
### Wrapper function for calling Simulated Annealing
def RunSA(run_id, dir, netlist, plc_file, action_probs, num_actions, max_temperature, num_iters, seed, spiral_flag):
run_dir = dir + "/run_" + str(run_id)
os.mkdir(run_dir)
# create param_map
param_map = { }
param_map["id"] = run_id
param_map["dir"] = dir
param_map["netlist"] = netlist
param_map["plc_file"] = plc_file
param_map["action_probs"] = action_probs
param_map["num_actions"] = num_actions
param_map["max_temperature"] = max_temperature
param_map["num_iters"] = num_iters
param_map["seed"] = seed
param_map["spiral_flag"] = spiral_flag
param_file = run_dir + "/params.json"
f = open(param_file, "w")
f.write(json.dumps(param_map, indent = 4))
f.close()
design = netlist.split('/')[-1].split('.')[0]
sa = SimulatedAnnealing(design, run_dir, netlist, plc_file, action_probs, num_actions, max_temperature, num_iters, seed, spiral_flag)
### Run Simulated Annealing with multiple threads
def RunParallelSA(config_json_file):
# parse the configuration json file
f = open(config_json_file)
params = json.load(f)
f.close()
netlist = params["netlist"]
plc_file = params["plc_file"]
action_probs_list = params["action_probs"]
num_actions_list = params["num_actions(xn)"]
max_temperature_list = params["max_temperature"]
num_iters_list = params["num_iters"]
seed_list = params["seed"]
num_cores = params["num_cores"]
spiral_flag_list = params["spiral_flag"]
result_dir ="./result_" + str(datetime.fromtimestamp(time.time()))
os.mkdir(result_dir)
params_file = result_dir + "/params.json"
f = open(params_file, "w")
f.write(json.dumps(params, indent = 4))
f.close()
param_list = []
for action_probs in action_probs_list:
for num_actions in num_actions_list:
for max_temperature in max_temperature_list:
for num_iters in num_iters_list:
for seed in seed_list:
for spiral_flag in spiral_flag_list:
param_map = { }
param_map["id"] = len(param_list)
param_map["dir"] = result_dir
param_map["netlist"] = netlist
param_map["plc_file"] = plc_file
param_map["action_probs"] = action_probs
param_map["num_actions"] = num_actions
param_map["max_temperature"] = max_temperature
param_map["num_iters"] = num_iters
param_map["seed"] = seed
param_map["spiral_flag"] = (spiral_flag == 'True' or spiral_flag == 'TRUE')
param_list.append(param_map)
with multiprocessing.Manager() as manager:
# create lists in server memory
runtime_list = manager.list([])
cut_list = manager.list([])
remaining_runs = len(param_list)
run_id = 0
while (remaining_runs > 0):
num_thread = min(num_cores, remaining_runs)
process_list = []
for i in range(num_thread):
param_map = param_list[run_id]
p = multiprocessing.Process(target = RunSA,
args = (param_map["id"],
param_map["dir"],
param_map["netlist"],
param_map["plc_file"],
param_map["action_probs"],
param_map["num_actions"],
param_map["max_temperature"],
param_map["num_iters"],
param_map["seed"],
param_map["spiral_flag"]))
run_id = run_id + 1
p.start()
process_list.append(p)
for process in process_list:
process.join()
remaining_runs -= num_thread
summary_files = glob.glob(result_dir + "/*/*.summary")
cost_list = []
for summary_file in summary_files:
with open(summary_file) as f:
content = f.read().splitlines()
f.close()
# find the start_idx
start_idx = 0
for i in range(len(content)):
items = content[i].split()
if (len(items) == 1):
start_idx = i
break
if (len(cost_list) == 0):
for i in range(start_idx, len(content)):
cost_list.append(float(content[i]))
else:
for i in range(start_idx, len(content)):
idx = i - start_idx
if (idx >= len(cost_list)):
cost_list.append(float(content[i]))
else:
cost_list[idx] = min(cost_list[idx], float(content[i]))
f = open(result_dir + "/cost_summary.txt", "w")
for cost in cost_list:
f.write(str(cost) + "\n")
f.close()
if __name__ == "__main__":
config_file = "config.json"
RunParallelSA(config_file)