+/*
+ * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
+ *
+ * This file is part of libFirm.
+ *
+ * This file may be distributed and/or modified under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation and appearing in the file LICENSE.GPL included in the
+ * packaging of this file.
+ *
+ * Licensees holding valid libFirm Professional Edition licenses may use
+ * this file in accordance with the libFirm Commercial License.
+ * Agreement provided with the Software.
+ *
+ * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+ * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE.
+ */
+
+/**
+ * @file
+ * @brief Heuristic PBQP solver.
+ * @date 02.10.2008
+ * @author Sebastian Buchwald
+ * @version $Id$
+ */
+#include "config.h"
+
#include "adt/array.h"
#include "assert.h"
#include "error.h"
+#include "bucket.h"
#include "heuristical.h"
+#include "optimal.h"
+#if KAPS_DUMP
#include "html_dumper.h"
+#endif
#include "kaps.h"
#include "matrix.h"
#include "pbqp_edge.h"
#include "pbqp_node_t.h"
#include "vector.h"
-static pbqp_edge **edge_bucket;
-static pbqp_node **node_buckets[4];
-static pbqp_node **reduced_bucket;
-
-static void init_buckets(void)
-{
- int i;
-
- edge_bucket = NEW_ARR_F(pbqp_edge *, 0);
- reduced_bucket = NEW_ARR_F(pbqp_node *, 0);
-
- for (i = 0; i < 4; ++i) {
- node_buckets[i] = NEW_ARR_F(pbqp_node *, 0);
- }
-}
-
-static void fill_node_buckets(pbqp *pbqp)
-{
- unsigned node_index;
- unsigned node_len;
-
- assert(pbqp);
- node_len = pbqp->num_nodes;
-
- for (node_index = 0; node_index < node_len; ++node_index) {
- unsigned arity;
- pbqp_node *node = get_node(pbqp, node_index);
-
- if (!node) continue;
-
- arity = ARR_LEN(node->edges);
-
- /* We have only one bucket for nodes with arity >= 3. */
- if (arity > 3) {
- arity = 3;
- }
-
- node->bucket_index = ARR_LEN(node_buckets[arity]);
-
- ARR_APP1(pbqp_node *, node_buckets[arity], node);
- }
-}
-
-static void normalize_towards_source(pbqp *pbqp, pbqp_edge *edge)
-{
- pbqp_matrix *mat;
- pbqp_node *src_node;
- pbqp_node *tgt_node;
- vector *src_vec;
- vector *tgt_vec;
- int src_len;
- int tgt_len;
- int src_index;
-
- assert(pbqp);
- assert(edge);
-
- src_node = edge->src;
- tgt_node = edge->tgt;
- assert(src_node);
- assert(tgt_node);
-
- src_vec = src_node->costs;
- tgt_vec = tgt_node->costs;
- assert(src_vec);
- assert(tgt_vec);
-
- src_len = src_vec->len;
- tgt_len = tgt_vec->len;
- assert(src_len > 0);
- assert(tgt_len > 0);
-
- mat = edge->costs;
- assert(mat);
-
- /* Normalize towards source node. */
- for (src_index = 0; src_index < src_len; ++src_index) {
- num min = pbqp_matrix_get_row_min(mat, src_index, tgt_vec);
-
- if (min != 0) {
- pbqp_matrix_sub_row_value(mat, src_index, tgt_vec, min);
- src_vec->entries[src_index].data += min;
-
- // TODO add to edge_list if inf
- }
- }
-}
-
-static void normalize_towards_target(pbqp *pbqp, pbqp_edge *edge)
-{
- pbqp_matrix *mat;
- pbqp_node *src_node;
- pbqp_node *tgt_node;
- vector *src_vec;
- vector *tgt_vec;
- int src_len;
- int tgt_len;
- int tgt_index;
-
- assert(pbqp);
- assert(edge);
-
- src_node = edge->src;
- tgt_node = edge->tgt;
- assert(src_node);
- assert(tgt_node);
-
- src_vec = src_node->costs;
- tgt_vec = tgt_node->costs;
- assert(src_vec);
- assert(tgt_vec);
-
- src_len = src_vec->len;
- tgt_len = tgt_vec->len;
- assert(src_len > 0);
- assert(tgt_len > 0);
-
- mat = edge->costs;
- assert(mat);
+#include "timing.h"
- for (tgt_index = 0; tgt_index < tgt_len; ++tgt_index) {
- num min = pbqp_matrix_get_col_min(mat, tgt_index, src_vec);
-
- if (min != 0) {
- pbqp_matrix_sub_col_value(mat, tgt_index, src_vec, min);
- tgt_vec->entries[tgt_index].data += min;
-
- // TODO add to edge_list if inf
- }
- }
-}
-
-static void simplify_edge(pbqp *pbqp, pbqp_edge *edge)
+static void apply_RN(pbqp *pbqp)
{
- pbqp_matrix *mat;
- pbqp_node *src_node;
- pbqp_node *tgt_node;
- vector *src_vec;
- vector *tgt_vec;
- int src_len;
- int tgt_len;
+ pbqp_node *node = NULL;
+ unsigned min_index = 0;
assert(pbqp);
- assert(edge);
- src_node = edge->src;
- tgt_node = edge->tgt;
- assert(src_node);
- assert(tgt_node);
-
- if(pbqp->dump_file) {
- char txt[100];
- sprintf(txt, "Simplification of Edge n%d-n%d", src_node->index, tgt_node->index);
- dump_section(pbqp->dump_file, 3, txt);
- }
-
- src_vec = src_node->costs;
- tgt_vec = tgt_node->costs;
- assert(src_vec);
- assert(tgt_vec);
-
- src_len = src_vec->len;
- tgt_len = tgt_vec->len;
- assert(src_len > 0);
- assert(tgt_len > 0);
-
- mat = edge->costs;
- assert(mat);
+ /* We want to reduce a node with maximum degree. */
+ node = get_node_with_max_degree();
+ assert(node);
+ assert(pbqp_node_get_degree(node) > 2);
+#if KAPS_DUMP
if (pbqp->dump_file) {
- fputs("Input:<br>\n", pbqp->dump_file);
- dump_simplifyedge(pbqp, edge);
+ char txt[100];
+ sprintf(txt, "RN-Reduction of Node n%d", node->index);
+ dump_section(pbqp->dump_file, 2, txt);
+ pbqp_dump_graph(pbqp);
}
+#endif
- normalize_towards_source(pbqp, edge);
- normalize_towards_target(pbqp, edge);
+ min_index = get_local_minimal_alternative(pbqp, node);
+#if KAPS_DUMP
if (pbqp->dump_file) {
- fputs("<br>\nOutput:<br>\n", pbqp->dump_file);
- dump_simplifyedge(pbqp, edge);
- }
-
- if (pbqp_matrix_is_zero(mat, src_vec, tgt_vec)) {
- if (pbqp->dump_file) {
- fputs("edge has been eliminated", pbqp->dump_file);
-
- delete_edge(edge);
- }
- }
-}
-
-static void reorder_node(pbqp_node *node)
-{
- unsigned arity;
- unsigned old_arity;
- unsigned old_bucket_len;
-
- assert(node);
-
- arity = ARR_LEN(node->edges);
-
- /* Equal bucket as before */
- if (arity > 2) return;
-
- /* Assume node lost one incident edge. */
- old_arity = arity + 1;
-
- if (ARR_LEN(node_buckets[old_arity]) <= (int)node->bucket_index
- || node_buckets[old_arity][node->bucket_index] != node) {
- /* Old arity is new arity, so we have nothing to do. */
- return;
+ fprintf(pbqp->dump_file, "node n%d is set to %d<br><br>\n",
+ node->index, min_index);
}
+#endif
- old_bucket_len = ARR_LEN(node_buckets[old_arity]);
- assert (node_buckets[old_arity][node->bucket_index] == node);
+#if KAPS_STATISTIC
+ FILE *fh = fopen("solutions.pb", "a");
+ fprintf(fh, "[%u]", min_index);
+ fclose(fh);
+ pbqp->num_rn++;
+#endif
- /* Delete node from old bucket... */
- node_buckets[old_arity][node->bucket_index]
- = node_buckets[old_arity][old_bucket_len - 1];
- ARR_SHRINKLEN(node_buckets[old_arity], (int)old_bucket_len - 1);
-
- /* ..and add to new one. */
- node->bucket_index = ARR_LEN(node_buckets[arity]);
- ARR_APP1(pbqp_node *, node_buckets[arity], node);
+ /* Now that we found the local minimum set all other costs to infinity. */
+ select_alternative(node, min_index);
}
-void solve_pbqp_heuristical(pbqp *pbqp)
+static void apply_heuristic_reductions(pbqp *pbqp)
{
- unsigned node_index;
- unsigned node_len;
-
- assert(pbqp);
-
- if (pbqp->dump_file) {
- pbqp_dump_input(pbqp);
- dump_section(pbqp->dump_file, 1, "2. Simplification of Cost Matrices");
- }
-
- node_len = pbqp->num_nodes;
-
- init_buckets();
-
- /* First simplify all edges. */
- for (node_index = 0; node_index < node_len; ++node_index) {
- unsigned edge_index;
- pbqp_node *node = get_node(pbqp, node_index);
- pbqp_edge **edges;
- unsigned edge_len;
-
- if (!node) continue;
-
- edges = node->edges;
- edge_len = ARR_LEN(edges);
-
- for (edge_index = 0; edge_index < edge_len; ++edge_index) {
- pbqp_edge *edge = edges[edge_index];
-
- /* Simplify only once per edge. */
- if (node_index != edge->src->index) continue;
-
- simplify_edge(pbqp, edge);
- }
- }
-
- /* Put node into bucket representing their arity. */
- fill_node_buckets(pbqp);
-
for (;;) {
- if (ARR_LEN(edge_bucket) > 0) {
- panic("Please implement edge simplification");
- } else if (ARR_LEN(node_buckets[1]) > 0) {
- applyRI(pbqp);
- } else if (ARR_LEN(node_buckets[2]) > 0) {
- panic("Please implement RII simplification");
- } else if (ARR_LEN(node_buckets[3]) > 0) {
- panic("Please implement RN simplification");
+ if (edge_bucket_get_length(edge_bucket) > 0) {
+ apply_edge(pbqp);
+ } else if (node_bucket_get_length(node_buckets[1]) > 0) {
+ apply_RI(pbqp);
+ } else if (node_bucket_get_length(node_buckets[2]) > 0) {
+ apply_RII(pbqp);
+ } else if (node_bucket_get_length(node_buckets[3]) > 0) {
+ apply_RN(pbqp);
} else {
- break;
- }
- }
-
- if (pbqp->dump_file) {
- dump_section(pbqp->dump_file, 1, "4. Determine Solution/Minimum");
- dump_section(pbqp->dump_file, 2, "4.1. Trivial Solution");
- }
-
- /* Solve trivial nodes and calculate solution. */
- node_len = ARR_LEN(node_buckets[0]);
- for (node_index = 0; node_index < node_len; ++node_index) {
- pbqp_node *node = node_buckets[0][node_index];
- assert(node);
-
- node->solution = vector_get_min_index(node->costs);
- pbqp->solution += node->costs->entries[node->solution].data;
- if (pbqp->dump_file) {
- fprintf(pbqp->dump_file, "node n%d is set to %d<br>\n", node->index, node->solution);
- dump_node(pbqp, node);
- }
- }
-
- if (pbqp->dump_file) {
- dump_section(pbqp->dump_file, 2, "Minimum");
- fprintf(pbqp->dump_file, "Minimum is equal to %d.", pbqp->solution);
- dump_section(pbqp->dump_file, 2, "Back Propagation");
- }
-
- /* Solve reduced nodes. */
- node_len = ARR_LEN(reduced_bucket);
- for (node_index = node_len; node_index > 0; --node_index) {
- pbqp_node *node = reduced_bucket[node_index - 1];
- assert(node);
-
- switch (ARR_LEN(node->edges)) {
- case 1:
- back_propagate_RI(pbqp, node);
- break;
- case 2:
- panic("Please implement back propagation for RII");
- break;
- default:
- panic("Only nodes with degree one or two should be in this bucket");
- break;
+ return;
}
}
}
-void applyRI(pbqp *pbqp)
-{
- pbqp_node **bucket = node_buckets[1];
- unsigned bucket_len = ARR_LEN(bucket);
- pbqp_node *node = bucket[bucket_len - 1];
- pbqp_edge *edge = node->edges[0];
- pbqp_matrix *mat = edge->costs;
- int is_src = edge->src == node;
- pbqp_node *other_node;
-
- if (is_src) {
- other_node = edge->tgt;
- } else {
- other_node = edge->src;
- }
-
- if (pbqp->dump_file) {
- char txt[100];
- sprintf(txt, "RI-Reduktion of Node n%d", node->index);
- dump_section(pbqp->dump_file, 2, txt);
- pbqp_dump_graph(pbqp);
- fputs("<br>\nBefore reduction:<br>\n", pbqp->dump_file);
- dump_node(pbqp, node);
- dump_node(pbqp, other_node);
- dump_edge(pbqp, edge);
- }
-
- if (is_src) {
- pbqp_matrix_add_to_all_cols(mat, node->costs);
- normalize_towards_target(pbqp, edge);
- } else {
- pbqp_matrix_add_to_all_rows(mat, node->costs);
- normalize_towards_source(pbqp, edge);
- }
- disconnect_edge(other_node, edge);
-
- if (pbqp->dump_file) {
- fputs("<br>\nAfter reduction:<br>\n", pbqp->dump_file);
- dump_node(pbqp, other_node);
- }
-
- /* Remove node from bucket... */
- ARR_SHRINKLEN(bucket, (int)bucket_len - 1);
- reorder_node(other_node);
-
- /* ...and add it to back propagation list. */
- ARR_APP1(pbqp_node *, reduced_bucket, node);
-}
-
-void back_propagate_RI(pbqp *pbqp, pbqp_node *node)
+void solve_pbqp_heuristical(pbqp *pbqp)
{
- pbqp_edge *edge;
- pbqp_node *other;
- pbqp_matrix *mat;
- vector *vec;
- int is_src;
+ /* Reduce nodes degree ... */
+ initial_simplify_edges(pbqp);
- assert(pbqp);
- assert(node);
+ /* ... and put node into bucket representing their degree. */
+ fill_node_buckets(pbqp);
- edge = node->edges[0];
- mat = edge->costs;
- is_src = edge->src == node;
- vec = node->costs;
+#if KAPS_STATISTIC
+ FILE *fh = fopen("solutions.pb", "a");
+ fprintf(fh, "Solution");
+ fclose(fh);
+#endif
+
+ apply_heuristic_reductions(pbqp);
+
+ pbqp->solution = determine_solution(pbqp);
+
+#if KAPS_STATISTIC
+ fh = fopen("solutions.pb", "a");
+ #if KAPS_USE_UNSIGNED
+ fprintf(fh, ": %u RE:%u R0:%u R1:%u R2:%u RM:%u RN/BF:%u\n", pbqp->solution,
+ pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2,
+ pbqp->num_rm, pbqp->num_rn);
+ #else
+ fprintf(fh, ": %lld RE:%u R0:%u R1:%u R2:%u RM:%u RN/BF:%u\n", pbqp->solution,
+ pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2,
+ pbqp->num_rm, pbqp->num_rn);
+ #endif
+ fclose(fh);
+#endif
- if (is_src) {
- other = edge->tgt;
- assert(other);
- vector_add_matrix_col(vec, mat, other->solution);
- } else {
- other = edge->src;
- assert(other);
- vector_add_matrix_row(vec, mat, other->solution);
- }
+ /* Solve reduced nodes. */
+ back_propagate(pbqp);
- node->solution = vector_get_min_index(vec);
- if (pbqp->dump_file) {
- fprintf(pbqp->dump_file, "node n%d is set to %d<br>\n", node->index, node->solution);
- }
+ free_buckets();
}