[pypy-svn] r73735 - in pypy/branch/blackhole-improvement/pypy/tool/algo: . test
arigo at codespeak.net
arigo at codespeak.net
Wed Apr 14 14:04:23 CEST 2010
Author: arigo
Date: Wed Apr 14 14:04:22 2010
New Revision: 73735
Modified:
pypy/branch/blackhole-improvement/pypy/tool/algo/color.py
pypy/branch/blackhole-improvement/pypy/tool/algo/test/test_color.py
Log:
Find an optimal graph coloring. The result is not optimal with respect
to the number of copies potentially needed, though.
Modified: pypy/branch/blackhole-improvement/pypy/tool/algo/color.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/tool/algo/color.py (original)
+++ pypy/branch/blackhole-improvement/pypy/tool/algo/color.py Wed Apr 14 14:04:22 2010
@@ -1,9 +1,4 @@
"""
-the whole algorithm is based on the following paper:
-http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.86.1578&rep=rep1&type=pdf
-
-and the source code of libFIRM (file ir/be/becopyheur.c):
-http://pp.info.uni-karlsruhe.de/firm/Main_Page
"""
@@ -58,13 +53,18 @@
seen.add(v)
return result
-
-##class Unit(object):
-## """An optimization unit. Represents a phi instruction."""
-## def __init__(self, result, args):
-## self.result = result
-## self.args = args
-
-## def optimize(self, depgraph):
-## self.queue = []
-## self.insert_qnode(QNode(...
+ def find_node_coloring(self):
+ """Return a random minimal node coloring, assuming that
+ the graph is chordal."""
+ result = {}
+ for v in self.lexicographic_order():
+ forbidden = 0 # bitset
+ for n in self.neighbours[v]:
+ if n in result:
+ forbidden |= (1 << result[n])
+ # find the lowest 0 bit
+ num = 0
+ while forbidden & (1 << num):
+ num += 1
+ result[v] = num
+ return result
Modified: pypy/branch/blackhole-improvement/pypy/tool/algo/test/test_color.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/tool/algo/test/test_color.py (original)
+++ pypy/branch/blackhole-improvement/pypy/tool/algo/test/test_color.py Wed Apr 14 14:04:22 2010
@@ -25,3 +25,10 @@
'cabde', 'cabed', 'cadbe', 'cadeb', 'caebd', 'caedb',
]
assert dg.size_of_largest_clique() == 3
+ coloring = dg.find_node_coloring()
+ assert len(coloring) == 5
+ assert sorted(coloring.keys()) == list('abcde')
+ assert set(coloring.values()) == set([0, 1, 2])
+ for v1, v2list in dg.neighbours.items():
+ for v2 in v2list:
+ assert coloring[v1] != coloring[v2]
More information about the Pypy-commit
mailing list