From 1fe86ba2b350dc82201ad160a2aa534b013abb23 Mon Sep 17 00:00:00 2001 From: Ricardo Rocha Date: Thu, 5 Jan 2012 16:04:32 +0000 Subject: [PATCH] fix parallel_findall/3 --- OPTYap/opt.config.h | 1 - OPTYap/opt.init.c | 4 +- OPTYap/opt.preds.c | 124 ++++++++++---------------------------------- OPTYap/or.structs.h | 2 +- pl/yapor.yap | 13 +++-- 5 files changed, 39 insertions(+), 105 deletions(-) diff --git a/OPTYap/opt.config.h b/OPTYap/opt.config.h index cde5fccd4..7d9ca99ba 100644 --- a/OPTYap/opt.config.h +++ b/OPTYap/opt.config.h @@ -26,7 +26,6 @@ #define THREADS_INDIRECT_BUCKETS ((MAX_THREADS - THREADS_DIRECT_BUCKETS) / THREADS_DIRECT_BUCKETS) /* (1024 - 32) / 32 = 31 */ #define THREADS_NUM_BUCKETS (THREADS_DIRECT_BUCKETS + THREADS_INDIRECT_BUCKETS) #define TG_ANSWER_SLOTS 20 -#define MAX_LENGTH_ANSWER 1000 #define MAX_BRANCH_DEPTH 1000 /********************************************************************** diff --git a/OPTYap/opt.init.c b/OPTYap/opt.init.c index 22a08a1c6..44b7d7cd3 100644 --- a/OPTYap/opt.init.c +++ b/OPTYap/opt.init.c @@ -165,9 +165,9 @@ void Yap_init_global_optyap_data(int max_table_size, int n_workers, int sch_loop void Yap_init_local_optyap_data(int wid) { -#ifdef THREADS_CONSUMER_SHARING +#if defined(YAPOR_THREADS) || defined(THREADS_CONSUMER_SHARING) CACHE_REGS -#endif /* THREADS_CONSUMER_SHARING */ +#endif /* YAPOR_THREADS || THREADS_CONSUMER_SHARING */ #if defined(TABLING) && (defined(YAPOR) || defined(THREADS)) /* local data related to memory management */ diff --git a/OPTYap/opt.preds.c b/OPTYap/opt.preds.c index 2b712c364..4cafc5fd1 100644 --- a/OPTYap/opt.preds.c +++ b/OPTYap/opt.preds.c @@ -57,6 +57,7 @@ static Int p_yapor_start( USES_REGS1 ); static Int p_yapor_workers( USES_REGS1 ); static Int p_worker( USES_REGS1 ); static Int p_parallel_new_answer( USES_REGS1 ); +static Int p_parallel_get_answers( USES_REGS1 ); static Int p_show_statistics_or( USES_REGS1 ); #endif /* YAPOR */ #if defined(YAPOR) && defined(TABLING) @@ -66,9 +67,6 @@ static Int p_get_optyap_statistics( USES_REGS1 ); #ifdef YAPOR static inline realtime current_time(void); -static inline int parallel_new_answer_putchar(int sno, int ch); -static inline void show_answers(void); -static inline void answer_to_stdout(char *answer); #endif /* YAPOR */ #ifdef TABLING @@ -107,12 +105,6 @@ static inline struct page_statistics show_statistics_table_subgoal_answer_frames ** Macros & Declarations ** ************************************/ -#ifdef YAPOR -#define TIME_RESOLUTION 1000000 -static int length_answer; -static qg_ans_fr_ptr actual_answer; -#endif /* YAPOR */ - struct page_statistics { #ifdef USE_PAGES_MALLOC long pages_allocated; /* same as struct pages (opt.structs.h) */ @@ -230,6 +222,7 @@ void Yap_init_optyap_preds(void) { Yap_InitCPred("$c_yapor_workers", 1, p_yapor_workers, SafePredFlag|SyncPredFlag|HiddenPredFlag); Yap_InitCPred("$c_worker", 0, p_worker, SafePredFlag|SyncPredFlag|HiddenPredFlag); Yap_InitCPred("$c_parallel_new_answer", 1, p_parallel_new_answer, SafePredFlag|SyncPredFlag|HiddenPredFlag); + Yap_InitCPred("$c_parallel_get_answers", 1, p_parallel_get_answers, SafePredFlag|SyncPredFlag|HiddenPredFlag); Yap_InitCPred("or_statistics", 1, p_show_statistics_or, SafePredFlag|SyncPredFlag); #endif /* YAPOR */ #if defined(YAPOR) && defined(TABLING) @@ -243,7 +236,6 @@ void Yap_init_optyap_preds(void) { void finish_yapor(void) { GLOBAL_execution_time = current_time() - GLOBAL_execution_time; GLOBAL_parallel_mode = PARALLEL_MODE_ON; - /* show_answers(); */ return; } #endif /* YAPOR */ @@ -727,12 +719,11 @@ static Int p_worker( USES_REGS1 ) { static Int p_parallel_new_answer( USES_REGS1 ) { + qg_ans_fr_ptr actual_answer; or_fr_ptr leftmost_or_fr; - length_answer = 0; ALLOC_QG_ANSWER_FRAME(actual_answer); - Yap_plwrite(ARG1, parallel_new_answer_putchar, 4, 1200); - AnsFr_answer(actual_answer)[length_answer] = 0; + AnsFr_answer(actual_answer) = Deref(ARG1); AnsFr_next(actual_answer) = NULL; leftmost_or_fr = CUT_leftmost_or_frame(); LOCK_OR_FRAME(leftmost_or_fr); @@ -747,6 +738,26 @@ static Int p_parallel_new_answer( USES_REGS1 ) { } +static Int p_parallel_get_answers( USES_REGS1 ){ + Term t = TermNil; + + if (OrFr_qg_solutions(LOCAL_top_or_fr)) { + qg_ans_fr_ptr aux_answer1, aux_answer2; + aux_answer1 = SolFr_first(OrFr_qg_solutions(LOCAL_top_or_fr)); + while (aux_answer1) { + t = MkPairTerm(AnsFr_answer(aux_answer1), t); + aux_answer2 = aux_answer1; + aux_answer1 = AnsFr_next(aux_answer1); + FREE_QG_ANSWER_FRAME(aux_answer2); + } + FREE_QG_SOLUTION_FRAME(OrFr_qg_solutions(LOCAL_top_or_fr)); + OrFr_qg_solutions(LOCAL_top_or_fr) = NULL; + } + Yap_unify(ARG1, t); + return (TRUE); +} + + static Int p_show_statistics_or( USES_REGS1 ) { struct page_statistics stats; long bytes, total_bytes = 0; @@ -1029,95 +1040,16 @@ static Int p_get_optyap_statistics( USES_REGS1 ) { #ifdef YAPOR static inline realtime current_time(void) { +#define TIME_RESOLUTION 1000000 + struct timeval tempo; + gettimeofday(&tempo, NULL); + return ((realtime)tempo.tv_sec + (realtime)tempo.tv_usec / TIME_RESOLUTION); /* to get time as Yap */ /* double now, interval; Yap_cputime_interval(&now, &interval); return ((realtime)now); */ - struct timeval tempo; - gettimeofday(&tempo, NULL); - return ((realtime)tempo.tv_sec + (realtime)tempo.tv_usec / TIME_RESOLUTION); -} - -static inline int parallel_new_answer_putchar(int sno, int ch) { - AnsFr_answer(actual_answer)[length_answer++] = ch; - return ch; -} - - -static inline void show_answers(void) { - CACHE_REGS - int i, answers = 0; - if (OrFr_qg_solutions(LOCAL_top_or_fr)) { - qg_ans_fr_ptr aux_answer1, aux_answer2; - aux_answer1 = SolFr_first(OrFr_qg_solutions(LOCAL_top_or_fr)); - while (aux_answer1) { - answer_to_stdout(AnsFr_answer(aux_answer1)); - aux_answer2 = aux_answer1; - aux_answer1 = AnsFr_next(aux_answer1); - FREE_QG_ANSWER_FRAME(aux_answer2); - answers++; - } - FREE_QG_SOLUTION_FRAME(OrFr_qg_solutions(LOCAL_top_or_fr)); - OrFr_qg_solutions(LOCAL_top_or_fr) = NULL; - } - switch(answers) { - case 0: - Sfprintf(Serror, "[ no answers found"); - break; - case 1: - Sfprintf(Serror, "[ 1 answer found"); - break; - default: - Sfprintf(Serror, "[ %d answers found", answers); - break; - } - Sfprintf(Serror, " (in %f seconds) ]\n\n", GLOBAL_execution_time); - Sflush(Serror); - - return; -} - - -static inline void answer_to_stdout(char *answer) { - int length_answer = 0, length_output = 0, caracter, list, par_rectos; - char output[MAX_LENGTH_ANSWER]; - while (1) { - length_answer += 2; - while (answer[length_answer] != ']') { - length_answer++; - caracter = 0; - while (answer[length_answer] != ',' && answer[length_answer] != ']') - caracter = caracter * 10 + answer[length_answer++] - '0'; - output[length_output++] = caracter; - } - length_answer++; - output[length_output++] = ' '; - output[length_output++] = '='; - output[length_output++] = ' '; - if (answer[length_answer++] == ',') { - list = 1; - output[length_output++] = '['; - } else list = 0; - par_rectos = 1; - while (1) { - if (answer[length_answer] == '[') par_rectos++; - else if (answer[length_answer] == ']' && --par_rectos == 0) break; - output[length_output++] = answer[length_answer++]; - } - if (list) output[length_output++] = ']'; - if (answer[++length_answer] != ']') { - output[length_output++] = ' '; - output[length_output++] = ';'; - output[length_output++] = ' '; - } - else break; - } - output[length_output] = 0; - Sfprintf(Serror, " %s\n", output); - Sflush(Serror); - return; } #endif /* YAPOR */ diff --git a/OPTYap/or.structs.h b/OPTYap/or.structs.h index 05b4ac42c..c9e88df96 100644 --- a/OPTYap/or.structs.h +++ b/OPTYap/or.structs.h @@ -108,7 +108,7 @@ typedef struct query_goal_solution_frame{ ** ---------------------------------------- */ typedef struct query_goal_answer_frame{ - char answer[MAX_LENGTH_ANSWER]; + Term answer; struct query_goal_answer_frame *next; } *qg_ans_fr_ptr; diff --git a/pl/yapor.yap b/pl/yapor.yap index 1620bb6c4..a50af54c0 100644 --- a/pl/yapor.yap +++ b/pl/yapor.yap @@ -129,7 +129,9 @@ parallel_findall(Template,Goal,Answers) :- ( '$parallel_findall_query'(Template,Goal) ; - findall(X,'$parallel_findall_recorded'(X), Answers) + '$c_parallel_get_answers'(Refs), + '$parallel_findall_recorded'(Refs,Answers), + eraseall(parallel_findall) ). parallel_findall(Template,Goal,Answers) :- findall(Template,Goal,Answers). @@ -137,14 +139,15 @@ parallel_findall(Template,Goal,Answers) :- '$parallel_findall_query'(Template,Goal) :- '$c_yapor_start', '$execute'(Goal), - recordz(parallel_findall,Template,_), -%% '$c_parallel_new_answer'(Ref), + recordz(parallel_findall,Template,Ref), + '$c_parallel_new_answer'(Ref), fail. '$parallel_findall_query'(_,_). -'$parallel_findall_recorded'(Template) :- +'$parallel_findall_recorded'([],[]) :- !. +'$parallel_findall_recorded'([Ref|Refs],[Template|Answers]):- recorded(parallel_findall,Template,Ref), - erase(Ref). + '$parallel_findall_recorded'(Refs,Answers).