diff --git a/library/tries/base_dbtries.c b/library/tries/base_dbtries.c index 917505523..7d54e33f8 100644 --- a/library/tries/base_dbtries.c +++ b/library/tries/base_dbtries.c @@ -208,12 +208,13 @@ /* Local Procedures */ /* -------------------------- */ -static TrNode depth_reduction(TrEntry trie, TrNode depth_node, YAP_Int opt_level); -static TrNode breadth_reduction(TrEntry trie, TrNode breadth_node, YAP_Int opt_level); +static void simplification_reduction(TrEntry trie); +static TrNode depth_reduction(TrEntry trie, TrNode depth_node, YAP_Int opt_level); +static TrNode breadth_reduction(TrEntry trie, TrNode breadth_node, YAP_Int opt_level); static inline int compare_label_nodes(TrData data1, TrData data2); -static inline void move_after(TrData data_source, TrData data_dest); -static inline void move_last_data_after(TrData moveto_data); -static inline void set_depth_breadth_reduction_current_data(TrData data); +static inline void move_after(TrData data_source, TrData data_dest); +static inline void move_last_data_after(TrData moveto_data); +static inline void set_depth_breadth_reduction_current_data(TrData data); /* -------------------------- */ @@ -235,6 +236,8 @@ YAP_Term trie_depth_breadth(TrEntry trie, TrEntry db_trie, YAP_Int opt_level, YA core_set_trie_db_return_term(YAP_MkAtomTerm(YAP_LookupAtom("false"))); core_initialize_depth_breadth_trie(TrEntry_trie(db_trie), &depth_node, &breadth_node); set_depth_breadth_reduction_current_data(NULL); + /* We only need to simplify the trie once! */ + simplification_reduction(trie); while (TrNode_child(TrEntry_trie(trie))) { nested_trie = depth_reduction(trie, depth_node, opt_level); if (nested_trie) { @@ -301,6 +304,27 @@ void set_depth_breadth_reduction_current_data(TrData data) { } +static +void simplification_reduction(TrEntry trie) { + TrNode node; + TrData stop_data, new_data, data = NULL; + stop_data = TrData_previous(TrEntry_first_data(trie)); + data = TrEntry_traverse_data(trie) = TrEntry_last_data(trie); + while ((data != stop_data) && (data != NULL)) { + node = core_simplification_reduction(TRIE_ENGINE, TrData_leaf(data), &trie_data_destruct); + if (node) { + new_trie_data(new_data, trie, node); + PUT_DATA_IN_LEAF_TRIE_NODE(node, new_data); + } + if (data == TrEntry_traverse_data(trie)) { + data = TrData_previous(data); + TrEntry_traverse_data(trie) = data; + } else + data = TrEntry_traverse_data(trie); + } +} + + static TrNode depth_reduction(TrEntry trie, TrNode depth_node, YAP_Int opt_level) { TrNode node; @@ -309,9 +333,7 @@ TrNode depth_reduction(TrEntry trie, TrNode depth_node, YAP_Int opt_level) { stop_data = TrData_previous(TrEntry_first_data(trie)); data = TrEntry_traverse_data(trie) = TrEntry_last_data(trie); while (data != stop_data) { -// printf("hi0\n"); node = core_depth_reduction(TRIE_ENGINE, TrData_leaf(data), depth_node, opt_level, &trie_data_construct, &trie_data_destruct, &trie_data_copy, &trie_data_order_correction); -// printf("bye0\n"); if (node && IS_FUNCTOR_NODE(TrNode_parent(node)) && (strcmp(YAP_AtomName(YAP_NameOfFunctor((YAP_Functor)(~ApplTag & TrNode_entry(TrNode_parent(node))))), NESTED_TRIE_TERM) == 0)) { //nested trie stop procedure return nested trie node return node; @@ -338,9 +360,7 @@ TrNode breadth_reduction(TrEntry trie, TrNode breadth_node, YAP_Int opt_level) { stop_data = TrData_previous(TrEntry_first_data(trie)); data = TrEntry_traverse_data(trie) = TrEntry_last_data(trie); while ((data != stop_data) && (data != NULL)) { -// printf("hi\n"); node = core_breadth_reduction(TRIE_ENGINE, TrData_leaf(data), breadth_node, opt_level, &trie_data_construct, &trie_data_destruct, &trie_data_copy, &trie_data_order_correction); -// printf("bye\n"); if (node && IS_FUNCTOR_NODE(TrNode_parent(node)) && (strcmp(YAP_AtomName(YAP_NameOfFunctor((YAP_Functor)(~ApplTag & TrNode_entry(TrNode_parent(node))))), NESTED_TRIE_TERM) == 0)) { //nested trie stop procedure return nested trie node return node; diff --git a/library/tries/core_dbtries.c b/library/tries/core_dbtries.c index 820d48043..58c096203 100644 --- a/library/tries/core_dbtries.c +++ b/library/tries/core_dbtries.c @@ -206,13 +206,22 @@ /* Local Procedures */ /* -------------------------- */ -inline void displaynode(TrNode node); inline int traverse_get_counter(TrNode node); inline YAP_Term generate_label(YAP_Int Index); YAP_Term update_depth_breadth_trie(TrEngine engine, TrNode root, YAP_Int opt_level, void (*construct_function)(TrNode), void (*destruct_function)(TrNode), void (*copy_function)(TrNode, TrNode), void (*correct_order_function)(void)); YAP_Term get_return_node_term(TrNode node); void traverse_and_replace_nested_trie(TrNode node, YAP_Int nested_trie_id, YAP_Term new_term); TrNode replace_nested_trie(TrNode node, TrNode child, YAP_Term new_term); +inline TrNode get_simplification_sibling(TrNode node); +inline TrNode check_parent_first(TrNode node); +inline TrNode TrNode_myparent(TrNode node); + +/* -------------------------- */ +/* Debug Procedures */ +/* -------------------------- */ + +inline void displaynode(TrNode node); +inline void displayentry(TrNode node); /* -------------------------- */ @@ -274,6 +283,17 @@ void displaynode(TrNode node) { } +inline +void displayentry(TrNode node) { + printf("Entry Contains Bottom Up:\n"); + while (node) { + displaynode(node); + node = TrNode_parent(node); + } + printf("--- End of Entry ---\n"); +} + + inline void traverse_and_replace_nested_trie(TrNode node, YAP_Int nested_trie_id, YAP_Term new_term) { TrNode child, temp; @@ -463,6 +483,52 @@ void core_finalize_depth_breadth_trie(TrNode depth_node, TrNode breadth_node) { } +inline +TrNode get_simplification_sibling(TrNode node) { + TrNode sibling = node; + while (sibling != NULL && TrNode_entry(sibling) != PairEndTag) + sibling = TrNode_next(sibling); + if (sibling != NULL && TrNode_entry(sibling) == PairEndTag) return sibling; + sibling = node; + while (sibling != NULL && TrNode_entry(sibling) != PairEndTag) + sibling = TrNode_previous(sibling); + return sibling; +} + +inline +TrNode check_parent_first(TrNode node) { + TrNode simplification; + if (TrNode_entry(TrNode_myparent(node)) != PairInitTag) { + simplification = check_parent_first(TrNode_myparent(node)); + if (simplification != NULL && TrNode_entry(simplification) == PairEndTag) return simplification; + } + simplification = get_simplification_sibling(node); + return simplification; +} + +inline +TrNode TrNode_myparent(TrNode node) { + TrNode parent = TrNode_parent(node); + while (parent != NULL && IS_FUNCTOR_NODE(parent)) + parent = TrNode_parent(parent); + return parent; +} + +TrNode core_simplification_reduction(TrEngine engine, TrNode node, void (*destruct_function)(TrNode)) { + /* Try to find the greatest parent that has a sibling that is a PairEndTag: this indicates a deep simplification */ + node = check_parent_first(TrNode_myparent(node)); + if (node != NULL) { + /* do breadth reduction simplification */ + node = TrNode_parent(node); + DATA_DESTRUCT_FUNCTION = destruct_function; + remove_child_nodes(TrNode_child(node)); + TrNode_child(node) = NULL; + node = trie_node_check_insert(node, PairEndTag); + INCREMENT_ENTRIES(CURRENT_TRIE_ENGINE); + } + return node; +} + TrNode core_depth_reduction(TrEngine engine, TrNode node, TrNode depth_node, YAP_Int opt_level, void (*construct_function)(TrNode), void (*destruct_function)(TrNode), void (*copy_function)(TrNode, TrNode), void (*correct_order_function)(void)) { TrNode leaf = node; @@ -534,14 +600,18 @@ TrNode core_breadth_reduction(TrEngine engine, TrNode node, TrNode breadth_node, YAP_Term t, *stack_top; int count = -1; TrNode child; + + /* Simplification with breadth reduction (faster dbtrie execution worse BDD) + child = core_simplification_reduction(engine, node, destruct_function); + if (child) return child; + */ + /* collect breadth nodes */ stack_args_base = stack_args = AUXILIARY_TERM_STACK; stack_top = AUXILIARY_TERM_STACK + CURRENT_AUXILIARY_TERM_STACK_SIZE - 1; node = TrNode_parent(TrNode_parent(node)); -// printf("1\n"); // printf("start node: "); displaynode(node); if (IS_FUNCTOR_NODE(node)) { -// printf("2\n"); while(IS_FUNCTOR_NODE(node)) node = TrNode_parent(node); child = TrNode_child(node); @@ -613,6 +683,7 @@ TrNode core_breadth_reduction(TrEngine engine, TrNode node, TrNode breadth_node, do { if (TrNode_entry(child) == PairEndTag) { /* do breadth reduction simplification */ + printf("I should never arrive here, please contact Theo!\n"); node = TrNode_parent(child); DATA_DESTRUCT_FUNCTION = destruct_function; remove_child_nodes(TrNode_child(node)); @@ -676,10 +747,7 @@ TrNode core_breadth_reduction(TrEngine engine, TrNode node, TrNode breadth_node, child = TrNode_parent(child); } child = TrNode_next(child); - // printf("Siblings: ");displaynode(child); - } while (child); -// printf("pass through\n"); } if (!count) { /* termination condition */ @@ -699,7 +767,6 @@ TrNode core_breadth_reduction(TrEngine engine, TrNode node, TrNode breadth_node, node = trie_node_check_insert(node, t); node = trie_node_check_insert(node, PairEndTag); INCREMENT_ENTRIES(CURRENT_TRIE_ENGINE); -// printf("end node: "); displaynode(node); return node; } diff --git a/library/tries/core_dbtries.h b/library/tries/core_dbtries.h index 053a366fe..39b2b42f6 100644 --- a/library/tries/core_dbtries.h +++ b/library/tries/core_dbtries.h @@ -219,6 +219,7 @@ void core_set_label_counter(YAP_Int value); YAP_Int core_get_label_counter(void); void core_initialize_depth_breadth_trie(TrNode node, TrNode *depth_node, TrNode *breadth_node); void core_finalize_depth_breadth_trie(TrNode depth_node, TrNode breadth_node); +TrNode core_simplification_reduction(TrEngine engine, TrNode node, void (*destruct_function)(TrNode)); TrNode core_depth_reduction(TrEngine engine, TrNode node, TrNode depth_node, YAP_Int opt_level, void (*construct_function)(TrNode), void (*destruct_function)(TrNode), void (*copy_function)(TrNode, TrNode), void (*correct_order_function)(void)); TrNode core_breadth_reduction(TrEngine engine, TrNode node, TrNode breadth_node, YAP_Int opt_level, void (*construct_function)(TrNode), void (*destruct_function)(TrNode), void (*copy_function)(TrNode, TrNode), void (*correct_order_function)(void)); YAP_Term core_get_trie_db_return_term(void); diff --git a/library/tries/core_tries.c b/library/tries/core_tries.c index ffc0567d5..7540d8797 100644 --- a/library/tries/core_tries.c +++ b/library/tries/core_tries.c @@ -519,6 +519,7 @@ TrNode core_trie_load(TrEngine engine, FILE *file, void (*load_function)(TrNode, DATA_LOAD_FUNCTION = load_function; node = core_trie_open(engine); traverse_and_load(node, file); + if (n) n = 0; // just added to remove the warning of not used! return node; } @@ -1450,6 +1451,7 @@ void traverse_and_load(TrNode parent, FILE *file) { traverse_and_load(child, file); } while (fscanf(file, "%lu", &t)); CURRENT_DEPTH--; + if (n) n = 0; // just added to remove the warning of not used! return; }