Less clever and more useful colouring

This commit is contained in:
Robin Leroy
2025-01-01 23:13:52 +01:00
parent 9040757e91
commit 4526baa6ae

View File

@@ -2,7 +2,7 @@
import copy import copy
import time import time
from itertools import chain from itertools import chain, groupby
from multiprocessing import Manager, Process, cpu_count from multiprocessing import Manager, Process, cpu_count
import networkx as nx import networkx as nx
@@ -520,11 +520,6 @@ class KernelDG(nx.DiGraph):
lcd_line_numbers = {} lcd_line_numbers = {}
for dep in lcd: for dep in lcd:
lcd_line_numbers[dep] = [x.line_number for x, lat in lcd[dep]["dependencies"]] lcd_line_numbers[dep] = [x.line_number for x, lat in lcd[dep]["dependencies"]]
# add color scheme
graph.graph["node"] = {"colorscheme": "spectral9"}
graph.graph["edge"] = {"colorscheme": "spectral9"}
min_color = 2
available_colors = 8
# create LCD edges # create LCD edges
for dep in lcd_line_numbers: for dep in lcd_line_numbers:
@@ -566,79 +561,51 @@ class KernelDG(nx.DiGraph):
graph.edges[e]["penwidth"] = 3 graph.edges[e]["penwidth"] = 3
# Color the cycles created by loop-carried dependencies, longest first, never recoloring # Color the cycles created by loop-carried dependencies, longest first, never recoloring
# any node, so that the longest LCD and most long chains that are involved in the loop are # any node or edge, so that the longest LCD and most long chains that are involved in the
# legible. # loop are legible.
for i, dep in enumerate(sorted(lcd, key=lambda dep: -lcd[dep]["latency"])): lcd_by_latencies = sorted(
# For cycles that are broken by already-colored (longer) cycles, the color need not be (
# the same for each yet-uncolored arc. (latency, list(deps))
# Do not use the same color for such an arc as for the cycles that delimit it. This is for latency, deps in groupby(lcd, lambda dep: lcd[dep]["latency"])
# always possible with 3 colors, as each arc is only adjacent to the preceding and ),
# following interrupting cycles. reverse=True
# Since we color edges as well as nodes, there would be room for a more interesting )
# graph coloring problem: we could avoid having unrelated arcs with the same color node_colors = {}
# meeting at the same vertex, and retain the same color between arcs of the same cycle edge_colors = {}
# that are interrupted by a single vertex. We mostly ignore this problem. colors_used = 0
for i, (latency, deps) in enumerate(lcd_by_latencies):
# The longest cycle will always have color 1, the second longest cycle will always have color = None
# color 2 except where it overlaps with with the longest cycle, etc.; for arcs that are for dep in deps:
# part of short cycles, the colors will be less predictable. path = lcd_line_numbers[dep]
default_color = min_color + i % available_colors for n in path:
arc = [] if n not in node_colors:
arc_source = lcd_line_numbers[dep][-1] if not color:
arcs = [] color = colors_used + 1
for n in lcd_line_numbers[dep]: colors_used += 1
if "fillcolor" in graph.nodes[n]: node_colors[n] = color
arcs.append((arc, (arc_source, n))) for u, v in zip(path, path[1:] + [path[0]]):
arc = [] if (u, v) not in edge_colors:
arc_source = n # Dont introduce a color just for an edge.
else: if not color:
arc.append(n) color = colors_used
if not arcs: # Unconstrained cycle. edge_colors[u, v] = color
arcs.append((arc, tuple())) max_color = min(11, colors_used)
colorscheme = f"spectral{max(3, max_color)}"
graph.graph["node"] = {"colorscheme" : colorscheme}
graph.graph["edge"] = {"colorscheme" : colorscheme}
for n, color in node_colors.items():
if "style" not in graph.nodes[n]:
graph.nodes[n]["style"] = "filled"
else: else:
arcs.append((arc, (arc_source, lcd_line_numbers[dep][0]))) graph.nodes[n]["style"] += ",filled"
# Try to color the whole cycle with its default color, then with a single color, then graph.nodes[n]["fillcolor"] = color
# with different colors by arc, preferring the default. if (max_color >= 4 and color == 1) or (max_color >= 10 and color in (1, 2, max_color)):
forbidden_colors = set( graph.nodes[n]["fontcolor"] = "white"
graph.nodes[n]["fillcolor"] for arc, extremities in arcs for n in extremities for (u, v), color in edge_colors.items():
if "fillcolor" in graph.nodes[n] # The backward edge of the cycle is represented as the corresponding forward
) # edge with the attribute dir=back.
global_color = None edge = graph.edges[u, v] if (u, v) in graph.edges else graph.edges[v, u]
if default_color not in forbidden_colors: edge["color"] = color
global_color = default_color
elif len(forbidden_colors) < available_colors:
global_color = next(
c for c in range(min_color, min_color + available_colors + 1)
if c not in forbidden_colors
)
for arc, extremities in arcs:
if global_color:
color = global_color
else:
color = default_color
while color in (graph.nodes[n].get("fillcolor") for n in extremities):
color = min_color + (color + 1) % available_colors
for n in arc:
if "style" not in graph.nodes[n]:
graph.nodes[n]["style"] = "filled"
else:
graph.nodes[n]["style"] += ",filled"
graph.nodes[n]["fillcolor"] = color
if extremities:
(source, sink) = extremities
else:
source = sink = arc[0]
arc = arc[1:]
for u, v in zip([source] + arc, arc + [sink]):
# The backward edge of the cycle is represented as the corresponding forward
# edge with the attribute dir=back.
edge = graph.edges[v, u] if (v, u) in graph.edges else graph.edges[u, v]
if arc:
if "color" in edge:
raise AssertionError(
f"Recoloring {u}->{v} in arc ({source}) {arc} ({sink}) of {dep}"
)
edge["color"] = color
# rename node from [idx] to [idx mnemonic] and add shape # rename node from [idx] to [idx mnemonic] and add shape
mapping = {} mapping = {}