From 31b3eeaae69ef6fdbcf26d9bcd1a2b5676f2a366 Mon Sep 17 00:00:00 2001 From: Ricardo Rocha Date: Tue, 26 Jul 2011 15:26:01 +0100 Subject: [PATCH 1/3] fix output stream usage --- OPTYap/opt.preds.c | 56 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/OPTYap/opt.preds.c b/OPTYap/opt.preds.c index b6e0ca10f..0e49cf70c 100644 --- a/OPTYap/opt.preds.c +++ b/OPTYap/opt.preds.c @@ -393,17 +393,17 @@ static Int p_show_tabled_predicates( USES_REGS1 ) { IOSTREAM *out; tab_ent_ptr tab_ent; - if ((out = YAP_TermToStream(Deref(ARG1))) == NULL) + if (!PL_get_stream_handle(Yap_InitSlot(Deref(ARG1) PASS_REGS), &out)) return (FALSE); tab_ent = GLOBAL_root_tab_ent; Sfprintf(out, "Tabled predicates\n"); if (tab_ent == NULL) Sfprintf(out, " NONE\n"); - else - while(tab_ent) { - Sfprintf(out, " %s/%d\n", AtomName(TabEnt_atom(tab_ent)), TabEnt_arity(tab_ent)); - tab_ent = TabEnt_next(tab_ent); - } + else while(tab_ent) { + Sfprintf(out, " %s/%d\n", AtomName(TabEnt_atom(tab_ent)), TabEnt_arity(tab_ent)); + tab_ent = TabEnt_next(tab_ent); + } + PL_release_stream(out); return (TRUE); } @@ -413,7 +413,7 @@ static Int p_show_table( USES_REGS1 ) { Term mod, t; tab_ent_ptr tab_ent; - if ((out = YAP_TermToStream(Deref(ARG1))) == NULL) + if (!PL_get_stream_handle(Yap_InitSlot(Deref(ARG1) PASS_REGS), &out)) return (FALSE); mod = Deref(ARG2); t = Deref(ARG3); @@ -421,9 +421,12 @@ static Int p_show_table( USES_REGS1 ) { tab_ent = RepPredProp(PredPropByAtom(AtomOfTerm(t), mod))->TableOfPred; else if (IsApplTerm(t)) tab_ent = RepPredProp(PredPropByFunc(FunctorOfTerm(t), mod))->TableOfPred; - else + else { + PL_release_stream(out); return (FALSE); + } show_table(tab_ent, SHOW_MODE_STRUCTURE, out); + PL_release_stream(out); return (TRUE); } @@ -432,20 +435,31 @@ static Int p_show_all_tables( USES_REGS1 ) { IOSTREAM *out; tab_ent_ptr tab_ent; - if ((out = YAP_TermToStream(Deref(ARG1))) == NULL) + if (!PL_get_stream_handle(Yap_InitSlot(Deref(ARG1) PASS_REGS), &out)) return (FALSE); tab_ent = GLOBAL_root_tab_ent; while(tab_ent) { show_table(tab_ent, SHOW_MODE_STRUCTURE, out); tab_ent = TabEnt_next(tab_ent); } + PL_release_stream(out); return (TRUE); } static Int p_show_all_local_tables( USES_REGS1 ) { #ifdef THREADS + IOSTREAM *out; + tab_ent_ptr tab_ent; + if (!PL_get_stream_handle(Yap_InitSlot(Deref(ARG1) PASS_REGS), &out)) + return (FALSE); + tab_ent = GLOBAL_root_tab_ent; + while(tab_ent) { + show_table(tab_ent, SHOW_MODE_STRUCTURE, out); + tab_ent = TabEnt_next(tab_ent); + } + PL_release_stream(out); #else p_show_all_tables(); #endif /* THREADS */ @@ -456,9 +470,10 @@ static Int p_show_all_local_tables( USES_REGS1 ) { static Int p_show_global_trie( USES_REGS1 ) { IOSTREAM *out; - if ((out = YAP_TermToStream(Deref(ARG1))) == NULL) + if (!PL_get_stream_handle(Yap_InitSlot(Deref(ARG1) PASS_REGS), &out)) return (FALSE); show_global_trie(SHOW_MODE_STRUCTURE, out); + PL_release_stream(out); return (TRUE); } @@ -468,7 +483,7 @@ static Int p_show_statistics_table( USES_REGS1 ) { Term mod, t; tab_ent_ptr tab_ent; - if ((out = YAP_TermToStream(Deref(ARG1))) == NULL) + if (!PL_get_stream_handle(Yap_InitSlot(Deref(ARG1) PASS_REGS), &out)) return (FALSE); mod = Deref(ARG2); t = Deref(ARG3); @@ -476,9 +491,12 @@ static Int p_show_statistics_table( USES_REGS1 ) { tab_ent = RepPredProp(PredPropByAtom(AtomOfTerm(t), mod))->TableOfPred; else if (IsApplTerm(t)) tab_ent = RepPredProp(PredPropByFunc(FunctorOfTerm(t), mod))->TableOfPred; - else + else { + PL_release_stream(out); return (FALSE); + } show_table(tab_ent, SHOW_MODE_STATISTICS, out); + PL_release_stream(out); return (TRUE); } @@ -487,7 +505,7 @@ static Int p_show_statistics_tabling( USES_REGS1 ) { IOSTREAM *out; long total_bytes = 0, aux_bytes; - if ((out = YAP_TermToStream(Deref(ARG1))) == NULL) + if (!PL_get_stream_handle(Yap_InitSlot(Deref(ARG1) PASS_REGS), &out)) return (FALSE); aux_bytes = 0; Sfprintf(out, "Execution data structures\n"); @@ -518,15 +536,17 @@ static Int p_show_statistics_tabling( USES_REGS1 ) { #else Sfprintf(out, "Total memory in use (I+II+III): %10ld bytes\n", total_bytes); #endif /* USE_PAGES_MALLOC */ + PL_release_stream(out); return (TRUE); } static Int p_show_statistics_global_trie( USES_REGS1 ) { IOSTREAM *out; - if ((out = YAP_TermToStream(Deref(ARG1))) == NULL) + if (!PL_get_stream_handle(Yap_InitSlot(Deref(ARG1) PASS_REGS), &out)) return (FALSE); show_global_trie(SHOW_MODE_STATISTICS, out); + PL_release_stream(out); return (TRUE); } #endif /* TABLING */ @@ -592,7 +612,7 @@ static Int p_yapor_workers( USES_REGS1 ) { return Yap_unify(MkIntegerTerm(GLOBAL_number_workers),ARG1); #else return FALSE; -#endif +#endif /* YAPOR_THREADS */ } @@ -628,7 +648,7 @@ static Int p_show_statistics_or( USES_REGS1 ) { IOSTREAM *out; long total_bytes = 0, aux_bytes; - if ((out = YAP_TermToStream(Deref(ARG1))) == NULL) + if (!PL_get_stream_handle(Yap_InitSlot(Deref(ARG1) PASS_REGS), &out)) return (FALSE); aux_bytes = 0; Sfprintf(out, "Execution data structures\n"); @@ -649,6 +669,7 @@ static Int p_show_statistics_or( USES_REGS1 ) { #else Sfprintf(out, "Total memory in use (I+II): %10ld bytes\n", total_bytes); #endif /* USE_PAGES_MALLOC */ + PL_release_stream(out); return (TRUE); } #endif /* YAPOR */ @@ -664,7 +685,7 @@ static Int p_show_statistics_opt( USES_REGS1 ) { IOSTREAM *out; long total_bytes = 0, aux_bytes; - if ((out = YAP_TermToStream(Deref(ARG1))) == NULL) + if (!PL_get_stream_handle(Yap_InitSlot(Deref(ARG1) PASS_REGS), &out)) return (FALSE); aux_bytes = 0; Sfprintf(out, "Execution data structures\n"); @@ -707,6 +728,7 @@ static Int p_show_statistics_opt( USES_REGS1 ) { #else Sfprintf(out, "Total memory in use (I+II+III+IV): %10ld bytes\n", total_bytes); #endif /* USE_PAGES_MALLOC */ + PL_release_stream(out); return (TRUE); } #endif /* YAPOR && TABLING */ From 3345709bdb30654c807e4d1c65a634e1602e7695 Mon Sep 17 00:00:00 2001 From: Ricardo Rocha Date: Tue, 26 Jul 2011 15:51:52 +0100 Subject: [PATCH 2/3] avoid InitWorker twice for main thread --- C/init.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/C/init.c b/C/init.c index 5a3ff9cb5..17d8e2358 100755 --- a/C/init.c +++ b/C/init.c @@ -1205,9 +1205,11 @@ InitCodes(void) #include "ihstruct.h" #if THREADS Yap_InitThread(0); -#endif +#endif /* THREADS */ InitGlobal(); +#if !THREADS InitWorker(0); +#endif /* THREADS */ InitFirstWorkerThreadHandle(); /* make sure no one else can use these two atoms */ CurrentModule = 0; From efc59cf8d62b851f95a9ecfab046e5ccdcf5335c Mon Sep 17 00:00:00 2001 From: Ricardo Rocha Date: Tue, 26 Jul 2011 16:49:49 +0100 Subject: [PATCH 3/3] DepFr_cons field initialization for threads --- C/heapgc.c | 4 ++-- C/threads.c | 3 +++ OPTYap/opt.init.c | 10 +++++++++- OPTYap/opt.structs.h | 2 ++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/C/heapgc.c b/C/heapgc.c index 27848ef8c..d505a7d1f 100755 --- a/C/heapgc.c +++ b/C/heapgc.c @@ -3225,7 +3225,7 @@ compact_heap( USES_REGS1 ) */ #ifdef TABLING - if (depfr != NULL && gc_B >= DepFr_cons_cp(depfr)) { + if (depfr && gc_B >= DepFr_cons_cp(depfr)) { gc_B = DepFr_cons_cp(depfr); depfr = DepFr_next(depfr); } @@ -3408,7 +3408,7 @@ icompact_heap( USES_REGS1 ) */ #ifdef TABLING - if (depfr != NULL && gc_B >= DepFr_cons_cp(depfr)) { + if (depfr && gc_B >= DepFr_cons_cp(depfr)) { gc_B = DepFr_cons_cp(depfr); depfr = DepFr_next(depfr); } diff --git a/C/threads.c b/C/threads.c index 2e4a69658..d9e2701e7 100755 --- a/C/threads.c +++ b/C/threads.c @@ -191,6 +191,9 @@ setup_engine(int myworker_id, int init_thread) GLOBAL_NOfThreadsCreated++; DEBUG_TLOCK_ACCESS(2, myworker_id); pthread_mutex_unlock(&(REMOTE_ThreadHandle(myworker_id).tlock)); +#ifdef TABLING + DepFr_cons_cp(LOCAL_top_dep_fr) = B; /* same as in Yap_init_root_frames() */ +#endif /* TABLING */ } static void diff --git a/OPTYap/opt.init.c b/OPTYap/opt.init.c index 45a84a846..e7104482d 100644 --- a/OPTYap/opt.init.c +++ b/OPTYap/opt.init.c @@ -140,7 +140,9 @@ void Yap_init_global_optyap_data(int max_table_size, int n_workers, int sch_loop GLOBAL_last_sg_fr = NULL; GLOBAL_check_sg_fr = NULL; #endif /* LIMIT_TABLING */ +#ifdef YAPOR new_dependency_frame(GLOBAL_root_dep_fr, FALSE, NULL, NULL, NULL, NULL, NULL); +#endif /* YAPOR */ for (i = 0; i < MAX_TABLE_VARS; i++) { CELL *pt = GLOBAL_table_var_enumerator_addr(i); RESET_VARIABLE(pt); @@ -175,10 +177,12 @@ void Yap_init_local_optyap_data(int wid) { /* local data related to tabling */ REMOTE_next_free_ans_node(wid) = NULL; REMOTE_top_sg_fr(wid) = NULL; - REMOTE_top_dep_fr(wid) = GLOBAL_root_dep_fr; #ifdef YAPOR + REMOTE_top_dep_fr(wid) = GLOBAL_root_dep_fr; Set_REMOTE_top_cp_on_stack(wid, (choiceptr) LOCAL_LocalBase); /* ??? */ REMOTE_top_susp_or_fr(wid) = GLOBAL_root_or_fr; +#else + new_dependency_frame(REMOTE_top_dep_fr(wid), FALSE, NULL, NULL, NULL, NULL, NULL); #endif /* YAPOR */ #endif /* TABLING */ return; @@ -214,7 +218,11 @@ void Yap_init_root_frames(void) { #ifdef TABLING /* root dependency frame */ +#ifdef YAPOR DepFr_cons_cp(GLOBAL_root_dep_fr) = B; +#else + DepFr_cons_cp(LOCAL_top_dep_fr) = B; +#endif /* YAPOR */ #endif /* TABLING */ } diff --git a/OPTYap/opt.structs.h b/OPTYap/opt.structs.h index 59db85dca..9ac8bf3ee 100644 --- a/OPTYap/opt.structs.h +++ b/OPTYap/opt.structs.h @@ -209,7 +209,9 @@ struct global_optyap_data { struct subgoal_frame *last_subgoal_frame; struct subgoal_frame *check_subgoal_frame; #endif /* LIMIT_TABLING */ +#ifdef YAPOR struct dependency_frame *root_dependency_frame; +#endif /* YAPOR */ CELL table_var_enumerator[MAX_TABLE_VARS]; #ifdef TABLE_LOCK_AT_WRITE_LEVEL lockvar table_lock[TABLE_LOCK_BUCKETS];