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 if(edge->src == node) /* node is edge src node */
164 vector_add_matrix_col(vec, edge->costs, neighbor->solution);
165 else /* node is edge tgt node */
166 vector_add_matrix_row(vec, edge->costs, neighbor->solution);
169 assert(vector_get_min(vec) != INF_COSTS);
170 node->solution = vector_get_min_index(vec);
173 if (pbqp->dump_file) {
174 fprintf(pbqp->dump_file, "node n%d is set to %d<br>\n", node->index, node->solution);
178 obstack_free(&pbqp->obstack, vec);
181 static void back_propagate_ld(pbqp *pbqp)
184 unsigned node_len = node_bucket_get_length(reduced_bucket);
189 if (pbqp->dump_file) {
190 dump_section(pbqp->dump_file, 2, "Back Propagation");
194 for (node_index = node_len; node_index > 0; --node_index) {
195 pbqp_node *node = reduced_bucket[node_index - 1];
197 switch (pbqp_node_get_degree(node)) {
199 back_propagate_RI(pbqp, node);
202 back_propagate_RII(pbqp, node);
205 back_propagate_RN(pbqp, node);
211 static void apply_RN_co_without_selection(pbqp *pbqp, plist_t *rpeo)
213 pbqp_node *node = NULL;
214 pbqp_node *neighbor = NULL;
215 pbqp_edge *edge = NULL;
221 /* get last element from reverse perfect elimination order */
222 node = plist_last(rpeo)->data;
223 /* remove element from reverse perfect elimination order */
224 plist_erase(rpeo, plist_last(rpeo));
225 /* insert node at the beginning of rpeo so the rpeo already exits after pbqp solving */
226 plist_insert_front(rpeo, node);
227 } while(node_is_reduced(node));
230 assert(pbqp_node_get_degree(node) > 2);
233 if (pbqp->dump_file) {
235 sprintf(txt, "RN-Reduction of Node n%d", node->index);
236 dump_section(pbqp->dump_file, 2, txt);
237 pbqp_dump_graph(pbqp);
241 /* Disconnect neighbor nodes */
242 for(idx = 0; idx < pbqp_node_get_degree(node); idx++) {
243 /* get neighbor node */
244 edge = node->edges[idx];
245 neighbor = edge->src == node ? edge->tgt : edge->src;
247 assert(neighbor != node);
249 if(!is_connected(neighbor, edge))
252 disconnect_edge(neighbor, edge);
253 reorder_node(neighbor);
256 /* Remove node from old bucket */
257 node_bucket_remove(&node_buckets[3], node);
259 /* Add node to back propagation list. */
260 node_bucket_insert(&reduced_bucket, node);
265 static void apply_heuristic_reductions_co(pbqp *pbqp, plist_t *rpeo)
269 ir_timer_t *t_edge = ir_timer_new();
270 ir_timer_t *t_r1 = ir_timer_new();
271 ir_timer_t *t_r2 = ir_timer_new();
272 ir_timer_t *t_rn = ir_timer_new();
276 if (edge_bucket_get_length(edge_bucket) > 0) {
278 ir_timer_start(t_edge);
284 ir_timer_stop(t_edge);
286 } else if (node_bucket_get_length(node_buckets[1]) > 0) {
288 ir_timer_start(t_r1);
296 } else if (node_bucket_get_length(node_buckets[2]) > 0) {
298 ir_timer_start(t_r2);
306 } else if (node_bucket_get_length(node_buckets[3]) > 0) {
308 ir_timer_start(t_rn);
311 apply_RN_co_without_selection(pbqp, rpeo);
318 printf("PBQP RE reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_edge) / 1000.0);
319 printf("PBQP R1 reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_r1) / 1000.0);
320 printf("PBQP R2 reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_r2) / 1000.0);
321 printf("PBQP RN reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_rn) / 1000.0);
329 void solve_pbqp_heuristical_co_ld(pbqp *pbqp, plist_t *rpeo)
331 /* Reduce nodes degree ... */
332 initial_simplify_edges(pbqp);
334 /* ... and put node into bucket representing their degree. */
335 fill_node_buckets(pbqp);
338 FILE *fh = fopen("solutions.pb", "a");
339 fprintf(fh, "Solution");
343 apply_heuristic_reductions_co(pbqp, rpeo);
345 pbqp->solution = determine_solution(pbqp);
348 fh = fopen("solutions.pb", "a");
349 #if KAPS_USE_UNSIGNED
350 fprintf(fh, ": %u RE:%u R0:%u R1:%u R2:%u RM:%u RN/BF:%u\n", pbqp->solution,
351 pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2,
352 pbqp->num_rm, pbqp->num_rn);
354 fprintf(fh, ": %lld RE:%u R0:%u R1:%u R2:%u RM:%u RN/BF:%u\n", pbqp->solution,
355 pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2,
356 pbqp->num_rm, pbqp->num_rn);
361 /* Solve reduced nodes. */
362 back_propagate_ld(pbqp);