2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Partitioned Boolean Quadratic Problem (PBQP) solver.
24 * @author Sebastian Buchwald
29 #include "adt/array.h"
30 #include "adt/xmalloc.h"
34 #include "pbqp_edge.h"
35 #include "pbqp_edge_t.h"
36 #include "pbqp_node.h"
37 #include "pbqp_node_t.h"
40 pbqp_node_t *get_node(pbqp_t *pbqp, unsigned index)
42 return pbqp->nodes[index];
45 pbqp_edge_t *get_edge(pbqp_t *pbqp, unsigned src_index, unsigned tgt_index)
50 if (tgt_index < src_index) {
51 unsigned tmp = src_index;
52 src_index = tgt_index;
56 pbqp_node_t *src_node = get_node(pbqp, src_index);
57 pbqp_node_t *tgt_node = get_node(pbqp, tgt_index);
60 len = ARR_LEN(src_node->edges);
62 for (i = 0; i < len; ++i) {
63 pbqp_edge_t *cur_edge = src_node->edges[i];
64 if (cur_edge->tgt == tgt_node) {
72 pbqp_t *alloc_pbqp(unsigned number_nodes)
74 pbqp_t *pbqp = XMALLOC(pbqp_t);
76 obstack_init(&pbqp->obstack);
79 pbqp->num_nodes = number_nodes;
81 pbqp->dump_file = NULL;
83 pbqp->nodes = OALLOCNZ(&pbqp->obstack, pbqp_node_t*, number_nodes);
97 void free_pbqp(pbqp_t *pbqp)
99 obstack_free(&pbqp->obstack, NULL);
103 void add_node_costs(pbqp_t *pbqp, unsigned node_index, vector_t *costs)
105 pbqp_node_t *node = get_node(pbqp, node_index);
108 node = alloc_node(pbqp, node_index, costs);
109 pbqp->nodes[node_index] = node;
111 vector_add(node->costs, costs);
115 void add_edge_costs(pbqp_t *pbqp, unsigned src_index, unsigned tgt_index,
116 pbqp_matrix_t *costs)
118 pbqp_edge_t *edge = get_edge(pbqp, src_index, tgt_index);
120 if (tgt_index < src_index) {
121 pbqp_matrix_transpose(pbqp, costs);
122 add_edge_costs(pbqp, tgt_index, src_index, costs);
127 edge = alloc_edge(pbqp, src_index, tgt_index, costs);
129 pbqp_matrix_add(edge->costs, costs);
133 num get_node_solution(pbqp_t *pbqp, unsigned node_index)
135 pbqp_node_t *node = get_node(pbqp, node_index);
137 return node->solution;
140 num get_solution(pbqp_t *pbqp)
142 return pbqp->solution;
146 void set_dumpfile(pbqp *pbqp, FILE *f)