-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmcnc.py
362 lines (303 loc) · 10.7 KB
/
mcnc.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
"""MCNC Circuit Benchmark Suite
Current supported items:
- PDWorkshop 91
"""
__all__ = [
"PD91_BASE_DIR", "PD91_BENCHMARKS", "PD91_TECHNOLOGIES",
"yal_to_json_str", "pd91_yal_to_json_file",
"parent_module_to_netlist",
"remove_singleton_edge",
"remove_duplicated_nodes",
"parse_yal_file", "yal_to_nx",
]
import os
import sys
import json
import logging
import subprocess
if sys.version <= "3.7":
try:
from collections.abc import OrderedDict
except ImportError:
from collections import OrderedDict
else:
OrderedDict = dict
import networkx as nx
import matplotlib as mpl
import matplotlib.pyplot as plt
from .hypergraph import hyperedges2edges
from .visualize import plot_ele_nx
DIR_PATH = os.path.dirname(__file__)
YAL2JSON_REL_PATH = os.path.join("yal", "c-parser", "yal2json")
YAL2JSON_ABS_PATH = os.path.join(DIR_PATH, YAL2JSON_REL_PATH)
PD91_BASE_DIR = os.path.join(DIR_PATH, "mcnc", "pd91", "bench")
PD91_BENCHMARKS = {
# block design
"block" : {
"ami33" : {
"format" : ["yal", "vpnr"],
"technology" : "ami"
},
"ami49" : {
"format" : ["yal", "vpnr"],
"technology" : "ami"
},
"apte" : {
"format" : ["yal", "vpnr"],
# TODO: technology
},
"hp" : {
"format" : ["yal", "vpnr"],
"technology" : "ami"
},
"xerox" : {
"format" : ["yal", "vpnr"],
"technology" : "xerox"
},
},
# floorplan
"floorplan" : {
"fan" : {
"format" : ["yal", "vpnr"],
# self-contained technology
},
# TODO: xerox
},
# mixed
"mixed" : {
"a3" : {
"format" : ["yal", "vpnr"],
# self-contained technology
},
"g2" : {
"format" : ["yal", "vpnr"],
# self-contained technology
},
"t1" : {
"format" : ["yal", "vpnr"],
# self-contained technology
},
},
# std cell (benchmark only, no tech lib)
"stdcell" : {
"biomed" : {
"format" : ["yal", "vpnr"],
"technology" : "db"
},
"fract" : {
"format" : ["yal", "vpnr"],
"technology" : "db"
},
"industry1" : {
"format" : ["yal", "vpnr"],
# self-contained technology
},
"industry2" : {
"format" : ["yal", "vpnr"],
# self-contained technology
},
"industry3" : {
"format" : ["yal", "vpnr"],
# self-contained technology
},
# TODO: primary1 and primary2
# need fix the YAL c parser to parse HEIGHT and WIDTH
# "primary1" : {
# "format" : ["yal", "vpnr"],
# "technology" : "sclib"
# },
# "primary2" : {
# "format" : ["yal", "vpnr"],
# "technology" : "sclib"
# },
"struct" : {
"format" : ["yal", "vpnr"],
"technology" : "db"
},
}
}
PD91_TECHNOLOGIES = {
"stdcell" : {
"db" : {
"format" : ["yal", "vpnr"],
},
"sclib" : {
"format" : ["yal", "vpnr"],
},
# TODO: gate array
# need fix the YAL c parser to parse HEIGHT and WIDTH
}
}
def yal_to_json_str(src_yal_path:str) -> str:
"""Convert a YAL file to JSON string
:param src_yal_path: path to the source YAL file
:return: a JSON string obtained by yal2json
"""
assert os.path.exists(src_yal_path), ValueError("YAL not found")
result = subprocess.run([YAL2JSON_ABS_PATH, src_yal_path],
capture_output=True, text=True)
# TODO: error handling
return result.stdout
def pd91_yal_to_json_file(base_dir:str):
"""Batch processing utility function: convert all PD91 benchmarks
from YAL files into JSON files
"""
# translate PD91 benchmark YAL files
for category in PD91_BENCHMARKS:
for benchmark in PD91_BENCHMARKS[category]:
if "yal" in PD91_BENCHMARKS[category][benchmark]["format"]:
print("=" * 64)
print("translating {} from yal to json".format(benchmark))
print("=" * 64)
src_yal_path = os.path.join(PD91_BASE_DIR, category,
benchmark + ".yal")
dst_json_path = os.path.join(PD91_BASE_DIR, category,
benchmark + ".json")
subprocess.run([
YAL2JSON_ABS_PATH,
src_yal_path,
dst_json_path
])
def parent_module_to_netlist(module:dict) -> (OrderedDict, OrderedDict):
"""Extract netlist in a hyper-graph form from the top module dict
:param module: input top-level flattened design
:return: (vertices, hyperedges)
vertices is an OrderedDict of instances, key=name, value=attributes
hyperedges is OrderedDict of connections, key=name, value=[instance names]
"""
# input module should be a parent/top module
assert "ModType" in module, TypeError()
assert "Network" in module, TypeError()
assert (module["ModType"] == "PARENT"), TypeError()
instances = OrderedDict()
netlist = OrderedDict()
# (1) extract netlist connection information
# as well as instance type (which module)
for instance in module["Network"]:
instance_name = instance["instancename"]
module_name = instance["modulename"]
# insert instance with attributes
assert instance_name not in instances
instances[instance_name] = {
"modulename" : module_name,
# TODO: other attributes
}
# insert signals or update signals with connected instances
for signal in instance["signalnames"]:
if (signal not in netlist) or (netlist[signal] is None):
# insert the signal with initalization
netlist[signal] = [instance_name, ]
else:
# update connected instances for the signal
netlist[signal].append(instance_name)
return instances, netlist
def remove_singleton_edge(hyperedges:OrderedDict) -> OrderedDict:
"""Remove signal edge which has only 1 instance connected (e.g., I/O)
:param hyperedges: input hyperedges (NOTE: will be mutated)
:return: mutated hyperedges
"""
# NOTE: we cannot mutate the OrderedDict when iterating it
# so we maintain old keys and keys to be deleted
original_ids = hyperedges.keys()
singleton_ids = []
for _id in original_ids:
if len(hyperedges[_id]) <= 1:
singleton_ids.append(_id)
for _id in singleton_ids:
del hyperedges[_id]
return hyperedges
def remove_duplicated_nodes(hyperedges:OrderedDict, verbose=False):
"""Remove duplicated nodes in the netlist to make sure that each
unique node appears only once in each net.
:param netlist: input netlist, a list of nodes
:return: output netlist after duplication removal
"""
_no_dup = OrderedDict()
for key in hyperedges:
origin_hyperedge = hyperedges[key]
unique_hyperedge = list(OrderedDict.fromkeys(origin_hyperedge))
_no_dup[key] = unique_hyperedge
if (verbose):
if len(unique_hyperedge) < len(origin_hyperedge):
num_dup_nodes = len(origin_hyperedge) - len(unique_hyperedge)
print(key, "has %d duplicated nodes" % num_dup_nodes)
return _no_dup
def parse_yal_file(yal_path:str) -> dict:
"""Parse YAL file and get vertices and hyperedges
:param yal_path: path to the YAL file
:return: parsed dict {vertices, hyperedges}
"""
vertices = None; hyperedges = None
# get the parsed JSON string of the YAL file
json_str = yal_to_json_str(yal_path)
# load json string to a list of Python dict
modules = json.loads(json_str)
# search for top modules
num_top_modules = 0
for module in modules:
if (module["ModType"] == "PARENT"):
num_top_modules += 1
vertices, hyperedges = parent_module_to_netlist(module)
# only 1 parent/top module is allowed in a benchmark
assert num_top_modules == 1, ValueError("More than one top module found")
return {
"vertices" : vertices,
"hyperedges" : hyperedges,
}
def yal_to_nx(yal_path:str, verbose=False) -> nx.Graph:
"""Parse YAL file and obtain a correpsonding NetworkX graph
"""
# parse YAL file to get a dict
results = parse_yal_file(yal_path)
# use OrderedGraph to make plot reproduciable
if sys.version >= "3.7":
G = nx.Graph()
else:
G = nx.OrderedGraph()
# extract and add nodes
nodes = results["vertices"]
G.add_nodes_from(nodes.items())
# sanity check
for _v in G.nodes:
assert G.nodes[_v] == nodes[_v]
# extract hyperedges
hyperedges = results["hyperedges"]
# remove duplicated nodes in each net
if verbose:
print("Removing duplicated nodes...")
hyperedges = remove_duplicated_nodes(hyperedges)
# remove singleton hyperedges/edges
if verbose:
print("Removing singleton hyperedge...")
hyperedges = remove_singleton_edge(hyperedges)
# transform hyperedges to edges
if verbose:
print("Transforming hyperedges to edges...")
hyperedges = hyperedges.values()
edges = hyperedges2edges(hyperedges)
for edge in edges:
G.add_edge(edge["src"], edge["dst"], weight=edge["weight"])
if verbose:
print("NetworkX graph constructed")
return G
if __name__ == "__main__":
FORMAT = "[%(filename)s:%(lineno)s - %(funcName)20s() ] %(message)s"
logging.basicConfig(format=FORMAT, level=logging.INFO)
for category in PD91_BENCHMARKS:
for benchmark in PD91_BENCHMARKS[category]:
if "yal" in PD91_BENCHMARKS[category][benchmark]["format"]:
print("benchmark: ", benchmark)
yal_path = os.path.join(PD91_BASE_DIR, category,
benchmark + ".yal")
G = yal_to_nx(yal_path)
if benchmark == "struct":
fig = plt.figure(figsize=(18, 10))
ax = plt.axes()
plot_ele_nx(G, ax, layout="shell",
node_color_keyword="modulename")
plt.show()
fig = plt.figure(figsize=(18, 10))
ax = plt.axes()
plot_ele_nx(G, ax, layout="spectral",
node_color_keyword="modulename")
plt.show()