Compare commits

..

3 commits

5 changed files with 4853 additions and 352 deletions

View file

@ -264,8 +264,146 @@ def combine_nodes(G, node1, node2, new_node):
return H
def convert_edge_list_to_highlight_dict(edge_list):
# Define a list of colors to assign to each connection
colors = ["red", "blue", "green", "orange", "purple", "cyan", "yellow", "pink"]
highlight_edges_with_colors = {}
connection_to_color = {}
# Iterate through the edge list
for connection, edge_start, edge_end in edge_list:
# Assign a color to the connection if not already assigned
if connection not in connection_to_color:
connection_to_color[connection] = colors[len(connection_to_color) % len(colors)]
# Add the edge to the dictionary with the assigned color
highlight_edges_with_colors[(edge_start, edge_end)] = connection_to_color[connection]
return highlight_edges_with_colors
# Draw the graph
from matplotlib.collections import LineCollection
def draw_angled_edges_torus(edge, pos, ax, edge_color="gray", linewidth=1,found_reverse=False):
lines = []
start = pos[edge[0]]
end = pos[edge[1]]
if abs(edge[0] - edge[1])==1:
if edge[0] in [0,4,8,12]:
offset_x = - 1.5
else:
offset_x = 0.5
offset_y = 0
else:
offset_x = 0
if edge[0] in [0,1,2,3]:
offset_y = -1.5
else:
offset_y = 0.5
if start[0] == end[0] or start[1] == end[1]: # Straight horizontal or vertical edge
# Create a single 90-degree turn
mid = (start[0], end[1]) # Midpoint with a 90-degree turn
lines.append([start, mid]) # First segment
lines.append([mid, end]) # Second segment
else:
# Create two 90-degree turns for non-straight edges
if abs(start[0] - end[0]) > abs(start[1] - end[1]): # Horizontal first
mid1 = (end[0]+offset_x, start[1]+offset_y) # Go past the node horizontally
mid2 = (end[0]+offset_x, end[1]+offset_y) # Come back vertically
else: # Vertical first
mid1 = (start[0]+offset_x, end[1]+offset_y) # Go past the node vertically
mid2 = (end[0]+offset_x, end[1]+offset_y) # Come back horizontally
lines.append([start, mid1]) # First segment
lines.append([mid1, mid2]) # Second segment
lines.append([mid2, end]) # Final segment
# Create a LineCollection for the edges
lc = LineCollection(lines, colors=edge_color, linewidths=linewidth)
ax.add_collection(lc)
if edge_color != "gray":
print(lines)
for line in lines:
(x0, y0), (x1, y1) = line
print(f"Edge: {edge}, Start: ({x0}, {y0}), End: ({x1}, {y1})")
if not found_reverse:
ax.annotate("",
xy=(x1, y1), xytext=(x0, y0),
arrowprops=dict(
arrowstyle="->",
color=edge_color,
lw=linewidth,
shrinkA=0, shrinkB=0 # optional: prevent shortening
))
else:
ax.annotate("",
xy=(x0, y0), xytext=(x1, y1),
arrowprops=dict(
arrowstyle="->",
color=edge_color,
lw=linewidth,
shrinkA=0, shrinkB=0
))
ax.autoscale()
ax.set_aspect('equal')
def draw_folded_torus_noc(mesh_size, G_NoC, highlight_edges_with_colors, title):
"""
Draws a folded torus NoC graph with angled edges and highlights specific edges with specified colors.
Parameters:
mesh_size (int): The size of the mesh.
G_NoC (networkx.DiGraph): The graph to draw.
highlight_edges_with_colors (dict): A dictionary where keys are edges (tuples) and values are colors (strings).
title (str): The title of the plot.
"""
pos = {}
for y in range(mesh_size):
for x in range(mesh_size):
if x % 2 == 0:
y_pos = y
else:
y_pos = y + 0.2
if y % 2 == 0:
x_pos = x
else:
x_pos = x + 0.2
node_number = coord_to_number(y, x, mesh_size)
pos[node_number] = (x_pos, y_pos)
G_NoC_no_duplicates = G_NoC.copy()
# Remove duplicate edges (edges going in both directions between the same pair of nodes)
edges_to_remove = []
for u, v in G_NoC_no_duplicates.edges():
if (G_NoC_no_duplicates.has_edge(v, u) and G_NoC_no_duplicates.has_edge(u, v) and ((u, v) not in edges_to_remove)):
edges_to_remove.append((v, u))
# Remove the identified edges from the new graph
G_NoC_no_duplicates.remove_edges_from(edges_to_remove)
plt.figure(figsize=(8, 8))
ax = plt.gca()
for edge in G_NoC_no_duplicates.edges():
# Check if the edge is in the highlight dictionary and get its color
found_reverse = False # Initialize a flag to track if the reverse edge was found
color = highlight_edges_with_colors.get(edge)
if color is None: # If the edge is not found
color = highlight_edges_with_colors.get(tuple(reversed(edge)), "gray")
if color != "gray": # If the reverse edge was found
found_reverse = True
draw_angled_edges_torus(edge, pos, ax, edge_color=color, linewidth=1,found_reverse=found_reverse)
nx.draw_networkx_nodes(G_NoC_no_duplicates, pos, node_color="lightblue", node_size=500)
nx.draw_networkx_labels(G_NoC_no_duplicates, pos, labels={node: node for node in G_NoC_no_duplicates.nodes()}, font_size=12, font_color="black")
plt.title(title, fontsize=16)
plt.axis('off')
plt.show()
# ======================================================================
# Create images for the NoC with the chosen paths highlighted

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
@ -40,7 +40,7 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
@ -146,7 +146,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [