#include "timing.h"
pbqp_edge **edge_bucket;
+pbqp_edge **rm_bucket;
pbqp_node **node_buckets[4];
pbqp_node **reduced_bucket = NULL;
static int buckets_filled = 0;
edge_bucket_insert(&edge_bucket, edge);
}
+static void insert_into_rm_bucket(pbqp_edge *edge)
+{
+ if (edge_bucket_contains(rm_bucket, edge)) {
+ /* Edge is already inserted. */
+ return;
+ }
+
+ edge_bucket_insert(&rm_bucket, edge);
+}
+
static void init_buckets(void)
{
int i;
edge_bucket_init(&edge_bucket);
+ edge_bucket_init(&rm_bucket);
node_bucket_init(&reduced_bucket);
for (i = 0; i < 4; ++i) {
}
edge_bucket_free(&edge_bucket);
+ edge_bucket_free(&rm_bucket);
node_bucket_free(&reduced_bucket);
buckets_filled = 0;
node_len = pbqp->num_nodes;
#if KAPS_TIMING
- ir_timer_t *t_fill_buckets = ir_timer_register("be_pbqp_fill_buckets", "PBQP Fill Nodes into buckets");
- ir_timer_reset_and_start(t_fill_buckets);
+ ir_timer_t *t_fill_buckets = ir_timer_new();
+ ir_timer_start(t_fill_buckets);
#endif
for (node_index = 0; node_index < node_len; ++node_index) {
#if KAPS_TIMING
ir_timer_stop(t_fill_buckets);
- printf("%-20s: %8.3lf msec\n", ir_timer_get_description(t_fill_buckets), (double)ir_timer_elapsed_usec(t_fill_buckets) / 1000.0);
+ printf("PBQP Fill Nodes into buckets: %8.3lf msec\n", (double)ir_timer_elapsed_usec(t_fill_buckets) / 1000.0);
#endif
}
/* Reconnect the source's edges with the target node. */
for (edge_index = 0; edge_index < edge_len; ++edge_index) {
pbqp_edge *old_edge = src_node->edges[edge_index];
+ pbqp_edge *new_edge;
pbqp_matrix *old_matrix;
pbqp_matrix *new_matrix;
pbqp_node *other_node;
+ vector *other_vec;
unsigned other_len;
unsigned other_index;
unsigned tgt_index;
assert(old_matrix);
if (old_edge->tgt == src_node) {
- other_node = edge->src;
+ other_node = old_edge->src;
other_len = old_matrix->rows;
}
else {
- other_node = edge->tgt;
+ other_node = old_edge->tgt;
other_len = old_matrix->cols;
}
assert(other_node);
+ other_vec = other_node->costs;
new_matrix = pbqp_matrix_alloc(pbqp, tgt_len, other_len);
for (tgt_index = 0; tgt_index < tgt_len; ++tgt_index) {
unsigned src_index = mapping[tgt_index];
+ if (tgt_vec->entries[tgt_index].data == INF_COSTS)
+ continue;
+
for (other_index = 0; other_index < other_len; ++other_index) {
+ if (other_vec->entries[other_index].data == INF_COSTS)
+ continue;
+
new_matrix->entries[tgt_index*other_len+other_index] = old_matrix->entries[other_index*src_len+src_index];
}
}
for (tgt_index = 0; tgt_index < tgt_len; ++tgt_index) {
unsigned src_index = mapping[tgt_index];
+ if (tgt_vec->entries[tgt_index].data == INF_COSTS)
+ continue;
+
for (other_index = 0; other_index < other_len; ++other_index) {
+ if (other_vec->entries[other_index].data == INF_COSTS)
+ continue;
+
new_matrix->entries[tgt_index*other_len+other_index] = old_matrix->entries[src_index*other_len+other_index];
}
}
add_edge_costs(pbqp, tgt_node->index, other_node->index, new_matrix);
- disconnect_edge(src_node, old_edge);
- disconnect_edge(other_node, old_edge);
+ delete_edge(old_edge);
+ reorder_node(src_node);
+ reorder_node(other_node);
+
+ new_edge = get_edge(pbqp, tgt_node->index, other_node->index);
+ insert_into_rm_bucket(new_edge);
}
/* Reduce the remaining source node via RI. */
if (onlyOneZero) {
DEL_ARR_F(mapping);
return;
+ }
onlyOneZero = 1;
mapping[src_index] = tgt_index;
/* Reconnect the target's edges with the source node. */
for (edge_index = 0; edge_index < edge_len; ++edge_index) {
pbqp_edge *old_edge = tgt_node->edges[edge_index];
+ pbqp_edge *new_edge;
pbqp_matrix *old_matrix;
pbqp_matrix *new_matrix;
pbqp_node *other_node;
+ vector *other_vec;
unsigned other_len;
unsigned other_index;
unsigned src_index;
assert(old_matrix);
if (old_edge->tgt == tgt_node) {
- other_node = edge->src;
+ other_node = old_edge->src;
other_len = old_matrix->rows;
}
else {
- other_node = edge->tgt;
+ other_node = old_edge->tgt;
other_len = old_matrix->cols;
}
assert(other_node);
+ other_vec = other_node->costs;
new_matrix = pbqp_matrix_alloc(pbqp, src_len, other_len);
for (src_index = 0; src_index < src_len; ++src_index) {
unsigned tgt_index = mapping[src_index];
+ if (src_vec->entries[src_index].data == INF_COSTS)
+ continue;
+
for (other_index = 0; other_index < other_len; ++other_index) {
+ if (other_vec->entries[other_index].data == INF_COSTS)
+ continue;
+
new_matrix->entries[src_index*other_len+other_index] = old_matrix->entries[other_index*tgt_len+tgt_index];
}
}
for (src_index = 0; src_index < src_len; ++src_index) {
unsigned tgt_index = mapping[src_index];
+ if (src_vec->entries[src_index].data == INF_COSTS)
+ continue;
+
for (other_index = 0; other_index < other_len; ++other_index) {
+ if (other_vec->entries[other_index].data == INF_COSTS)
+ continue;
+
new_matrix->entries[src_index*other_len+other_index] = old_matrix->entries[tgt_index*other_len+other_index];
}
}
add_edge_costs(pbqp, src_node->index, other_node->index, new_matrix);
- disconnect_edge(tgt_node, old_edge);
- disconnect_edge(other_node, old_edge);
+ delete_edge(old_edge);
+ reorder_node(tgt_node);
+ reorder_node(other_node);
+
+ new_edge = get_edge(pbqp, src_node->index, other_node->index);
+ insert_into_rm_bucket(new_edge);
}
/* Reduce the remaining source node via RI. */
apply_RI(pbqp);
}
+/**
+ * Merge neighbors into the given node.
+ */
+void apply_RM(pbqp *pbqp, pbqp_node *node)
+{
+ pbqp_edge **edges;
+ unsigned edge_index;
+ unsigned edge_len;
+
+ assert(node);
+ assert(pbqp);
+
+ edges = node->edges;
+ edge_len = pbqp_node_get_degree(node);
+
+ /* Check all incident edges. */
+ for (edge_index = 0; edge_index < edge_len; ++edge_index) {
+ pbqp_edge *edge = edges[edge_index];
+
+ insert_into_rm_bucket(edge);
+ }
+
+ /* ALAP: Merge neighbors into given node. */
+ while(edge_bucket_get_length(rm_bucket) > 0) {
+ pbqp_edge *edge = edge_bucket_pop(&rm_bucket);
+ assert(edge);
+
+ if (edge->src == node)
+ merge_target_into_source(pbqp, edge);
+ else
+ merge_source_into_target(pbqp, edge);
+ }
+}
+
void reorder_node(pbqp_node *node)
{
unsigned degree = pbqp_node_get_degree(node);
node_bucket_insert(&node_buckets[degree], node);
}
-#if KAPS_STATISTIC
-void check_melting_possibility(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;
- 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);
-
- if (src_len == 1 && tgt_len == 1) {
- //panic("Something is wrong");
- }
-
- int allRowsOk = 1;
- for (src_index = 0; src_index < src_len; ++src_index) {
- int onlyOneZero = 0;
- if (src_vec->entries[src_index].data == INF_COSTS) {
- continue;
- }
- for (tgt_index = 0; tgt_index < tgt_len; ++tgt_index) {
- if (tgt_vec->entries[tgt_index].data == INF_COSTS) {
- continue;
- }
- if (mat->entries[src_index * tgt_len + tgt_index] == 0) {
- if (onlyOneZero) {
- onlyOneZero = 0;
- break;
- } else {
- onlyOneZero = 1;
- continue;
- }
- }
- if (mat->entries[src_index * tgt_len + tgt_index] == INF_COSTS) {
- continue;
- }
- onlyOneZero = 0;
- break;
- }
- allRowsOk &= onlyOneZero;
- }
-
- int allColsOk = 1;
- for (tgt_index = 0; tgt_index < tgt_len; ++tgt_index) {
- int onlyOneZero = 0;
- if (tgt_vec->entries[tgt_index].data == INF_COSTS) {
- continue;
- }
- for (src_index = 0; src_index < src_len; ++src_index) {
- if (src_vec->entries[src_index].data == INF_COSTS) {
- continue;
- }
- if (mat->entries[src_index * tgt_len + tgt_index] == 0) {
- if (onlyOneZero) {
- onlyOneZero = 0;
- break;
- } else {
- onlyOneZero = 1;
- continue;
- }
- }
- if (mat->entries[src_index * tgt_len + tgt_index] == INF_COSTS) {
- continue;
- }
- onlyOneZero = 0;
- break;
- }
- allColsOk &= onlyOneZero;
- }
-
- if (allRowsOk || allColsOk) {
- pbqp->num_rm++;
- }
-}
-#endif
-
void simplify_edge(pbqp *pbqp, pbqp_edge *edge)
{
pbqp_matrix *mat;
assert(pbqp);
#if KAPS_TIMING
- ir_timer_t *t_int_simpl = ir_timer_register("be_pbqp_init_simp", "PBQP Initial simplify edges");
- ir_timer_reset_and_start(t_int_simpl);
+ ir_timer_t *t_int_simpl = ir_timer_new();
+ ir_timer_start(t_int_simpl);
#endif
#if KAPS_DUMP
#if KAPS_TIMING
ir_timer_stop(t_int_simpl);
- printf("%-20s: %8.3lf msec\n", ir_timer_get_description(t_int_simpl), (double)ir_timer_elapsed_usec(t_int_simpl) / 1000.0);
+ printf("PBQP Initial simplify edges: %8.3lf msec\n", (double)ir_timer_elapsed_usec(t_int_simpl) / 1000.0);
#endif
}
num solution = 0;
#if KAPS_TIMING
- ir_timer_t *t_det_solution = ir_timer_register("be_det_solution", "PBQP Determine Solution");
+ ir_timer_t *t_det_solution = ir_timer_new();
ir_timer_reset_and_start(t_det_solution);
#endif
#if KAPS_TIMING
ir_timer_stop(t_det_solution);
- printf("%-20s: %8.3lf msec\n", ir_timer_get_description(t_det_solution), (double)ir_timer_elapsed_usec(t_det_solution) / 1000.0);
+ printf("PBQP Determine Solution: %8.3lf msec\n", (double)ir_timer_elapsed_usec(t_det_solution) / 1000.0);
#endif
return solution;