88dbebde3e6bdf7e246b4f3a5d2315d508d2ed6e
[libfirm] / kaps.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   Partitioned Boolean Quadratic Problem (PBQP) solver.
23  * @date    02.10.2008
24  * @author  Sebastian Buchwald
25  * @version $Id$
26  */
27 #include "config.h"
28
29 #include "adt/array.h"
30
31 #include "kaps.h"
32 #include "matrix.h"
33 #include "pbqp_edge.h"
34 #include "pbqp_edge_t.h"
35 #include "pbqp_node.h"
36 #include "pbqp_node_t.h"
37 #include "vector.h"
38
39 pbqp_node *get_node(pbqp *pbqp, unsigned index)
40 {
41         return pbqp->nodes[index];
42 }
43
44 pbqp_edge *get_edge(pbqp *pbqp, unsigned src_index, unsigned tgt_index)
45 {
46         int i;
47         int len;
48
49         if (tgt_index < src_index) {
50                 unsigned tmp = src_index;
51                 src_index    = tgt_index;
52                 tgt_index    = tmp;
53         }
54
55         pbqp_node *src_node = get_node(pbqp, src_index);
56         pbqp_node *tgt_node = get_node(pbqp, tgt_index);
57         assert(src_node);
58         assert(tgt_node);
59
60         len = ARR_LEN(src_node->edges);
61
62         for (i = 0; i < len; ++i) {
63                 pbqp_edge *cur_edge = src_node->edges[i];
64                 if (cur_edge->tgt == tgt_node) {
65                         return cur_edge;
66                 }
67         }
68
69         return NULL;
70 }
71
72 pbqp *alloc_pbqp(unsigned number_nodes)
73 {
74         pbqp* pbqp = xmalloc(sizeof(*pbqp));
75
76         obstack_init(&pbqp->obstack);
77
78         pbqp->solution = 0;
79         pbqp->num_nodes = number_nodes;
80 #if     KAPS_DUMP
81         pbqp->dump_file = NULL;
82 #endif
83         pbqp->nodes = obstack_alloc(&pbqp->obstack, number_nodes
84                         * sizeof(*pbqp->nodes));
85         memset(pbqp->nodes, 0, number_nodes * sizeof(*pbqp->nodes));
86 #if KAPS_STATISTIC
87         pbqp->num_bf = 0;
88         pbqp->num_edges = 0;
89         pbqp->num_r0 = 0;
90         pbqp->num_r1 = 0;
91         pbqp->num_r2 = 0;
92         pbqp->num_rm = 0;
93         pbqp->num_rn = 0;
94 #endif
95
96         return pbqp;
97 }
98
99 void free_pbqp(pbqp *pbqp)
100 {
101         obstack_free(&pbqp->obstack, NULL);
102         xfree(pbqp);
103 }
104
105 void add_node_costs(pbqp *pbqp, unsigned node_index, vector *costs)
106 {
107         pbqp_node *node = get_node(pbqp, node_index);
108
109         if (node == NULL) {
110                 node = alloc_node(pbqp, node_index, costs);
111                 pbqp->nodes[node_index] = node;
112         } else {
113                 vector_add(node->costs, costs);
114         }
115 }
116
117 void add_edge_costs(pbqp *pbqp, unsigned src_index, unsigned tgt_index,
118                 pbqp_matrix *costs)
119 {
120         pbqp_edge *edge = get_edge(pbqp, src_index, tgt_index);
121
122         if (tgt_index < src_index) {
123                 pbqp_matrix_transpose(pbqp, costs);
124                 add_edge_costs(pbqp, tgt_index, src_index, costs);
125                 return;
126         }
127
128         if (edge == NULL) {
129                 edge = alloc_edge(pbqp, src_index, tgt_index, costs);
130         } else {
131                 pbqp_matrix_add(edge->costs, costs);
132         }
133 }
134
135 num get_node_solution(pbqp *pbqp, unsigned node_index)
136 {
137         pbqp_node *node = get_node(pbqp, node_index);
138         assert(node);
139
140         return node->solution;
141 }
142
143 num get_solution(pbqp *pbqp)
144 {
145         return pbqp->solution;
146 }
147
148 #if     KAPS_DUMP
149 void set_dumpfile(pbqp *pbqp, FILE *f)
150 {
151         assert(pbqp);
152         pbqp->dump_file = f;
153 }
154 #endif