From d1327dba9c768613313e536cb0b1a1d348701894 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Santos=20Costa?= Date: Tue, 5 Apr 2011 10:01:34 +0100 Subject: [PATCH 1/5] memcpy does not cope well with overlapped segments. --- H/trim_trail.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/H/trim_trail.h b/H/trim_trail.h index 016d61efe..81233c769 100644 --- a/H/trim_trail.h +++ b/H/trim_trail.h @@ -83,7 +83,7 @@ int size; pt0++; size = TR - pt0; - memcpy(pbase, pt0, size * sizeof(struct trail_frame)); + memmove(pbase, pt0, size * sizeof(struct trail_frame)); TR = pbase + size; } } From 26dc567a0f9ee857630d39b57e76cda04e369d72 Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Fri, 8 Apr 2011 11:58:22 +0100 Subject: [PATCH 2/5] fix bad overflow handling. --- C/index.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/C/index.c b/C/index.c index ad45f6e57..10ebf5263 100644 --- a/C/index.c +++ b/C/index.c @@ -839,6 +839,7 @@ sort_group(GroupDef *grp, CELL *top, struct intermediates *cint) base = top; while (top+2*max > (CELL *)Yap_TrailTop) { if (!Yap_growtrail(2*max*CellSize, TRUE)) { + Yap_Error_Size = 2*max*CellSize; save_machine_regs(); siglongjmp(cint->CompilerBotch,4); return; @@ -2066,6 +2067,7 @@ groups_in(ClauseDef *min, ClauseDef *max, GroupDef *grp, struct intermediates *c siglongjmp(cint->CompilerBotch,4); #else if (!Yap_growtrail(sz, TRUE)) { + Yap_Error_Size = sz; save_machine_regs(); siglongjmp(cint->CompilerBotch,4); return 0; @@ -2194,6 +2196,7 @@ emit_switch_space(UInt n, UInt item_size, struct intermediates *cint, CELL func_ UInt sz = sizeof(LogUpdIndex)+n*item_size; LogUpdIndex *cl = (LogUpdIndex *)Yap_AllocCodeSpace(sz); if (cl == NULL) { + Yap_Error_Size = sz; /* grow stack */ save_machine_regs(); siglongjmp(cint->CompilerBotch,2); @@ -2214,6 +2217,7 @@ emit_switch_space(UInt n, UInt item_size, struct intermediates *cint, CELL func_ UInt sz = sizeof(StaticIndex)+n*item_size; StaticIndex *cl = (StaticIndex *)Yap_AllocCodeSpace(sz); if (cl == NULL) { + Yap_Error_Size = sz; /* grow stack */ save_machine_regs(); siglongjmp(cint->CompilerBotch,2); From b11a3f894cefe7a7da2fe2423e11fc980cef0c52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Santos=20Costa?= Date: Wed, 13 Apr 2011 14:49:03 +0100 Subject: [PATCH 3/5] make sure to initialise both fields in trail entry (helpful for valgrind). --- H/amiops.h | 6 +++--- packages/YapR | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/H/amiops.h b/H/amiops.h index 2498a0e17..ec4818797 100644 --- a/H/amiops.h +++ b/H/amiops.h @@ -267,9 +267,9 @@ Binding Macros for Multiple Assignment Variables. #define REF_TO_TRENTRY(REF) AbsPair(((CELL *)&((REF)->Flags))) #define CLREF_TO_TRENTRY(REF) AbsPair(((CELL *)&((REF)->ClFlags))) -#define TRAIL_REF(REF) TrailTerm(TR++) = REF_TO_TRENTRY(REF) -#define TRAIL_CLREF(REF) TrailTerm(TR++) = CLREF_TO_TRENTRY(REF) -#define TRAIL_LINK(REF) TrailTerm(TR++) = AbsPair((CELL *)(REF)) +#define TRAIL_REF(REF) RESET_VARIABLE(&TrailVal(TR)), TrailTerm(TR++) = REF_TO_TRENTRY(REF) +#define TRAIL_CLREF(REF) RESET_VARIABLE(&TrailVal(TR)), TrailTerm(TR++) = CLREF_TO_TRENTRY(REF) +#define TRAIL_LINK(REF) RESET_VARIABLE(&TrailVal(TR)), TrailTerm(TR++) = AbsPair((CELL *)(REF)) #define TRAIL_FRAME(FR) DO_TRAIL(AbsPair((CELL *)(Yap_TrailBase)), FR) extern void Yap_WakeUp(CELL *v); diff --git a/packages/YapR b/packages/YapR index 5c2419f04..90a2cd308 160000 --- a/packages/YapR +++ b/packages/YapR @@ -1 +1 @@ -Subproject commit 5c2419f04dcd32f6929be1785621ed57918af1a4 +Subproject commit 90a2cd3082694f77d9ac5568b0f9d57cb7c27af5 From 4de6a9e3ef39526e5282a1f12f55a00007791379 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Santos=20Costa?= Date: Thu, 14 Apr 2011 18:49:55 +0100 Subject: [PATCH 4/5] update configuration options for threads/or-parallelism. --- configure | 53 ++++++++++++++++++++++++++++++++++++++-------------- configure.in | 48 +++++++++++++++++++++++++++++++++-------------- 2 files changed, 73 insertions(+), 28 deletions(-) diff --git a/configure b/configure index cb0bab7a8..e5d5eef48 100755 --- a/configure +++ b/configure @@ -1425,7 +1425,7 @@ Optional Features: --disable-FEATURE do not include FEATURE (same as --enable-FEATURE=no) --enable-FEATURE[=ARG] include FEATURE [ARG=yes] --enable-tabling support tabling - --enable-or-parallelism support or-parallelism as: env-copy,sba,a-cow + --enable-or-parallelism support or-parallelism as: copy,sba,a-cow,threads --enable-rational-trees support infinite rational trees --enable-coroutining support co-routining, attributed variables and constraints --enable-depth-limit support depth-bound computation @@ -5962,8 +5962,6 @@ fi if test "$threads" = yes then - $as_echo "#define SUPPORT_THREADS 1" >>confdefs.h - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for pthread_create in -lpthread" >&5 $as_echo_n "checking for pthread_create in -lpthread... " >&6; } if test "${ac_cv_lib_pthread_pthread_create+set}" = set; then : @@ -7488,19 +7486,46 @@ if test "$threads" = "yes" then YAP_EXTRAS="$YAP_EXTRAS -D_GNU_SOURCE" fi + case "$orparallelism" in + sba) + { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5 +$as_echo "$as_me: error: in \`$ac_pwd':" >&2;} +as_fn_error $? "--or-parallelism=sba incompatible with threads + ;; + a-cow +See \`config.log' for more details" "$LINENO" 5 ; } + { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5 +$as_echo "$as_me: error: in \`$ac_pwd':" >&2;} +as_fn_error $? "--or-parallelism=a-cow incompatible with threads + ;; + copy +See \`config.log' for more details" "$LINENO" 5 ; } + { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5 +$as_echo "$as_me: error: in \`$ac_pwd':" >&2;} +as_fn_error $? "--or-parallelism=copy incompatible with threads + ;; + yes|threads +See \`config.log' for more details" "$LINENO" 5 ; } + YAP_EXTRAS="$YAP_EXTRAS -DYAPOR_THREADS=1" + ;; + esac +else + case "$orparallelism" in + sba) + YAP_EXTRAS="$YAP_EXTRAS -DYAPOR_SBA=1" + ;; + a-cow) + YAP_EXTRAS="$YAP_EXTRAS -DYAPOR_COW=1" + ;; + threads) + YAP_EXTRAS="$YAP_EXTRAS -DYAPOR_THREADS=1" + ;; + yes|copy) + YAP_EXTRAS="$YAP_EXTRAS -DYAPOR_COPY=1" + ;; + esac fi -case "$orparallelism" in - sba) - YAP_EXTRAS="$YAP_EXTRAS -DYAPOR_SBA=1" - ;; - a-cow) - YAP_EXTRAS="$YAP_EXTRAS -DYAPOR_COW=1" - ;; - yes|env-copy) - YAP_EXTRAS="$YAP_EXTRAS -DYAPOR_COPY=1" - ;; -esac if test "$cutc" = "yes" then diff --git a/configure.in b/configure.in index b4a4fe504..e407453b5 100644 --- a/configure.in +++ b/configure.in @@ -60,7 +60,7 @@ AC_ARG_ENABLE(tabling, [ --enable-tabling support tabling ], tabling="$enableval", tabling=no) AC_ARG_ENABLE(or-parallelism, - [ --enable-or-parallelism support or-parallelism as: env-copy,sba,a-cow ], + [ --enable-or-parallelism support or-parallelism as: copy,sba,a-cow,threads ], orparallelism="$enableval", orparallelism=no) AC_ARG_ENABLE(rational-trees, [ --enable-rational-trees support infinite rational trees ], @@ -733,7 +733,6 @@ fi if test "$threads" = yes then - AC_DEFINE(SUPPORT_THREADS, 1) AC_CHECK_LIB(pthread,pthread_create) AC_CHECK_HEADERS(pthread.h) AC_CHECK_FUNCS(pthread_mutexattr_setkind_np pthread_mutexattr_settype) @@ -1271,20 +1270,41 @@ if test "$threads" = "yes" if test "$GCC" = "yes" then YAP_EXTRAS="$YAP_EXTRAS -D_GNU_SOURCE" - fi + fi + case "$orparallelism" in + sba) + AC_MSG_FAILURE( + [--or-parallelism=sba incompatible with threads] + ;; + a-cow) + AC_MSG_FAILURE( + [--or-parallelism=a-cow incompatible with threads] + ;; + copy) + AC_MSG_FAILURE( + [--or-parallelism=copy incompatible with threads] + ;; + yes|threads) + YAP_EXTRAS="$YAP_EXTRAS -DYAPOR_THREADS=1" + ;; + esac +else + case "$orparallelism" in + sba) + YAP_EXTRAS="$YAP_EXTRAS -DYAPOR_SBA=1" + ;; + a-cow) + YAP_EXTRAS="$YAP_EXTRAS -DYAPOR_COW=1" + ;; + threads) + YAP_EXTRAS="$YAP_EXTRAS -DYAPOR_THREADS=1" + ;; + yes|copy) + YAP_EXTRAS="$YAP_EXTRAS -DYAPOR_COPY=1" + ;; + esac fi -case "$orparallelism" in - sba) - YAP_EXTRAS="$YAP_EXTRAS -DYAPOR_SBA=1" - ;; - a-cow) - YAP_EXTRAS="$YAP_EXTRAS -DYAPOR_COW=1" - ;; - yes|env-copy) - YAP_EXTRAS="$YAP_EXTRAS -DYAPOR_COPY=1" - ;; -esac if test "$cutc" = "yes" then From 8fb8aec9bff92dc67470ea5578e81bc32dd8be58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Santos=20Costa?= Date: Thu, 14 Apr 2011 18:51:11 +0100 Subject: [PATCH 5/5] MULTIPLE_STACKS - or-parallelism/threads/tabling YAPOR_THREADS - threads with or-parallelism make tabling require reference counting for dynamic predicates. --- C/absmi.c | 28 +++++++++++++++++----------- C/amasm.c | 6 ++++-- C/arrays.c | 6 +++--- C/cdmgr.c | 16 ++++++++-------- C/dbase.c | 36 ++++++++++++++++++------------------ C/globals.c | 12 ++++++------ C/grow.c | 24 ++++++++++++------------ C/index.c | 7 ++++--- C/tracer.c | 4 ++++ C/utilpreds.c | 2 +- H/Yap.h | 12 ++++++++++-- H/Yatom.h | 4 +++- H/amiops.h | 6 ++++++ H/clause.h | 2 +- config.h.in | 1 - misc/LOCALS | 2 +- 16 files changed, 98 insertions(+), 70 deletions(-) diff --git a/C/absmi.c b/C/absmi.c index d8e3a2f17..2bea1c218 100755 --- a/C/absmi.c +++ b/C/absmi.c @@ -1086,7 +1086,7 @@ Yap_absmi(int inp) } /* HEY, leave indexing block alone!! */ /* check if we are the ones using this code */ -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS PELOCK(1, ap); PP = ap; DEC_CLREF_COUNT(cl); @@ -1387,7 +1387,7 @@ Yap_absmi(int inp) } /* HEY, leave indexing block alone!! */ /* check if we are the ones using this code */ -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS PELOCK(2, ap); PP = ap; DEC_CLREF_COUNT(cl); @@ -1480,7 +1480,7 @@ Yap_absmi(int inp) /* only meaningful with THREADS on! */ /* lock logical updates predicate. */ Op(lock_lu, p); -#if defined(YAPOR) || defined(THREADS) +#if PARALLEL_YAP if (PP) { GONext(); } @@ -1508,10 +1508,12 @@ Yap_absmi(int inp) check_trail(TR); /* say that an environment is using this clause */ /* we have our own copy for the clause */ -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS { LogUpdClause *cl = PREG->u.L.ClBase; +#if PARALLEL_YAP PredEntry *ap = cl->ClPred; +#endif /* always add an extra reference */ INC_CLREF_COUNT(cl); @@ -1581,7 +1583,7 @@ Yap_absmi(int inp) } setregs(); -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS /* always add an extra reference */ INC_CLREF_COUNT(cl); TRAIL_CLREF(cl); @@ -1633,7 +1635,7 @@ Yap_absmi(int inp) /* say that an environment is using this clause */ /* we have our own copy for the clause */ -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS /* always add an extra reference */ INC_CLREF_COUNT(cl); TRAIL_CLREF(cl); @@ -1746,7 +1748,7 @@ Yap_absmi(int inp) #endif /* YAPOR */ SET_BB(B_YREG); ENDCACHE_Y(); -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS INC_CLREF_COUNT(ClauseCodeToDynamicClause(PREG)); UNLOCK(DynamicLock(PREG)); TRAIL_CLREF(ClauseCodeToDynamicClause(PREG)); @@ -1808,7 +1810,7 @@ Yap_absmi(int inp) #endif /* FROZEN_STACKS */ SET_BB(B_YREG); ENDCACHE_Y(); -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS INC_CLREF_COUNT(ClauseCodeToDynamicClause(PREG)); TRAIL_CLREF(ClauseCodeToDynamicClause(PREG)); UNLOCK(DynamicLock(PREG)); @@ -2067,7 +2069,7 @@ Yap_absmi(int inp) if (IN_BETWEEN(H0,pt1,H) && IsAttVar(pt1)) goto failloop; flags = *pt1; -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS if (FlagOn(DBClMask, flags)) { DBRef dbr = DBStructFlagsToDBStruct(pt1); int erase; @@ -2086,7 +2088,9 @@ Yap_absmi(int inp) if (flags & IndexMask) { LogUpdIndex *cl = ClauseFlagsToLogUpdIndex(pt1); int erase; +#if PARALLEL_YAP PredEntry *ap = cl->ClPred; +#endif PELOCK(8,ap); DEC_CLREF_COUNT(cl); @@ -2110,7 +2114,9 @@ Yap_absmi(int inp) } else { LogUpdClause *cl = ClauseFlagsToLogUpdClause(pt1); int erase; +#if PARALLEL_YAP PredEntry *ap = cl->ClPred; +#endif PELOCK(9,ap); DEC_CLREF_COUNT(cl); @@ -7926,7 +7932,7 @@ Yap_absmi(int inp) /* fprintf(stderr,"> %p/%p %d %d\n",cl,ap,ap->TimeStampOfPred,PREG->u.Ills.s);*/ PREG = PREG->u.Ills.l1; /* indicate the indexing code is being used */ -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS /* just store a reference */ INC_CLREF_COUNT(cl); TRAIL_CLREF(cl); @@ -8032,7 +8038,7 @@ Yap_absmi(int inp) } /* HEY, leave indexing block alone!! */ /* check if we are the ones using this code */ -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS DEC_CLREF_COUNT(cl); /* clear the entry from the trail */ B->cp_tr--; diff --git a/C/amasm.c b/C/amasm.c index b0cc48c9b..ef9415126 100755 --- a/C/amasm.c +++ b/C/amasm.c @@ -3052,7 +3052,7 @@ do_pass(int pass_no, yamop **entry_codep, int assembling, int *clause_has_blobsp } cl_u->luc.ClExt = NULL; cl_u->luc.ClPrev = cl_u->luc.ClNext = NULL; -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS //INIT_LOCK(cl_u->luc.ClLock); INIT_CLREF_COUNT(&(cl_u->luc)); #endif @@ -3071,6 +3071,8 @@ do_pass(int pass_no, yamop **entry_codep, int assembling, int *clause_has_blobsp cl_u->ic.ClRefCount = 0; #if defined(YAPOR) || defined(THREADS) INIT_LOCK(cl_u->ic.ClLock); +#endif +#ifdef MULTIPLE_STACKS INIT_CLREF_COUNT(&(cl_u->ic)); #endif } @@ -3122,7 +3124,7 @@ do_pass(int pass_no, yamop **entry_codep, int assembling, int *clause_has_blobsp cl_u->lui.ClSize = size; cl_u->lui.ClRefCount = 0; // INIT_LOCK(cl_u->lui.ClLock); -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS INIT_CLREF_COUNT(&(cl_u->lui)); #endif } diff --git a/C/arrays.c b/C/arrays.c index 2fca5031e..2114aa77d 100644 --- a/C/arrays.c +++ b/C/arrays.c @@ -392,7 +392,7 @@ AccessNamedArray(Atom a, Int indx USES_REGS) if (TRef != 0L) { DBRef ref = DBRefOfTerm(TRef); -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS LOCK(ref->lock); INC_DBREF_COUNT(ref); TRAIL_REF(ref); /* So that fail will erase it */ @@ -2305,17 +2305,17 @@ p_static_array_to_term( USES_REGS1 ) if (TRef != 0L) { DBRef ref = DBRefOfTerm(TRef); -#if defined(YAPOR) || defined(THREADS) LOCK(ref->lock); +#if MULTIPLE_STACKS INC_DBREF_COUNT(ref); TRAIL_REF(ref); /* So that fail will erase it */ - UNLOCK(ref->lock); #else if (!(ref->Flags & InUseMask)) { ref->Flags |= InUseMask; TRAIL_REF(ref); /* So that fail will erase it */ } #endif + UNLOCK(ref->lock); } else { TRef = TermNil; } diff --git a/C/cdmgr.c b/C/cdmgr.c index ce34e43c6..6620385ae 100644 --- a/C/cdmgr.c +++ b/C/cdmgr.c @@ -1451,12 +1451,12 @@ Yap_kill_iblock(ClauseUnion *blk, ClauseUnion *parent_blk, PredEntry *ap) LogUpdIndex *c = (LogUpdIndex *)blk; if (parent_blk != NULL) { LogUpdIndex *cl = (LogUpdIndex *)parent_blk; -#if defined(THREADS) || defined(YAPOR) +#if MULTIPLE_STACKS /* protect against attempts at erasing */ cl->ClRefCount++; #endif kill_first_log_iblock(c, cl, ap); -#if defined(THREADS) || defined(YAPOR) +#if MULTIPLE_STACKS cl->ClRefCount--; #endif } else { @@ -1501,12 +1501,12 @@ Yap_ErLogUpdIndex(LogUpdIndex *clau) if (clau->ClFlags & SwitchRootMask) { kill_first_log_iblock(clau, NULL, clau->ClPred); } else { -#if defined(THREADS) || defined(YAPOR) +#if MULTIPLE_STACKS /* protect against attempts at erasing */ clau->ClRefCount++; #endif kill_first_log_iblock(clau, clau->ParentIndex, clau->ClPred); -#if defined(THREADS) || defined(YAPOR) +#if MULTIPLE_STACKS /* protect against attempts at erasing */ clau->ClRefCount--; #endif @@ -2274,7 +2274,7 @@ addclause(Term t, yamop *cp, int mode, Term mod, Term *t4ref) if (pflags & LogUpdatePredFlag) { LogUpdClause *cl = (LogUpdClause *)ClauseCodeToLogUpdClause(cp); tf = MkDBRefTerm((DBRef)cl); -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS TRAIL_CLREF(cl); /* So that fail will erase it */ INC_CLREF_COUNT(cl); #else @@ -4428,7 +4428,7 @@ fetch_next_lu_clause(PredEntry *pe, yamop *i_code, Term th, Term tb, Term tr, ya return FALSE; } rtn = MkDBRefTerm((DBRef)cl); -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS TRAIL_CLREF(cl); /* So that fail will erase it */ INC_CLREF_COUNT(cl); #else @@ -4575,7 +4575,7 @@ fetch_next_lu_clause_erase(PredEntry *pe, yamop *i_code, Term th, Term tb, Term return FALSE; } rtn = MkDBRefTerm((DBRef)cl); -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS TRAIL_CLREF(cl); /* So that fail will erase it */ INC_CLREF_COUNT(cl); #else @@ -5071,7 +5071,7 @@ p_nth_clause( USES_REGS1 ) return FALSE; } if (pe->PredFlags & LogUpdatePredFlag) { -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS TRAIL_CLREF(cl); /* So that fail will erase it */ INC_CLREF_COUNT(cl); #else diff --git a/C/dbase.c b/C/dbase.c index c7272fc3f..efa76d597 100755 --- a/C/dbase.c +++ b/C/dbase.c @@ -113,7 +113,7 @@ typedef struct idb_queue { Functor id; /* identify this as being pointed to by a DBRef */ SMALLUNSGN Flags; /* always required */ -#if defined(YAPOR) || defined(THREADS) +#if PARALLEL_YAP rwlock_t QRWLock; /* a simple lock to protect this entry */ #endif QueueEntry *FirstInQueue, *LastInQueue; @@ -1670,7 +1670,7 @@ record(int Flag, Term key, Term t_data, Term t_code USES_REGS) else x->Flags |= DBNoCode; x->Parent = p; -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS x->Flags |= DBClMask; x->ref_count = 1; #else @@ -1742,7 +1742,7 @@ record_at(int Flag, DBRef r0, Term t_data, Term t_code USES_REGS) else x->Flags |= DBNoCode; x->Parent = p; -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS x->Flags |= DBClMask; x->ref_count = 1; #else @@ -1806,7 +1806,7 @@ new_lu_db_entry(Term t, PredEntry *pe) struct db_globs dbg; int d_flag = 0; -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS /* we cannot allow sharing between threads (for now) */ if (!(pe->PredFlags & ThreadLocalPredFlag)) d_flag |= InQueue; @@ -1838,7 +1838,7 @@ new_lu_db_entry(Term t, PredEntry *pe) cl->ClTimeStart = 0L; } cl->ClTimeEnd = TIMESTAMP_EOT; -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS // INIT_LOCK(cl->ClLock); INIT_CLREF_COUNT(cl); ipc->opc = Yap_opcode(_copy_idb_term); @@ -1960,7 +1960,7 @@ p_rcda( USES_REGS1 ) cl = record_lu(pe, Deref(ARG2), MkFirst); if (cl != NULL) { TRAIL_CLREF(cl); -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS INC_CLREF_COUNT(cl); #else cl->ClFlags |= InUseMask; @@ -2070,7 +2070,7 @@ p_rcdz( USES_REGS1 ) cl = record_lu(pe, t2, MkLast); if (cl != NULL) { TRAIL_CLREF(cl); -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS INC_CLREF_COUNT(cl); #else cl->ClFlags |= InUseMask; @@ -2917,7 +2917,7 @@ lu_nth_recorded(PredEntry *pe, Int Count USES_REGS) cl = Yap_NthClause(pe, Count); if (cl == NULL) return FALSE; -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS PELOCK(65,pe); TRAIL_CLREF(cl); /* So that fail will erase it */ INC_CLREF_COUNT(cl); @@ -2959,7 +2959,7 @@ nth_recorded(DBProp AtProp, Int Count USES_REGS) return FALSE; } } -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS LOCK(ref->lock); READ_UNLOCK(AtProp->DBRWLock); TRAIL_REF(ref); /* So that fail will erase it */ @@ -3276,7 +3276,7 @@ i_recorded(DBProp AtProp, Term t3 USES_REGS) /* This should be after any non-tagged terms, because the routines in grow.c go from upper to lower addresses */ TRef = MkDBRefTerm(ref); -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS LOCK(ref->lock); TRAIL_REF(ref); /* So that fail will erase it */ INC_DBREF_COUNT(ref); @@ -3421,7 +3421,7 @@ c_recorded(int flags USES_REGS) READ_UNLOCK(ref0->Parent->DBRWLock); TRef = MkDBRefTerm(ref); EXTRA_CBACK_ARG(3,1) = (CELL)ref; -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS LOCK(ref->lock); TRAIL_REF(ref); /* So that fail will erase it */ INC_DBREF_COUNT(ref); @@ -3664,17 +3664,17 @@ p_first_instance( USES_REGS1 ) } TRef = MkDBRefTerm(ref); /* we have a pointer to the term available */ -#if defined(YAPOR) || defined(THREADS) LOCK(ref->lock); +#if MULTIPLE_STACKS TRAIL_REF(ref); /* So that fail will erase it */ INC_DBREF_COUNT(ref); - UNLOCK(ref->lock); #else if (!(ref->Flags & InUseMask)) { ref->Flags |= InUseMask; TRAIL_REF(ref); /* So that fail will erase it */ } #endif + UNLOCK(ref->lock); while ((TermDB = GetDBTermFromDBEntry(ref PASS_REGS)) == (CELL)0) { /* oops, we are in trouble, not enough stack space */ if (Yap_Error_TYPE == OUT_OF_ATTVARS_ERROR) { @@ -3942,7 +3942,7 @@ RemoveDBEntry(DBRef entryref USES_REGS) || B->cp_ap == RETRY_C_RECORDEDP_CODE) && EXTRA_CBACK_ARG(3,1) == (CELL)entryref) { /* make it clear the entry has been released */ -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS DEC_DBREF_COUNT(entryref); #else entryref->Flags &= ~InUseMask; @@ -3993,7 +3993,7 @@ find_next_clause(DBRef ref0 USES_REGS) newp = ref->Code; /* and next let's tell the world this clause is being used, just like if we were executing a standard retry_and_mark */ -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS { DynamicClause *cl = ClauseCodeToDynamicClause(newp); @@ -4805,7 +4805,7 @@ Yap_LUInstance(LogUpdClause *cl, UInt arity) } } } -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS cl->ClRefCount++; TRAIL_CLREF(cl); /* So that fail will erase it */ #else @@ -5427,17 +5427,17 @@ p_fetch_reference_from_index( USES_REGS1 ) return FALSE; pos = IntOfTerm(t2); el = (DBRef)(table->DBT.Contents[pos]); -#if defined(YAPOR) || defined(THREADS) LOCK(el->lock); +#if MULTIPLE_STACKS TRAIL_REF(el); /* So that fail will erase it */ INC_DBREF_COUNT(el); - UNLOCK(el->lock); #else if (!(el->Flags & InUseMask)) { el->Flags |= InUseMask; TRAIL_REF(el); } #endif + UNLOCK(el->lock); return Yap_unify(ARG3, MkDBRefTerm(el)); } diff --git a/C/globals.c b/C/globals.c index 61afec71d..1e0b51d17 100644 --- a/C/globals.c +++ b/C/globals.c @@ -1024,7 +1024,7 @@ p_nb_linkval( USES_REGS1 ) static Int -p_nb_create_accumulator(void) +p_nb_create_accumulator( USES_REGS1 ) { Term t = Deref(ARG1), acct, to; CELL *destp; @@ -1041,7 +1041,7 @@ p_nb_create_accumulator(void) if (!Yap_unify(ARG2, acct)) { return FALSE; } - to = CopyTermToArena(t, GlobalArena, TRUE, TRUE, 2, &GlobalArena, garena_overflow_size(ArenaPt(GlobalArena))); + to = CopyTermToArena(t, GlobalArena, TRUE, TRUE, 2, &GlobalArena, garena_overflow_size(ArenaPt(GlobalArena) PASS_REGS) PASS_REGS); if (to == 0L) return FALSE; destp = RepAppl(Deref(ARG2)); @@ -1050,7 +1050,7 @@ p_nb_create_accumulator(void) } static Int -p_nb_add_to_accumulator(void) +p_nb_add_to_accumulator( USES_REGS1 ) { Term t = Deref(ARG1), t0, tadd; Functor f; @@ -1091,7 +1091,7 @@ p_nb_add_to_accumulator(void) target[1] = source[1]; } else { /* we need to create a new long int */ - new = CopyTermToArena(new, GlobalArena, TRUE, TRUE, 2, &GlobalArena, garena_overflow_size(ArenaPt(GlobalArena))); + new = CopyTermToArena(new, GlobalArena, TRUE, TRUE, 2, &GlobalArena, garena_overflow_size(ArenaPt(GlobalArena) PASS_REGS) PASS_REGS); destp = RepAppl(Deref(ARG1)); destp[1] = new; } @@ -1118,7 +1118,7 @@ p_nb_add_to_accumulator(void) new = Yap_MkApplTerm(FunctorPlus, 2, t2); new = Yap_Eval(new); - new = CopyTermToArena(new, GlobalArena, TRUE, TRUE, 2, &GlobalArena, garena_overflow_size(ArenaPt(GlobalArena))); + new = CopyTermToArena(new, GlobalArena, TRUE, TRUE, 2, &GlobalArena, garena_overflow_size(ArenaPt(GlobalArena) PASS_REGS) PASS_REGS); destp = RepAppl(Deref(ARG1)); destp[1] = new; @@ -1129,7 +1129,7 @@ p_nb_add_to_accumulator(void) static Int -p_nb_accumulator_value(void) +p_nb_accumulator_value( USES_REGS1 ) { Term t = Deref(ARG1), to; Functor f; diff --git a/C/grow.c b/C/grow.c index c4166c176..07e752eb9 100755 --- a/C/grow.c +++ b/C/grow.c @@ -214,7 +214,7 @@ MoveLocalAndTrail( USES_REGS1 ) #endif } -#ifdef THREADS +#ifdef YAPOR_THREADS static void CopyLocalAndTrail( USES_REGS1 ) @@ -401,14 +401,14 @@ AdjustTrail(int adjusting_heap, int thread_copying USES_REGS) { volatile tr_fr_ptr ptt, tr_base = (tr_fr_ptr)Yap_TrailBase; -#if defined(YAPOR) && defined(THREADS) +#if defined(YAPOR_THREADS) if (thread_copying == STACK_INCREMENTAL_COPYING) { ptt = (tr_fr_ptr)(LOCAL_end_trail_copy); tr_base = (tr_fr_ptr)(LOCAL_start_trail_copy); } else { #endif ptt = TR; -#if defined(YAPOR) && defined(THREADS) +#if defined(YAPOR_THREADS) } #endif /* moving the trail is simple */ @@ -462,7 +462,7 @@ AdjustLocal(int thread_copying USES_REGS) register CELL reg, *pt, *pt_bot; /* Adjusting the local */ -#if defined(YAPOR) && defined(THREADS) +#if defined(YAPOR_THREADS) if (thread_copying == STACK_INCREMENTAL_COPYING) { pt = (CELL *) (LOCAL_end_local_copy); pt_bot = (CELL *) (LOCAL_start_local_copy); @@ -470,7 +470,7 @@ AdjustLocal(int thread_copying USES_REGS) #endif pt = LCL0; pt_bot = ASP; -#if defined(YAPOR) && defined(THREADS) +#if defined(YAPOR_THREADS) } #endif while (pt > pt_bot) { @@ -551,7 +551,7 @@ AdjustGlobal(long sz, int thread_copying USES_REGS) * to clean the global now that functors are just variables pointing to * the code */ -#if defined(YAPOR) && defined(THREADS) +#if defined(YAPOR_THREADS) if (thread_copying == STACK_INCREMENTAL_COPYING) { pt = (CELL *) (LOCAL_start_global_copy); pt_max = (CELL *) (LOCAL_end_global_copy); @@ -559,7 +559,7 @@ AdjustGlobal(long sz, int thread_copying USES_REGS) #endif pt = H0; pt_max = (H-sz/CellSize); -#if defined(YAPOR) && defined(THREADS) +#if defined(YAPOR_THREADS) } #endif pt = H0; @@ -790,7 +790,7 @@ static_growheap(long size, int fix_code, struct intermediates *cip, tr_fr_ptr *o gc_verbose = Yap_is_gc_verbose(); heap_overflows++; if (gc_verbose) { -#if defined(YAPOR) || defined(THREADS) +#if defined(YAPOR_THREADS) fprintf(Yap_stderr, "%% Worker Id %d:\n", worker_id); #endif fprintf(Yap_stderr, "%% Database Overflow %d\n", heap_overflows); @@ -936,7 +936,7 @@ static_growglobal(long request, CELL **ptr, CELL *hsplit USES_REGS) vb_msg1 = 'D'; vb_msg2 = "Delay"; } -#if defined(YAPOR) || defined(THREADS) +#if defined(YAPOR_THREADS) fprintf(Yap_stderr, "%% Worker Id %d:\n", worker_id); #endif fprintf(Yap_stderr, "%% %cO %s Overflow %d\n", vb_msg1, vb_msg2, delay_overflows); @@ -1357,7 +1357,7 @@ growatomtable( USES_REGS1 ) } atom_table_overflows++; if (gc_verbose) { -#if defined(YAPOR) || defined(THREADS) +#if defined(YAPOR_THREADS) fprintf(Yap_stderr, "%% Worker Id %d:\n", worker_id); #endif fprintf(Yap_stderr, "%% Atom Table Overflow %d\n", atom_table_overflows); @@ -1441,7 +1441,7 @@ Yap_growglobal(CELL **ptr) CACHE_REGS unsigned long sz = sizeof(CELL) * K16; -#if defined(YAPOR) && !defined(THREADS) +#if defined(YAPOR_THREADS) if (Yap_number_workers != 1) { Yap_Error(OUT_OF_STACK_ERROR,TermNil,"cannot grow Global: more than a worker/thread running"); return(FALSE); @@ -1837,7 +1837,7 @@ p_inform_heap_overflows( USES_REGS1 ) return(Yap_unify(tn, ARG1) && Yap_unify(tt, ARG2)); } -#if defined(THREADS) && defined(YAPOR) +#if defined(YAPOR_THREADS) void Yap_CopyThreadStacks(int worker_q, int worker_p, int incremental) { diff --git a/C/index.c b/C/index.c index 10ebf5263..eedfc0d02 100644 --- a/C/index.c +++ b/C/index.c @@ -2190,6 +2190,7 @@ emit_type_switch(compiler_vm_op op, struct intermediates *cint) static yamop * emit_switch_space(UInt n, UInt item_size, struct intermediates *cint, CELL func_mask) { + CACHE_REGS PredEntry *ap = cint->CurrentPred; if (ap->PredFlags & LogUpdatePredFlag) { @@ -5218,7 +5219,7 @@ kill_clause(yamop *ipc, yamop *bg, yamop *lt, path_stack_entry *sp0, PredEntry * return sp; } else { if ( -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS blk->ClRefCount == 0 #else !(blk->ClFlags & InUseMask) @@ -6821,7 +6822,7 @@ Yap_FollowIndexingCode(PredEntry *ap, yamop *ipc, Term Terms[3], yamop *ap_pc, y } *--ASP = MkIntegerTerm(ap->TimeStampOfPred); /* indicate the indexing code is being used */ -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS /* just store a reference */ INC_CLREF_COUNT(cl); TRAIL_CLREF(cl); @@ -6883,7 +6884,7 @@ Yap_FollowIndexingCode(PredEntry *ap, yamop *ipc, Term Terms[3], yamop *ap_pc, y } else { newpc = ipc->u.OtILl.d; } -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS B->cp_tr--; TR--; DEC_CLREF_COUNT(cl); diff --git a/C/tracer.c b/C/tracer.c index bab8c7c37..9874ca225 100755 --- a/C/tracer.c +++ b/C/tracer.c @@ -163,6 +163,10 @@ low_level_trace(yap_low_level_port port, PredEntry *pred, CELL *args) LOCK(Yap_heap_regs->low_level_trace_lock); sc = Yap_heap_regs; vsc_count++; + if (vsc_count < 45000LL) + return; + if (vsc_count == 47456LL) + jmp_deb(1); #ifdef THREADS MY_ThreadHandle.thread_inst_count++; #endif diff --git a/C/utilpreds.c b/C/utilpreds.c index 543db8b2e..8684cc32c 100644 --- a/C/utilpreds.c +++ b/C/utilpreds.c @@ -146,7 +146,7 @@ copy_complex_term(CELL *pt0, CELL *pt0_end, int share, int newattvs, CELL *ptf, f = (Functor)(*ap2); if (IsExtensionFunctor(f)) { -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS if (f == FunctorDBRef) { DBRef entryref = DBRefOfTerm(d0); if (entryref->Flags & LogUpdMask) { diff --git a/H/Yap.h b/H/Yap.h index 9d320f73f..bd06fd5e7 100755 --- a/H/Yap.h +++ b/H/Yap.h @@ -43,7 +43,7 @@ #error Do not define multiple or-parallel models #endif /* (YAPOR_COPY && (YAPOR_COW || YAPOR_SBA)) || (YAPOR_COW && YAPOR_SBA) */ -#if defined(YAPOR_COPY) || defined(YAPOR_COW) || defined(YAPOR_SBA) || defined(THREADS) +#if defined(YAPOR_COPY) || defined(YAPOR_COW) || defined(YAPOR_SBA) || defined(YAPOR_THREADS) #define YAPOR 1 #endif /* YAPOR_COPY || YAPOR_COW || YAPOR_SBA */ @@ -60,6 +60,14 @@ #endif #endif /* YAPOR */ +#if defined(YAPOR) || defined(THREADS) || defined(TABLING) +#define MULTIPLE_STACKS 1 +#endif + +#if defined(YAPOR) || defined(THREADS) +#define PARALLEL_YAP 1 +#endif + #if defined(YAPOR) || defined(TABLING) #undef TRAILING_REQUIRES_BRANCH #endif /* YAPOR || TABLING */ @@ -76,7 +84,7 @@ #endif #endif -#if defined(SUPPORT_THREADS) || defined(SUPPORT_CONDOR) +#if defined(THREADS) || defined(SUPPORT_CONDOR) #define USE_SYSTEM_MALLOC 1 #endif diff --git a/H/Yatom.h b/H/Yatom.h index 751ab0ecb..95f7ca01f 100755 --- a/H/Yatom.h +++ b/H/Yatom.h @@ -863,6 +863,8 @@ typedef struct DB_STRUCT CELL Flags; /* Term Flags */ #if defined(YAPOR) || defined(THREADS) lockvar lock; /* a simple lock to protect this entry */ +#endif +#if MULTIPLE_STACKS Int ref_count; /* how many branches are using this entry */ #endif CELL NOfRefsTo; /* Number of references pointing here */ @@ -880,7 +882,7 @@ typedef struct DB_STRUCT #define DBStructFlagsToDBStruct(X) ((DBRef)((char *)(X) - (CELL) &(((DBRef) NULL)->Flags))) -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STAACKS #define INIT_DBREF_COUNT(X) (X)->ref_count = 0 #define INC_DBREF_COUNT(X) (X)->ref_count++ #define DEC_DBREF_COUNT(X) (X)->ref_count-- diff --git a/H/amiops.h b/H/amiops.h index ec4818797..d58cfb0f1 100644 --- a/H/amiops.h +++ b/H/amiops.h @@ -267,9 +267,15 @@ Binding Macros for Multiple Assignment Variables. #define REF_TO_TRENTRY(REF) AbsPair(((CELL *)&((REF)->Flags))) #define CLREF_TO_TRENTRY(REF) AbsPair(((CELL *)&((REF)->ClFlags))) +#if FROZEN_STACKS #define TRAIL_REF(REF) RESET_VARIABLE(&TrailVal(TR)), TrailTerm(TR++) = REF_TO_TRENTRY(REF) #define TRAIL_CLREF(REF) RESET_VARIABLE(&TrailVal(TR)), TrailTerm(TR++) = CLREF_TO_TRENTRY(REF) #define TRAIL_LINK(REF) RESET_VARIABLE(&TrailVal(TR)), TrailTerm(TR++) = AbsPair((CELL *)(REF)) +#else +#define TRAIL_REF(REF) TrailTerm(TR++) = REF_TO_TRENTRY(REF) +#define TRAIL_CLREF(REF) TrailTerm(TR++) = CLREF_TO_TRENTRY(REF) +#define TRAIL_LINK(REF) TrailTerm(TR++) = AbsPair((CELL *)(REF)) +#endif #define TRAIL_FRAME(FR) DO_TRAIL(AbsPair((CELL *)(Yap_TrailBase)), FR) extern void Yap_WakeUp(CELL *v); diff --git a/H/clause.h b/H/clause.h index 09a7aa5aa..a4a5212e6 100644 --- a/H/clause.h +++ b/H/clause.h @@ -182,7 +182,7 @@ typedef struct dbterm_list { #define DynamicLock(X) (ClauseCodeToDynamicClause(X)->ClLock) -#if defined(YAPOR) || defined(THREADS) +#if MULTIPLE_STACKS #define INIT_CLREF_COUNT(X) (X)->ClRefCount = 0 #define INC_CLREF_COUNT(X) (X)->ClRefCount++ #define DEC_CLREF_COUNT(X) (X)->ClRefCount-- diff --git a/config.h.in b/config.h.in index cd2a85c5d..b6c216c59 100755 --- a/config.h.in +++ b/config.h.in @@ -48,7 +48,6 @@ #undef HOST_ALIAS #undef SUPPORT_CONDOR -#undef SUPPORT_THREADS #undef USE_PTHREAD_LOCKING #undef HAVE_SYS_WAIT_H diff --git a/misc/LOCALS b/misc/LOCALS index b16b4c798..1f1c205a6 100644 --- a/misc/LOCALS +++ b/misc/LOCALS @@ -149,7 +149,7 @@ struct PL_local_data *Yap_ld_ PL_local_data_p =Yap_InitThreadIO(wid) struct open_query_struct* _execution execution =NULL // Ricardo's stuff -#if (defined(YAPOR) || defined(TABLING)) && defined(THREADS) +#if MULTIPLE_STACKS struct worker worker WORKER void #endif