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)
44 edge = node->edges[0];
46 is_src = edge->src == node;
53 node->solution = pbqp_matrix_get_col_min_index(mat, other->solution, vec);
58 node->solution = pbqp_matrix_get_row_min_index(mat, other->solution, vec);
62 if (pbqp->dump_file) {
63 fprintf(pbqp->dump_file, "node n%d is set to %d<br>\n", node->index, node->solution);
68 static void back_propagate_RII(pbqp_t *pbqp, pbqp_node_t *node)
70 pbqp_edge_t *src_edge = node->edges[0];
71 pbqp_edge_t *tgt_edge = node->edges[1];
72 int src_is_src = src_edge->src == node;
73 int tgt_is_src = tgt_edge->src == node;
74 pbqp_matrix_t *src_mat;
75 pbqp_matrix_t *tgt_mat;
76 pbqp_node_t *src_node;
77 pbqp_node_t *tgt_node;
86 src_node = src_edge->tgt;
88 src_node = src_edge->src;
92 tgt_node = tgt_edge->tgt;
94 tgt_node = tgt_edge->src;
97 /* Swap nodes if necessary. */
98 if (tgt_node->index < src_node->index) {
99 pbqp_node_t *tmp_node;
100 pbqp_edge_t *tmp_edge;
110 src_is_src = src_edge->src == node;
111 tgt_is_src = tgt_edge->src == node;
114 src_mat = src_edge->costs;
115 tgt_mat = tgt_edge->costs;
117 node_vec = node->costs;
119 row_index = src_node->solution;
120 col_index = tgt_node->solution;
122 vec = vector_copy(pbqp, node_vec);
125 vector_add_matrix_col(vec, src_mat, row_index);
127 vector_add_matrix_row(vec, src_mat, row_index);
131 vector_add_matrix_col(vec, tgt_mat, col_index);
133 vector_add_matrix_row(vec, tgt_mat, col_index);
136 node->solution = vector_get_min_index(vec);
139 if (pbqp->dump_file) {
140 fprintf(pbqp->dump_file, "node n%d is set to %d<br>\n", node->index, node->solution);
144 obstack_free(&pbqp->obstack, vec);
147 static void back_propagate_RN(pbqp_t *pbqp, pbqp_node_t *node)
149 vector_t *vec = NULL;
150 pbqp_node_t *neighbor = NULL;
151 pbqp_edge_t *edge = NULL;
152 unsigned edge_index = 0;
156 vec = vector_copy(pbqp, node->costs);
158 for(edge_index = 0; edge_index < pbqp_node_get_degree(node); edge_index++) {
159 /* get neighbor node */
160 edge = node->edges[edge_index];
161 neighbor = edge->src == node ? edge->tgt : edge->src;
163 /* node is edge src node */
164 if(edge->src == node)
165 vector_add_matrix_col(vec, edge->costs, neighbor->solution);
166 /* node is edge tgt node */
168 vector_add_matrix_row(vec, edge->costs, neighbor->solution);
171 assert(vector_get_min(vec) != INF_COSTS);
172 node->solution = vector_get_min_index(vec);
175 if (pbqp->dump_file) {
176 fprintf(pbqp->dump_file, "node n%d is set to %d<br>\n", node->index, node->solution);
180 obstack_free(&pbqp->obstack, vec);
183 static void back_propagate_ld(pbqp_t *pbqp)
186 unsigned node_len = node_bucket_get_length(reduced_bucket);
191 if (pbqp->dump_file) {
192 dump_section(pbqp->dump_file, 2, "Back Propagation");
196 for (node_index = node_len; node_index > 0; --node_index) {
197 pbqp_node_t *node = reduced_bucket[node_index - 1];
199 switch (pbqp_node_get_degree(node)) {
201 back_propagate_RI(pbqp, node);
204 back_propagate_RII(pbqp, node);
207 back_propagate_RN(pbqp, node);
213 static void merge_into_RN_node(pbqp_t *pbqp, plist_t *rpeo)
215 pbqp_node_t *node = NULL;
220 /* get last element from reverse perfect elimination order */
221 node = (pbqp_node_t*)plist_last(rpeo)->data;
222 /* remove element from reverse perfect elimination order */
223 plist_erase(rpeo, plist_last(rpeo));
224 /* insert node at the beginning of rpeo so the rpeo already exits after pbqp solving */
225 plist_insert_front(rpeo, node);
226 } while(node_is_reduced(node));
229 assert(pbqp_node_get_degree(node) > 2);
231 /* Check whether we can merge a neighbor into the current node. */
232 apply_RM(pbqp, node);
235 static void apply_RN_co_without_selection(pbqp_t *pbqp)
246 if (node_is_reduced(node))
250 if (pbqp->dump_file) {
252 sprintf(txt, "RN-Reduction of Node n%d", node->index);
253 dump_section(pbqp->dump_file, 2, txt);
254 pbqp_dump_graph(pbqp);
258 /* Disconnect neighbor nodes */
259 for(edge_index = 0; edge_index < pbqp_node_get_degree(node); edge_index++) {
261 pbqp_node_t *neighbor;
263 /* get neighbor node */
264 edge = node->edges[edge_index];
267 neighbor = edge->src == node ? edge->tgt : edge->src;
270 assert(neighbor != node);
272 if(!is_connected(neighbor, edge))
275 disconnect_edge(neighbor, edge);
276 reorder_node_after_edge_deletion(neighbor);
279 /* Remove node from old bucket */
280 node_bucket_remove(&node_buckets[3], node);
282 /* Add node to back propagation list. */
283 node_bucket_insert(&reduced_bucket, node);
286 static void apply_heuristic_reductions_co(pbqp_t *pbqp, plist_t *rpeo)
290 ir_timer_t *t_edge = ir_timer_new();
291 ir_timer_t *t_r1 = ir_timer_new();
292 ir_timer_t *t_r2 = ir_timer_new();
293 ir_timer_t *t_rn = ir_timer_new();
297 if (edge_bucket_get_length(edge_bucket) > 0) {
299 ir_timer_start(t_edge);
305 ir_timer_stop(t_edge);
307 } else if (node_bucket_get_length(node_buckets[1]) > 0) {
309 ir_timer_start(t_r1);
317 } else if (node_bucket_get_length(node_buckets[2]) > 0) {
319 ir_timer_start(t_r2);
327 } else if (merged_node != NULL) {
329 ir_timer_start(t_rn);
332 apply_RN_co_without_selection(pbqp);
337 } else if (node_bucket_get_length(node_buckets[3]) > 0) {
339 ir_timer_start(t_rn);
342 merge_into_RN_node(pbqp, rpeo);
349 printf("PBQP RE reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_edge) / 1000.0);
350 printf("PBQP R1 reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_r1) / 1000.0);
351 printf("PBQP R2 reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_r2) / 1000.0);
352 printf("PBQP RN reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_rn) / 1000.0);
360 void solve_pbqp_heuristical_co_ld(pbqp_t *pbqp, plist_t *rpeo)
362 /* Reduce nodes degree ... */
363 initial_simplify_edges(pbqp);
365 /* ... and put node into bucket representing their degree. */
366 fill_node_buckets(pbqp);
369 FILE *fh = fopen("solutions.pb", "a");
370 fprintf(fh, "Solution");
374 apply_heuristic_reductions_co(pbqp, rpeo);
376 pbqp->solution = determine_solution(pbqp);
379 fh = fopen("solutions.pb", "a");
380 #if KAPS_USE_UNSIGNED
381 fprintf(fh, ": %u RE:%u R0:%u R1:%u R2:%u RM:%u RN/BF:%u\n", pbqp->solution,
382 pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2,
383 pbqp->num_rm, pbqp->num_rn);
385 fprintf(fh, ": %lld RE:%u R0:%u R1:%u R2:%u RM:%u RN/BF:%u\n", pbqp->solution,
386 pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2,
387 pbqp->num_rm, pbqp->num_rn);
392 /* Solve reduced nodes. */
393 back_propagate_ld(pbqp);