4 * Created on: 06.05.2010
10 #include "adt/array.h"
15 #include "heuristical_co_ld.h"
18 #include "html_dumper.h"
22 #include "pbqp_edge.h"
23 #include "pbqp_edge_t.h"
24 #include "pbqp_node.h"
25 #include "pbqp_node_t.h"
31 static void back_propagate_RI(pbqp_t *pbqp, pbqp_node_t *node)
41 edge = node->edges[0];
43 is_src = edge->src == node;
48 node->solution = pbqp_matrix_get_col_min_index(mat, other->solution, vec);
51 node->solution = pbqp_matrix_get_row_min_index(mat, other->solution, vec);
55 if (pbqp->dump_file) {
56 fprintf(pbqp->dump_file, "node n%d is set to %d<br>\n", node->index, node->solution);
61 static void back_propagate_RII(pbqp_t *pbqp, pbqp_node_t *node)
63 pbqp_edge_t *src_edge = node->edges[0];
64 pbqp_edge_t *tgt_edge = node->edges[1];
65 int src_is_src = src_edge->src == node;
66 int tgt_is_src = tgt_edge->src == node;
67 pbqp_matrix_t *src_mat;
68 pbqp_matrix_t *tgt_mat;
69 pbqp_node_t *src_node;
70 pbqp_node_t *tgt_node;
79 src_node = src_edge->tgt;
81 src_node = src_edge->src;
85 tgt_node = tgt_edge->tgt;
87 tgt_node = tgt_edge->src;
90 /* Swap nodes if necessary. */
91 if (tgt_node->index < src_node->index) {
92 pbqp_node_t *tmp_node;
93 pbqp_edge_t *tmp_edge;
103 src_is_src = src_edge->src == node;
104 tgt_is_src = tgt_edge->src == node;
107 src_mat = src_edge->costs;
108 tgt_mat = tgt_edge->costs;
110 node_vec = node->costs;
112 row_index = src_node->solution;
113 col_index = tgt_node->solution;
115 vec = vector_copy(pbqp, node_vec);
118 vector_add_matrix_col(vec, src_mat, row_index);
120 vector_add_matrix_row(vec, src_mat, row_index);
124 vector_add_matrix_col(vec, tgt_mat, col_index);
126 vector_add_matrix_row(vec, tgt_mat, col_index);
129 node->solution = vector_get_min_index(vec);
132 if (pbqp->dump_file) {
133 fprintf(pbqp->dump_file, "node n%d is set to %d<br>\n", node->index, node->solution);
137 obstack_free(&pbqp->obstack, vec);
140 static void back_propagate_RN(pbqp_t *pbqp, pbqp_node_t *node)
142 vector_t *vec = NULL;
143 pbqp_node_t *neighbor = NULL;
144 pbqp_edge_t *edge = NULL;
145 unsigned edge_index = 0;
149 vec = vector_copy(pbqp, node->costs);
151 for(edge_index = 0; edge_index < pbqp_node_get_degree(node); edge_index++) {
152 /* get neighbor node */
153 edge = node->edges[edge_index];
154 neighbor = edge->src == node ? edge->tgt : edge->src;
156 /* node is edge src node */
157 if(edge->src == node)
158 vector_add_matrix_col(vec, edge->costs, neighbor->solution);
159 /* node is edge tgt node */
161 vector_add_matrix_row(vec, edge->costs, neighbor->solution);
164 assert(vector_get_min(vec) != INF_COSTS);
165 node->solution = vector_get_min_index(vec);
168 if (pbqp->dump_file) {
169 fprintf(pbqp->dump_file, "node n%d is set to %d<br>\n", node->index, node->solution);
173 obstack_free(&pbqp->obstack, vec);
176 static void back_propagate_ld(pbqp_t *pbqp)
179 unsigned node_len = node_bucket_get_length(reduced_bucket);
184 if (pbqp->dump_file) {
185 pbqp_dump_section(pbqp->dump_file, 2, "Back Propagation");
189 for (node_index = node_len; node_index > 0; --node_index) {
190 pbqp_node_t *node = reduced_bucket[node_index - 1];
192 switch (pbqp_node_get_degree(node)) {
194 back_propagate_RI(pbqp, node);
197 back_propagate_RII(pbqp, node);
200 back_propagate_RN(pbqp, node);
206 static void merge_into_RN_node(pbqp_t *pbqp, plist_t *rpeo)
208 pbqp_node_t *node = NULL;
213 /* get last element from reverse perfect elimination order */
214 node = (pbqp_node_t*)plist_last(rpeo)->data;
215 /* remove element from reverse perfect elimination order */
216 plist_erase(rpeo, plist_last(rpeo));
217 /* insert node at the beginning of rpeo so the rpeo already exits after pbqp solving */
218 plist_insert_front(rpeo, node);
219 } while(node_is_reduced(node));
221 assert(pbqp_node_get_degree(node) > 2);
223 /* Check whether we can merge a neighbor into the current node. */
224 apply_RM(pbqp, node);
227 static void apply_RN_co_without_selection(pbqp_t *pbqp)
237 if (node_is_reduced(node))
241 if (pbqp->dump_file) {
243 sprintf(txt, "RN-Reduction of Node n%d", node->index);
244 pbqp_dump_section(pbqp->dump_file, 2, txt);
245 pbqp_dump_graph(pbqp);
249 /* Disconnect neighbor nodes */
250 for(edge_index = 0; edge_index < pbqp_node_get_degree(node); edge_index++) {
252 pbqp_node_t *neighbor;
254 /* get neighbor node */
255 edge = node->edges[edge_index];
257 neighbor = edge->src == node ? edge->tgt : edge->src;
259 assert(neighbor != node);
261 if(!is_connected(neighbor, edge))
264 disconnect_edge(neighbor, edge);
265 reorder_node_after_edge_deletion(neighbor);
268 /* Remove node from old bucket */
269 node_bucket_remove(&node_buckets[3], node);
271 /* Add node to back propagation list. */
272 node_bucket_insert(&reduced_bucket, node);
275 static void apply_heuristic_reductions_co(pbqp_t *pbqp, plist_t *rpeo)
279 ir_timer_t *t_edge = ir_timer_new();
280 ir_timer_t *t_r1 = ir_timer_new();
281 ir_timer_t *t_r2 = ir_timer_new();
282 ir_timer_t *t_rn = ir_timer_new();
286 if (edge_bucket_get_length(edge_bucket) > 0) {
288 ir_timer_start(t_edge);
294 ir_timer_stop(t_edge);
296 } else if (node_bucket_get_length(node_buckets[1]) > 0) {
298 ir_timer_start(t_r1);
306 } else if (node_bucket_get_length(node_buckets[2]) > 0) {
308 ir_timer_start(t_r2);
316 } else if (merged_node != NULL) {
318 ir_timer_start(t_rn);
321 apply_RN_co_without_selection(pbqp);
326 } else if (node_bucket_get_length(node_buckets[3]) > 0) {
328 ir_timer_start(t_rn);
331 merge_into_RN_node(pbqp, rpeo);
338 printf("PBQP RE reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_edge) / 1000.0);
339 printf("PBQP R1 reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_r1) / 1000.0);
340 printf("PBQP R2 reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_r2) / 1000.0);
341 printf("PBQP RN reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_rn) / 1000.0);
349 void solve_pbqp_heuristical_co_ld(pbqp_t *pbqp, plist_t *rpeo)
351 /* Reduce nodes degree ... */
352 initial_simplify_edges(pbqp);
354 /* ... and put node into bucket representing their degree. */
355 fill_node_buckets(pbqp);
358 FILE *fh = fopen("solutions.pb", "a");
359 fprintf(fh, "Solution");
363 apply_heuristic_reductions_co(pbqp, rpeo);
365 pbqp->solution = determine_solution(pbqp);
368 fh = fopen("solutions.pb", "a");
369 #if KAPS_USE_UNSIGNED
370 fprintf(fh, ": %u RE:%u R0:%u R1:%u R2:%u RM:%u RN/BF:%u\n", pbqp->solution,
371 pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2,
372 pbqp->num_rm, pbqp->num_rn);
374 fprintf(fh, ": %lld RE:%u R0:%u R1:%u R2:%u RM:%u RN/BF:%u\n", pbqp->solution,
375 pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2,
376 pbqp->num_rm, pbqp->num_rn);
381 /* Solve reduced nodes. */
382 back_propagate_ld(pbqp);