Presentation is loading. Please wait.

Presentation is loading. Please wait.

ANALYSIS AND IMPLEMENTATION OF GRAPH COLORING ALGORITHMS FOR REGISTER ALLOCATION By, Sumeeth K. C Vasanth K.

Similar presentations


Presentation on theme: "ANALYSIS AND IMPLEMENTATION OF GRAPH COLORING ALGORITHMS FOR REGISTER ALLOCATION By, Sumeeth K. C Vasanth K."— Presentation transcript:

1 ANALYSIS AND IMPLEMENTATION OF GRAPH COLORING ALGORITHMS FOR REGISTER ALLOCATION By, Sumeeth K. C Vasanth K

2 Contents Introduction Aim of the Project Register Allocaton via Graph Coloring Design Implementation Testing Results and Observations

3 Introduction Register allocation consists of building the interference graph and attempting to find a k-coloring of the graph where k is the number of registers available. The existing solution is a Chaitin-style register allocation via graph coloring. Merging of two nodes can be used to enhance the Chaitin's graph coloring algorithm so that coloring algorithm can continue.

4 Aim of the Project To understand the concept of Register Allocation in Compilers and the application of Graph Coloring in Register Allocation. To implement the Node Merging Algorithm to enhance the Graph Coloring Register Allocation. To implement the various Graph Coloring Algorithms and compare them.

5 Register Allocation via Graph Coloring Register Allocation Variables may be stored in the main memory or in registers. The goal of register allocation is to decrease the number of memory accesses by keeping as many as possible variables in registers. Register Allocation decides which values to keep in registers and which in memory.

6 Register Allocation via Graph Coloring Register Allocation Simultaneously alive variables cannot be allocated to the same register. Variables whose life times do not overlap can be allocated to the same register. These constraints can be represented as an interference graph where nodes are variables.

7 Register Allocation via Graph Coloring Graph Coloring A graph G is k-colorable if its nodes can be labeled with integers 1... k so that no edge in G connects two nodes with the same label. Vertex Coloring assigns colors to the vertices such that no two adjacent vertices get the same color. Edge Coloring assigns a color to each edge so that no two adjacent edges share the same color.

8 Register Allocation via Graph Coloring Graph Coloring Register Allocation Use of graph coloring to model the register allocation problem. - Build an interference graph. - Live ranges are nodes in the graph. - Edges between nodes indicate that they cannot share a physical register. The nodes interfere. - Each color represents a physical register. - Neighbor nodes cannot share the same color.

9 Design Overview The project is concerned with the implementation of various graph coloring algorithms and testing them on the interference graph database and on random graphs. The graph coloring algorithms all work on the adjacency matrix created either from interference graphs or randomly created graphs.

10 Design The following algorithms implemented in this project. Graph Coloring by Node Merging. Recursive Largest First Coloring. Incidence Degree Ordering. First Fit Algorithm. Degree Based Graph Coloring.

11 Design Node Merging Algorithm The node merging algorithm performs merging of two unconnected nodes together into a single node. This effectively means that the merged nodes will get the same color in the final graph. This translates to the fact that those two nodes can be assigned the same register.

12 Design Node Merging Algorithm Calculate candidate list containing the node pairs to be merged. Sort the candidate list based on their ratio of the number of common neighbors to the total number of neighbors of the smaller degree node. Merge the node-pairs in candidate list. When the nodes are merged, the degree of the nodes which are adjaent to the merged nodes is decreased.

13 Design First Fit Algorithm This is the simplest and faster heuristics available for graph coloring problem. In this algorithm we try to color any vertex with first available color, starting from any arbitrary vertex. Advantages are simplicity and speed. But experiment results prove that, it gives inferior results compared to algorithms based on degree ordering.

14 Design Recursive Largest First Coloring The recursive largest first (rlf) algorithm is a greedy algorithm that colors the graph one color at a time, coloring as many vertices the same color as possible, then moving on to the next color. Initially start the coloring from an arbitrary vertex. Color this vertex color i. Color its first non-adjacent node with color i and recursively continue with this node as the initial node.

15 Design Saturation Degree Algorithm This is a sequential coloring algorithm with dynamically established order of vertices. The degree of saturation of a vertex X is the number of different colors at the vertices adjacent to X. This algorithm starts by assigning color 1 to a vertex of maximal degree. The next vertex to be colored in the sequential coloring procedure is the vertex with maximal degree.

16 Design Incidence Degree Algorithm This algorithm makes use of incidence degree of nodes of graph which is number of its colored neighbours. Coloring is started from first node and when more than one color is available, the color of the node that has maximum incidence degree is chosen for the node. When a node cannot be colored by any of already used color, a new color has to be used to color the node.

17 Design Degree based algorithm Degree of a node is defined as number of edges converging on that node which is same as its number of neighbors. Nodes of graph are sorted in decreasing order of their degrees. Coloring starts from first node of the graph, and when a node is colored with a new color, algorithm tries to color all its uncolored neighbors with same color.

18 Implementation First Fit Algorithm This is the simplest and faster heuristics available for graph coloring problem. In this algorithm we try to color any vertex with first available color, starting from any arbitrary vertex. The design of the algorithm is as follows -Initialize an array, which at any point in time contains all colored nodes to null. Let adj be the adjacency matrix of the graph.

19 Implementation First Fit Alogrithm For each node i in graph if i is uncolored for each node j in list arr if no neighbor of i has same color as j color[i]=color[j] add i to list arr and increment count. count gives chromatic number of graph.

20 Implementation Recursive Largest First Algorithm The Recursive Largest First algorithm colors as many vertices with one color as possible before moving on to the next color. Whenever a node is colored, algorithm checks whether its non-neighbors can be colored by same color. Recursion stops when no non-neighbor node of a colored node can be colored with color of that node.

21 Implementation Recursive Largest First Algorithm The pseudo code is as follows colorCount = 1; for each node i in graph if it is uncolored coloring(i)‏ coloring(n)‏ for each neighbor i of n if i is uncolored color[i] = colorCount coloring(i)‏

22 Implementation Node Merging Algorithm Given G = (V, E). Compute the canditate list of node pairs to be merged For each v1 in V For each v2 in V If there is no edge between v1 and v2 v1 and v2 are merge canditates Calculate cn = number of common neighbours of v1 and v2 Calculate ni = number of neighbours of the node in the node pair that has the least number of neighbours. Calculate ratio = cn / ni.

23 Implementation Node Merging Algorithm Sort the node pairs in the decreasing order of their ratio. Merge the node pairs v1 and v2 For each v in V If there is an edge between v and v1 or v and v2 add an edge between v and v1, and v and v2 Update the adjacency matrix. Calculate the canditate list again until no node pairs are found as merge canditates.

24 Implementation Incidence Degree Algorithm Given G = (V, E). Initially Color = 0. For each v1 in V For each v2 in list of already colored nodes If there v1 and v2 are unconnected get the color of v2 For each v in V if v & v1 are adjacent & color of v is the color of v2 consider the next node in the list of colored nodes and repeat the steps again. else color the node with the color of v2.

25 Implementation Incidence Degree Algorithm Find the node with the maximum incidence degree to be colored next. Add the node to the list of already ccolored nodes. Increase the color number and repeat the steps again till all nodes are colored.

26 Implementation Degree Based Algorithm Given G = (V,E). Compute Degree(v) for all v in V. Set uncolored =V sorted in decreasing order Degree(v). set currentColor = 0. while there are uncolored nodes: set A = first element of uncolored remove A from uncolored set Color(A) = currentColor

27 Implementation Degree Based Algorithm Set coloredWithCurrent = {A} for each v in uncolored if v is not adjacent to anything in coloredWithCurrent set Color(v)=currentColor. add v to currentColor. remove v from uncolored. end if end for currentColor = currentColor + 1. end while.

28 Testing Procedure In order to compare the effectiveness of the node merging algorithm and other implemented algorithms, we used Appel's database of 27,921 graphs that were produced from an actual compiler. We have implemented a random graph generator which when given the number of nodes, generates the graph. This gives varied graphs each time even if the number of nodes are the same.

29 Testing The test cases were classified based on the number of nodes in the graphs. The criteria for comparison between the algorithms implemented are the number of colors used to color the graphs and the time taken to color them. We also used random graphs that have different number of nodes and densities.

30 Testing Test Cases The algorithms are tested on Interference Graphs. Random Graphs. User given inputs.

31 Results and Observations Graphs of size within 50.

32 Results and Observation Graph size between 50 - 100

33 Results and Observation Graph size between 100 - 200

34 Results and Observation Graphs size above 200

35 Results and Observation The tables show the following findings: Node Merging gives an equal coloring compared to other algorithms in its first implementation itself. From these results we can expect that the node merging algorithm can give a better coloring for the graphs if heuristics for selection of node pairs for merging can be applied and the algorithm profiled.

36 Conclusion It appears to us that using node merging to enhance Chaitin's algorithm would improve the Chaitin's algorithm for coloring the graphs. Our first attempt at the node merging heuristic resulted in the algorithm giving the coloring which was essentially the same when compared to other graph coloring algorithms. We conclude from this that it is possible to improve the algorithm by employing different heuristics.

37 THANK YOU


Download ppt "ANALYSIS AND IMPLEMENTATION OF GRAPH COLORING ALGORITHMS FOR REGISTER ALLOCATION By, Sumeeth K. C Vasanth K."

Similar presentations


Ads by Google