Content uploaded by Khaled Elleithy

Author content

All content in this area was uploaded by Khaled Elleithy

Content may be subject to copyright.

A SIMULATED ANNEALING ALGORITHM FOR REGISTER

ALLOCATION

K. M. Elleithy* and E. G. AbdelFattah**

* Computer Engineering Department, King Fahd University of Petroleum and Minerals, Dhahran, Saudi Arabia

** Computer Department, Moshmit Biherah Company, Alexnadria, Egypt

ABSTRACT: In this paper a simulated annealing algorithm for register allocation is

presented. The algorithm is based on the Metropolis procedure. The procedure

accepts a new solution with less profit based on a probabilistic function. The

objective of the heuristic is to color a graph representing the register allocation

problem to maximize the profit function. The profit function is defined using the

profit gained from coloring a subset of the nodes. The algorithm presented in the

paper has a linear time asymptotic complexity. The experimental results show

optimal solutions in many of the graphs used for testing.

1. INTRODUCTION

Register allocation may be viewed as a graph coloring problem, where coloring a graph is an

assignment of a color to each of its nodes such that two connected nodes have different

colors. Each node in the graph stands for a computed quantity that resides in a machine

register. Two nodes are connected by an edge if the quantities are simultaneously alive at

some point in the object program.

The graph coloring problem is NP-C problem. Exhaustive search approaches for coloring a

graph imply an exponential time complexity. Heuristics of reasonable time complexity are

required for large size problems. A number of factors can be considered in designing a graph

coloring heuristics, e.g., a vertex of high degree is harder to color than a vertex of low degree,

vertices with the same neighborhood should be colored alike, and coloring many vertices with

the same color is a good idea.

In the last decade simulated annealing (SA) has been used extensively in solving various

combinatorial problems, e.g., traveling salesman problem [1], computer generated

holograms[2], RF power efficiency[3], heat exchangers[4], and allocation[5]. SA is an

adaptive heuristic and belongs to the class of non-deterministic algorithms. Kirkpatrich,

Gelatt and Vecchi first introduced the heuristic in 1983 [6]. The name of Simulated Annealing

is inspirited from the process of carefully cooling molten metals in order to obtain a good

crystal structure. During annealing, a metal is heated to a very high temperature, where the

molecules will have a greater degree of freedom to move, and then slowly cooled. By cooling

the metal at a proper rate, atoms will have an increased chance to regain proper crystal

structure. The movement of molecules is analogous to the generation of new neighborhood in

an optimization process. The objective function is non-convex; this means that it has multiple

maxima. A greedy iterative improvement algorithm starts off with an initial solution

S

0

and

climbs along the curve to find a local maximum L. The only way to find the global maximum

G, is to slide the hill along the local maximum L, which is a characteristic of simulated

annealing algorithm.

In this paper a new non-deterministic algorithm based on simulated annealing is presented.

Experimental results are discussed in the paper.

2. THE ALGORITHM

In this section a simulated annealing algorithm for register allocation is presented. The

algorithm is based on the Metropolis procedure. The procedure accepts a new solution with

less profit based on a probabilistic function. The objective of the heuristic is to color a graph

representing the register allocation problem to maximize a profit function. The profit function

is defined using the profit gained from coloring a subset of the nodes. The algorithm

presented in the paper has a linear time asymptotic complexity.

The algorithm is shown in Figure 1. The core of the algorithm is the Metropolis procedure

given in Figure 2, which uses a procedure (NEWSOLUTION) to generate a local neighbor

new_solution for any given solution. If the profit of the new_solution is greater than the profit

of the current solution, then certainly the new_solution is acceptable. If the new_solution has

less profit, Metropolis will accept the new_solution on a probabilistic basis. A random

number is generated in the range 0 to 1. If this random number is smaller than

e

delta temperature/

, where delta is the difference in profits, then the inferior solution is

accepted.

The objective of the heuristic is to color a graph representing the allocation problem to

maximize a profit function. The profit function is defined as follows:

Profit

)()(

11

nodesuncoloredall

i

i

nodescoloredall

i

i

nodenetsavenodenetsave

(1)

netsave node

i

( )

: A function representing the profit gained from allocating a variable to a

register.

The heuristic proceeds as follows:

1- Find the set of largest profitable nodes. The number of nodes in this set is equal to the

number of available colors.

2- Color a new node from the uncolored set if it is possible. If there is no more nodes that

can be colored or time >= maxtime then stop.

3- Calculate the change in profit after adding the colored node from step 2.

4- If the change is positive then add the node of step 2 to the colored set, otherwise the node

is colored based on a probabilistic comparison.

5- Go to step 2.

Example

Apply the algorithm to the graph of Figure 3 using the following parameters:

No of nodes = 10 Alpha = 0.8

No of colors = 4 Beta = 1

Cooling Schedule = 1 Temperature = 100

Procedure Simulated Annealing (initial_solution, initial_temperature, alpha, beta, cooling_schedule,

Maxtime);

{alpha is the cooling rate < 1};

{beta is a constant > 1};

begin

temperature = initial_temperature;

solution = initial_solution;

time = 0;

repeat

call Metropolis (solution, temperature, cooling_schedule);

time = time + cooling_schedule;

temperature = alpha * temperature;

cooling_schedule = beta * cooling_schedule;

until (time >= Maxtime);

end {of Simulated Annealing}.

Figure 1: Procedure for Simulated Annealing Algorithm.

Procedure Metropolis (solution, temperature, cooling_schedule);

begin

repeat

call NEWSOLUTION;

delta = new_profit - old_profit

If ((delta > 0) or (random <

e

delta temperature/

)) then

solution = new_solution;

cooling_schedule = cooling_schedule - 1;

until (cooling_schedule = 0);

end {of Metropolis}.

Figure 2: The Metropolis Procedure.

The netsave for different nodes is as follows:

Node #

1

2

3

4

5

6

7

8

9

10

Netsave

10

-30

-10

40

20

-5

50

-15

20

55

The algorithm proceeds as follows:

Step 1 : The algorithm assigns the four available colors to the nodes which have maximum

netsave as follows :

v

10

is assigned color1

v

4

is assigned color3

v

7

is assigned color2

v

5

is assigned color4.

1

2

3

4

5

6

7

8

9

10

Figure 3: A Graph for Testing Coloring by Simulated Annealing.

Step2 : The least uncolored node is

v

1

, which has positive netsave, the first colored node is

node

v

4

with color3, this color can be assigned to

v

1

, since

v

1

is not connected to

v

4

.

Step3 : Although

v

2

has negative netsave, it is assigned color4 (it is not assigned color3, since

it is connected to

v

1

(color3)). This means that metropolis accepts the inferior solution (i.e.

the random number generated is less than

e

delta temperature/

).

Step4 :

v

3

can be colored by color2, but

v

3

has negative netsave. Metropolis rejects the

solution (i.e. the random number generated is greater than

e

delta temperature/

), that is

why

v

3

is not colored.

Step5 :

v

6

is assigned color2, although it has negative netsave. This means that metropolis

procedure accepts the inferior solution.

Step6 : No possible color is available for

v

8

, since it is connected to nodes colored by all

different available colors.

v

9

has positive netsave, it is assigned color1, since it is

connected to

v

4

(color3),

v

5

(color4), and

v

6

(color2).

Step7 : The best solution is the one obtained in step2 (assignment #1).

3. EXPERIMENTAL RESULTS

In this section we apply the simulated annealing algorithm for register allocation on a graph

of size 128. The graph used for this study is of random nature and random values for the

nodes' netsave are generated. The effect of different parameters on the proposed algorithm

performance is evaluated. The effect of the following parameters is considered: number of

colors, cooling-schedule, beta, temperature, and alpha.

3.1 Number of Colors

To evaluate the effect of number of colors on the profit gained from coloring the graph and

the number of trials to get the best solution, the number of colors has been changed from 10 to

20. The values for the parameters cooling-schedule, beta, temperature, and alpha are 1.0, 1.0,

100, and 0.8 respectively. Figure 4 shows the profit versus number of colors and Figure 5

shows the number of trials versus the number of colors.

Figure 4 The Profit Versus Number of

Colors.

Figure 5. The Number of Trials versus

the Number of Colors

The following observations are concluded:

1- The best solution achieved is 2965. This value is constant for colors greater than or equal

to 14.

2- While increasing the number of colors (i.e. greater than 14), the profit remains constant

but the number of trials decreases. The effect of increasing the number of colors is

reflected on decreasing the number of trials.

3- The graph can be colored using 14 colors only while achieving a netsave of 2965.

3.2 Cooling Schedule (m)

To evaluate the effect of cooling schedule on the profit gained from coloring the graph and

the number of trials to get the best solution, the cooling schedule has been changed from 1 to

20. The values for the parameters beta, temperature, and alpha are 1.0, 100, and 0.8

respectively. Figure 6 shows the profit versus cooling schedule and Figure 7 shows the

number of trials versus cooling schedule. The following is noticed:

1- The best solution is obtained at m = 1, and for m > 1 the solution may be less than or

equal to the solution at m = 1.

2- If the best solution remains constant while m is increased, the number of trials remains

constant as well.

3- If the solution decreases as m increases, the number of trials decreases as well.

4- The profit does not change with m if the number of colors is greater than a certain

threshold value (14 in this example).

5- As m increases there is a better chance for a negative solution to be accepted. This implies

that the solution may be worse or better than the previous solution. For example if the

number of colors is 10 and m is increased from 4 to 5, the profit drops from 2565 to 2415

and the number of trials drops from 107 to 99, and if the number of colors is 13 and m

increases from 9 to 10, the profit increases from 2685 to 2865.

2500

2550

2600

2650

2700

2750

2800

2850

2900

2950

3000

8 13 18 23

Profit

colors

105

107

109

111

113

115

117

8 13 18 23

Trials

Colors

Figure 6: The Profit versus Cooling Schedule.

Figure 7: The Number of Trials Versus Cooling Schedule.

3. 3 Beta

To evaluate the effect of beta on the profit gained from coloring the graph and the number

of trials to get the best solution, beta has been changed from 1 to 20. The values for the

parameters cooling-schedule, temperature, and alpha are 1.0, 100, and 0.8 respectively.

Figure 8 shows the profit versus Beta and Figure 9 shows the number of trials versus Beta.

The following is noticed:

2300

2400

2500

2600

2700

2800

2900

3000

0 5 10 15 20 25

Profit

M

10

11

12

13

14

15

20

96

98

100

102

104

106

108

110

112

114

116

0 5 10 15 20 25

Trials

M

10

11

12

13

14

15

20

No of

Colors

No of

Colors

1- The best solution is obtained at low values for beta. This value is equal to 1 except for

number of colors equal to 12 and 13, the best solution is obtained at beta equal to 3.

2- If the solution remains constant while beta is increased, the number of trials may decrease

slightly ( number of colors is 15 ), or remains constant ( number of colors is 20 ).

3- If the solution decreases as beta increases, the number of trials decreases as well.

4- The profit does not change with beta if the number of colors is greater than a certain

threshold value (14 in this example).

5- As beta increases there is a better chance for a negative solution to be accepted. This

implies that the solution may be worse or better than the previous solution. For example if

the number of colors is 11 and beta is increased from 1 to 2, the profit drops from 2705 to

2555 and the number of trials drops from 109 to 101, but if the number of colors is 12 and

beta increases from 2 to 3, the profit increases from 2695 to 2785.

Figure 8: The Profit versus Beta.

Figure 9: The Number of Trials Versus Beta.

3.4 Temperature

2300

2400

2500

2600

2700

2800

2900

3000

0 5 10 15 20 25

Profit

Beta

10

11

12

13

14

15

20

90

95

100

105

110

115

120

1

3

5

7

9

11

13

15

17

19

Trials

Beta

10

11

12

13

14

15

20

No of

Colors

No of

Colors

To evaluate the effect of temperature on the profit gained from coloring the graph and the

number of trials to get the best solution, the temperature has been changed from 1 to 100. The

values for the parameters cooling-schedule, beta, and alpha are 1.0, 1.0, and 0.8 respectively

Figure 10 shows the profit versus temperature and Figure 11 shows the number of trials

versus temperature The following was noticed :

1- The best solution is obtained at low values of temperature.

2- If the solution remains constant while temperature is increased, the number of trials

remains constant as well.

3- If the solution decreases as temperature increases, the number of trials decreases as well.

4- The profit does not change with temperature if the number of colors is greater than or

equal to a certain threshold value (14 in this example).

5- As temperature increases there is a better chance for a negative solution to be accepted.

This implies that the solution may be worse or better than the previous solution. If the

number of colors is equal to 11 and temperature is increased from 10 to 20 the profit

increases from 2885 to 2895, but if the temperature is increased from 20 to 30 the profit

drops from 2895 to 2835.

3.5 Alpha

To evaluate the effect of alpha on the profit gained from coloring the graph and the number of

trials to get the best solution, alpha has been changed from 0.1 to 0.99 The values for the

parameters cooling-schedule, beta, and temperature are 1.0, 1.0, and 100 respectively. Figure

12 and 13 show the profit versus Alpha and Figure 14 shows the number of trials versus

Alpha. The following is noticed :

1- The best solution is obtained at low values for alpha. This value is less than or equal to

0.6, except for number of colors equal to 11 the best solution is obtained at alpha in the

range 0.2 to 0.6.

2- If the solution remains constant while alpha is increased, the number of trials remains

constant as well.

3- If the solution decreases as alpha increases, the number of trials decreases as well.

4- The profit does not change with alpha if the number of colors is greater than or equal to

14, except for the values of alpha in the range .95 to .99 where a significant change is

noticed.

5- As alpha decreases there is no chance that a negative solution is accepted. That explains

why the solution remains constant for alpha less than or equal to 0.6.

6- The profit achieved is very sensitive for values of alpha greater than 0.95.

Figure 10: The Profit Versus Temperature.

2500

2600

2700

2800

2900

3000

0 10 20 30 40 50 60 70 80 90 100 110

Profit

Temperature

10

11

12

13

14

15

20

No of

Colors

Figure 11: The Number of Trials versus Temperature

Figure 12: The Profit versus Alpha.

Figure 13: The Profit Versus Alpha (Alpha > 0.9 )

100

102

104

106

108

110

112

114

116

118

0 10 20

Trials

Temperature

10

11

12

13

14

15

20

2000

2200

2400

2600

2800

3000

3200

0 0.2 0.4 0.6 0.8 1 1.2

Profit

Alpha

10

11

12

13

14

15

20

2200

2300

2400

2500

2600

2700

2800

2900

3000

3100

0.89 0.9 0.91 0.92 0.93 0.94 0.95 0.96 0.97 0.98 0.99 1

Profit

Alpha

10

11

12

13

14

15

20

No of

Colors

No of

Colors

No of

Colors

Figure 14: The Number of Trials Versus Alpha.

CONCLUSIONS

In this paper a simulated annealing algorithm for register allocation was presented. The

algorithm uses the Metropolis procedure to accept a new solution with less profit based on a

probabilistic function. Random generated graphs were used to thoroughly study the behavior

of the algorithm parameters. The experimental results show optimal solutions in many of the

graphs used for testing. The algorithm presented in the paper has a linear time asymptotic

complexity

ACKNOWLEDGEMENTS

The first author of this paper would like to acknowledge the support of King Fahd University

of Petroleum and Minerals in preparation and presentation of this paper.

REFERENCES

[1] Peng, M., Gupta, N. K. and Armitage, A., "An investigation into the improvement of local minima of the

Hopefield network", Neural networks, Vol. 9, 1241-1253, Oct. 1996.

[2] Taniguchi, M., Kurokawa, K. and Itoh, K., "Sidelobeless multiple-object discriminant filters recorded as

discrete-type computer generated holograms", Applied optics, Vol. 36, 9138-9145, Dec. 1997.

[3] Wilson, J., "A simulated annealing algorithm for optimizing RF power efficiency in coupled-cavity traveling-

wave tubes", IEEE Transactions on Electron Devices, Vol. 44, 2295-2299, Dec. 1997.

[4] Athier, G., Floquet, P. and Pibouleau, L., "Synthesis of heat-exchanger network by simulated annealing and

NLP procedures", AIChE Journal, Vol. 43, 3007-3020, Nov. 1997.

[5] Duncan, A. A. and Hendry, D. C., "High-Level synthesis of DSP datapaths by global optimization of variable

lifetimes", IEE proceedings: Computers and Digital Techniques, Vol. 142, 215-224, May 1995.

[6] Kirkpatrich, S., Gelatt, J. C., and Vecchi, M., "Optimization by Simulated Annealing" Science, Vol. 220,

498-516, May 1983.

90

95

100

105

110

115

120

0 0.2 0.4 0.6 0.8 1 1.2

Trials

Alpha

10

11

12

13

14

15

20

No of

Colors

**