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 *pbqp, pbqp_node *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 *pbqp, pbqp_node *node)
70 pbqp_edge *src_edge = node->edges[0];
71 pbqp_edge *tgt_edge = node->edges[1];
72 int src_is_src = src_edge->src == node;
73 int tgt_is_src = tgt_edge->src == 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) {
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 *pbqp, pbqp_node *node)
150 pbqp_node *neighbor = NULL;
151 pbqp_edge *edge = NULL;
156 vec = vector_copy(pbqp, node->costs);
158 for(idx = 0; idx < pbqp_node_get_degree(node); idx++) {
159 /* get neighbor node */
160 edge = node->edges[idx];
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 *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 *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 apply_RN_co_without_selection(pbqp *pbqp, plist_t *rpeo)
215 pbqp_node *node = NULL;
216 pbqp_node *neighbor = NULL;
217 pbqp_edge *edge = NULL;
223 /* get last element from reverse perfect elimination order */
224 node = plist_last(rpeo)->data;
225 /* remove element from reverse perfect elimination order */
226 plist_erase(rpeo, plist_last(rpeo));
227 /* insert node at the beginning of rpeo so the rpeo already exits after pbqp solving */
228 plist_insert_front(rpeo, node);
229 } while(node_is_reduced(node));
232 assert(pbqp_node_get_degree(node) > 2);
235 if (pbqp->dump_file) {
237 sprintf(txt, "RN-Reduction of Node n%d", node->index);
238 dump_section(pbqp->dump_file, 2, txt);
239 pbqp_dump_graph(pbqp);
243 /* Disconnect neighbor nodes */
244 for(idx = 0; idx < pbqp_node_get_degree(node); idx++) {
245 /* get neighbor node */
246 edge = node->edges[idx];
247 neighbor = edge->src == node ? edge->tgt : edge->src;
249 assert(neighbor != node);
251 if(!is_connected(neighbor, edge))
254 disconnect_edge(neighbor, edge);
255 reorder_node(neighbor);
258 /* Remove node from old bucket */
259 node_bucket_remove(&node_buckets[3], node);
261 /* Add node to back propagation list. */
262 node_bucket_insert(&reduced_bucket, node);
267 static void apply_heuristic_reductions_co(pbqp *pbqp, plist_t *rpeo)
271 ir_timer_t *t_edge = ir_timer_new();
272 ir_timer_t *t_r1 = ir_timer_new();
273 ir_timer_t *t_r2 = ir_timer_new();
274 ir_timer_t *t_rn = ir_timer_new();
278 if (edge_bucket_get_length(edge_bucket) > 0) {
280 ir_timer_start(t_edge);
286 ir_timer_stop(t_edge);
288 } else if (node_bucket_get_length(node_buckets[1]) > 0) {
290 ir_timer_start(t_r1);
298 } else if (node_bucket_get_length(node_buckets[2]) > 0) {
300 ir_timer_start(t_r2);
308 } else if (node_bucket_get_length(node_buckets[3]) > 0) {
310 ir_timer_start(t_rn);
313 apply_RN_co_without_selection(pbqp, rpeo);
320 printf("PBQP RE reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_edge) / 1000.0);
321 printf("PBQP R1 reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_r1) / 1000.0);
322 printf("PBQP R2 reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_r2) / 1000.0);
323 printf("PBQP RN reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_rn) / 1000.0);
331 void solve_pbqp_heuristical_co_ld(pbqp *pbqp, plist_t *rpeo)
333 /* Reduce nodes degree ... */
334 initial_simplify_edges(pbqp);
336 /* ... and put node into bucket representing their degree. */
337 fill_node_buckets(pbqp);
340 FILE *fh = fopen("solutions.pb", "a");
341 fprintf(fh, "Solution");
345 apply_heuristic_reductions_co(pbqp, rpeo);
347 pbqp->solution = determine_solution(pbqp);
350 fh = fopen("solutions.pb", "a");
351 #if KAPS_USE_UNSIGNED
352 fprintf(fh, ": %u RE:%u R0:%u R1:%u R2:%u RM:%u RN/BF:%u\n", pbqp->solution,
353 pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2,
354 pbqp->num_rm, pbqp->num_rn);
356 fprintf(fh, ": %lld RE:%u R0:%u R1:%u R2:%u RM:%u RN/BF:%u\n", pbqp->solution,
357 pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2,
358 pbqp->num_rm, pbqp->num_rn);
363 /* Solve reduced nodes. */
364 back_propagate_ld(pbqp);