2023-05-05 05:51:30 +02:00
|
|
|
# See LICENSE for licensing information.
|
|
|
|
|
#
|
|
|
|
|
# Copyright (c) 2016-2023 Regents of the University of California, Santa Cruz
|
|
|
|
|
# All rights reserved.
|
|
|
|
|
#
|
2023-05-09 22:23:01 +02:00
|
|
|
import heapq
|
2023-05-29 06:25:11 +02:00
|
|
|
from copy import deepcopy
|
2023-05-05 05:51:30 +02:00
|
|
|
from openram import debug
|
2023-05-29 06:25:11 +02:00
|
|
|
from openram.base.vector import vector
|
2023-05-22 22:08:21 +02:00
|
|
|
from openram.base.vector3d import vector3d
|
2023-06-06 04:33:45 +02:00
|
|
|
from openram.tech import drc
|
2023-05-22 22:08:21 +02:00
|
|
|
from .direction import direction
|
2023-05-29 18:18:55 +02:00
|
|
|
from .hanan_node import hanan_node
|
2023-06-01 23:24:40 +02:00
|
|
|
from .hanan_probe import hanan_probe
|
2023-05-05 05:51:30 +02:00
|
|
|
|
|
|
|
|
|
2023-05-29 18:18:55 +02:00
|
|
|
class hanan_graph:
|
|
|
|
|
""" This is the Hanan graph created from the blockages. """
|
2023-05-05 05:51:30 +02:00
|
|
|
|
2023-05-22 22:08:21 +02:00
|
|
|
def __init__(self, router):
|
2023-05-09 22:23:01 +02:00
|
|
|
|
2023-05-29 21:43:43 +02:00
|
|
|
# This is the Hanan router that uses this graph
|
2023-05-22 22:08:21 +02:00
|
|
|
self.router = router
|
2023-06-01 23:24:40 +02:00
|
|
|
self.source_nodes = []
|
|
|
|
|
self.target_nodes = []
|
2023-05-05 05:51:30 +02:00
|
|
|
|
|
|
|
|
|
2023-06-01 23:24:40 +02:00
|
|
|
def inside_shape(self, point, shape):
|
|
|
|
|
""" Return if the point is inside the shape. """
|
2023-05-29 06:25:11 +02:00
|
|
|
|
2023-06-01 23:24:40 +02:00
|
|
|
# Check if they're on the same layer
|
|
|
|
|
if point.z != self.router.get_zindex(shape.lpp):
|
|
|
|
|
return False
|
|
|
|
|
# Check if the point is inside the shape
|
|
|
|
|
ll, ur = shape.rect
|
|
|
|
|
return shape.on_segment(ll, point, ur)
|
2023-05-29 06:25:11 +02:00
|
|
|
|
|
|
|
|
|
2023-05-31 05:09:10 +02:00
|
|
|
def is_probe_blocked(self, p1, p2):
|
|
|
|
|
"""
|
|
|
|
|
Return if a probe sent from p1 to p2 encounters a blockage.
|
|
|
|
|
The probe must be sent vertically or horizontally.
|
|
|
|
|
This function assumes that p1 and p2 are on the same layer.
|
|
|
|
|
"""
|
|
|
|
|
|
2023-06-01 23:24:40 +02:00
|
|
|
probe_shape = hanan_probe(p1, p2, self.router.vert_lpp if p1.z else self.router.horiz_lpp)
|
2023-05-31 05:09:10 +02:00
|
|
|
# Check if any blockage blocks this probe
|
|
|
|
|
for blockage in self.graph_blockages:
|
2023-06-01 23:24:40 +02:00
|
|
|
# Check if two shapes overlap
|
|
|
|
|
if blockage.overlaps(probe_shape):
|
2023-05-31 05:09:10 +02:00
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
2023-05-30 22:36:38 +02:00
|
|
|
def create_graph(self, source, target):
|
2023-05-29 06:25:11 +02:00
|
|
|
""" Create the Hanan graph to run routing on later. """
|
2023-05-30 22:36:38 +02:00
|
|
|
debug.info(0, "Creating the Hanan graph for source '{0}' and target'{1}'.".format(source, target))
|
2023-05-05 05:51:30 +02:00
|
|
|
|
2023-06-01 23:24:40 +02:00
|
|
|
self.source = source
|
|
|
|
|
self.target = target
|
|
|
|
|
|
2023-05-05 05:51:30 +02:00
|
|
|
# Find the region to be routed and only include objects inside that region
|
2023-05-30 22:36:38 +02:00
|
|
|
region = deepcopy(source)
|
2023-06-04 17:46:59 +02:00
|
|
|
region.bbox([target])
|
|
|
|
|
region = region.inflated_pin(multiple=1)
|
2023-05-29 06:25:11 +02:00
|
|
|
debug.info(0, "Routing region is {}".format(region.rect))
|
2023-05-05 05:51:30 +02:00
|
|
|
|
2023-05-22 22:08:21 +02:00
|
|
|
# Find the blockages that are in the routing area
|
|
|
|
|
self.graph_blockages = []
|
2023-06-05 20:27:05 +02:00
|
|
|
for blockage in self.get_blockages(source.name):
|
2023-06-04 17:46:59 +02:00
|
|
|
# Set the region's lpp to current blockage's lpp so that the
|
|
|
|
|
# overlaps method works
|
|
|
|
|
region.lpp = blockage.lpp
|
2023-05-29 06:25:11 +02:00
|
|
|
if region.overlaps(blockage):
|
2023-05-22 22:08:21 +02:00
|
|
|
self.graph_blockages.append(blockage)
|
|
|
|
|
debug.info(0, "Number of blockages detected in the routing region: {}".format(len(self.graph_blockages)))
|
|
|
|
|
|
2023-05-30 22:36:38 +02:00
|
|
|
# Create the Hanan graph
|
2023-06-01 23:24:40 +02:00
|
|
|
x_values, y_values = self.generate_cartesian_values()
|
2023-05-30 22:36:38 +02:00
|
|
|
self.generate_hanan_nodes(x_values, y_values)
|
|
|
|
|
self.remove_blocked_nodes()
|
|
|
|
|
debug.info(0, "Number of nodes in the routing graph: {}".format(len(self.nodes)))
|
|
|
|
|
|
|
|
|
|
|
2023-06-05 20:27:05 +02:00
|
|
|
def get_blockages(self, pin_name):
|
|
|
|
|
"""
|
|
|
|
|
Return all blockages for this routing region, including pins with
|
|
|
|
|
different name.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# Create a copy of blockages
|
|
|
|
|
blockages = self.router.blockages[:]
|
|
|
|
|
# Create a copy of pins with different name than the routed pins
|
2023-06-06 04:33:45 +02:00
|
|
|
offset = self.router.layer_widths[0] / 2
|
|
|
|
|
for name, pins in self.router.pins.items():
|
2023-06-05 20:27:05 +02:00
|
|
|
if name == pin_name:
|
|
|
|
|
continue
|
|
|
|
|
for pin in pins:
|
2023-06-06 04:33:45 +02:00
|
|
|
blockages.append(deepcopy(pin).inflated_pin(spacing=offset, multiple=1))
|
2023-06-05 20:27:05 +02:00
|
|
|
return blockages
|
|
|
|
|
|
|
|
|
|
|
2023-06-01 23:24:40 +02:00
|
|
|
def generate_cartesian_values(self):
|
2023-05-30 22:36:38 +02:00
|
|
|
"""
|
2023-06-04 17:46:59 +02:00
|
|
|
Generate x and y values from all the corners of the shapes in the
|
|
|
|
|
routing region.
|
2023-05-30 22:36:38 +02:00
|
|
|
"""
|
|
|
|
|
|
2023-05-30 06:49:00 +02:00
|
|
|
x_values = set()
|
|
|
|
|
y_values = set()
|
2023-05-30 22:36:38 +02:00
|
|
|
|
2023-06-04 17:46:59 +02:00
|
|
|
# Add the source and target values
|
2023-06-01 23:24:40 +02:00
|
|
|
for shape in [self.source, self.target]:
|
2023-05-29 06:25:11 +02:00
|
|
|
aspect_ratio = shape.width() / shape.height()
|
|
|
|
|
# If the pin is tall or fat, add two points on the ends
|
|
|
|
|
if aspect_ratio <= 0.5: # Tall pin
|
2023-06-06 04:33:45 +02:00
|
|
|
points = [shape.uc(), shape.bc()]
|
2023-05-29 06:25:11 +02:00
|
|
|
elif aspect_ratio >= 2: # Fat pin
|
2023-06-06 04:33:45 +02:00
|
|
|
points = [shape.lc(), shape.rc()]
|
2023-05-29 06:25:11 +02:00
|
|
|
else: # Square-like pin
|
2023-06-06 04:33:45 +02:00
|
|
|
points = [shape.center()]
|
2023-05-29 21:43:43 +02:00
|
|
|
for p in points:
|
2023-05-30 06:49:00 +02:00
|
|
|
x_values.add(p.x)
|
|
|
|
|
y_values.add(p.y)
|
2023-05-30 22:36:38 +02:00
|
|
|
|
2023-05-29 06:25:11 +02:00
|
|
|
# Add corners for blockages
|
2023-06-06 04:33:45 +02:00
|
|
|
offset = drc["grid"]
|
2023-05-22 22:08:21 +02:00
|
|
|
for blockage in self.graph_blockages:
|
|
|
|
|
ll, ur = blockage.rect
|
2023-06-06 04:33:45 +02:00
|
|
|
# Add minimum offset to the blockage corner nodes to prevent overlaps
|
2023-05-30 06:49:00 +02:00
|
|
|
x_values.update([ll.x - offset, ur.x + offset])
|
|
|
|
|
y_values.update([ll.y - offset, ur.y + offset])
|
|
|
|
|
|
|
|
|
|
# Sort x and y values
|
|
|
|
|
x_values = list(x_values)
|
|
|
|
|
y_values = list(y_values)
|
|
|
|
|
x_values.sort()
|
|
|
|
|
y_values.sort()
|
2023-05-22 22:08:21 +02:00
|
|
|
|
2023-05-30 22:36:38 +02:00
|
|
|
return x_values, y_values
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generate_hanan_nodes(self, x_values, y_values):
|
|
|
|
|
"""
|
|
|
|
|
Generate all Hanan nodes using the cartesian values and connect the
|
|
|
|
|
orthogonal neighbors.
|
|
|
|
|
"""
|
|
|
|
|
|
2023-05-30 06:49:00 +02:00
|
|
|
y_len = len(y_values)
|
2023-06-04 17:46:59 +02:00
|
|
|
left_offset = -(y_len * 2)
|
2023-05-30 06:49:00 +02:00
|
|
|
self.nodes = []
|
2023-05-22 22:08:21 +02:00
|
|
|
for x in x_values:
|
|
|
|
|
for y in y_values:
|
2023-05-30 06:49:00 +02:00
|
|
|
below_node = hanan_node([x, y, 0])
|
|
|
|
|
above_node = hanan_node([x, y, 1])
|
2023-05-31 05:09:10 +02:00
|
|
|
|
2023-05-30 06:49:00 +02:00
|
|
|
# Connect these two neighbors
|
|
|
|
|
below_node.add_neighbor(above_node)
|
2023-05-31 05:09:10 +02:00
|
|
|
|
2023-06-04 17:46:59 +02:00
|
|
|
# Find potential orthogonal neighbor nodes
|
2023-05-31 05:09:10 +02:00
|
|
|
belows = []
|
|
|
|
|
aboves = []
|
2023-05-30 06:49:00 +02:00
|
|
|
count = len(self.nodes) // 2
|
|
|
|
|
if count % y_len: # Down
|
2023-05-31 05:09:10 +02:00
|
|
|
belows.append(-2)
|
|
|
|
|
aboves.append(-1)
|
2023-05-30 06:49:00 +02:00
|
|
|
if count >= y_len: # Left
|
2023-06-04 17:46:59 +02:00
|
|
|
belows.append(left_offset)
|
|
|
|
|
aboves.append(left_offset + 1)
|
2023-05-31 05:09:10 +02:00
|
|
|
|
|
|
|
|
# Add these connections if not blocked by a blockage
|
|
|
|
|
for i in belows:
|
|
|
|
|
node = self.nodes[i]
|
|
|
|
|
if not self.is_probe_blocked(below_node.center, node.center):
|
|
|
|
|
below_node.add_neighbor(node)
|
|
|
|
|
for i in aboves:
|
|
|
|
|
node = self.nodes[i]
|
|
|
|
|
if not self.is_probe_blocked(above_node.center, node.center):
|
|
|
|
|
above_node.add_neighbor(node)
|
|
|
|
|
|
2023-06-01 23:24:40 +02:00
|
|
|
# Save source and target nodes
|
|
|
|
|
for node in [below_node, above_node]:
|
|
|
|
|
if self.inside_shape(node.center, self.source):
|
|
|
|
|
self.source_nodes.append(node)
|
|
|
|
|
elif self.inside_shape(node.center, self.target):
|
|
|
|
|
self.target_nodes.append(node)
|
|
|
|
|
|
2023-05-30 06:49:00 +02:00
|
|
|
self.nodes.append(below_node)
|
|
|
|
|
self.nodes.append(above_node)
|
2023-05-22 22:08:21 +02:00
|
|
|
|
2023-05-30 22:36:38 +02:00
|
|
|
|
|
|
|
|
def remove_blocked_nodes(self):
|
|
|
|
|
""" Remove the Hanan nodes that are blocked by a blockage. """
|
|
|
|
|
|
2023-05-30 20:10:34 +02:00
|
|
|
for i in range(len(self.nodes) - 1, -1, -1):
|
|
|
|
|
node = self.nodes[i]
|
2023-05-30 06:49:00 +02:00
|
|
|
point = node.center
|
2023-05-22 22:08:21 +02:00
|
|
|
for blockage in self.graph_blockages:
|
2023-05-31 05:09:10 +02:00
|
|
|
# Remove if the node is inside a blockage
|
2023-06-01 23:24:40 +02:00
|
|
|
if self.inside_shape(point, blockage):
|
2023-05-30 06:49:00 +02:00
|
|
|
node.remove_all_neighbors()
|
|
|
|
|
self.nodes.remove(node)
|
2023-05-22 22:08:21 +02:00
|
|
|
break
|
2023-05-09 22:23:01 +02:00
|
|
|
|
|
|
|
|
|
2023-06-01 23:24:40 +02:00
|
|
|
def find_shortest_path(self):
|
2023-05-09 22:23:01 +02:00
|
|
|
"""
|
|
|
|
|
Find the shortest path from the source node to target node using the
|
|
|
|
|
A* algorithm.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# Heuristic function to calculate the scores
|
2023-05-29 06:25:11 +02:00
|
|
|
def h(node):
|
2023-06-01 23:24:40 +02:00
|
|
|
""" Return the estimated distance to the closest target. """
|
2023-05-29 06:25:11 +02:00
|
|
|
min_dist = float("inf")
|
2023-06-01 23:24:40 +02:00
|
|
|
for t in self.target_nodes:
|
2023-05-29 06:25:11 +02:00
|
|
|
dist = t.center.distance(node.center) + abs(t.center.z - node.center.z)
|
|
|
|
|
if dist < min_dist:
|
|
|
|
|
min_dist = dist
|
|
|
|
|
return min_dist
|
|
|
|
|
|
|
|
|
|
# Initialize data structures to be used for A* search
|
2023-05-09 22:23:01 +02:00
|
|
|
queue = []
|
|
|
|
|
close_set = set()
|
|
|
|
|
came_from = {}
|
|
|
|
|
g_scores = {}
|
|
|
|
|
f_scores = {}
|
|
|
|
|
|
2023-05-29 06:25:11 +02:00
|
|
|
# Initialize score values for the source nodes
|
2023-06-01 23:24:40 +02:00
|
|
|
for node in self.source_nodes:
|
2023-05-29 06:25:11 +02:00
|
|
|
g_scores[node.id] = 0
|
|
|
|
|
f_scores[node.id] = h(node)
|
|
|
|
|
heapq.heappush(queue, (f_scores[node.id], node.id, node))
|
2023-05-09 22:23:01 +02:00
|
|
|
|
|
|
|
|
# Run the A* algorithm
|
|
|
|
|
while len(queue) > 0:
|
|
|
|
|
# Get the closest node from the queue
|
|
|
|
|
current = heapq.heappop(queue)[2]
|
|
|
|
|
|
2023-06-04 17:46:59 +02:00
|
|
|
# Skip this node if already discovered
|
2023-05-09 22:23:01 +02:00
|
|
|
if current in close_set:
|
|
|
|
|
continue
|
|
|
|
|
close_set.add(current)
|
|
|
|
|
|
|
|
|
|
# Check if we've reached the target
|
2023-06-01 23:24:40 +02:00
|
|
|
if current in self.target_nodes:
|
2023-05-09 22:23:01 +02:00
|
|
|
path = []
|
|
|
|
|
while current.id in came_from:
|
|
|
|
|
path.append(current)
|
|
|
|
|
current = came_from[current.id]
|
|
|
|
|
path.append(current)
|
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
# Update neighbor scores
|
|
|
|
|
for node in current.neighbors:
|
2023-05-23 03:16:49 +02:00
|
|
|
tentative_score = current.get_edge_cost(node) + g_scores[current.id]
|
2023-05-09 22:23:01 +02:00
|
|
|
if node.id not in g_scores or tentative_score < g_scores[node.id]:
|
|
|
|
|
came_from[node.id] = current
|
|
|
|
|
g_scores[node.id] = tentative_score
|
|
|
|
|
f_scores[node.id] = tentative_score + h(node)
|
|
|
|
|
heapq.heappush(queue, (f_scores[node.id], node.id, node))
|
|
|
|
|
|
|
|
|
|
# Return None if not connected
|
|
|
|
|
return None
|