forked from Thinklab-SJTU/EDA-AI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Params.py
171 lines (155 loc) · 5.29 KB
/
Params.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
##
# @file Params.py
# @author Yibo Lin
# @date Apr 2018
# @brief User parameters
#
import os
import sys
import json
import math
from collections import OrderedDict
import pdb
class Params:
"""
@brief Parameter class
"""
def __init__(self):
"""
@brief initialization
"""
filename = os.path.join(os.path.dirname(__file__), 'params.json')
self.__dict__ = {}
params_dict = {}
with open(filename, "r") as f:
params_dict = json.load(f, object_pairs_hook=OrderedDict)
for key, value in params_dict.items():
if 'default' in value:
self.__dict__[key] = value['default']
else:
self.__dict__[key] = None
self.__dict__['params_dict'] = params_dict
def printWelcome(self):
"""
@brief print welcome message
"""
content = """\
========================================================
DREAMPlace
Yibo Lin (http://yibolin.com)
David Z. Pan (http://users.ece.utexas.edu/~dpan)
========================================================"""
print(content)
def printHelp(self):
"""
@brief print help message for JSON parameters
"""
content = self.toMarkdownTable()
print(content)
def toMarkdownTable(self):
"""
@brief convert to markdown table
"""
key_length = len('JSON Parameter')
key_length_map = []
default_length = len('Default')
default_length_map = []
description_length = len('Description')
description_length_map = []
def getDefaultColumn(key, value):
if sys.version_info.major < 3: # python 2
flag = isinstance(value['default'], unicode)
else: #python 3
flag = isinstance(value['default'], str)
if flag and not value['default'] and 'required' in value:
return value['required']
else:
return value['default']
for key, value in self.params_dict.items():
key_length_map.append(len(key))
default_length_map.append(len(str(getDefaultColumn(key, value))))
description_length_map.append(len(value['descripton']))
key_length = max(key_length, key_length_map[-1])
default_length = max(default_length, default_length_map[-1])
description_length = max(description_length, description_length_map[-1])
content = "| %s %s| %s %s| %s %s|\n" % (
'JSON Parameter',
" " * (key_length - len('JSON Parameter') + 1),
'Default',
" " * (default_length - len('Default') + 1),
'Description',
" " * (description_length - len('Description') + 1)
)
content += "| %s | %s | %s |\n" % (
"-" * (key_length + 1),
"-" * (default_length + 1),
"-" * (description_length + 1)
)
count = 0
for key, value in self.params_dict.items():
content += "| %s %s| %s %s| %s %s|\n" % (
key,
" " * (key_length - key_length_map[count] + 1),
str(getDefaultColumn(key, value)),
" " * (default_length - default_length_map[count] + 1),
value['descripton'],
" " * (description_length - description_length_map[count] + 1)
)
count += 1
return content
def toJson(self):
"""
@brief convert to json
"""
data = {}
for key, value in self.__dict__.items():
if key != 'params_dict':
data[key] = value
return data
def fromJson(self, data):
"""
@brief load form json
"""
for key, value in data.items():
self.__dict__[key] = value
def dump(self, filename):
"""
@brief dump to json file
"""
with open(filename, 'w') as f:
json.dump(self.toJson(), f)
def load(self, filename):
"""
@brief load from json file
"""
with open(filename, 'r') as f:
self.fromJson(json.load(f))
def __str__(self):
"""
@brief string
"""
return str(self.toJson())
def __repr__(self):
"""
@brief print
"""
return self.__str__()
def design_name(self):
"""
@brief speculate the design name for dumping out intermediate solutions
"""
if self.aux_input:
design_name = os.path.basename(self.aux_input).replace(".aux", "").replace(".AUX", "")
elif self.verilog_input:
design_name = os.path.basename(self.verilog_input).replace(".v", "").replace(".V", "")
elif self.def_input:
design_name = os.path.basename(self.def_input).replace(".def", "").replace(".DEF", "")
return design_name
def solution_file_suffix(self):
"""
@brief speculate placement solution file suffix
"""
if self.def_input is not None and os.path.exists(self.def_input): # LEF/DEF
return "def"
else: # Bookshelf
return "pl"