pbqp: add pbqp_ prefix to fix nameclashes
[libfirm] / ir / kaps / heuristical.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   Heuristic PBQP solver.
23  * @date    02.10.2008
24  * @author  Sebastian Buchwald
25  */
26 #include "config.h"
27
28 #include "adt/array.h"
29 #include "assert.h"
30 #include "error.h"
31
32 #include "bucket.h"
33 #include "heuristical.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 #include "timing.h"
47
48 static void apply_RN(pbqp_t *pbqp)
49 {
50         pbqp_node_t *node      = NULL;
51         unsigned     min_index = 0;
52
53         assert(pbqp);
54
55         /* We want to reduce a node with maximum degree. */
56         node = get_node_with_max_degree();
57         assert(pbqp_node_get_degree(node) > 2);
58
59 #if KAPS_DUMP
60         if (pbqp->dump_file) {
61                 char     txt[100];
62                 sprintf(txt, "RN-Reduction of Node n%d", node->index);
63                 pbqp_dump_section(pbqp->dump_file, 2, txt);
64                 pbqp_dump_graph(pbqp);
65         }
66 #endif
67
68         min_index = get_local_minimal_alternative(pbqp, node);
69
70 #if KAPS_DUMP
71         if (pbqp->dump_file) {
72                 fprintf(pbqp->dump_file, "node n%d is set to %d<br><br>\n",
73                                         node->index, min_index);
74         }
75 #endif
76
77 #if KAPS_STATISTIC
78         FILE *fh = fopen("solutions.pb", "a");
79         fprintf(fh, "[%u]", min_index);
80         fclose(fh);
81         pbqp->num_rn++;
82 #endif
83
84         /* Now that we found the local minimum set all other costs to infinity. */
85         select_alternative(node, min_index);
86 }
87
88 static void apply_heuristic_reductions(pbqp_t *pbqp)
89 {
90         for (;;) {
91                 if (edge_bucket_get_length(edge_bucket) > 0) {
92                         apply_edge(pbqp);
93                 } else if (node_bucket_get_length(node_buckets[1]) > 0) {
94                         apply_RI(pbqp);
95                 } else if (node_bucket_get_length(node_buckets[2]) > 0) {
96                         apply_RII(pbqp);
97                 } else if (node_bucket_get_length(node_buckets[3]) > 0) {
98                         apply_RN(pbqp);
99                 } else {
100                         return;
101                 }
102         }
103 }
104
105 void solve_pbqp_heuristical(pbqp_t *pbqp)
106 {
107         /* Reduce nodes degree ... */
108         initial_simplify_edges(pbqp);
109
110         /* ... and put node into bucket representing their degree. */
111         fill_node_buckets(pbqp);
112
113 #if KAPS_STATISTIC
114         FILE *fh = fopen("solutions.pb", "a");
115         fprintf(fh, "Solution");
116         fclose(fh);
117 #endif
118
119         apply_heuristic_reductions(pbqp);
120
121         pbqp->solution = determine_solution(pbqp);
122
123 #if KAPS_STATISTIC
124         fh = fopen("solutions.pb", "a");
125         #if KAPS_USE_UNSIGNED
126                 fprintf(fh, ": %u RE:%u R0:%u R1:%u R2:%u RM:%u RN/BF:%u\n", pbqp->solution,
127                                 pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2,
128                                 pbqp->num_rm, pbqp->num_rn);
129         #else
130                 fprintf(fh, ": %lld RE:%u R0:%u R1:%u R2:%u RM:%u RN/BF:%u\n", pbqp->solution,
131                                 pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2,
132                                 pbqp->num_rm, pbqp->num_rn);
133         #endif
134         fclose(fh);
135 #endif
136
137         /* Solve reduced nodes. */
138         back_propagate(pbqp);
139
140         free_buckets();
141 }