Theo corrected a bug in the dbtries implementation

This commit is contained in:
Theo 2014-10-15 19:55:40 +01:00
parent 46e1ce96b4
commit b9236fa5fa
4 changed files with 106 additions and 16 deletions

View File

@ -208,6 +208,7 @@
/* Local Procedures */ /* Local Procedures */
/* -------------------------- */ /* -------------------------- */
static void simplification_reduction(TrEntry trie);
static TrNode depth_reduction(TrEntry trie, TrNode depth_node, YAP_Int opt_level); 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 TrNode breadth_reduction(TrEntry trie, TrNode breadth_node, YAP_Int opt_level);
static inline int compare_label_nodes(TrData data1, TrData data2); static inline int compare_label_nodes(TrData data1, TrData data2);
@ -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_set_trie_db_return_term(YAP_MkAtomTerm(YAP_LookupAtom("false")));
core_initialize_depth_breadth_trie(TrEntry_trie(db_trie), &depth_node, &breadth_node); core_initialize_depth_breadth_trie(TrEntry_trie(db_trie), &depth_node, &breadth_node);
set_depth_breadth_reduction_current_data(NULL); set_depth_breadth_reduction_current_data(NULL);
/* We only need to simplify the trie once! */
simplification_reduction(trie);
while (TrNode_child(TrEntry_trie(trie))) { while (TrNode_child(TrEntry_trie(trie))) {
nested_trie = depth_reduction(trie, depth_node, opt_level); nested_trie = depth_reduction(trie, depth_node, opt_level);
if (nested_trie) { 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 static
TrNode depth_reduction(TrEntry trie, TrNode depth_node, YAP_Int opt_level) { TrNode depth_reduction(TrEntry trie, TrNode depth_node, YAP_Int opt_level) {
TrNode node; 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)); stop_data = TrData_previous(TrEntry_first_data(trie));
data = TrEntry_traverse_data(trie) = TrEntry_last_data(trie); data = TrEntry_traverse_data(trie) = TrEntry_last_data(trie);
while (data != stop_data) { 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); 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)) { 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 //nested trie stop procedure return nested trie node
return 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)); stop_data = TrData_previous(TrEntry_first_data(trie));
data = TrEntry_traverse_data(trie) = TrEntry_last_data(trie); data = TrEntry_traverse_data(trie) = TrEntry_last_data(trie);
while ((data != stop_data) && (data != NULL)) { 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); 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)) { 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 //nested trie stop procedure return nested trie node
return node; return node;

View File

@ -206,13 +206,22 @@
/* Local Procedures */ /* Local Procedures */
/* -------------------------- */ /* -------------------------- */
inline void displaynode(TrNode node);
inline int traverse_get_counter(TrNode node); inline int traverse_get_counter(TrNode node);
inline YAP_Term generate_label(YAP_Int Index); 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 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); 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); 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); 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 inline
void traverse_and_replace_nested_trie(TrNode node, YAP_Int nested_trie_id, YAP_Term new_term) { void traverse_and_replace_nested_trie(TrNode node, YAP_Int nested_trie_id, YAP_Term new_term) {
TrNode child, temp; 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 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; TrNode leaf = node;
@ -534,14 +600,18 @@ TrNode core_breadth_reduction(TrEngine engine, TrNode node, TrNode breadth_node,
YAP_Term t, *stack_top; YAP_Term t, *stack_top;
int count = -1; int count = -1;
TrNode child; 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 */ /* collect breadth nodes */
stack_args_base = stack_args = AUXILIARY_TERM_STACK; stack_args_base = stack_args = AUXILIARY_TERM_STACK;
stack_top = AUXILIARY_TERM_STACK + CURRENT_AUXILIARY_TERM_STACK_SIZE - 1; stack_top = AUXILIARY_TERM_STACK + CURRENT_AUXILIARY_TERM_STACK_SIZE - 1;
node = TrNode_parent(TrNode_parent(node)); node = TrNode_parent(TrNode_parent(node));
// printf("1\n");
// printf("start node: "); displaynode(node); // printf("start node: "); displaynode(node);
if (IS_FUNCTOR_NODE(node)) { if (IS_FUNCTOR_NODE(node)) {
// printf("2\n");
while(IS_FUNCTOR_NODE(node)) while(IS_FUNCTOR_NODE(node))
node = TrNode_parent(node); node = TrNode_parent(node);
child = TrNode_child(node); child = TrNode_child(node);
@ -613,6 +683,7 @@ TrNode core_breadth_reduction(TrEngine engine, TrNode node, TrNode breadth_node,
do { do {
if (TrNode_entry(child) == PairEndTag) { if (TrNode_entry(child) == PairEndTag) {
/* do breadth reduction simplification */ /* do breadth reduction simplification */
printf("I should never arrive here, please contact Theo!\n");
node = TrNode_parent(child); node = TrNode_parent(child);
DATA_DESTRUCT_FUNCTION = destruct_function; DATA_DESTRUCT_FUNCTION = destruct_function;
remove_child_nodes(TrNode_child(node)); 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_parent(child);
} }
child = TrNode_next(child); child = TrNode_next(child);
// printf("Siblings: ");displaynode(child);
} while (child); } while (child);
// printf("pass through\n");
} }
if (!count) { if (!count) {
/* termination condition */ /* 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, t);
node = trie_node_check_insert(node, PairEndTag); node = trie_node_check_insert(node, PairEndTag);
INCREMENT_ENTRIES(CURRENT_TRIE_ENGINE); INCREMENT_ENTRIES(CURRENT_TRIE_ENGINE);
// printf("end node: "); displaynode(node);
return node; return node;
} }

View File

@ -219,6 +219,7 @@ void core_set_label_counter(YAP_Int value);
YAP_Int core_get_label_counter(void); YAP_Int core_get_label_counter(void);
void core_initialize_depth_breadth_trie(TrNode node, TrNode *depth_node, TrNode *breadth_node); 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); 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_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)); 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); YAP_Term core_get_trie_db_return_term(void);

View File

@ -519,6 +519,7 @@ TrNode core_trie_load(TrEngine engine, FILE *file, void (*load_function)(TrNode,
DATA_LOAD_FUNCTION = load_function; DATA_LOAD_FUNCTION = load_function;
node = core_trie_open(engine); node = core_trie_open(engine);
traverse_and_load(node, file); traverse_and_load(node, file);
if (n) n = 0; // just added to remove the warning of not used!
return node; return node;
} }
@ -1450,6 +1451,7 @@ void traverse_and_load(TrNode parent, FILE *file) {
traverse_and_load(child, file); traverse_and_load(child, file);
} while (fscanf(file, "%lu", &t)); } while (fscanf(file, "%lu", &t));
CURRENT_DEPTH--; CURRENT_DEPTH--;
if (n) n = 0; // just added to remove the warning of not used!
return; return;
} }