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)
39 edge = node->edges[0];
41 is_src = edge->src == node;
46 node->solution = pbqp_matrix_get_col_min_index(mat, other->solution, vec);
49 node->solution = pbqp_matrix_get_row_min_index(mat, other->solution, vec);
53 if (pbqp->dump_file) {
54 fprintf(pbqp->dump_file, "node n%d is set to %d<br>\n", node->index, node->solution);
59 static void back_propagate_RII(pbqp_t *pbqp, pbqp_node_t *node)
61 pbqp_edge_t *src_edge = node->edges[0];
62 pbqp_edge_t *tgt_edge = node->edges[1];
63 int src_is_src = src_edge->src == node;
64 int tgt_is_src = tgt_edge->src == node;
65 pbqp_matrix_t *src_mat;
66 pbqp_matrix_t *tgt_mat;
67 pbqp_node_t *src_node;
68 pbqp_node_t *tgt_node;
77 src_node = src_edge->tgt;
79 src_node = src_edge->src;
83 tgt_node = tgt_edge->tgt;
85 tgt_node = tgt_edge->src;
88 /* Swap nodes if necessary. */
89 if (tgt_node->index < src_node->index) {
90 pbqp_node_t *tmp_node;
91 pbqp_edge_t *tmp_edge;
101 src_is_src = src_edge->src == node;
102 tgt_is_src = tgt_edge->src == node;
105 src_mat = src_edge->costs;
106 tgt_mat = tgt_edge->costs;
108 node_vec = node->costs;
110 row_index = src_node->solution;
111 col_index = tgt_node->solution;
113 vec = vector_copy(pbqp, node_vec);
116 vector_add_matrix_col(vec, src_mat, row_index);
118 vector_add_matrix_row(vec, src_mat, row_index);
122 vector_add_matrix_col(vec, tgt_mat, col_index);
124 vector_add_matrix_row(vec, tgt_mat, col_index);
127 node->solution = vector_get_min_index(vec);
130 if (pbqp->dump_file) {
131 fprintf(pbqp->dump_file, "node n%d is set to %d<br>\n", node->index, node->solution);
135 obstack_free(&pbqp->obstack, vec);
138 static void back_propagate_RN(pbqp_t *pbqp, pbqp_node_t *node)
140 vector_t *vec = NULL;
141 pbqp_node_t *neighbor = NULL;
142 pbqp_edge_t *edge = NULL;
143 unsigned edge_index = 0;
147 vec = vector_copy(pbqp, node->costs);
149 for(edge_index = 0; edge_index < pbqp_node_get_degree(node); edge_index++) {
150 /* get neighbor node */
151 edge = node->edges[edge_index];
152 neighbor = edge->src == node ? edge->tgt : edge->src;
154 /* node is edge src node */
155 if(edge->src == node)
156 vector_add_matrix_col(vec, edge->costs, neighbor->solution);
157 /* node is edge tgt node */
159 vector_add_matrix_row(vec, edge->costs, neighbor->solution);
162 assert(vector_get_min(vec) != INF_COSTS);
163 node->solution = vector_get_min_index(vec);
166 if (pbqp->dump_file) {
167 fprintf(pbqp->dump_file, "node n%d is set to %d<br>\n", node->index, node->solution);
171 obstack_free(&pbqp->obstack, vec);
174 static void back_propagate_ld(pbqp_t *pbqp)
177 unsigned node_len = node_bucket_get_length(reduced_bucket);
182 if (pbqp->dump_file) {
183 dump_section(pbqp->dump_file, 2, "Back Propagation");
187 for (node_index = node_len; node_index > 0; --node_index) {
188 pbqp_node_t *node = reduced_bucket[node_index - 1];
190 switch (pbqp_node_get_degree(node)) {
192 back_propagate_RI(pbqp, node);
195 back_propagate_RII(pbqp, node);
198 back_propagate_RN(pbqp, node);
204 static void merge_into_RN_node(pbqp_t *pbqp, plist_t *rpeo)
206 pbqp_node_t *node = NULL;
211 /* get last element from reverse perfect elimination order */
212 node = (pbqp_node_t*)plist_last(rpeo)->data;
213 /* remove element from reverse perfect elimination order */
214 plist_erase(rpeo, plist_last(rpeo));
215 /* insert node at the beginning of rpeo so the rpeo already exits after pbqp solving */
216 plist_insert_front(rpeo, node);
217 } while(node_is_reduced(node));
219 assert(pbqp_node_get_degree(node) > 2);
221 /* Check whether we can merge a neighbor into the current node. */
222 apply_RM(pbqp, node);
225 static void apply_RN_co_without_selection(pbqp_t *pbqp)
235 if (node_is_reduced(node))
239 if (pbqp->dump_file) {
241 sprintf(txt, "RN-Reduction of Node n%d", node->index);
242 dump_section(pbqp->dump_file, 2, txt);
243 pbqp_dump_graph(pbqp);
247 /* Disconnect neighbor nodes */
248 for(edge_index = 0; edge_index < pbqp_node_get_degree(node); edge_index++) {
250 pbqp_node_t *neighbor;
252 /* get neighbor node */
253 edge = node->edges[edge_index];
255 neighbor = edge->src == node ? edge->tgt : edge->src;
257 assert(neighbor != node);
259 if(!is_connected(neighbor, edge))
262 disconnect_edge(neighbor, edge);
263 reorder_node_after_edge_deletion(neighbor);
266 /* Remove node from old bucket */
267 node_bucket_remove(&node_buckets[3], node);
269 /* Add node to back propagation list. */
270 node_bucket_insert(&reduced_bucket, node);
273 static void apply_heuristic_reductions_co(pbqp_t *pbqp, plist_t *rpeo)
277 ir_timer_t *t_edge = ir_timer_new();
278 ir_timer_t *t_r1 = ir_timer_new();
279 ir_timer_t *t_r2 = ir_timer_new();
280 ir_timer_t *t_rn = ir_timer_new();
284 if (edge_bucket_get_length(edge_bucket) > 0) {
286 ir_timer_start(t_edge);
292 ir_timer_stop(t_edge);
294 } else if (node_bucket_get_length(node_buckets[1]) > 0) {
296 ir_timer_start(t_r1);
304 } else if (node_bucket_get_length(node_buckets[2]) > 0) {
306 ir_timer_start(t_r2);
314 } else if (merged_node != NULL) {
316 ir_timer_start(t_rn);
319 apply_RN_co_without_selection(pbqp);
324 } else if (node_bucket_get_length(node_buckets[3]) > 0) {
326 ir_timer_start(t_rn);
329 merge_into_RN_node(pbqp, rpeo);
336 printf("PBQP RE reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_edge) / 1000.0);
337 printf("PBQP R1 reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_r1) / 1000.0);
338 printf("PBQP R2 reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_r2) / 1000.0);
339 printf("PBQP RN reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_rn) / 1000.0);
347 void solve_pbqp_heuristical_co_ld(pbqp_t *pbqp, plist_t *rpeo)
349 /* Reduce nodes degree ... */
350 initial_simplify_edges(pbqp);
352 /* ... and put node into bucket representing their degree. */
353 fill_node_buckets(pbqp);
356 FILE *fh = fopen("solutions.pb", "a");
357 fprintf(fh, "Solution");
361 apply_heuristic_reductions_co(pbqp, rpeo);
363 pbqp->solution = determine_solution(pbqp);
366 fh = fopen("solutions.pb", "a");
367 #if KAPS_USE_UNSIGNED
368 fprintf(fh, ": %u RE:%u R0:%u R1:%u R2:%u RM:%u RN/BF:%u\n", pbqp->solution,
369 pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2,
370 pbqp->num_rm, pbqp->num_rn);
372 fprintf(fh, ": %lld RE:%u R0:%u R1:%u R2:%u RM:%u RN/BF:%u\n", pbqp->solution,
373 pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2,
374 pbqp->num_rm, pbqp->num_rn);
379 /* Solve reduced nodes. */
380 back_propagate_ld(pbqp);