-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample.py
More file actions
245 lines (195 loc) · 7.04 KB
/
Copy pathexample.py
File metadata and controls
245 lines (195 loc) · 7.04 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
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
#!/usr/bin/env python3
"""
H-Anchor: Hierarchical Anchor-Based Placement Algorithm
Example Usage and Demonstration
This script demonstrates the H-Anchor algorithm on various
synthetic benchmarks, showing how the HNSW-inspired hierarchical
approach handles different circuit topologies.
"""
import sys
import time
from h_anchor_fast import HAnchorPlacer, PlacementConfig, ScoringMethod
from benchmarks import (
generate_random_netlist,
generate_clustered_netlist,
generate_mesh_netlist,
generate_datapath_netlist,
generate_heterogeneous_netlist,
generate_small_world_netlist,
)
from visualization import PlacementVisualizer, visualize_placement
def run_placement(name: str, graph, cells, config=None):
"""Run H-Anchor placement on a netlist and print results."""
print("\n" + "=" * 60)
print(f" {name}")
print("=" * 60)
print(f" Cells: {len(cells):,}")
print(f" Edges: {graph.number_of_edges():,}")
print("=" * 60)
if config is None:
config = PlacementConfig(
num_layers=5,
top_layer_size=50,
die_width=1000,
die_height=1000,
)
placer = HAnchorPlacer(config)
placer.load_netlist(graph, cells)
start_time = time.time()
placer.run()
elapsed = time.time() - start_time
print(placer.get_placement_stats())
print(f"\n Total time: {elapsed:.2f} seconds")
return placer
def example_random():
"""Example: Random netlist."""
graph, cells = generate_random_netlist(
num_cells=1000,
num_edges=3000,
seed=42
)
return run_placement("Random Netlist (1K cells)", graph, cells)
def example_clustered():
"""Example: Clustered/hierarchical netlist."""
graph, cells = generate_clustered_netlist(
num_clusters=8,
cells_per_cluster=125,
intra_cluster_density=0.2,
inter_cluster_density=0.005,
seed=42
)
return run_placement("Clustered Netlist (8 blocks × 125 cells)", graph, cells)
def example_mesh():
"""Example: Mesh topology."""
graph, cells = generate_mesh_netlist(
rows=32,
cols=32,
diagonal_connections=True,
seed=42
)
return run_placement("Mesh Topology (32×32)", graph, cells)
def example_datapath():
"""Example: Datapath-like structure."""
graph, cells = generate_datapath_netlist(
num_stages=16,
width=64,
feedback_ratio=0.05,
seed=42
)
return run_placement("Datapath (16 stages × 64 bits)", graph, cells)
def example_heterogeneous():
"""Example: Heterogeneous FPGA-like netlist."""
graph, cells = generate_heterogeneous_netlist(
num_standard_cells=800,
num_rams=20,
num_dsps=30,
num_ios=100,
seed=42
)
# For heterogeneous designs, use PageRank to identify important blocks
config = PlacementConfig(
num_layers=5,
top_layer_size=50,
scoring_method=ScoringMethod.PAGERANK,
die_width=1000,
die_height=1000,
)
return run_placement("Heterogeneous FPGA (RAMs, DSPs, IOs)", graph, cells, config)
def example_small_world():
"""Example: Small-world network."""
graph, cells = generate_small_world_netlist(
num_cells=1000,
k=6,
p=0.1,
seed=42
)
return run_placement("Small-World Network (1K cells)", graph, cells)
def example_large_scale():
"""Example: Larger scale placement."""
print("\nGenerating large-scale netlist (10K cells)...")
graph, cells = generate_random_netlist(
num_cells=10000,
num_edges=40000,
seed=42
)
config = PlacementConfig(
num_layers=6,
top_layer_size=100,
decimation_factor=0.2,
top_layer_iterations=300,
refinement_iterations=30,
die_width=3000,
die_height=3000,
)
return run_placement("Large-Scale (10K cells, 40K edges)", graph, cells, config)
def demo_visualization(placer: HAnchorPlacer):
"""Demonstrate visualization capabilities."""
print("\n" + "=" * 60)
print(" Visualization Demo")
print("=" * 60)
viz = PlacementVisualizer(placer)
print("\n[1] Hierarchy Layers...")
viz.plot_hierarchy_layers()
print("\n[2] Placement Progression...")
viz.plot_placement_progression()
print("\n[3] Final Placement...")
viz.plot_placement(use_legal=True, show_edges=True)
print("\n[4] Wirelength Distribution...")
viz.plot_wirelength_distribution()
def main():
"""Main entry point."""
print("""
╔═══════════════════════════════════════════════════════════╗
║ H-Anchor: Hierarchical Anchor-Based Placement ║
║ Inspired by HNSW (Hierarchical Navigable Small World) ║
╚═══════════════════════════════════════════════════════════╝
This algorithm uses a multi-level anchor-driven approach:
1. HIERARCHY CONSTRUCTION (Bottom-Up)
- Score cells by PageRank/Degree centrality
- Select anchors with spatial inhibition (spread out)
- Build layers from dense (all cells) to sparse (key anchors)
2. TOP-DOWN PLACEMENT
- Place global anchors first (top layer)
- Descend through layers, projecting new cells
- Refine with force-directed optimization
- Anchors have higher "mass" (inertia)
3. LEGALIZATION
- Snap to placement rows
- Resolve overlaps
""")
# Parse command line arguments
if len(sys.argv) > 1:
example = sys.argv[1].lower()
examples = {
'random': example_random,
'clustered': example_clustered,
'mesh': example_mesh,
'datapath': example_datapath,
'heterogeneous': example_heterogeneous,
'smallworld': example_small_world,
'large': example_large_scale,
}
if example in examples:
placer = examples[example]()
if '--viz' in sys.argv or '-v' in sys.argv:
demo_visualization(placer)
else:
print(f"Unknown example: {example}")
print(f"Available: {', '.join(examples.keys())}")
sys.exit(1)
else:
# Run default demo
print("Running default demo (clustered netlist)...")
print("Use: python example.py <example> [--viz]")
print("Examples: random, clustered, mesh, datapath, heterogeneous, smallworld, large\n")
placer = example_clustered()
# Ask for visualization
try:
response = input("\nShow visualization? [y/N]: ").strip().lower()
if response == 'y':
demo_visualization(placer)
except (EOFError, KeyboardInterrupt):
pass
print("\nDone!")
if __name__ == "__main__":
main()