2019-05-14 23:44:49 +02:00
|
|
|
import os, copy
|
2019-04-19 10:27:06 +02:00
|
|
|
from collections import defaultdict
|
|
|
|
|
|
|
|
|
|
import gdsMill
|
|
|
|
|
import tech
|
|
|
|
|
import math
|
|
|
|
|
import globals
|
|
|
|
|
import debug
|
|
|
|
|
from vector import vector
|
|
|
|
|
from pin_layout import pin_layout
|
2019-05-27 22:08:59 +02:00
|
|
|
|
2019-04-30 08:57:25 +02:00
|
|
|
class timing_graph():
|
2019-07-25 22:25:58 +02:00
|
|
|
"""
|
|
|
|
|
Implements a directed graph
|
|
|
|
|
Nodes are currently just Strings.
|
2019-04-30 08:57:25 +02:00
|
|
|
"""
|
2019-04-19 10:27:06 +02:00
|
|
|
|
|
|
|
|
def __init__(self):
|
2019-04-30 08:57:25 +02:00
|
|
|
self.graph = defaultdict(set)
|
2019-05-14 23:44:49 +02:00
|
|
|
self.all_paths = []
|
2019-04-19 10:27:06 +02:00
|
|
|
|
2019-04-30 08:57:25 +02:00
|
|
|
def add_edge(self, src_node, dest_node):
|
2019-04-19 10:27:06 +02:00
|
|
|
"""Adds edge to graph. Nodes added as well if they do not exist."""
|
2019-07-25 22:25:58 +02:00
|
|
|
|
2019-04-30 08:57:25 +02:00
|
|
|
src_node = src_node.lower()
|
|
|
|
|
dest_node = dest_node.lower()
|
|
|
|
|
self.graph[src_node].add(dest_node)
|
2019-04-19 10:27:06 +02:00
|
|
|
|
2019-04-30 08:57:25 +02:00
|
|
|
def add_node(self, node):
|
2019-04-19 10:27:06 +02:00
|
|
|
"""Add node to graph with no edges"""
|
2019-07-25 22:25:58 +02:00
|
|
|
|
2019-04-30 08:57:25 +02:00
|
|
|
node = node.lower()
|
|
|
|
|
if not node in self.graph:
|
|
|
|
|
self.graph[node] = set()
|
2019-04-19 10:27:06 +02:00
|
|
|
|
|
|
|
|
def remove_edges(self, node):
|
2019-07-25 22:25:58 +02:00
|
|
|
|
2019-04-19 10:27:06 +02:00
|
|
|
"""Helper function to remove edges, useful for removing vdd/gnd"""
|
2019-04-30 08:57:25 +02:00
|
|
|
node = node.lower()
|
|
|
|
|
self.graph[node] = set()
|
2019-04-19 10:27:06 +02:00
|
|
|
|
2019-07-25 22:25:58 +02:00
|
|
|
def get_all_paths(self, src_node, dest_node, remove_rail_nodes=True, reduce_paths=True):
|
2019-04-30 08:57:25 +02:00
|
|
|
"""Traverse all paths from source to destination"""
|
2019-07-25 22:25:58 +02:00
|
|
|
|
2019-04-30 08:57:25 +02:00
|
|
|
src_node = src_node.lower()
|
|
|
|
|
dest_node = dest_node.lower()
|
|
|
|
|
|
2019-07-25 22:25:58 +02:00
|
|
|
# Remove vdd and gnd by default
|
|
|
|
|
# Will require edits if separate supplies are implemented.
|
|
|
|
|
if remove_rail_nodes:
|
|
|
|
|
# Names are also assumed.
|
2019-04-30 08:57:25 +02:00
|
|
|
self.remove_edges('vdd')
|
|
|
|
|
self.remove_edges('gnd')
|
|
|
|
|
|
2019-04-19 10:27:06 +02:00
|
|
|
# Mark all the vertices as not visited
|
|
|
|
|
visited = set()
|
|
|
|
|
|
|
|
|
|
# Create an array to store paths
|
|
|
|
|
path = []
|
2019-05-14 23:44:49 +02:00
|
|
|
self.all_paths = []
|
2019-04-19 10:27:06 +02:00
|
|
|
|
|
|
|
|
# Call the recursive helper function to print all paths
|
2019-05-14 23:44:49 +02:00
|
|
|
self.get_all_paths_util(src_node, dest_node, visited, path)
|
2019-06-26 01:37:35 +02:00
|
|
|
debug.info(2, "Paths found={}".format(len(self.all_paths)))
|
2019-07-25 22:25:58 +02:00
|
|
|
|
|
|
|
|
if reduce_paths:
|
|
|
|
|
self.reduce_paths()
|
|
|
|
|
|
2019-05-14 23:44:49 +02:00
|
|
|
return self.all_paths
|
2019-07-25 22:25:58 +02:00
|
|
|
|
|
|
|
|
def reduce_paths(self):
|
|
|
|
|
""" Remove any path that is a subset of another path """
|
|
|
|
|
|
|
|
|
|
self.all_paths = [p1 for p1 in self.all_paths if not any(set(p1)<=set(p2) for p2 in self.all_paths if p1 is not p2)]
|
2019-05-14 23:44:49 +02:00
|
|
|
|
|
|
|
|
def get_all_paths_util(self, cur_node, dest_node, visited, path):
|
2019-04-30 08:57:25 +02:00
|
|
|
"""Recursive function to find all paths in a Depth First Search manner"""
|
2019-07-25 22:25:58 +02:00
|
|
|
|
2019-04-19 10:27:06 +02:00
|
|
|
# Mark the current node as visited and store in path
|
2019-04-30 08:57:25 +02:00
|
|
|
visited.add(cur_node)
|
|
|
|
|
path.append(cur_node)
|
2019-04-19 10:27:06 +02:00
|
|
|
|
|
|
|
|
# If current vertex is same as destination, then print
|
|
|
|
|
# current path[]
|
2019-04-30 08:57:25 +02:00
|
|
|
if cur_node == dest_node:
|
2019-05-14 23:44:49 +02:00
|
|
|
self.all_paths.append(copy.deepcopy(path))
|
2019-04-19 10:27:06 +02:00
|
|
|
else:
|
|
|
|
|
# If current vertex is not destination
|
2019-07-25 22:25:58 +02:00
|
|
|
# Recur for all the vertices adjacent to this vertex
|
2019-04-30 08:57:25 +02:00
|
|
|
for node in self.graph[cur_node]:
|
|
|
|
|
if node not in visited:
|
2019-05-14 23:44:49 +02:00
|
|
|
self.get_all_paths_util(node, dest_node, visited, path)
|
2019-04-19 10:27:06 +02:00
|
|
|
|
|
|
|
|
# Remove current vertex from path[] and mark it as unvisited
|
|
|
|
|
path.pop()
|
2019-04-30 08:57:25 +02:00
|
|
|
visited.remove(cur_node)
|
2019-04-19 10:27:06 +02:00
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
""" override print function output """
|
2019-07-25 22:25:58 +02:00
|
|
|
|
|
|
|
|
return "Nodes: {}\nEdges:{} ".format(list(self.graph), self.graph)
|