6 #include "heuristical.h"
8 #include "html_dumper.h"
12 #include "pbqp_edge.h"
13 #include "pbqp_edge_t.h"
14 #include "pbqp_node.h"
15 #include "pbqp_node_t.h"
21 static pbqp_edge **edge_bucket;
22 static pbqp_node **node_buckets[4];
23 static pbqp_node **reduced_bucket = NULL;
24 static int buckets_filled = 0;
30 /* Forward declarations. */
31 static void apply_Brute_Force(pbqp *pbqp);
33 static void insert_into_edge_bucket(pbqp_edge *edge)
35 if (edge_bucket_contains(edge_bucket, edge)) {
36 /* Edge is already inserted. */
40 edge_bucket_insert(&edge_bucket, edge);
43 static void init_buckets(void)
47 edge_bucket_init(&edge_bucket);
48 node_bucket_init(&reduced_bucket);
50 for (i = 0; i < 4; ++i) {
51 node_bucket_init(&node_buckets[i]);
55 static void free_buckets(void)
59 for (i = 0; i < 4; ++i) {
60 node_bucket_free(&node_buckets[i]);
63 edge_bucket_free(&edge_bucket);
64 node_bucket_free(&reduced_bucket);
69 static void fill_node_buckets(pbqp *pbqp)
75 node_len = pbqp->num_nodes;
78 ir_timer_t *t_fill_buckets = ir_timer_register("be_pbqp_fill_buckets", "PBQP Fill Nodes into buckets");
79 ir_timer_reset_and_start(t_fill_buckets);
82 for (node_index = 0; node_index < node_len; ++node_index) {
84 pbqp_node *node = get_node(pbqp, node_index);
88 degree = pbqp_node_get_degree(node);
90 /* We have only one bucket for nodes with arity >= 3. */
95 node_bucket_insert(&node_buckets[degree], node);
101 ir_timer_stop(t_fill_buckets);
102 printf("%-20s: %8.3lf msec\n", ir_timer_get_description(t_fill_buckets), (double)ir_timer_elapsed_usec(t_fill_buckets) / 1000.0);
106 static void normalize_towards_source(pbqp *pbqp, pbqp_edge *edge)
120 src_node = edge->src;
121 tgt_node = edge->tgt;
125 src_vec = src_node->costs;
126 tgt_vec = tgt_node->costs;
130 src_len = src_vec->len;
131 tgt_len = tgt_vec->len;
138 /* Normalize towards source node. */
139 for (src_index = 0; src_index < src_len; ++src_index) {
140 num min = pbqp_matrix_get_row_min(mat, src_index, tgt_vec);
143 if (src_vec->entries[src_index].data == INF_COSTS) {
144 pbqp_matrix_set_row_value(mat, src_index, 0);
146 pbqp_matrix_sub_row_value(mat, src_index, tgt_vec, min);
148 src_vec->entries[src_index].data = pbqp_add(
149 src_vec->entries[src_index].data, min);
151 if (min == INF_COSTS) {
153 unsigned edge_len = pbqp_node_get_degree(src_node);
155 for (edge_index = 0; edge_index < edge_len; ++edge_index) {
156 pbqp_edge *edge_candidate = src_node->edges[edge_index];
157 if (edge_candidate != edge) {
158 insert_into_edge_bucket(edge_candidate);
166 static void normalize_towards_target(pbqp *pbqp, pbqp_edge *edge)
180 src_node = edge->src;
181 tgt_node = edge->tgt;
185 src_vec = src_node->costs;
186 tgt_vec = tgt_node->costs;
190 src_len = src_vec->len;
191 tgt_len = tgt_vec->len;
198 for (tgt_index = 0; tgt_index < tgt_len; ++tgt_index) {
199 num min = pbqp_matrix_get_col_min(mat, tgt_index, src_vec);
202 if (tgt_vec->entries[tgt_index].data == INF_COSTS) {
203 pbqp_matrix_set_col_value(mat, tgt_index, 0);
205 pbqp_matrix_sub_col_value(mat, tgt_index, src_vec, min);
207 tgt_vec->entries[tgt_index].data = pbqp_add(
208 tgt_vec->entries[tgt_index].data, min);
210 if (min == INF_COSTS) {
212 unsigned edge_len = pbqp_node_get_degree(tgt_node);
214 for (edge_index = 0; edge_index < edge_len; ++edge_index) {
215 pbqp_edge *edge_candidate = tgt_node->edges[edge_index];
216 if (edge_candidate != edge) {
217 insert_into_edge_bucket(edge_candidate);
225 static void reorder_node(pbqp_node *node)
227 unsigned degree = pbqp_node_get_degree(node);
228 /* Assume node lost one incident edge. */
229 unsigned old_degree = degree + 1;
231 if (!buckets_filled) return;
233 /* Same bucket as before */
234 if (degree > 2) return;
236 if (!node_bucket_contains(node_buckets[old_degree], node)) {
237 /* Old arity is new arity, so we have nothing to do. */
238 assert(node_bucket_contains(node_buckets[degree], node));
242 /* Delete node from old bucket... */
243 node_bucket_remove(&node_buckets[old_degree], node);
245 /* ..and add to new one. */
246 node_bucket_insert(&node_buckets[degree], node);
250 static void check_melting_possibility(pbqp *pbqp, pbqp_edge *edge)
265 src_node = edge->src;
266 tgt_node = edge->tgt;
270 src_vec = src_node->costs;
271 tgt_vec = tgt_node->costs;
275 src_len = src_vec->len;
276 tgt_len = tgt_vec->len;
283 if (src_len == 1 && tgt_len == 1) {
284 //panic("Something is wrong");
288 for (src_index = 0; src_index < src_len; ++src_index) {
290 if (src_vec->entries[src_index].data == INF_COSTS) {
293 for (tgt_index = 0; tgt_index < tgt_len; ++tgt_index) {
294 if (tgt_vec->entries[tgt_index].data == INF_COSTS) {
297 if (mat->entries[src_index * tgt_len + tgt_index] == 0) {
306 if (mat->entries[src_index * tgt_len + tgt_index] == INF_COSTS) {
312 allRowsOk &= onlyOneZero;
316 for (tgt_index = 0; tgt_index < tgt_len; ++tgt_index) {
318 if (tgt_vec->entries[tgt_index].data == INF_COSTS) {
321 for (src_index = 0; src_index < src_len; ++src_index) {
322 if (src_vec->entries[src_index].data == INF_COSTS) {
325 if (mat->entries[src_index * tgt_len + tgt_index] == 0) {
334 if (mat->entries[src_index * tgt_len + tgt_index] == INF_COSTS) {
340 allColsOk &= onlyOneZero;
343 if (allRowsOk && allColsOk) {
349 static void simplify_edge(pbqp *pbqp, pbqp_edge *edge)
362 src_node = edge->src;
363 tgt_node = edge->tgt;
367 /* If edge are already deleted, we have nothing to do. */
368 if (!is_connected(src_node, edge) || !is_connected(tgt_node, edge))
372 if (pbqp->dump_file) {
374 sprintf(txt, "Simplification of Edge n%d-n%d", src_node->index, tgt_node->index);
375 dump_section(pbqp->dump_file, 3, txt);
379 src_vec = src_node->costs;
380 tgt_vec = tgt_node->costs;
384 src_len = src_vec->len;
385 tgt_len = tgt_vec->len;
393 if (pbqp->dump_file) {
394 fputs("Input:<br>\n", pbqp->dump_file);
395 dump_simplifyedge(pbqp, edge);
399 normalize_towards_source(pbqp, edge);
400 normalize_towards_target(pbqp, edge);
403 if (pbqp->dump_file) {
404 fputs("<br>\nOutput:<br>\n", pbqp->dump_file);
405 dump_simplifyedge(pbqp, edge);
409 if (pbqp_matrix_is_zero(mat, src_vec, tgt_vec)) {
411 if (pbqp->dump_file) {
412 fputs("edge has been eliminated<br>\n", pbqp->dump_file);
423 reorder_node(src_node);
424 reorder_node(tgt_node);
428 static void initial_simplify_edges(pbqp *pbqp)
436 ir_timer_t *t_int_simpl = ir_timer_register("be_pbqp_init_simp", "PBQP Initial simplify edges");
437 ir_timer_reset_and_start(t_int_simpl);
441 if (pbqp->dump_file) {
442 pbqp_dump_input(pbqp);
443 dump_section(pbqp->dump_file, 1, "2. Simplification of Cost Matrices");
447 node_len = pbqp->num_nodes;
451 /* First simplify all edges. */
452 for (node_index = 0; node_index < node_len; ++node_index) {
454 pbqp_node *node = get_node(pbqp, node_index);
461 edge_len = pbqp_node_get_degree(node);
463 for (edge_index = 0; edge_index < edge_len; ++edge_index) {
464 pbqp_edge *edge = edges[edge_index];
466 /* Simplify only once per edge. */
467 if (node != edge->src) continue;
469 simplify_edge(pbqp, edge);
474 ir_timer_stop(t_int_simpl);
475 printf("%-20s: %8.3lf msec\n", ir_timer_get_description(t_int_simpl), (double)ir_timer_elapsed_usec(t_int_simpl) / 1000.0);
479 static num determine_solution(pbqp *pbqp)
486 ir_timer_t *t_det_solution = ir_timer_register("be_det_solution", "PBQP Determine Solution");
487 ir_timer_reset_and_start(t_det_solution);
497 file = pbqp->dump_file;
500 dump_section(file, 1, "4. Determine Solution/Minimum");
501 dump_section(file, 2, "4.1. Trivial Solution");
505 /* Solve trivial nodes and calculate solution. */
506 node_len = node_bucket_get_length(node_buckets[0]);
510 pbqp->num_r0 = node_len;
514 for (node_index = 0; node_index < node_len; ++node_index) {
515 pbqp_node *node = node_buckets[0][node_index];
518 node->solution = vector_get_min_index(node->costs);
519 solution = pbqp_add(solution,
520 node->costs->entries[node->solution].data);
524 fprintf(file, "node n%d is set to %d<br>\n", node->index, node->solution);
525 dump_node(file, node);
532 dump_section(file, 2, "Minimum");
533 #if KAPS_USE_UNSIGNED
534 fprintf(file, "Minimum is equal to %u.", solution);
536 fprintf(file, "Minimum is equal to %lld.", solution);
542 ir_timer_stop(t_det_solution);
543 printf("%-20s: %8.3lf msec\n", ir_timer_get_description(t_det_solution), (double)ir_timer_elapsed_usec(t_det_solution) / 1000.0);
549 static void back_propagate(pbqp *pbqp)
552 unsigned node_len = node_bucket_get_length(reduced_bucket);
557 if (pbqp->dump_file) {
558 dump_section(pbqp->dump_file, 2, "Back Propagation");
562 for (node_index = node_len; node_index > 0; --node_index) {
563 pbqp_node *node = reduced_bucket[node_index - 1];
565 switch (pbqp_node_get_degree(node)) {
567 back_propagate_RI(pbqp, node);
570 back_propagate_RII(pbqp, node);
573 panic("Only nodes with degree one or two should be in this bucket");
579 static void apply_heuristic_reductions(pbqp *pbqp)
582 if (edge_bucket_get_length(edge_bucket) > 0) {
584 } else if (node_bucket_get_length(node_buckets[1]) > 0) {
586 } else if (node_bucket_get_length(node_buckets[2]) > 0) {
588 } else if (node_bucket_get_length(node_buckets[3]) > 0) {
596 static void apply_heuristic_reductions_co(pbqp *pbqp, plist_t *rpeo)
600 ir_timer_t *t_edge = ir_timer_register("be_pbqp_edges", "pbqp reduce independent edges");
601 ir_timer_t *t_r0 = ir_timer_register("be_pbqp_r0", "pbqp R0 reductions");
602 ir_timer_t *t_r1 = ir_timer_register("be_pbqp_r1", "pbqp R1 reductions");
603 ir_timer_t *t_r2 = ir_timer_register("be_pbqp_r2", "pbqp R2 reductions");
604 ir_timer_t *t_rn = ir_timer_register("be_pbqp_rN", "pbqp RN reductions");
607 ir_timer_reset(t_edge);
608 ir_timer_reset(t_r0);
609 ir_timer_reset(t_r1);
610 ir_timer_reset(t_r2);
611 ir_timer_reset(t_rn);
615 if (edge_bucket_get_length(edge_bucket) > 0) {
617 ir_timer_start(t_r0);
625 } else if (node_bucket_get_length(node_buckets[1]) > 0) {
627 ir_timer_start(t_r1);
635 } else if (node_bucket_get_length(node_buckets[2]) > 0) {
637 ir_timer_start(t_r2);
645 } else if (node_bucket_get_length(node_buckets[3]) > 0) {
647 ir_timer_start(t_rn);
650 apply_RN_co(pbqp, rpeo);
657 printf("%-20s: %8.3lf msec\n", ir_timer_get_description(t_edge), (double)ir_timer_elapsed_usec(t_edge) / 1000.0);
658 printf("%-20s: %8.3lf msec\n", ir_timer_get_description(t_r0), (double)ir_timer_elapsed_usec(t_r0) / 1000.0);
659 printf("%-20s: %8.3lf msec\n", ir_timer_get_description(t_r1), (double)ir_timer_elapsed_usec(t_r1) / 1000.0);
660 printf("%-20s: %8.3lf msec\n", ir_timer_get_description(t_r2), (double)ir_timer_elapsed_usec(t_r2) / 1000.0);
661 printf("%-20s: %8.3lf msec\n", ir_timer_get_description(t_rn), (double)ir_timer_elapsed_usec(t_rn) / 1000.0);
669 void solve_pbqp_heuristical(pbqp *pbqp)
671 /* Reduce nodes degree ... */
672 initial_simplify_edges(pbqp);
674 /* ... and put node into bucket representing their degree. */
675 fill_node_buckets(pbqp);
678 FILE *fh = fopen("solutions.pb", "a");
679 fprintf(fh, "Solution");
683 apply_heuristic_reductions(pbqp);
685 pbqp->solution = determine_solution(pbqp);
688 fh = fopen("solutions.pb", "a");
689 fprintf(fh, ": %lld RE:%u R0:%u R1:%u R2:%u RN/BF:%u\n", pbqp->solution,
690 pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2,
695 /* Solve reduced nodes. */
696 back_propagate(pbqp);
701 void solve_pbqp_heuristical_co(pbqp *pbqp, plist_t *rpeo)
703 /* Reduce nodes degree ... */
704 initial_simplify_edges(pbqp);
706 /* ... and put node into bucket representing their degree. */
707 fill_node_buckets(pbqp);
710 FILE *fh = fopen("solutions.pb", "a");
711 fprintf(fh, "Solution");
715 apply_heuristic_reductions_co(pbqp, rpeo);
717 pbqp->solution = determine_solution(pbqp);
720 fh = fopen("solutions.pb", "a");
721 fprintf(fh, ": %lld RE:%u R0:%u R1:%u R2:%u RN/BF:%u\n", pbqp->solution,
722 pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2,
727 /* Solve reduced nodes. */
728 back_propagate(pbqp);
733 void apply_edge(pbqp *pbqp)
735 pbqp_edge *edge = edge_bucket_pop(&edge_bucket);
737 simplify_edge(pbqp, edge);
740 void apply_RI(pbqp *pbqp)
742 pbqp_node *node = node_bucket_pop(&node_buckets[1]);
743 pbqp_edge *edge = node->edges[0];
744 pbqp_matrix *mat = edge->costs;
745 int is_src = edge->src == node;
746 pbqp_node *other_node;
748 assert(pbqp_node_get_degree(node) == 1);
751 other_node = edge->tgt;
753 other_node = edge->src;
757 if (pbqp->dump_file) {
759 sprintf(txt, "RI-Reduction of Node n%d", node->index);
760 dump_section(pbqp->dump_file, 2, txt);
761 pbqp_dump_graph(pbqp);
762 fputs("<br>\nBefore reduction:<br>\n", pbqp->dump_file);
763 dump_node(pbqp->dump_file, node);
764 dump_node(pbqp->dump_file, other_node);
765 dump_edge(pbqp->dump_file, edge);
770 pbqp_matrix_add_to_all_cols(mat, node->costs);
771 normalize_towards_target(pbqp, edge);
773 pbqp_matrix_add_to_all_rows(mat, node->costs);
774 normalize_towards_source(pbqp, edge);
776 disconnect_edge(other_node, edge);
779 if (pbqp->dump_file) {
780 fputs("<br>\nAfter reduction:<br>\n", pbqp->dump_file);
781 dump_node(pbqp->dump_file, other_node);
785 reorder_node(other_node);
793 /* Add node to back propagation list. */
794 node_bucket_insert(&reduced_bucket, node);
797 void apply_RII(pbqp *pbqp)
799 pbqp_node *node = node_bucket_pop(&node_buckets[2]);
800 pbqp_edge *src_edge = node->edges[0];
801 pbqp_edge *tgt_edge = node->edges[1];
802 int src_is_src = src_edge->src == node;
803 int tgt_is_src = tgt_edge->src == node;
804 pbqp_matrix *src_mat;
805 pbqp_matrix *tgt_mat;
820 assert(pbqp_node_get_degree(node) == 2);
823 src_node = src_edge->tgt;
825 src_node = src_edge->src;
829 tgt_node = tgt_edge->tgt;
831 tgt_node = tgt_edge->src;
834 /* Swap nodes if necessary. */
835 if (tgt_node->index < src_node->index) {
847 src_is_src = src_edge->src == node;
848 tgt_is_src = tgt_edge->src == node;
852 if (pbqp->dump_file) {
854 sprintf(txt, "RII-Reduction of Node n%d", node->index);
855 dump_section(pbqp->dump_file, 2, txt);
856 pbqp_dump_graph(pbqp);
857 fputs("<br>\nBefore reduction:<br>\n", pbqp->dump_file);
858 dump_node(pbqp->dump_file, src_node);
859 dump_edge(pbqp->dump_file, src_edge);
860 dump_node(pbqp->dump_file, node);
861 dump_edge(pbqp->dump_file, tgt_edge);
862 dump_node(pbqp->dump_file, tgt_node);
866 src_mat = src_edge->costs;
867 tgt_mat = tgt_edge->costs;
869 src_vec = src_node->costs;
870 tgt_vec = tgt_node->costs;
871 node_vec = node->costs;
873 row_len = src_vec->len;
874 col_len = tgt_vec->len;
875 node_len = node_vec->len;
877 mat = pbqp_matrix_alloc(pbqp, row_len, col_len);
879 for (row_index = 0; row_index < row_len; ++row_index) {
880 for (col_index = 0; col_index < col_len; ++col_index) {
881 vec = vector_copy(pbqp, node_vec);
884 vector_add_matrix_col(vec, src_mat, row_index);
886 vector_add_matrix_row(vec, src_mat, row_index);
890 vector_add_matrix_col(vec, tgt_mat, col_index);
892 vector_add_matrix_row(vec, tgt_mat, col_index);
895 mat->entries[row_index * col_len + col_index] = vector_get_min(vec);
897 obstack_free(&pbqp->obstack, vec);
901 pbqp_edge *edge = get_edge(pbqp, src_node->index, tgt_node->index);
903 /* Disconnect node. */
904 disconnect_edge(src_node, src_edge);
905 disconnect_edge(tgt_node, tgt_edge);
913 /* Add node to back propagation list. */
914 node_bucket_insert(&reduced_bucket, node);
917 edge = alloc_edge(pbqp, src_node->index, tgt_node->index, mat);
920 pbqp_matrix_add(edge->costs, mat);
922 /* Free local matrix. */
923 obstack_free(&pbqp->obstack, mat);
925 reorder_node(src_node);
926 reorder_node(tgt_node);
930 if (pbqp->dump_file) {
931 fputs("<br>\nAfter reduction:<br>\n", pbqp->dump_file);
932 dump_edge(pbqp->dump_file, edge);
936 /* Edge has changed so we simplify it. */
937 simplify_edge(pbqp, edge);
940 static void select_alternative(pbqp_node *node, unsigned selected_index)
946 unsigned max_degree = pbqp_node_get_degree(node);
949 node->solution = selected_index;
950 node_vec = node->costs;
951 node_len = node_vec->len;
952 assert(selected_index < node_len);
954 /* Set all other costs to infinity. */
955 for (node_index = 0; node_index < node_len; ++node_index) {
956 if (node_index != selected_index) {
957 node_vec->entries[node_index].data = INF_COSTS;
961 /* Add all incident edges to edge bucket, since they are now independent. */
962 for (edge_index = 0; edge_index < max_degree; ++edge_index) {
963 insert_into_edge_bucket(node->edges[edge_index]);
967 static pbqp_node *get_node_with_max_degree(void)
969 pbqp_node **bucket = node_buckets[3];
970 unsigned bucket_len = node_bucket_get_length(bucket);
971 unsigned bucket_index;
972 unsigned max_degree = 0;
973 pbqp_node *result = NULL;
975 for (bucket_index = 0; bucket_index < bucket_len; ++bucket_index) {
976 pbqp_node *candidate = bucket[bucket_index];
977 unsigned degree = pbqp_node_get_degree(candidate);
979 if (degree > max_degree) {
988 static unsigned get_local_minimal_alternative(pbqp *pbqp, pbqp_node *node)
995 unsigned max_degree = 0;
998 unsigned min_index = 0;
1004 node_vec = node->costs;
1005 node_len = node_vec->len;
1007 for (node_index = 0; node_index < node_len; ++node_index) {
1008 num value = node_vec->entries[node_index].data;
1010 for (edge_index = 0; edge_index < max_degree; ++edge_index) {
1011 edge = node->edges[edge_index];
1013 is_src = edge->src == node;
1016 vec = vector_copy(pbqp, edge->tgt->costs);
1017 vector_add_matrix_row(vec, mat, node_index);
1019 vec = vector_copy(pbqp, edge->src->costs);
1020 vector_add_matrix_col(vec, mat, node_index);
1023 value = pbqp_add(value, vector_get_min(vec));
1025 obstack_free(&pbqp->obstack, vec);
1030 min_index = node_index;
1037 void apply_RN(pbqp *pbqp)
1039 // printf("### ---- RN\n");
1041 pbqp_node *node = NULL;
1042 unsigned min_index = 0;
1046 /* We want to reduce a node with maximum degree. */
1047 node = get_node_with_max_degree();
1049 assert(pbqp_node_get_degree(node) > 2);
1052 if (pbqp->dump_file) {
1054 sprintf(txt, "RN-Reduction of Node n%d", node->index);
1055 dump_section(pbqp->dump_file, 2, txt);
1056 pbqp_dump_graph(pbqp);
1060 min_index = get_local_minimal_alternative(pbqp, node);
1063 if (pbqp->dump_file) {
1064 fprintf(pbqp->dump_file, "node n%d is set to %d<br><br>\n",
1065 node->index, min_index);
1071 FILE *fh = fopen("solutions.pb", "a");
1072 fprintf(fh, "[%u]", min_index);
1078 /* Now that we found the local minimum set all other costs to infinity. */
1079 select_alternative(node, min_index);
1082 void apply_RN_co(pbqp *pbqp, plist_t *rpeo)
1084 // printf("### ---- RN\n");
1086 pbqp_node *node = NULL;
1087 unsigned min_index = 0;
1091 /* We want to reduce the first node in reverse perfect elimination order. */
1093 /* get first element from reverse perfect elimination order */
1094 node = plist_first(rpeo)->data;
1095 /* remove element from reverse perfect elimination order */
1096 plist_erase(rpeo, plist_first(rpeo));
1097 plist_insert_back(rpeo, node);
1098 } while(node_is_reduced(node));
1100 // node = plist_first(rpeo)->data;
1101 // plist_erase(rpeo, plist_first(rpeo));
1104 assert(pbqp_node_get_degree(node) > 2);
1107 if (pbqp->dump_file) {
1109 sprintf(txt, "RN-Reduction of Node n%d", node->index);
1110 dump_section(pbqp->dump_file, 2, txt);
1111 pbqp_dump_graph(pbqp);
1115 min_index = get_local_minimal_alternative(pbqp, node);
1118 if (pbqp->dump_file) {
1119 fprintf(pbqp->dump_file, "node n%d is set to %d<br><br>\n",
1120 node->index, min_index);
1126 FILE *fh = fopen("solutions.pb", "a");
1127 fprintf(fh, "[%u]", min_index);
1133 /* Now that we found the local minimum set all other costs to infinity. */
1134 select_alternative(node, min_index);
1139 static void apply_brute_force_reductions(pbqp *pbqp)
1142 if (edge_bucket_get_length(edge_bucket) > 0) {
1144 } else if (node_bucket_get_length(node_buckets[1]) > 0) {
1146 } else if (node_bucket_get_length(node_buckets[2]) > 0) {
1148 } else if (node_bucket_get_length(node_buckets[3]) > 0) {
1149 apply_Brute_Force(pbqp);
1156 static unsigned get_minimal_alternative(pbqp *pbqp, pbqp_node *node)
1159 unsigned node_index;
1161 unsigned min_index = 0;
1162 num min = INF_COSTS;
1163 unsigned bucket_index;
1167 node_vec = node->costs;
1168 node_len = node_vec->len;
1169 bucket_index = node->bucket_index;
1171 for (node_index = 0; node_index < node_len; ++node_index) {
1172 pbqp_node_bucket bucket_deg3;
1174 unsigned bucket_0_length;
1175 unsigned bucket_red_length;
1177 char *tmp = obstack_finish(&pbqp->obstack);
1179 node_bucket_init(&bucket_deg3);
1181 /* Some node buckets and the edge bucket should be empty. */
1182 assert(node_bucket_get_length(node_buckets[1]) == 0);
1183 assert(node_bucket_get_length(node_buckets[2]) == 0);
1184 assert(edge_bucket_get_length(edge_bucket) == 0);
1186 /* char *tmp = obstack_finish(&pbqp->obstack); */
1188 /* Save current PBQP state. */
1189 node_bucket_copy(&bucket_deg3, node_buckets[3]);
1190 node_bucket_shrink(&node_buckets[3], 0);
1191 node_bucket_deep_copy(pbqp, &node_buckets[3], bucket_deg3);
1192 node_bucket_update(pbqp, node_buckets[3]);
1193 bucket_0_length = node_bucket_get_length(node_buckets[0]);
1194 bucket_red_length = node_bucket_get_length(reduced_bucket);
1196 /* Select alternative and solve PBQP recursively. */
1197 select_alternative(node_buckets[3][bucket_index], node_index);
1198 apply_brute_force_reductions(pbqp);
1200 value = determine_solution(pbqp);
1204 min_index = node_index;
1207 /* Some node buckets and the edge bucket should still be empty. */
1208 assert(node_bucket_get_length(node_buckets[1]) == 0);
1209 assert(node_bucket_get_length(node_buckets[2]) == 0);
1210 assert(edge_bucket_get_length(edge_bucket) == 0);
1212 /* Clear modified buckets... */
1213 node_bucket_shrink(&node_buckets[3], 0);
1215 /* ... and restore old PBQP state. */
1216 node_bucket_shrink(&node_buckets[0], bucket_0_length);
1217 node_bucket_shrink(&reduced_bucket, bucket_red_length);
1218 node_bucket_copy(&node_buckets[3], bucket_deg3);
1219 node_bucket_update(pbqp, node_buckets[3]);
1222 /* obstack_free(&pbqp->obstack, tmp); */
1223 node_bucket_free(&bucket_deg3);
1225 obstack_free(&pbqp->obstack, tmp);
1231 void apply_Brute_Force(pbqp *pbqp)
1233 pbqp_node *node = NULL;
1234 unsigned min_index = 0;
1238 /* We want to reduce a node with maximum degree. */
1239 node = get_node_with_max_degree();
1241 assert(pbqp_node_get_degree(node) > 2);
1244 if (pbqp->dump_file) {
1246 sprintf(txt, "BF-Reduction of Node n%d", node->index);
1247 dump_section(pbqp->dump_file, 2, txt);
1248 pbqp_dump_graph(pbqp);
1256 min_index = get_minimal_alternative(pbqp, node);
1257 node = pbqp->nodes[node->index];
1260 if (pbqp->dump_file) {
1261 fprintf(pbqp->dump_file, "node n%d is set to %d<br><br>\n",
1262 node->index, min_index);
1269 FILE *fh = fopen("solutions.pb", "a");
1270 fprintf(fh, "[%u]", min_index);
1276 /* Now that we found the minimum set all other costs to infinity. */
1277 select_alternative(node, min_index);
1280 void solve_pbqp_brute_force(pbqp *pbqp)
1282 /* Reduce nodes degree ... */
1283 initial_simplify_edges(pbqp);
1285 /* ... and put node into bucket representing their degree. */
1286 fill_node_buckets(pbqp);
1289 FILE *fh = fopen("solutions.pb", "a");
1290 fprintf(fh, "Solution");
1294 apply_brute_force_reductions(pbqp);
1296 pbqp->solution = determine_solution(pbqp);
1299 fh = fopen("solutions.pb", "a");
1300 fprintf(fh, ": %lld RE:%u R0:%u R1:%u R2:%u RN/BF:%u\n", pbqp->solution,
1301 pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2,
1306 /* Solve reduced nodes. */
1307 back_propagate(pbqp);
1312 void back_propagate_RI(pbqp *pbqp, pbqp_node *node)
1323 edge = node->edges[0];
1325 is_src = edge->src == node;
1332 /* Update pointer for brute force solver. */
1333 other = pbqp->nodes[other->index];
1335 node->solution = pbqp_matrix_get_col_min_index(mat, other->solution, vec);
1340 /* Update pointer for brute force solver. */
1341 other = pbqp->nodes[other->index];
1343 node->solution = pbqp_matrix_get_row_min_index(mat, other->solution, vec);
1347 if (pbqp->dump_file) {
1348 fprintf(pbqp->dump_file, "node n%d is set to %d<br>\n", node->index, node->solution);
1353 void back_propagate_RII(pbqp *pbqp, pbqp_node *node)
1355 pbqp_edge *src_edge = node->edges[0];
1356 pbqp_edge *tgt_edge = node->edges[1];
1357 int src_is_src = src_edge->src == node;
1358 int tgt_is_src = tgt_edge->src == node;
1359 pbqp_matrix *src_mat;
1360 pbqp_matrix *tgt_mat;
1361 pbqp_node *src_node;
1362 pbqp_node *tgt_node;
1371 src_node = src_edge->tgt;
1373 src_node = src_edge->src;
1377 tgt_node = tgt_edge->tgt;
1379 tgt_node = tgt_edge->src;
1382 /* Swap nodes if necessary. */
1383 if (tgt_node->index < src_node->index) {
1384 pbqp_node *tmp_node;
1385 pbqp_edge *tmp_edge;
1387 tmp_node = src_node;
1388 src_node = tgt_node;
1389 tgt_node = tmp_node;
1391 tmp_edge = src_edge;
1392 src_edge = tgt_edge;
1393 tgt_edge = tmp_edge;
1395 src_is_src = src_edge->src == node;
1396 tgt_is_src = tgt_edge->src == node;
1399 /* Update pointer for brute force solver. */
1400 src_node = pbqp->nodes[src_node->index];
1401 tgt_node = pbqp->nodes[tgt_node->index];
1403 src_mat = src_edge->costs;
1404 tgt_mat = tgt_edge->costs;
1406 node_vec = node->costs;
1408 row_index = src_node->solution;
1409 col_index = tgt_node->solution;
1411 vec = vector_copy(pbqp, node_vec);
1414 vector_add_matrix_col(vec, src_mat, row_index);
1416 vector_add_matrix_row(vec, src_mat, row_index);
1420 vector_add_matrix_col(vec, tgt_mat, col_index);
1422 vector_add_matrix_row(vec, tgt_mat, col_index);
1425 node->solution = vector_get_min_index(vec);
1428 if (pbqp->dump_file) {
1429 fprintf(pbqp->dump_file, "node n%d is set to %d<br>\n", node->index, node->solution);
1433 obstack_free(&pbqp->obstack, vec);
1436 int node_is_reduced(pbqp_node *node)
1438 if (!reduced_bucket) return 0;
1440 if (pbqp_node_get_degree(node) == 0) return 1;
1442 return node_bucket_contains(reduced_bucket, node);