- brute force solver need own back propagation
[libfirm] / brute_force.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Brute force PBQP solver.
23  * @date    02.12.2008
24  * @author  Sebastian Buchwald
25  * @version $Id$
26  */
27 #include "config.h"
28
29 #include "assert.h"
30 #include "error.h"
31
32 #include "bucket.h"
33 #include "brute_force.h"
34 #include "optimal.h"
35 #if     KAPS_DUMP
36 #include "html_dumper.h"
37 #endif
38 #include "kaps.h"
39 #include "matrix.h"
40 #include "pbqp_edge.h"
41 #include "pbqp_edge_t.h"
42 #include "pbqp_node.h"
43 #include "pbqp_node_t.h"
44 #include "vector.h"
45
46 /* Forward declarations. */
47 static void apply_Brute_Force(pbqp *pbqp);
48
49 static void apply_brute_force_reductions(pbqp *pbqp)
50 {
51         for (;;) {
52                 if (edge_bucket_get_length(edge_bucket) > 0) {
53                         apply_edge(pbqp);
54                 } else if (node_bucket_get_length(node_buckets[1]) > 0) {
55                         apply_RI(pbqp);
56                 } else if (node_bucket_get_length(node_buckets[2]) > 0) {
57                         apply_RII(pbqp);
58                 } else if (node_bucket_get_length(node_buckets[3]) > 0) {
59                         apply_Brute_Force(pbqp);
60                 } else {
61                         return;
62                 }
63         }
64 }
65
66 static unsigned get_minimal_alternative(pbqp *pbqp, pbqp_node *node)
67 {
68         vector      *node_vec;
69         unsigned     node_index;
70         unsigned     node_len;
71         unsigned     min_index    = 0;
72         num          min          = INF_COSTS;
73         unsigned     bucket_index;
74
75         assert(pbqp);
76         assert(node);
77         node_vec     = node->costs;
78         node_len     = node_vec->len;
79         bucket_index = node->bucket_index;
80
81         for (node_index = 0; node_index < node_len; ++node_index) {
82                 pbqp_node_bucket bucket_deg3;
83                 num              value;
84                 unsigned         bucket_0_length;
85                 unsigned         bucket_red_length;
86
87                 char *tmp = obstack_finish(&pbqp->obstack);
88
89                 node_bucket_init(&bucket_deg3);
90
91                 /* Some node buckets and the edge bucket should be empty. */
92                 assert(node_bucket_get_length(node_buckets[1]) == 0);
93                 assert(node_bucket_get_length(node_buckets[2]) == 0);
94                 assert(edge_bucket_get_length(edge_bucket)     == 0);
95
96                 /* char *tmp = obstack_finish(&pbqp->obstack); */
97
98                 /* Save current PBQP state. */
99                 node_bucket_copy(&bucket_deg3, node_buckets[3]);
100                 node_bucket_shrink(&node_buckets[3], 0);
101                 node_bucket_deep_copy(pbqp, &node_buckets[3], bucket_deg3);
102                 node_bucket_update(pbqp, node_buckets[3]);
103                 bucket_0_length   = node_bucket_get_length(node_buckets[0]);
104                 bucket_red_length = node_bucket_get_length(reduced_bucket);
105
106                 /* Select alternative and solve PBQP recursively. */
107                 select_alternative(node_buckets[3][bucket_index], node_index);
108                 apply_brute_force_reductions(pbqp);
109
110                 value = determine_solution(pbqp);
111
112                 if (value < min) {
113                         min = value;
114                         min_index = node_index;
115                 }
116
117                 /* Some node buckets and the edge bucket should still be empty. */
118                 assert(node_bucket_get_length(node_buckets[1]) == 0);
119                 assert(node_bucket_get_length(node_buckets[2]) == 0);
120                 assert(edge_bucket_get_length(edge_bucket)     == 0);
121
122                 /* Clear modified buckets... */
123                 node_bucket_shrink(&node_buckets[3], 0);
124
125                 /* ... and restore old PBQP state. */
126                 node_bucket_shrink(&node_buckets[0], bucket_0_length);
127                 node_bucket_shrink(&reduced_bucket, bucket_red_length);
128                 node_bucket_copy(&node_buckets[3], bucket_deg3);
129                 node_bucket_update(pbqp, node_buckets[3]);
130
131                 /* Free copies. */
132                 /* obstack_free(&pbqp->obstack, tmp); */
133                 node_bucket_free(&bucket_deg3);
134
135                 obstack_free(&pbqp->obstack, tmp);
136         }
137
138         return min_index;
139 }
140
141 static void apply_Brute_Force(pbqp *pbqp)
142 {
143         pbqp_node   *node         = NULL;
144         unsigned     min_index    = 0;
145
146         assert(pbqp);
147
148         /* We want to reduce a node with maximum degree. */
149         node = get_node_with_max_degree();
150         assert(node);
151         assert(pbqp_node_get_degree(node) > 2);
152
153 #if     KAPS_DUMP
154         if (pbqp->dump_file) {
155                 char     txt[100];
156                 sprintf(txt, "BF-Reduction of Node n%d", node->index);
157                 dump_section(pbqp->dump_file, 2, txt);
158                 pbqp_dump_graph(pbqp);
159         }
160 #endif
161
162 #if KAPS_STATISTIC
163         dump++;
164 #endif
165
166         min_index = get_minimal_alternative(pbqp, node);
167         node = pbqp->nodes[node->index];
168
169 #if     KAPS_DUMP
170         if (pbqp->dump_file) {
171                 fprintf(pbqp->dump_file, "node n%d is set to %d<br><br>\n",
172                                         node->index, min_index);
173         }
174 #endif
175
176 #if KAPS_STATISTIC
177         dump--;
178         if (dump == 0) {
179                 FILE *fh = fopen("solutions.pb", "a");
180                 fprintf(fh, "[%u]", min_index);
181                 fclose(fh);
182                 pbqp->num_bf++;
183         }
184 #endif
185
186         /* Now that we found the minimum set all other costs to infinity. */
187         select_alternative(node, min_index);
188 }
189
190
191
192 static void back_propagate_RI(pbqp *pbqp, pbqp_node *node)
193 {
194         pbqp_edge   *edge;
195         pbqp_node   *other;
196         pbqp_matrix *mat;
197         vector      *vec;
198         int          is_src;
199
200         assert(pbqp);
201         assert(node);
202
203         edge = node->edges[0];
204         mat = edge->costs;
205         is_src = edge->src == node;
206         vec = node->costs;
207
208         if (is_src) {
209                 other = edge->tgt;
210                 assert(other);
211
212                 /* Update pointer for brute force solver. */
213                 other = pbqp->nodes[other->index];
214
215                 node->solution = pbqp_matrix_get_col_min_index(mat, other->solution, vec);
216         } else {
217                 other = edge->src;
218                 assert(other);
219
220                 /* Update pointer for brute force solver. */
221                 other = pbqp->nodes[other->index];
222
223                 node->solution = pbqp_matrix_get_row_min_index(mat, other->solution, vec);
224         }
225
226 #if     KAPS_DUMP
227         if (pbqp->dump_file) {
228                 fprintf(pbqp->dump_file, "node n%d is set to %d<br>\n", node->index, node->solution);
229         }
230 #endif
231 }
232
233 static void back_propagate_RII(pbqp *pbqp, pbqp_node *node)
234 {
235         pbqp_edge   *src_edge   = node->edges[0];
236         pbqp_edge   *tgt_edge   = node->edges[1];
237         int          src_is_src = src_edge->src == node;
238         int          tgt_is_src = tgt_edge->src == node;
239         pbqp_matrix *src_mat;
240         pbqp_matrix *tgt_mat;
241         pbqp_node   *src_node;
242         pbqp_node   *tgt_node;
243         vector      *vec;
244         vector      *node_vec;
245         unsigned     col_index;
246         unsigned     row_index;
247
248         assert(pbqp);
249
250         if (src_is_src) {
251                 src_node = src_edge->tgt;
252         } else {
253                 src_node = src_edge->src;
254         }
255
256         if (tgt_is_src) {
257                 tgt_node = tgt_edge->tgt;
258         } else {
259                 tgt_node = tgt_edge->src;
260         }
261
262         /* Swap nodes if necessary. */
263         if (tgt_node->index < src_node->index) {
264                 pbqp_node *tmp_node;
265                 pbqp_edge *tmp_edge;
266
267                 tmp_node = src_node;
268                 src_node = tgt_node;
269                 tgt_node = tmp_node;
270
271                 tmp_edge = src_edge;
272                 src_edge = tgt_edge;
273                 tgt_edge = tmp_edge;
274
275                 src_is_src = src_edge->src == node;
276                 tgt_is_src = tgt_edge->src == node;
277         }
278
279         /* Update pointer for brute force solver. */
280         src_node = pbqp->nodes[src_node->index];
281         tgt_node = pbqp->nodes[tgt_node->index];
282
283         src_mat = src_edge->costs;
284         tgt_mat = tgt_edge->costs;
285
286         node_vec = node->costs;
287
288         row_index = src_node->solution;
289         col_index = tgt_node->solution;
290
291         vec = vector_copy(pbqp, node_vec);
292
293         if (src_is_src) {
294                 vector_add_matrix_col(vec, src_mat, row_index);
295         } else {
296                 vector_add_matrix_row(vec, src_mat, row_index);
297         }
298
299         if (tgt_is_src) {
300                 vector_add_matrix_col(vec, tgt_mat, col_index);
301         } else {
302                 vector_add_matrix_row(vec, tgt_mat, col_index);
303         }
304
305         node->solution = vector_get_min_index(vec);
306
307 #if     KAPS_DUMP
308         if (pbqp->dump_file) {
309                 fprintf(pbqp->dump_file, "node n%d is set to %d<br>\n", node->index, node->solution);
310         }
311 #endif
312
313         obstack_free(&pbqp->obstack, vec);
314 }
315
316 static void back_propagate_brute_force(pbqp *pbqp)
317 {
318         unsigned node_index;
319         unsigned node_len   = node_bucket_get_length(reduced_bucket);
320
321         assert(pbqp);
322
323 #if     KAPS_DUMP
324         if (pbqp->dump_file) {
325                 dump_section(pbqp->dump_file, 2, "Back Propagation");
326         }
327 #endif
328
329         for (node_index = node_len; node_index > 0; --node_index) {
330                 pbqp_node *node = reduced_bucket[node_index - 1];
331
332                 switch (pbqp_node_get_degree(node)) {
333                         case 1:
334                                 back_propagate_RI(pbqp, node);
335                                 break;
336                         case 2:
337                                 back_propagate_RII(pbqp, node);
338                                 break;
339                         default:
340                                 panic("Only nodes with degree one or two should be in this bucket");
341                                 break;
342                 }
343         }
344 }
345
346 void solve_pbqp_brute_force(pbqp *pbqp)
347 {
348         /* Reduce nodes degree ... */
349         initial_simplify_edges(pbqp);
350
351         /* ... and put node into bucket representing their degree. */
352         fill_node_buckets(pbqp);
353
354 #if KAPS_STATISTIC
355         FILE *fh = fopen("solutions.pb", "a");
356         fprintf(fh, "Solution");
357         fclose(fh);
358 #endif
359
360         apply_brute_force_reductions(pbqp);
361
362         pbqp->solution = determine_solution(pbqp);
363
364 #if KAPS_STATISTIC
365         fh = fopen("solutions.pb", "a");
366         fprintf(fh, ": %lld RE:%u R0:%u R1:%u R2:%u RN/BF:%u\n", pbqp->solution,
367                         pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2,
368                         pbqp->num_bf);
369         fclose(fh);
370 #endif
371
372         /* Solve reduced nodes. */
373         back_propagate_brute_force(pbqp);
374
375         free_buckets();
376 }