X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=heuristical.c;h=98e7c4b2a13df4fd7b28f68aefeb04e7beba9f4c;hb=638dbf4aa72e0fa20c75ff638532afd558b102d0;hp=67bf77b9a9d87ac5e94cc2582327de74febcf4d9;hpb=8d8a101173da60182cf3d84d9b4a90ecdae1abcf;p=libfirm diff --git a/heuristical.c b/heuristical.c index 67bf77b9a..98e7c4b2a 100644 --- a/heuristical.c +++ b/heuristical.c @@ -4,7 +4,9 @@ #include "bucket.h" #include "heuristical.h" +#if KAPS_DUMP #include "html_dumper.h" +#endif #include "kaps.h" #include "matrix.h" #include "pbqp_edge.h" @@ -18,6 +20,10 @@ static pbqp_node **node_buckets[4]; static pbqp_node **reduced_bucket = NULL; static int buckets_filled = 0; +#if KAPS_STATISTIC +static int dump = 0; +#endif + /* Forward declarations. */ static void apply_Brute_Force(pbqp *pbqp); @@ -227,6 +233,7 @@ static void reorder_node(pbqp_node *node) node_bucket_insert(&node_buckets[degree], node); } +#if 0 static void check_melting_possibility(pbqp *pbqp, pbqp_edge *edge) { pbqp_matrix *mat; @@ -324,6 +331,7 @@ static void check_melting_possibility(pbqp *pbqp, pbqp_edge *edge) panic("Hurray"); } } +#endif static void simplify_edge(pbqp *pbqp, pbqp_edge *edge) { @@ -347,11 +355,13 @@ static void simplify_edge(pbqp *pbqp, pbqp_edge *edge) if (!is_connected(src_node, edge) || !is_connected(tgt_node, edge)) return; +#if KAPS_DUMP if (pbqp->dump_file) { char txt[100]; sprintf(txt, "Simplification of Edge n%d-n%d", src_node->index, tgt_node->index); dump_section(pbqp->dump_file, 3, txt); } +#endif src_vec = src_node->costs; tgt_vec = tgt_node->costs; @@ -366,29 +376,39 @@ static void simplify_edge(pbqp *pbqp, pbqp_edge *edge) mat = edge->costs; assert(mat); +#if KAPS_DUMP if (pbqp->dump_file) { fputs("Input:
\n", pbqp->dump_file); dump_simplifyedge(pbqp, edge); } +#endif normalize_towards_source(pbqp, edge); normalize_towards_target(pbqp, edge); +#if KAPS_DUMP if (pbqp->dump_file) { fputs("
\nOutput:
\n", pbqp->dump_file); dump_simplifyedge(pbqp, edge); } +#endif if (pbqp_matrix_is_zero(mat, src_vec, tgt_vec)) { +#if KAPS_DUMP if (pbqp->dump_file) { fputs("edge has been eliminated
\n", pbqp->dump_file); } +#endif + +#if KAPS_STATISTIC + if (dump == 0) { + pbqp->num_edges++; + } +#endif delete_edge(edge); reorder_node(src_node); reorder_node(tgt_node); - } else { - //check_melting_possibility(pbqp, edge); } } @@ -399,10 +419,12 @@ static void initial_simplify_edges(pbqp *pbqp) assert(pbqp); +#if KAPS_DUMP if (pbqp->dump_file) { pbqp_dump_input(pbqp); dump_section(pbqp->dump_file, 1, "2. Simplification of Cost Matrices"); } +#endif node_len = pbqp->num_nodes; @@ -431,19 +453,35 @@ static void initial_simplify_edges(pbqp *pbqp) } } -num determine_solution(FILE *file) +static num determine_solution(pbqp *pbqp) { unsigned node_index; unsigned node_len; - num solution; + num solution = 0; +#if KAPS_DUMP + FILE *file; +#endif + + assert(pbqp); + +#if KAPS_DUMP + file = pbqp->dump_file; if (file) { dump_section(file, 1, "4. Determine Solution/Minimum"); dump_section(file, 2, "4.1. Trivial Solution"); } +#endif /* Solve trivial nodes and calculate solution. */ node_len = node_bucket_get_length(node_buckets[0]); + +#if KAPS_STATISTIC + if (dump == 0) { + pbqp->num_r0 = node_len; + } +#endif + for (node_index = 0; node_index < node_len; ++node_index) { pbqp_node *node = node_buckets[0][node_index]; assert(node); @@ -451,16 +489,21 @@ num determine_solution(FILE *file) node->solution = vector_get_min_index(node->costs); solution = pbqp_add(solution, node->costs->entries[node->solution].data); + +#if KAPS_DUMP if (file) { fprintf(file, "node n%d is set to %d
\n", node->index, node->solution); dump_node(file, node); } +#endif } +#if KAPS_DUMP if (file) { dump_section(file, 2, "Minimum"); fprintf(file, "Minimum is equal to %lld.", solution); } +#endif return solution; } @@ -471,9 +514,12 @@ static void back_propagate(pbqp *pbqp) unsigned node_len = node_bucket_get_length(reduced_bucket); assert(pbqp); + +#if KAPS_DUMP if (pbqp->dump_file) { dump_section(pbqp->dump_file, 2, "Back Propagation"); } +#endif for (node_index = node_len; node_index > 0; --node_index) { pbqp_node *node = reduced_bucket[node_index - 1]; @@ -517,9 +563,23 @@ void solve_pbqp_heuristical(pbqp *pbqp) /* ... and put node into bucket representing their degree. */ fill_node_buckets(pbqp); +#if KAPS_STATISTIC + FILE *fh = fopen("solutions.pb", "a"); + fprintf(fh, "Solution"); + fclose(fh); +#endif + apply_heuristic_reductions(pbqp); - pbqp->solution = determine_solution(pbqp->dump_file); + pbqp->solution = determine_solution(pbqp); + +#if KAPS_STATISTIC + fh = fopen("solutions.pb", "a"); + fprintf(fh, ": %lld RE:%u R0:%u R1:%u R2:%u RN/BF:%u\n", pbqp->solution, + pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2, + pbqp->num_rn); + fclose(fh); +#endif /* Solve reduced nodes. */ back_propagate(pbqp); @@ -550,6 +610,7 @@ void apply_RI(pbqp *pbqp) other_node = edge->src; } +#if KAPS_DUMP if (pbqp->dump_file) { char txt[100]; sprintf(txt, "RI-Reduction of Node n%d", node->index); @@ -560,6 +621,7 @@ void apply_RI(pbqp *pbqp) dump_node(pbqp->dump_file, other_node); dump_edge(pbqp->dump_file, edge); } +#endif if (is_src) { pbqp_matrix_add_to_all_cols(mat, node->costs); @@ -570,13 +632,21 @@ void apply_RI(pbqp *pbqp) } disconnect_edge(other_node, edge); +#if KAPS_DUMP if (pbqp->dump_file) { fputs("
\nAfter reduction:
\n", pbqp->dump_file); dump_node(pbqp->dump_file, other_node); } +#endif reorder_node(other_node); +#if KAPS_STATISTIC + if (dump == 0) { + pbqp->num_r1++; + } +#endif + /* Add node to back propagation list. */ node_bucket_insert(&reduced_bucket, node); } @@ -635,6 +705,7 @@ void apply_RII(pbqp *pbqp) tgt_is_src = tgt_edge->src == node; } +#if KAPS_DUMP if (pbqp->dump_file) { char txt[100]; sprintf(txt, "RII-Reduction of Node n%d", node->index); @@ -647,6 +718,7 @@ void apply_RII(pbqp *pbqp) dump_edge(pbqp->dump_file, tgt_edge); dump_node(pbqp->dump_file, tgt_node); } +#endif src_mat = src_edge->costs; tgt_mat = tgt_edge->costs; @@ -689,6 +761,12 @@ void apply_RII(pbqp *pbqp) disconnect_edge(src_node, src_edge); disconnect_edge(tgt_node, tgt_edge); +#if KAPS_STATISTIC + if (dump == 0) { + pbqp->num_r2++; + } +#endif + /* Add node to back propagation list. */ node_bucket_insert(&reduced_bucket, node); @@ -704,10 +782,12 @@ void apply_RII(pbqp *pbqp) reorder_node(tgt_node); } +#if KAPS_DUMP if (pbqp->dump_file) { fputs("
\nAfter reduction:
\n", pbqp->dump_file); dump_edge(pbqp->dump_file, edge); } +#endif /* Edge has changed so we simplify it. */ simplify_edge(pbqp, edge); @@ -822,19 +902,32 @@ void apply_RN(pbqp *pbqp) assert(node); assert(pbqp_node_get_degree(node) > 2); +#if KAPS_DUMP if (pbqp->dump_file) { char txt[100]; sprintf(txt, "RN-Reduction of Node n%d", node->index); dump_section(pbqp->dump_file, 2, txt); pbqp_dump_graph(pbqp); } +#endif min_index = get_local_minimal_alternative(pbqp, node); +#if KAPS_DUMP if (pbqp->dump_file) { fprintf(pbqp->dump_file, "node n%d is set to %d

\n", node->index, min_index); } +#endif + +#if KAPS_STATISTIC + if (dump == 0) { + FILE *fh = fopen("solutions.pb", "a"); + fprintf(fh, "[%u]", min_index); + fclose(fh); + pbqp->num_rn++; + } +#endif /* Now that we found the local minimum set all other costs to infinity. */ select_alternative(node, min_index); @@ -864,35 +957,44 @@ static unsigned get_minimal_alternative(pbqp *pbqp, pbqp_node *node) unsigned node_len; unsigned min_index = 0; num min = INF_COSTS; + unsigned bucket_index; assert(pbqp); assert(node); - node_vec = node->costs; - node_len = node_vec->len; + node_vec = node->costs; + node_len = node_vec->len; + bucket_index = node->bucket_index; for (node_index = 0; node_index < node_len; ++node_index) { - pbqp_node_bucket bucket_deg0; pbqp_node_bucket bucket_deg3; - pbqp_node_bucket bucket_red; num value; + unsigned bucket_0_length; + unsigned bucket_red_length; + + char *tmp = obstack_finish(&pbqp->obstack); + + node_bucket_init(&bucket_deg3); /* Some node buckets and the edge bucket should be empty. */ assert(node_bucket_get_length(node_buckets[1]) == 0); assert(node_bucket_get_length(node_buckets[2]) == 0); assert(edge_bucket_get_length(edge_bucket) == 0); - char *tmp = obstack_finish(&pbqp->obstack); + /* char *tmp = obstack_finish(&pbqp->obstack); */ /* Save current PBQP state. */ - node_bucket_deep_copy(pbqp, &bucket_deg0, node_buckets[0]); - node_bucket_deep_copy(pbqp, &bucket_deg3, node_buckets[3]); - node_bucket_deep_copy(pbqp, &bucket_red, reduced_bucket); + node_bucket_copy(&bucket_deg3, node_buckets[3]); + node_bucket_shrink(&node_buckets[3], 0); + node_bucket_deep_copy(pbqp, &node_buckets[3], bucket_deg3); + node_bucket_update(pbqp, node_buckets[3]); + bucket_0_length = node_bucket_get_length(node_buckets[0]); + bucket_red_length = node_bucket_get_length(reduced_bucket); /* Select alternative and solve PBQP recursively. */ - select_alternative(node, node_index); + select_alternative(node_buckets[3][bucket_index], node_index); apply_brute_force_reductions(pbqp); - value = determine_solution(pbqp->dump_file); + value = determine_solution(pbqp); if (value < min) { min = value; @@ -905,20 +1007,19 @@ static unsigned get_minimal_alternative(pbqp *pbqp, pbqp_node *node) assert(edge_bucket_get_length(edge_bucket) == 0); /* Clear modified buckets... */ - node_bucket_clear(&node_buckets[0]); - node_bucket_clear(&node_buckets[3]); - node_bucket_clear(&reduced_bucket); + node_bucket_shrink(&node_buckets[3], 0); /* ... and restore old PBQP state. */ - node_bucket_copy(&node_buckets[0], bucket_deg0); + node_bucket_shrink(&node_buckets[0], bucket_0_length); + node_bucket_shrink(&reduced_bucket, bucket_red_length); node_bucket_copy(&node_buckets[3], bucket_deg3); - node_bucket_copy(&reduced_bucket, bucket_red); + node_bucket_update(pbqp, node_buckets[3]); /* Free copies. */ - obstack_free(&pbqp->obstack, tmp); - node_bucket_free(&bucket_deg0); + /* obstack_free(&pbqp->obstack, tmp); */ node_bucket_free(&bucket_deg3); - node_bucket_free(&bucket_red); + + obstack_free(&pbqp->obstack, tmp); } return min_index; @@ -936,19 +1037,38 @@ void apply_Brute_Force(pbqp *pbqp) assert(node); assert(pbqp_node_get_degree(node) > 2); +#if KAPS_DUMP if (pbqp->dump_file) { char txt[100]; sprintf(txt, "BF-Reduction of Node n%d", node->index); dump_section(pbqp->dump_file, 2, txt); pbqp_dump_graph(pbqp); } +#endif + +#if KAPS_STATISTIC + dump++; +#endif min_index = get_minimal_alternative(pbqp, node); + node = pbqp->nodes[node->index]; +#if KAPS_DUMP if (pbqp->dump_file) { fprintf(pbqp->dump_file, "node n%d is set to %d

\n", node->index, min_index); } +#endif + +#if KAPS_STATISTIC + dump--; + if (dump == 0) { + FILE *fh = fopen("solutions.pb", "a"); + fprintf(fh, "[%u]", min_index); + fclose(fh); + pbqp->num_bf++; + } +#endif /* Now that we found the minimum set all other costs to infinity. */ select_alternative(node, min_index); @@ -962,9 +1082,23 @@ void solve_pbqp_brute_force(pbqp *pbqp) /* ... and put node into bucket representing their degree. */ fill_node_buckets(pbqp); +#if KAPS_STATISTIC + FILE *fh = fopen("solutions.pb", "a"); + fprintf(fh, "Solution"); + fclose(fh); +#endif + apply_brute_force_reductions(pbqp); - pbqp->solution = determine_solution(pbqp->dump_file); + pbqp->solution = determine_solution(pbqp); + +#if KAPS_STATISTIC + fh = fopen("solutions.pb", "a"); + fprintf(fh, ": %lld RE:%u R0:%u R1:%u R2:%u RN/BF:%u\n", pbqp->solution, + pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2, + pbqp->num_bf); + fclose(fh); +#endif /* Solve reduced nodes. */ back_propagate(pbqp); @@ -991,17 +1125,26 @@ void back_propagate_RI(pbqp *pbqp, pbqp_node *node) if (is_src) { other = edge->tgt; assert(other); - vector_add_matrix_col(vec, mat, other->solution); + + /* Update pointer for brute force solver. */ + other = pbqp->nodes[other->index]; + + node->solution = pbqp_matrix_get_col_min_index(mat, other->solution, vec); } else { other = edge->src; assert(other); - vector_add_matrix_row(vec, mat, other->solution); + + /* Update pointer for brute force solver. */ + other = pbqp->nodes[other->index]; + + node->solution = pbqp_matrix_get_row_min_index(mat, other->solution, vec); } - node->solution = vector_get_min_index(vec); +#if KAPS_DUMP if (pbqp->dump_file) { fprintf(pbqp->dump_file, "node n%d is set to %d
\n", node->index, node->solution); } +#endif } void back_propagate_RII(pbqp *pbqp, pbqp_node *node) @@ -1050,6 +1193,10 @@ void back_propagate_RII(pbqp *pbqp, pbqp_node *node) tgt_is_src = tgt_edge->src == node; } + /* Update pointer for brute force solver. */ + src_node = pbqp->nodes[src_node->index]; + tgt_node = pbqp->nodes[tgt_node->index]; + src_mat = src_edge->costs; tgt_mat = tgt_edge->costs; @@ -1073,9 +1220,12 @@ void back_propagate_RII(pbqp *pbqp, pbqp_node *node) } node->solution = vector_get_min_index(vec); + +#if KAPS_DUMP if (pbqp->dump_file) { fprintf(pbqp->dump_file, "node n%d is set to %d
\n", node->index, node->solution); } +#endif obstack_free(&pbqp->obstack, vec); }