2010-04-03 05:58:14 +01:00
|
|
|
/************************************************************************
|
|
|
|
** **
|
|
|
|
** The YapTab/YapOr/OPTYap systems **
|
|
|
|
** **
|
|
|
|
** YapTab extends the Yap Prolog engine to support sequential tabling **
|
|
|
|
** YapOr extends the Yap Prolog engine to support or-parallelism **
|
|
|
|
** OPTYap extends the Yap Prolog engine to support or-parallel tabling **
|
|
|
|
** **
|
|
|
|
** **
|
|
|
|
** Yap Prolog was developed at University of Porto, Portugal **
|
|
|
|
** **
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
/************************************
|
|
|
|
** Includes & Prototypes **
|
|
|
|
************************************/
|
2005-05-31 09:19:31 +01:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
#include <stdlib.h>
|
2010-01-14 17:38:39 +00:00
|
|
|
#if HAVE_STRING_H
|
|
|
|
#include <string.h>
|
2010-04-19 02:34:08 +01:00
|
|
|
#endif /* HAVE_STRING_H */
|
2001-04-09 20:54:03 +01:00
|
|
|
#include "opt.mavar.h"
|
2014-03-08 11:50:20 +00:00
|
|
|
#ifdef YAPOR
|
|
|
|
#include "or.macros.h"
|
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2011-12-22 16:50:20 +00:00
|
|
|
#ifdef THREADS
|
2013-03-26 21:11:19 +00:00
|
|
|
static inline void **__get_insert_thread_bucket(void **, lockvar * USES_REGS);
|
|
|
|
static inline void **__get_thread_bucket(void ** USES_REGS);
|
2011-12-22 16:50:20 +00:00
|
|
|
static inline void abolish_thread_buckets(void **);
|
|
|
|
#endif /* THREADS */
|
|
|
|
static inline sg_node_ptr get_insert_subgoal_trie(tab_ent_ptr USES_REGS);
|
2013-03-26 21:11:19 +00:00
|
|
|
static inline sg_node_ptr __get_subgoal_trie(tab_ent_ptr USES_REGS);
|
2011-12-22 16:50:20 +00:00
|
|
|
static inline sg_node_ptr get_subgoal_trie_for_abolish(tab_ent_ptr USES_REGS);
|
|
|
|
static inline sg_fr_ptr *get_insert_subgoal_frame_addr(sg_node_ptr USES_REGS);
|
|
|
|
static inline sg_fr_ptr get_subgoal_frame(sg_node_ptr);
|
|
|
|
static inline sg_fr_ptr get_subgoal_frame_for_abolish(sg_node_ptr USES_REGS);
|
|
|
|
#ifdef THREADS_FULL_SHARING
|
2013-03-26 21:11:19 +00:00
|
|
|
static inline void __SgFr_batched_cached_answers_check_insert(sg_fr_ptr, ans_node_ptr USES_REGS);
|
2011-12-22 16:50:20 +00:00
|
|
|
static inline int SgFr_batched_cached_answers_check_remove(sg_fr_ptr, ans_node_ptr);
|
|
|
|
#endif /* THREADS_FULL_SHARING */
|
|
|
|
#ifdef THREADS_CONSUMER_SHARING
|
2013-03-26 21:11:19 +00:00
|
|
|
static inline void __add_to_tdv(int, int USES_REGS);
|
|
|
|
static inline void __check_for_deadlock(sg_fr_ptr USES_REGS);
|
|
|
|
static inline sg_fr_ptr __deadlock_detection(sg_fr_ptr USES_REGS);
|
2011-12-22 16:50:20 +00:00
|
|
|
#endif /* THREADS_CONSUMER_SHARING */
|
2013-03-26 21:11:19 +00:00
|
|
|
static inline Int __freeze_current_cp( USES_REGS1 );
|
|
|
|
static inline void __wake_frozen_cp(Int USES_REGS);
|
|
|
|
static inline void __abolish_frozen_cps_until(Int USES_REGS);
|
|
|
|
static inline void __abolish_frozen_cps_all( USES_REGS1 );
|
|
|
|
static inline void __adjust_freeze_registers( USES_REGS1 );
|
|
|
|
static inline void __mark_as_completed(sg_fr_ptr USES_REGS);
|
|
|
|
static inline void __unbind_variables(tr_fr_ptr, tr_fr_ptr USES_REGS);
|
|
|
|
static inline void __rebind_variables(tr_fr_ptr, tr_fr_ptr USES_REGS);
|
|
|
|
static inline void __restore_bindings(tr_fr_ptr, tr_fr_ptr USES_REGS);
|
|
|
|
static inline CELL *__expand_auxiliary_stack(CELL * USES_REGS);
|
|
|
|
static inline void __abolish_incomplete_subgoals(choiceptr USES_REGS);
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef YAPOR
|
2010-04-03 05:58:14 +01:00
|
|
|
static inline void pruning_over_tabling_data_structures(void);
|
2013-03-26 21:11:19 +00:00
|
|
|
static inline void __collect_suspension_frames(or_fr_ptr USES_REGS);
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef TIMESTAMP_CHECK
|
2010-04-03 05:58:14 +01:00
|
|
|
static inline susp_fr_ptr suspension_frame_to_resume(or_fr_ptr, long);
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2010-04-03 05:58:14 +01:00
|
|
|
static inline susp_fr_ptr suspension_frame_to_resume(or_fr_ptr);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif /* TIMESTAMP_CHECK */
|
|
|
|
#endif /* YAPOR */
|
|
|
|
#ifdef TABLING_INNER_CUTS
|
2010-04-03 05:58:14 +01:00
|
|
|
static inline void CUT_store_tg_answer(or_fr_ptr, ans_node_ptr, choiceptr, int);
|
|
|
|
static inline tg_sol_fr_ptr CUT_store_tg_answers(or_fr_ptr, tg_sol_fr_ptr, int);
|
|
|
|
static inline void CUT_validate_tg_answers(tg_sol_fr_ptr);
|
|
|
|
static inline void CUT_join_tg_solutions(tg_sol_fr_ptr *, tg_sol_fr_ptr);
|
|
|
|
static inline void CUT_join_solution_frame_tg_answers(tg_sol_fr_ptr);
|
|
|
|
static inline void CUT_join_solution_frames_tg_answers(tg_sol_fr_ptr);
|
|
|
|
static inline void CUT_free_tg_solution_frame(tg_sol_fr_ptr);
|
|
|
|
static inline void CUT_free_tg_solution_frames(tg_sol_fr_ptr);
|
|
|
|
static inline tg_sol_fr_ptr CUT_prune_tg_solution_frames(tg_sol_fr_ptr, int);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif /* TABLING_INNER_CUTS */
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-04-16 02:08:06 +01:00
|
|
|
/*********************************
|
|
|
|
** Tabling mode flags **
|
|
|
|
*********************************/
|
|
|
|
|
|
|
|
#define Flag_Batched 0x001
|
|
|
|
#define Flag_Local 0x002
|
|
|
|
#define Flags_SchedulingMode (Flag_Batched | Flag_Local)
|
|
|
|
#define Flag_ExecAnswers 0x010
|
|
|
|
#define Flag_LoadAnswers 0x020
|
|
|
|
#define Flags_AnswersMode (Flag_ExecAnswers | Flag_LoadAnswers)
|
|
|
|
#define Flag_LocalTrie 0x100
|
|
|
|
#define Flag_GlobalTrie 0x200
|
|
|
|
#define Flags_TrieMode (Flag_LocalTrie | Flag_GlobalTrie)
|
2013-12-19 16:56:55 +00:00
|
|
|
#define Flag_CoInductive 0x008
|
2010-04-16 02:08:06 +01:00
|
|
|
|
|
|
|
#define SetMode_Batched(X) (X) = ((X) & ~Flags_SchedulingMode) | Flag_Batched
|
|
|
|
#define SetMode_Local(X) (X) = ((X) & ~Flags_SchedulingMode) | Flag_Local
|
|
|
|
#define SetMode_ExecAnswers(X) (X) = ((X) & ~Flags_AnswersMode) | Flag_ExecAnswers
|
|
|
|
#define SetMode_LoadAnswers(X) (X) = ((X) & ~Flags_AnswersMode) | Flag_LoadAnswers
|
|
|
|
#define SetMode_LocalTrie(X) (X) = ((X) & ~Flags_TrieMode) | Flag_LocalTrie
|
|
|
|
#define SetMode_GlobalTrie(X) (X) = ((X) & ~Flags_TrieMode) | Flag_GlobalTrie
|
2013-12-19 16:56:55 +00:00
|
|
|
#define SetMode_CoInductive(X) (X) = (X) | Flag_CoInductive
|
2010-04-16 02:08:06 +01:00
|
|
|
#define IsMode_Batched(X) ((X) & Flag_Batched)
|
|
|
|
#define IsMode_Local(X) ((X) & Flag_Local)
|
|
|
|
#define IsMode_ExecAnswers(X) ((X) & Flag_ExecAnswers)
|
|
|
|
#define IsMode_LoadAnswers(X) ((X) & Flag_LoadAnswers)
|
|
|
|
#define IsMode_LocalTrie(X) ((X) & Flag_LocalTrie)
|
|
|
|
#define IsMode_GlobalTrie(X) ((X) & Flag_GlobalTrie)
|
2013-12-19 16:56:55 +00:00
|
|
|
#define IsMode_CoInductive(X) ((X) & Flag_CoInductive)
|
2010-04-16 02:08:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2010-04-19 02:34:08 +01:00
|
|
|
/******************************
|
|
|
|
** Tabling defines **
|
|
|
|
******************************/
|
2009-08-05 05:48:00 +01:00
|
|
|
|
2011-11-22 17:09:29 +00:00
|
|
|
/* traverse macros */
|
2010-04-15 01:09:59 +01:00
|
|
|
#define SHOW_MODE_STRUCTURE 0
|
|
|
|
#define SHOW_MODE_STATISTICS 1
|
2014-01-17 09:39:29 +00:00
|
|
|
typedef enum {
|
|
|
|
TRAVERSE_MODE_NORMAL = 0,
|
|
|
|
TRAVERSE_MODE_DOUBLE = 1,
|
|
|
|
TRAVERSE_MODE_DOUBLE2 = 2,
|
|
|
|
TRAVERSE_MODE_DOUBLE_END = 3,
|
|
|
|
TRAVERSE_MODE_BIGINT_OR_STRING = 4,
|
|
|
|
TRAVERSE_MODE_BIGINT_OR_STRING_END = 5,
|
|
|
|
TRAVERSE_MODE_LONGINT = 6,
|
|
|
|
TRAVERSE_MODE_LONGINT_END = 7
|
|
|
|
} traverse_mode_t;
|
2009-08-05 05:48:00 +01:00
|
|
|
/* do not change order !!! */
|
2010-04-23 04:22:10 +01:00
|
|
|
#define TRAVERSE_TYPE_SUBGOAL 0
|
|
|
|
#define TRAVERSE_TYPE_ANSWER 1
|
|
|
|
#define TRAVERSE_TYPE_GT_SUBGOAL 2
|
|
|
|
#define TRAVERSE_TYPE_GT_ANSWER 3
|
|
|
|
/* do not change order !!! */
|
2010-04-15 01:09:59 +01:00
|
|
|
#define TRAVERSE_POSITION_NEXT 0
|
|
|
|
#define TRAVERSE_POSITION_FIRST 1
|
|
|
|
#define TRAVERSE_POSITION_LAST 2
|
2009-08-05 05:48:00 +01:00
|
|
|
|
2011-11-09 11:00:31 +00:00
|
|
|
/* mode directed tabling */
|
|
|
|
#define MODE_DIRECTED_TAGBITS 0xF
|
|
|
|
#define MODE_DIRECTED_NUMBER_TAGBITS 4
|
|
|
|
#define MODE_DIRECTED_INDEX 1
|
2012-06-04 19:27:31 +01:00
|
|
|
#define MODE_DIRECTED_MIN 2
|
|
|
|
#define MODE_DIRECTED_MAX 3
|
|
|
|
#define MODE_DIRECTED_ALL 4
|
|
|
|
#define MODE_DIRECTED_SUM 5
|
2011-11-09 11:00:31 +00:00
|
|
|
#define MODE_DIRECTED_LAST 6
|
2012-06-04 19:27:31 +01:00
|
|
|
#define MODE_DIRECTED_FIRST 7
|
2011-11-09 11:00:31 +00:00
|
|
|
#define MODE_DIRECTED_SET(ARG,MODE) (((ARG) << MODE_DIRECTED_NUMBER_TAGBITS) + MODE)
|
|
|
|
#define MODE_DIRECTED_GET_ARG(X) ((X) >> MODE_DIRECTED_NUMBER_TAGBITS)
|
|
|
|
#define MODE_DIRECTED_GET_MODE(X) ((X) & MODE_DIRECTED_TAGBITS)
|
|
|
|
|
2010-04-03 05:58:14 +01:00
|
|
|
/* LowTagBits is 3 for 32 bit-machines and 7 for 64 bit-machines */
|
|
|
|
#define NumberOfLowTagBits (LowTagBits == 3 ? 2 : 3)
|
|
|
|
#define MakeTableVarTerm(INDEX) ((INDEX) << NumberOfLowTagBits)
|
|
|
|
#define VarIndexOfTableTerm(TERM) (((unsigned int) (TERM)) >> NumberOfLowTagBits)
|
|
|
|
#define VarIndexOfTerm(TERM) \
|
2011-05-10 11:47:18 +01:00
|
|
|
((((CELL) (TERM)) - GLOBAL_table_var_enumerator(0)) / sizeof(CELL))
|
2010-04-03 05:58:14 +01:00
|
|
|
#define IsTableVarTerm(TERM) \
|
2011-05-10 11:47:18 +01:00
|
|
|
((CELL) (TERM)) >= GLOBAL_table_var_enumerator(0) && \
|
|
|
|
((CELL) (TERM)) <= GLOBAL_table_var_enumerator(MAX_TABLE_VARS - 1)
|
2010-04-03 05:58:14 +01:00
|
|
|
#ifdef TRIE_COMPACT_PAIRS
|
|
|
|
#define PairTermMark NULL
|
|
|
|
#define CompactPairInit AbsPair((Term *) 0)
|
|
|
|
#define CompactPairEndTerm AbsPair((Term *) (LowTagBits + 1))
|
|
|
|
#define CompactPairEndList AbsPair((Term *) (2*(LowTagBits + 1)))
|
|
|
|
#endif /* TRIE_COMPACT_PAIRS */
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2011-11-22 17:09:29 +00:00
|
|
|
/* threads */
|
|
|
|
#if (_trie_retry_gterm - _trie_do_var + 1) + 1 <= 64 /* 60 (trie instructions) + 1 (ANSWER_TRIE_HASH_MARK) <= 64 */
|
|
|
|
#define ANSWER_LEAF_NODE_INSTR_BITS 6 /* 2^6 = 64 */
|
|
|
|
#define ANSWER_LEAF_NODE_INSTR_MASK 0x3F
|
|
|
|
#endif
|
|
|
|
#if SIZEOF_INT_P == 4
|
|
|
|
#define ANSWER_LEAF_NODE_MAX_THREADS (32 - ANSWER_LEAF_NODE_INSTR_BITS)
|
|
|
|
#elif SIZEOF_INT_P == 8
|
|
|
|
#define ANSWER_LEAF_NODE_MAX_THREADS (64 - ANSWER_LEAF_NODE_INSTR_BITS)
|
|
|
|
#else
|
|
|
|
#define ANSWER_LEAF_NODE_MAX_THREADS OOOOPPS!!! Unknown Pointer Sizeof
|
|
|
|
#endif /* SIZEOF_INT_P */
|
2011-12-22 16:50:20 +00:00
|
|
|
#define ANSWER_LEAF_NODE_INSTR_RELATIVE(NODE) (TrNode_instr(NODE) = TrNode_instr(NODE) - _trie_do_var + 1)
|
|
|
|
#define ANSWER_LEAF_NODE_INSTR_ABSOLUTE(NODE) (TrNode_instr(NODE) = (TrNode_instr(NODE) & ANSWER_LEAF_NODE_INSTR_MASK) + _trie_do_var - 1)
|
2011-11-22 17:09:29 +00:00
|
|
|
#define ANSWER_LEAF_NODE_SET_WID(NODE,WID) BITMAP_insert(TrNode_instr(NODE), WID + ANSWER_LEAF_NODE_INSTR_BITS)
|
|
|
|
#define ANSWER_LEAF_NODE_DEL_WID(NODE,WID) BITMAP_delete(TrNode_instr(NODE), WID + ANSWER_LEAF_NODE_INSTR_BITS)
|
|
|
|
#define ANSWER_LEAF_NODE_CHECK_WID(NODE,WID) BITMAP_member(TrNode_instr(NODE), WID + ANSWER_LEAF_NODE_INSTR_BITS)
|
|
|
|
|
2011-11-15 16:57:51 +00:00
|
|
|
/* choice points */
|
2009-07-03 00:54:39 +01:00
|
|
|
#define NORM_CP(CP) ((choiceptr)(CP))
|
|
|
|
#define GEN_CP(CP) ((struct generator_choicept *)(CP))
|
|
|
|
#define CONS_CP(CP) ((struct consumer_choicept *)(CP))
|
|
|
|
#define LOAD_CP(CP) ((struct loader_choicept *)(CP))
|
|
|
|
#ifdef DETERMINISTIC_TABLING
|
|
|
|
#define DET_GEN_CP(CP) ((struct deterministic_generator_choicept *)(CP))
|
|
|
|
#define IS_DET_GEN_CP(CP) (*(CELL*)(DET_GEN_CP(CP) + 1) <= MAX_TABLE_VARS)
|
|
|
|
#define IS_BATCHED_NORM_GEN_CP(CP) (GEN_CP(CP)->cp_dep_fr == NULL)
|
|
|
|
#define IS_BATCHED_GEN_CP(CP) (IS_DET_GEN_CP(CP) || IS_BATCHED_NORM_GEN_CP(CP))
|
|
|
|
#else
|
2011-12-22 16:50:20 +00:00
|
|
|
#ifdef THREADS_CONSUMER_SHARING
|
|
|
|
#define IS_BATCHED_GEN_CP(CP) (IsMode_Batched(TabEnt_mode(SgFr_tab_ent(GEN_CP(CP)->cp_sg_fr))))
|
|
|
|
#else
|
2009-07-03 00:54:39 +01:00
|
|
|
#define IS_BATCHED_GEN_CP(CP) (GEN_CP(CP)->cp_dep_fr == NULL)
|
2011-12-22 16:50:20 +00:00
|
|
|
#endif /* THREADS_CONSUMER_SHARING */
|
2009-07-03 00:54:39 +01:00
|
|
|
#endif /* DETERMINISTIC_TABLING */
|
2005-04-07 18:56:58 +01:00
|
|
|
|
2011-12-22 16:50:20 +00:00
|
|
|
/* tagging nodes */
|
2012-04-18 20:14:56 +01:00
|
|
|
#define TAG_AS_SUBGOAL_LEAF_NODE(NODE) TrNode_child(NODE) = (sg_node_ptr)((CELL) TrNode_child(NODE) | 0x1)
|
|
|
|
#define IS_SUBGOAL_LEAF_NODE(NODE) ((CELL) TrNode_child(NODE) & 0x1)
|
|
|
|
#define TAG_AS_ANSWER_LEAF_NODE(NODE) TrNode_parent(NODE) = (ans_node_ptr)((CELL) TrNode_parent(NODE) | 0x1)
|
|
|
|
#define IS_ANSWER_LEAF_NODE(NODE) ((CELL) TrNode_parent(NODE) & 0x1)
|
|
|
|
#define TAG_AS_ANSWER_INVALID_NODE(NODE) TrNode_parent(NODE) = (ans_node_ptr)((CELL) TrNode_parent(NODE) | 0x2)
|
|
|
|
#define IS_ANSWER_INVALID_NODE(NODE) ((CELL) TrNode_parent(NODE) & 0x2)
|
|
|
|
#define UNTAG_SUBGOAL_NODE(NODE) ((CELL) (NODE) & ~(0x1))
|
|
|
|
#define UNTAG_ANSWER_NODE(NODE) ((CELL) (NODE) & ~(0x3))
|
2005-05-31 01:49:49 +01:00
|
|
|
|
2011-11-15 16:57:51 +00:00
|
|
|
/* trie hashes */
|
2011-12-12 17:24:58 +00:00
|
|
|
#define MAX_NODES_PER_TRIE_LEVEL 8
|
|
|
|
#define MAX_NODES_PER_BUCKET (MAX_NODES_PER_TRIE_LEVEL / 2)
|
|
|
|
#define BASE_HASH_BUCKETS 64
|
2012-04-18 20:14:56 +01:00
|
|
|
#define HASH_ENTRY(ENTRY, NUM_BUCKETS) ((((CELL) ENTRY) >> NumberOfLowTagBits) & (NUM_BUCKETS - 1))
|
2011-12-12 17:24:58 +00:00
|
|
|
#define SUBGOAL_TRIE_HASH_MARK ((Term) MakeTableVarTerm(MAX_TABLE_VARS))
|
|
|
|
#define IS_SUBGOAL_TRIE_HASH(NODE) (TrNode_entry(NODE) == SUBGOAL_TRIE_HASH_MARK)
|
|
|
|
#define ANSWER_TRIE_HASH_MARK 0
|
|
|
|
#define IS_ANSWER_TRIE_HASH(NODE) (TrNode_instr(NODE) == ANSWER_TRIE_HASH_MARK)
|
|
|
|
#define GLOBAL_TRIE_HASH_MARK ((Term) MakeTableVarTerm(MAX_TABLE_VARS))
|
|
|
|
#define IS_GLOBAL_TRIE_HASH(NODE) (TrNode_entry(NODE) == GLOBAL_TRIE_HASH_MARK)
|
2012-04-18 20:14:56 +01:00
|
|
|
#define HASH_TRIE_LOCK(NODE) GLOBAL_trie_locks((((CELL) (NODE)) >> 5) & (TRIE_LOCK_BUCKETS - 1))
|
2009-09-27 02:31:31 +01:00
|
|
|
|
2011-11-15 16:57:51 +00:00
|
|
|
/* auxiliary stack */
|
|
|
|
#define STACK_PUSH_UP(ITEM, STACK) *--(STACK) = (CELL)(ITEM)
|
|
|
|
#define STACK_POP_UP(STACK) *--(STACK)
|
|
|
|
#define STACK_PUSH_DOWN(ITEM, STACK) *(STACK)++ = (CELL)(ITEM)
|
|
|
|
#define STACK_POP_DOWN(STACK) *(STACK)++
|
|
|
|
#define STACK_NOT_EMPTY(STACK, STACK_BASE) (STACK) != (STACK_BASE)
|
2011-04-29 14:59:17 +01:00
|
|
|
#if defined(YAPOR_COPY) || defined(YAPOR_COW) || defined(YAPOR_SBA)
|
2011-11-15 16:57:51 +00:00
|
|
|
#define AUX_STACK_CHECK_EXPAND(STACK, STACK_LIMIT) \
|
|
|
|
if ((STACK_LIMIT) >= (STACK)) \
|
|
|
|
Yap_Error(INTERNAL_ERROR, TermNil, "stack full (AUX_STACK_CHECK_EXPAND)")
|
2011-04-29 14:59:17 +01:00
|
|
|
#else /* YAPOR_THREADS */
|
2011-11-15 16:57:51 +00:00
|
|
|
#define AUX_STACK_CHECK_EXPAND(STACK, STACK_LIMIT) \
|
|
|
|
if ((STACK_LIMIT) >= (STACK)) \
|
|
|
|
STACK = expand_auxiliary_stack(STACK)
|
2010-04-03 05:58:14 +01:00
|
|
|
#endif /* YAPOR */
|
2011-11-15 16:57:51 +00:00
|
|
|
#define STACK_CHECK_EXPAND(STACK, STACK_LIMIT) \
|
|
|
|
if ((STACK_LIMIT) >= (STACK) + 4096) \
|
|
|
|
Yap_Error(INTERNAL_ERROR, TermNil, "stack full (STACK_CHECK_EXPAND)")
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2010-04-19 02:34:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*************************************
|
|
|
|
** Data structures macros **
|
|
|
|
*************************************/
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef YAPOR
|
2010-04-03 05:58:14 +01:00
|
|
|
#define frame_with_suspensions_not_collected(OR_FR) \
|
|
|
|
(OrFr_nearest_suspnode(OR_FR) == NULL)
|
2001-04-09 20:54:03 +01:00
|
|
|
#define find_dependency_node(SG_FR, LEADER_CP, DEP_ON_STACK) \
|
|
|
|
if (SgFr_gen_worker(SG_FR) == worker_id) { \
|
|
|
|
LEADER_CP = SgFr_gen_cp(SG_FR); \
|
|
|
|
DEP_ON_STACK = TRUE; \
|
|
|
|
} else { \
|
|
|
|
or_fr_ptr aux_or_fr = SgFr_gen_top_or_fr(SG_FR); \
|
|
|
|
while (! BITMAP_member(OrFr_members(aux_or_fr), worker_id)) \
|
|
|
|
aux_or_fr = OrFr_next(aux_or_fr); \
|
2010-01-14 15:58:19 +00:00
|
|
|
LEADER_CP = GetOrFr_node(aux_or_fr); \
|
2001-04-09 20:54:03 +01:00
|
|
|
DEP_ON_STACK = (LEADER_CP == SgFr_gen_cp(SG_FR)); \
|
|
|
|
}
|
|
|
|
#define find_leader_node(LEADER_CP, DEP_ON_STACK) \
|
|
|
|
{ dep_fr_ptr chain_dep_fr = LOCAL_top_dep_fr; \
|
|
|
|
while (YOUNGER_CP(DepFr_cons_cp(chain_dep_fr), LEADER_CP)) { \
|
|
|
|
if (LEADER_CP == DepFr_leader_cp(chain_dep_fr)) { \
|
|
|
|
DEP_ON_STACK |= DepFr_leader_dep_is_on_stack(chain_dep_fr); \
|
|
|
|
break; \
|
|
|
|
} else if (YOUNGER_CP(LEADER_CP, DepFr_leader_cp(chain_dep_fr))) { \
|
|
|
|
LEADER_CP = DepFr_leader_cp(chain_dep_fr); \
|
|
|
|
DEP_ON_STACK = DepFr_leader_dep_is_on_stack(chain_dep_fr); \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
chain_dep_fr = DepFr_next(chain_dep_fr); \
|
|
|
|
} \
|
|
|
|
}
|
2010-04-03 05:58:14 +01:00
|
|
|
#ifdef TIMESTAMP
|
|
|
|
#define DepFr_init_timestamp_field(DEP_FR) DepFr_timestamp(DEP_FR) = 0
|
|
|
|
#else
|
|
|
|
#define DepFr_init_timestamp_field(DEP_FR)
|
|
|
|
#endif /* TIMESTAMP */
|
|
|
|
#define YAPOR_SET_LOAD(CP_PTR) SCH_set_load(CP_PTR)
|
|
|
|
#define SgFr_init_yapor_fields(SG_FR) \
|
|
|
|
SgFr_gen_worker(SG_FR) = worker_id; \
|
|
|
|
SgFr_gen_top_or_fr(SG_FR) = LOCAL_top_or_fr
|
|
|
|
#define DepFr_init_yapor_fields(DEP_FR, DEP_ON_STACK, TOP_OR_FR) \
|
2011-12-05 16:54:22 +00:00
|
|
|
INIT_LOCK_DEP_FR(DEP_FR); \
|
2010-04-03 05:58:14 +01:00
|
|
|
DepFr_leader_dep_is_on_stack(DEP_FR) = DEP_ON_STACK; \
|
|
|
|
DepFr_top_or_fr(DEP_FR) = TOP_OR_FR; \
|
|
|
|
DepFr_init_timestamp_field(DEP_FR)
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
|
|
|
#define find_dependency_node(SG_FR, LEADER_CP, DEP_ON_STACK) \
|
2014-01-19 21:15:05 +00:00
|
|
|
LEADER_CP = SgFr_gen_cp(SG_FR)
|
2001-04-09 20:54:03 +01:00
|
|
|
#define find_leader_node(LEADER_CP, DEP_ON_STACK) \
|
|
|
|
{ dep_fr_ptr chain_dep_fr = LOCAL_top_dep_fr; \
|
|
|
|
while (YOUNGER_CP(DepFr_cons_cp(chain_dep_fr), LEADER_CP)) { \
|
|
|
|
if (EQUAL_OR_YOUNGER_CP(LEADER_CP, DepFr_leader_cp(chain_dep_fr))) { \
|
|
|
|
LEADER_CP = DepFr_leader_cp(chain_dep_fr); \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
chain_dep_fr = DepFr_next(chain_dep_fr); \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
#define YAPOR_SET_LOAD(CP_PTR)
|
|
|
|
#define SgFr_init_yapor_fields(SG_FR)
|
|
|
|
#define DepFr_init_yapor_fields(DEP_FR, DEP_ON_STACK, TOP_OR_FR)
|
|
|
|
#endif /* YAPOR */
|
|
|
|
|
2011-11-09 11:00:31 +00:00
|
|
|
#ifdef MODE_DIRECTED_TABLING
|
2011-12-22 16:50:20 +00:00
|
|
|
#define TabEnt_init_mode_directed_field(TAB_ENT, MODE_ARRAY) \
|
2011-11-09 11:00:31 +00:00
|
|
|
TabEnt_mode_directed(TAB_ENT) = MODE_ARRAY
|
2011-12-22 16:50:20 +00:00
|
|
|
#define SgEnt_init_mode_directed_fields(SG_ENT, MODE_ARRAY) \
|
|
|
|
SgEnt_invalid_chain(SG_ENT) = NULL; \
|
|
|
|
SgEnt_mode_directed(SG_ENT) = MODE_ARRAY
|
|
|
|
#define SgFr_init_mode_directed_fields(SG_FR, MODE_ARRAY) \
|
|
|
|
SgFr_invalid_chain(SG_FR) = NULL; \
|
2011-11-09 11:00:31 +00:00
|
|
|
SgFr_mode_directed(SG_FR) = MODE_ARRAY
|
2011-12-22 16:50:20 +00:00
|
|
|
#define AnsHash_init_previous_field(HASH, SG_FR) \
|
|
|
|
if (SgFr_hash_chain(SG_FR)) \
|
|
|
|
Hash_previous(SgFr_hash_chain(SG_FR)) = HASH; \
|
2011-11-09 11:00:31 +00:00
|
|
|
Hash_previous(HASH) = NULL
|
|
|
|
#else
|
2011-12-22 16:50:20 +00:00
|
|
|
#define TabEnt_init_mode_directed_field(TAB_ENT, MODE_ARRAY)
|
2012-01-05 11:41:46 +00:00
|
|
|
#define SgEnt_init_mode_directed_fields(SG_ENT, MODE_ARRAY)
|
2011-12-22 16:50:20 +00:00
|
|
|
#define SgFr_init_mode_directed_fields(SG_FR, MODE_ARRAY)
|
2011-11-09 11:00:31 +00:00
|
|
|
#define AnsHash_init_previous_field(HASH, SG_FR)
|
|
|
|
#endif /* MODE_DIRECTED_TABLING */
|
|
|
|
|
2011-12-05 16:54:22 +00:00
|
|
|
#if defined(YAPOR) || defined(THREADS_FULL_SHARING) || defined(THREADS_CONSUMER_SHARING)
|
|
|
|
#define INIT_LOCK_SG_FR(SG_FR) INIT_LOCK(SgFr_lock(SG_FR))
|
|
|
|
#define LOCK_SG_FR(SG_FR) LOCK(SgFr_lock(SG_FR))
|
|
|
|
#define UNLOCK_SG_FR(SG_FR) UNLOCK(SgFr_lock(SG_FR))
|
|
|
|
#else
|
|
|
|
#define INIT_LOCK_SG_FR(SG_FR)
|
|
|
|
#define LOCK_SG_FR(SG_FR)
|
|
|
|
#define UNLOCK_SG_FR(SG_FR)
|
|
|
|
#endif /* YAPOR || THREADS_FULL_SHARING || THREADS_CONSUMER_SHARING */
|
|
|
|
|
|
|
|
#ifdef YAPOR
|
|
|
|
#define INIT_LOCK_DEP_FR(DEP_FR) INIT_LOCK(DepFr_lock(DEP_FR))
|
|
|
|
#define LOCK_DEP_FR(DEP_FR) LOCK(DepFr_lock(DEP_FR))
|
|
|
|
#define UNLOCK_DEP_FR(DEP_FR) UNLOCK(DepFr_lock(DEP_FR))
|
|
|
|
#define IS_UNLOCKED_DEP_FR(DEP_FR) IS_UNLOCKED(DepFr_lock(DEP_FR))
|
|
|
|
#else
|
|
|
|
#define INIT_LOCK_DEP_FR(DEF_FR)
|
|
|
|
#define LOCK_DEP_FR(DEP_FR)
|
|
|
|
#define UNLOCK_DEP_FR(DEP_FR)
|
|
|
|
#define IS_UNLOCKED_DEP_FR(DEP_FR)
|
|
|
|
#endif /* YAPOR */
|
|
|
|
|
2011-12-22 16:50:20 +00:00
|
|
|
#if defined(SUBGOAL_TRIE_LOCK_AT_ENTRY_LEVEL) || defined(THREADS_NO_SHARING)
|
|
|
|
#define INIT_LOCK_TAB_ENT(TAB_ENT) INIT_LOCK(TabEnt_lock(TAB_ENT))
|
|
|
|
#else
|
|
|
|
#define INIT_LOCK_TAB_ENT(TAB_ENT)
|
|
|
|
#endif /* SUBGOAL_TRIE_LOCK_AT_ENTRY_LEVEL || THREADS_NO_SHARING */
|
|
|
|
|
2011-11-15 16:57:51 +00:00
|
|
|
#ifdef SUBGOAL_TRIE_LOCK_AT_ENTRY_LEVEL
|
|
|
|
#define LOCK_SUBGOAL_TRIE(TAB_ENT) LOCK(TabEnt_lock(TAB_ENT))
|
|
|
|
#define UNLOCK_SUBGOAL_TRIE(TAB_ENT) UNLOCK(TabEnt_lock(TAB_ENT))
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2011-11-16 11:42:04 +00:00
|
|
|
#define LOCK_SUBGOAL_TRIE(TAB_ENT)
|
|
|
|
#define UNLOCK_SUBGOAL_TRIE(TAB_ENT)
|
2011-11-15 16:57:51 +00:00
|
|
|
#endif /* SUBGOAL_TRIE_LOCK_AT_ENTRY_LEVEL */
|
|
|
|
|
|
|
|
#ifdef ANSWER_TRIE_LOCK_AT_ENTRY_LEVEL
|
2011-12-05 16:54:22 +00:00
|
|
|
#define LOCK_ANSWER_TRIE(SG_FR) LOCK_SG_FR(SG_FR)
|
|
|
|
#define UNLOCK_ANSWER_TRIE(SG_FR) UNLOCK_SG_FR(SG_FR)
|
2011-11-16 11:42:04 +00:00
|
|
|
#define AnsHash_init_chain_fields(HASH, SG_FR) \
|
|
|
|
AnsHash_init_previous_field(HASH, SG_FR); \
|
|
|
|
Hash_next(HASH) = SgFr_hash_chain(SG_FR); \
|
2011-11-15 16:57:51 +00:00
|
|
|
SgFr_hash_chain(SG_FR) = HASH
|
|
|
|
#else
|
2011-11-16 11:42:04 +00:00
|
|
|
#define LOCK_ANSWER_TRIE(SG_FR)
|
|
|
|
#define UNLOCK_ANSWER_TRIE(SG_FR)
|
|
|
|
#define AnsHash_init_chain_fields(HASH, SG_FR) \
|
2011-12-05 16:54:22 +00:00
|
|
|
LOCK_SG_FR(SG_FR); \
|
2011-11-16 11:42:04 +00:00
|
|
|
AnsHash_init_previous_field(HASH, SG_FR); \
|
|
|
|
Hash_next(HASH) = SgFr_hash_chain(SG_FR); \
|
|
|
|
SgFr_hash_chain(SG_FR) = HASH; \
|
2011-12-05 16:54:22 +00:00
|
|
|
UNLOCK_SG_FR(SG_FR)
|
2011-11-15 16:57:51 +00:00
|
|
|
#endif /* ANSWER_TRIE_LOCK_AT_ENTRY_LEVEL */
|
|
|
|
|
2011-11-16 11:42:04 +00:00
|
|
|
#ifdef SUBGOAL_TRIE_LOCK_USING_NODE_FIELD
|
|
|
|
#define LOCK_SUBGOAL_NODE(NODE) LOCK(TrNode_lock(NODE))
|
|
|
|
#define UNLOCK_SUBGOAL_NODE(NODE) UNLOCK(TrNode_lock(NODE))
|
2011-11-15 16:57:51 +00:00
|
|
|
#define SgNode_init_lock_field(NODE) INIT_LOCK(TrNode_lock(NODE))
|
2011-12-05 17:02:06 +00:00
|
|
|
#elif defined(SUBGOAL_TRIE_LOCK_USING_GLOBAL_ARRAY)
|
2011-11-16 11:42:04 +00:00
|
|
|
#define LOCK_SUBGOAL_NODE(NODE) LOCK(HASH_TRIE_LOCK(NODE))
|
|
|
|
#define UNLOCK_SUBGOAL_NODE(NODE) UNLOCK(HASH_TRIE_LOCK(NODE))
|
|
|
|
#define SgNode_init_lock_field(NODE)
|
2011-11-15 16:57:51 +00:00
|
|
|
#else
|
2011-11-16 11:42:04 +00:00
|
|
|
#define LOCK_SUBGOAL_NODE(NODE)
|
|
|
|
#define UNLOCK_SUBGOAL_NODE(NODE)
|
2011-11-15 16:57:51 +00:00
|
|
|
#define SgNode_init_lock_field(NODE)
|
2011-11-16 11:42:04 +00:00
|
|
|
#endif /* SUBGOAL_TRIE_LOCK_LEVEL */
|
2011-11-15 16:57:51 +00:00
|
|
|
|
2011-11-16 11:42:04 +00:00
|
|
|
#ifdef ANSWER_TRIE_LOCK_USING_NODE_FIELD
|
|
|
|
#define LOCK_ANSWER_NODE(NODE) LOCK(TrNode_lock(NODE))
|
|
|
|
#define UNLOCK_ANSWER_NODE(NODE) UNLOCK(TrNode_lock(NODE))
|
2011-11-15 16:57:51 +00:00
|
|
|
#define AnsNode_init_lock_field(NODE) INIT_LOCK(TrNode_lock(NODE))
|
2011-12-05 17:02:06 +00:00
|
|
|
#elif defined(ANSWER_TRIE_LOCK_USING_GLOBAL_ARRAY)
|
2011-11-16 11:42:04 +00:00
|
|
|
#define LOCK_ANSWER_NODE(NODE) LOCK(HASH_TRIE_LOCK(NODE))
|
|
|
|
#define UNLOCK_ANSWER_NODE(NODE) UNLOCK(HASH_TRIE_LOCK(NODE))
|
|
|
|
#define AnsNode_init_lock_field(NODE)
|
2011-11-15 16:57:51 +00:00
|
|
|
#else
|
2011-11-16 11:42:04 +00:00
|
|
|
#define LOCK_ANSWER_NODE(NODE)
|
|
|
|
#define UNLOCK_ANSWER_NODE(NODE)
|
2011-11-15 16:57:51 +00:00
|
|
|
#define AnsNode_init_lock_field(NODE)
|
2011-11-16 11:42:04 +00:00
|
|
|
#endif /* ANSWER_TRIE_LOCK_LEVEL */
|
2010-04-03 05:58:14 +01:00
|
|
|
|
2011-11-16 11:42:04 +00:00
|
|
|
#ifdef GLOBAL_TRIE_LOCK_USING_NODE_FIELD
|
|
|
|
#define LOCK_GLOBAL_NODE(NODE) LOCK(TrNode_lock(NODE))
|
|
|
|
#define UNLOCK_GLOBAL_NODE(NODE) UNLOCK(TrNode_lock(NODE))
|
2011-11-15 16:57:51 +00:00
|
|
|
#define GtNode_init_lock_field(NODE) INIT_LOCK(TrNode_lock(NODE))
|
2011-12-05 17:02:06 +00:00
|
|
|
#elif defined(GLOBAL_TRIE_LOCK_USING_GLOBAL_ARRAY)
|
2011-11-16 11:42:04 +00:00
|
|
|
#define LOCK_GLOBAL_NODE(NODE) LOCK(HASH_TRIE_LOCK(NODE))
|
|
|
|
#define UNLOCK_GLOBAL_NODE(NODE) UNLOCK(HASH_TRIE_LOCK(NODE))
|
|
|
|
#define GtNode_init_lock_field(NODE)
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2011-11-16 11:42:04 +00:00
|
|
|
#define LOCK_GLOBAL_NODE(NODE)
|
|
|
|
#define UNLOCK_GLOBAL_NODE(NODE)
|
2011-11-15 16:57:51 +00:00
|
|
|
#define GtNode_init_lock_field(NODE)
|
2011-11-16 11:42:04 +00:00
|
|
|
#endif /* GLOBAL_TRIE_LOCK_LEVEL */
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2011-12-22 16:50:20 +00:00
|
|
|
#ifdef THREADS_NO_SHARING
|
|
|
|
#define TabEnt_init_subgoal_trie_field(TAB_ENT) \
|
|
|
|
INIT_BUCKETS(&TabEnt_subgoal_trie(TAB_ENT), THREADS_NUM_BUCKETS)
|
|
|
|
#else
|
|
|
|
#define TabEnt_init_subgoal_trie_field(TAB_ENT) \
|
|
|
|
{ register sg_node_ptr sg_node; \
|
|
|
|
new_subgoal_trie_node(sg_node, 0, NULL, NULL, NULL); \
|
|
|
|
TabEnt_subgoal_trie(TAB_ENT) = sg_node; \
|
|
|
|
}
|
|
|
|
#endif /* THREADS_NO_SHARING */
|
|
|
|
|
|
|
|
#if defined(THREADS_FULL_SHARING)
|
|
|
|
#define SgFr_init_batched_fields(SG_FR) \
|
|
|
|
SgFr_batched_last_answer(SG_FR) = NULL; \
|
|
|
|
SgFr_batched_cached_answers(SG_FR) = NULL;
|
|
|
|
#else
|
|
|
|
#define SgFr_init_batched_fields(SG_FR)
|
|
|
|
#endif /* THREADS_FULL_SHARING */
|
|
|
|
|
|
|
|
#ifdef THREADS_CONSUMER_SHARING
|
|
|
|
#define DepFr_init_external_field(DEP_FR, IS_EXTERNAL) \
|
|
|
|
DepFr_external(DEP_FR) = IS_EXTERNAL
|
|
|
|
#else
|
|
|
|
#define DepFr_init_external_field(DEP_FR, IS_EXTERNAL)
|
|
|
|
#endif /* THREADS_CONSUMER_SHARING */
|
|
|
|
|
|
|
|
#if defined(THREADS_FULL_SHARING) || defined(THREADS_CONSUMER_SHARING)
|
|
|
|
#define DepFr_init_last_answer_field(DEP_FR, SG_FR) \
|
|
|
|
/* start with TrNode_child(DepFr_last_answer(DEP_FR)) ... */ \
|
|
|
|
/* ... pointing to SgEnt_first_answer(SgFr_sg_ent(SG_FR)) */ \
|
|
|
|
if (SG_FR) \
|
|
|
|
DepFr_last_answer(DEP_FR) = (ans_node_ptr) ( \
|
2012-04-18 20:14:56 +01:00
|
|
|
(CELL) (SgFr_sg_ent((sg_fr_ptr)SG_FR)) + \
|
|
|
|
(CELL) (&SgEnt_first_answer((sg_ent_ptr)DEP_FR)) - \
|
|
|
|
(CELL) (&TrNode_child((ans_node_ptr)DEP_FR))); \
|
2011-12-22 16:50:20 +00:00
|
|
|
else \
|
|
|
|
DepFr_last_answer(DEP_FR) = NULL
|
|
|
|
#else
|
|
|
|
#define DepFr_init_last_answer_field(DEP_FR, SG_FR) \
|
|
|
|
/* start with TrNode_child(DepFr_last_answer(DEP_FR)) ... */ \
|
|
|
|
/* ... pointing to SgFr_first_answer(SG_FR) */ \
|
|
|
|
if (SG_FR) \
|
|
|
|
DepFr_last_answer(DEP_FR) = (ans_node_ptr) ( \
|
2012-04-18 20:14:56 +01:00
|
|
|
(CELL) (SG_FR) + \
|
|
|
|
(CELL) (&SgFr_first_answer((sg_fr_ptr)DEP_FR)) - \
|
|
|
|
(CELL) (&TrNode_child((ans_node_ptr)DEP_FR))); \
|
2011-12-22 16:50:20 +00:00
|
|
|
else \
|
|
|
|
DepFr_last_answer(DEP_FR) = NULL
|
|
|
|
#endif /* THREADS_FULL_SHARING || THREADS_CONSUMER_SHARING */
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-11-09 11:00:31 +00:00
|
|
|
#define new_table_entry(TAB_ENT, PRED_ENTRY, ATOM, ARITY, MODE_ARRAY) \
|
2011-12-22 16:50:20 +00:00
|
|
|
ALLOC_TABLE_ENTRY(TAB_ENT); \
|
|
|
|
INIT_LOCK_TAB_ENT(TAB_ENT); \
|
|
|
|
TabEnt_pe(TAB_ENT) = PRED_ENTRY; \
|
|
|
|
TabEnt_atom(TAB_ENT) = ATOM; \
|
|
|
|
TabEnt_arity(TAB_ENT) = ARITY; \
|
|
|
|
TabEnt_flags(TAB_ENT) = 0; \
|
|
|
|
SetMode_Batched(TabEnt_flags(TAB_ENT)); \
|
|
|
|
SetMode_ExecAnswers(TabEnt_flags(TAB_ENT)); \
|
|
|
|
SetMode_LocalTrie(TabEnt_flags(TAB_ENT)); \
|
|
|
|
TabEnt_mode(TAB_ENT) = TabEnt_flags(TAB_ENT); \
|
|
|
|
if (IsMode_Local(yap_flags[TABLING_MODE_FLAG])) \
|
|
|
|
SetMode_Local(TabEnt_mode(TAB_ENT)); \
|
|
|
|
if (IsMode_LoadAnswers(yap_flags[TABLING_MODE_FLAG])) \
|
|
|
|
SetMode_LoadAnswers(TabEnt_mode(TAB_ENT)); \
|
|
|
|
if (IsMode_GlobalTrie(yap_flags[TABLING_MODE_FLAG])) \
|
|
|
|
SetMode_GlobalTrie(TabEnt_mode(TAB_ENT)); \
|
|
|
|
TabEnt_init_mode_directed_field(TAB_ENT, MODE_ARRAY); \
|
|
|
|
TabEnt_init_subgoal_trie_field(TAB_ENT); \
|
|
|
|
TabEnt_next(TAB_ENT) = GLOBAL_root_tab_ent; \
|
|
|
|
GLOBAL_root_tab_ent = TAB_ENT
|
|
|
|
|
|
|
|
#define new_subgoal_entry(SG_ENT) \
|
|
|
|
{ register ans_node_ptr ans_node; \
|
|
|
|
new_answer_trie_node(ans_node, 0, 0, NULL, NULL, NULL); \
|
|
|
|
ALLOC_SUBGOAL_ENTRY(SG_ENT); \
|
|
|
|
INIT_LOCK(SgEnt_lock(SG_ENT)); \
|
|
|
|
SgEnt_hash_chain(SG_ENT) = NULL; \
|
|
|
|
SgEnt_answer_trie(SG_ENT) = ans_node; \
|
|
|
|
SgEnt_first_answer(SG_ENT) = NULL; \
|
|
|
|
SgEnt_last_answer(SG_ENT) = NULL; \
|
|
|
|
SgEnt_sg_ent_state(SG_ENT) = ready; \
|
|
|
|
SgEnt_active_workers(SG_ENT) = 0; \
|
|
|
|
INIT_BUCKETS(&SgEnt_sg_fr(SG_ENT), THREADS_NUM_BUCKETS); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(THREADS_FULL_SHARING) || defined(THREADS_CONSUMER_SHARING)
|
|
|
|
#define new_subgoal_frame(SG_FR, SG_ENT) \
|
|
|
|
{ ALLOC_SUBGOAL_FRAME(SG_FR); \
|
|
|
|
SgFr_sg_ent(SG_FR) = SG_ENT ; \
|
|
|
|
SgFr_init_batched_fields(SG_FR); \
|
2011-10-22 16:49:13 +01:00
|
|
|
}
|
|
|
|
|
2011-12-22 16:50:20 +00:00
|
|
|
#define init_subgoal_frame(SG_FR) \
|
|
|
|
{ SgFr_state(SG_FR) = evaluating; \
|
|
|
|
SgFr_next(SG_FR) = LOCAL_top_sg_fr; \
|
|
|
|
LOCAL_top_sg_fr = SG_FR; \
|
|
|
|
}
|
|
|
|
#else
|
2011-11-09 11:00:31 +00:00
|
|
|
#define new_subgoal_frame(SG_FR, CODE, MODE_ARRAY) \
|
2010-04-05 03:34:02 +01:00
|
|
|
{ register ans_node_ptr ans_node; \
|
|
|
|
new_answer_trie_node(ans_node, 0, 0, NULL, NULL, NULL); \
|
|
|
|
ALLOC_SUBGOAL_FRAME(SG_FR); \
|
2011-12-05 16:54:22 +00:00
|
|
|
INIT_LOCK_SG_FR(SG_FR); \
|
2010-04-05 03:34:02 +01:00
|
|
|
SgFr_code(SG_FR) = CODE; \
|
|
|
|
SgFr_hash_chain(SG_FR) = NULL; \
|
|
|
|
SgFr_answer_trie(SG_FR) = ans_node; \
|
|
|
|
SgFr_first_answer(SG_FR) = NULL; \
|
|
|
|
SgFr_last_answer(SG_FR) = NULL; \
|
2011-12-22 16:50:20 +00:00
|
|
|
SgFr_init_mode_directed_fields(SG_FR, MODE_ARRAY); \
|
|
|
|
SgFr_state(SG_FR) = ready; \
|
2010-04-03 05:58:14 +01:00
|
|
|
}
|
2011-10-22 16:49:13 +01:00
|
|
|
|
2011-12-22 16:50:20 +00:00
|
|
|
#define init_subgoal_frame(SG_FR) \
|
|
|
|
{ SgFr_init_yapor_fields(SG_FR); \
|
|
|
|
SgFr_state(SG_FR) = evaluating; \
|
|
|
|
SgFr_next(SG_FR) = LOCAL_top_sg_fr; \
|
|
|
|
LOCAL_top_sg_fr = SG_FR; \
|
2010-04-03 05:58:14 +01:00
|
|
|
}
|
2011-12-22 16:50:20 +00:00
|
|
|
#endif /* THREADS_FULL_SHARING) || THREADS_CONSUMER_SHARING */
|
|
|
|
|
|
|
|
#define new_dependency_frame(DEP_FR, DEP_ON_STACK, TOP_OR_FR, LEADER_CP, CONS_CP, SG_FR, IS_EXTERNAL, NEXT) \
|
|
|
|
ALLOC_DEPENDENCY_FRAME(DEP_FR); \
|
|
|
|
DepFr_init_yapor_fields(DEP_FR, DEP_ON_STACK, TOP_OR_FR); \
|
|
|
|
DepFr_init_external_field(DEP_FR, IS_EXTERNAL); \
|
|
|
|
DepFr_backchain_cp(DEP_FR) = NULL; \
|
|
|
|
DepFr_leader_cp(DEP_FR) = NORM_CP(LEADER_CP); \
|
|
|
|
DepFr_cons_cp(DEP_FR) = NORM_CP(CONS_CP); \
|
|
|
|
DepFr_init_last_answer_field(DEP_FR, SG_FR); \
|
2010-04-03 05:58:14 +01:00
|
|
|
DepFr_next(DEP_FR) = NEXT
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2011-11-15 16:57:51 +00:00
|
|
|
#define new_suspension_frame(SUSP_FR, TOP_OR_FR_ON_STACK, TOP_DEP, TOP_SG, \
|
|
|
|
H_REG, B_REG, TR_REG, H_SIZE, B_SIZE, TR_SIZE) \
|
|
|
|
ALLOC_SUSPENSION_FRAME(SUSP_FR); \
|
|
|
|
SuspFr_top_or_fr_on_stack(SUSP_FR) = TOP_OR_FR_ON_STACK; \
|
|
|
|
SuspFr_top_dep_fr(SUSP_FR) = TOP_DEP; \
|
|
|
|
SuspFr_top_sg_fr(SUSP_FR) = TOP_SG; \
|
|
|
|
SuspFr_global_reg(SUSP_FR) = (void *) (H_REG); \
|
|
|
|
SuspFr_local_reg(SUSP_FR) = (void *) (B_REG); \
|
|
|
|
SuspFr_trail_reg(SUSP_FR) = (void *) (TR_REG); \
|
|
|
|
ALLOC_BLOCK(SuspFr_global_start(SUSP_FR), H_SIZE + B_SIZE + TR_SIZE, void *); \
|
|
|
|
SuspFr_local_start(SUSP_FR) = SuspFr_global_start(SUSP_FR) + H_SIZE; \
|
|
|
|
SuspFr_trail_start(SUSP_FR) = SuspFr_local_start(SUSP_FR) + B_SIZE; \
|
|
|
|
SuspFr_global_size(SUSP_FR) = H_SIZE; \
|
|
|
|
SuspFr_local_size(SUSP_FR) = B_SIZE; \
|
|
|
|
SuspFr_trail_size(SUSP_FR) = TR_SIZE; \
|
|
|
|
memcpy(SuspFr_global_start(SUSP_FR), SuspFr_global_reg(SUSP_FR), H_SIZE); \
|
|
|
|
memcpy(SuspFr_local_start(SUSP_FR), SuspFr_local_reg(SUSP_FR), B_SIZE); \
|
2001-04-09 20:54:03 +01:00
|
|
|
memcpy(SuspFr_trail_start(SUSP_FR), SuspFr_trail_reg(SUSP_FR), TR_SIZE)
|
|
|
|
|
2010-04-05 03:34:02 +01:00
|
|
|
#define new_subgoal_trie_node(NODE, ENTRY, CHILD, PARENT, NEXT) \
|
|
|
|
ALLOC_SUBGOAL_TRIE_NODE(NODE); \
|
|
|
|
TrNode_entry(NODE) = ENTRY; \
|
|
|
|
TrNode_child(NODE) = CHILD; \
|
|
|
|
TrNode_parent(NODE) = PARENT; \
|
2011-11-15 16:57:51 +00:00
|
|
|
TrNode_next(NODE) = NEXT; \
|
|
|
|
SgNode_init_lock_field(NODE)
|
2009-09-27 02:31:31 +01:00
|
|
|
|
2010-04-05 03:34:02 +01:00
|
|
|
#define new_answer_trie_node(NODE, INSTR, ENTRY, CHILD, PARENT, NEXT) \
|
2011-12-22 16:50:20 +00:00
|
|
|
ALLOC_ANSWER_TRIE_NODE(NODE); \
|
2010-04-05 03:34:02 +01:00
|
|
|
TrNode_instr(NODE) = INSTR; \
|
|
|
|
TrNode_entry(NODE) = ENTRY; \
|
|
|
|
TrNode_child(NODE) = CHILD; \
|
|
|
|
TrNode_parent(NODE) = PARENT; \
|
2011-11-15 16:57:51 +00:00
|
|
|
TrNode_next(NODE) = NEXT; \
|
|
|
|
AnsNode_init_lock_field(NODE)
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2010-04-03 05:58:14 +01:00
|
|
|
#define new_global_trie_node(NODE, ENTRY, CHILD, PARENT, NEXT) \
|
|
|
|
ALLOC_GLOBAL_TRIE_NODE(NODE); \
|
|
|
|
TrNode_entry(NODE) = ENTRY; \
|
|
|
|
TrNode_child(NODE) = CHILD; \
|
|
|
|
TrNode_parent(NODE) = PARENT; \
|
2011-11-15 16:57:51 +00:00
|
|
|
TrNode_next(NODE) = NEXT; \
|
|
|
|
GtNode_init_lock_field(NODE)
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2011-12-22 16:50:20 +00:00
|
|
|
#define new_answer_ref_node(NODE, ANSWER, NEXT, PREVIOUS) \
|
|
|
|
ALLOC_ANSWER_REF_NODE(NODE); \
|
|
|
|
RefNode_answer(NODE) = ANSWER; \
|
|
|
|
RefNode_next(NODE) = NEXT; \
|
|
|
|
RefNode_previous(NODE) = PREVIOUS
|
|
|
|
|
|
|
|
#define new_subgoal_trie_hash(HASH, NUM_NODES, TAB_ENT) \
|
|
|
|
ALLOC_SUBGOAL_TRIE_HASH(HASH); \
|
|
|
|
Hash_mark(HASH) = SUBGOAL_TRIE_HASH_MARK; \
|
|
|
|
Hash_num_buckets(HASH) = BASE_HASH_BUCKETS; \
|
|
|
|
ALLOC_BUCKETS(Hash_buckets(HASH), BASE_HASH_BUCKETS); \
|
|
|
|
Hash_num_nodes(HASH) = NUM_NODES
|
|
|
|
|
|
|
|
#define new_answer_trie_hash(HASH, NUM_NODES, SG_FR) \
|
|
|
|
ALLOC_ANSWER_TRIE_HASH(HASH); \
|
|
|
|
Hash_mark(HASH) = ANSWER_TRIE_HASH_MARK; \
|
|
|
|
Hash_num_buckets(HASH) = BASE_HASH_BUCKETS; \
|
|
|
|
ALLOC_BUCKETS(Hash_buckets(HASH), BASE_HASH_BUCKETS); \
|
|
|
|
Hash_num_nodes(HASH) = NUM_NODES; \
|
2011-11-09 11:00:31 +00:00
|
|
|
AnsHash_init_chain_fields(HASH, SG_FR)
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2011-12-22 16:50:20 +00:00
|
|
|
#define new_global_trie_hash(HASH, NUM_NODES) \
|
|
|
|
ALLOC_GLOBAL_TRIE_HASH(HASH); \
|
|
|
|
Hash_mark(HASH) = GLOBAL_TRIE_HASH_MARK; \
|
|
|
|
Hash_num_buckets(HASH) = BASE_HASH_BUCKETS; \
|
|
|
|
ALLOC_BUCKETS(Hash_buckets(HASH), BASE_HASH_BUCKETS); \
|
2010-04-03 05:58:14 +01:00
|
|
|
Hash_num_nodes(HASH) = NUM_NODES
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2005-08-04 16:45:56 +01:00
|
|
|
#ifdef LIMIT_TABLING
|
|
|
|
#define insert_into_global_sg_fr_list(SG_FR) \
|
2011-05-10 11:47:18 +01:00
|
|
|
SgFr_previous(SG_FR) = GLOBAL_last_sg_fr; \
|
2005-08-04 16:45:56 +01:00
|
|
|
SgFr_next(SG_FR) = NULL; \
|
2011-05-10 11:47:18 +01:00
|
|
|
if (GLOBAL_first_sg_fr == NULL) \
|
|
|
|
GLOBAL_first_sg_fr = SG_FR; \
|
2005-08-04 16:45:56 +01:00
|
|
|
else \
|
2011-05-10 11:47:18 +01:00
|
|
|
SgFr_next(GLOBAL_last_sg_fr) = SG_FR; \
|
|
|
|
GLOBAL_last_sg_fr = SG_FR
|
2005-08-04 16:45:56 +01:00
|
|
|
#define remove_from_global_sg_fr_list(SG_FR) \
|
|
|
|
if (SgFr_previous(SG_FR)) { \
|
|
|
|
if ((SgFr_next(SgFr_previous(SG_FR)) = SgFr_next(SG_FR)) != NULL) \
|
|
|
|
SgFr_previous(SgFr_next(SG_FR)) = SgFr_previous(SG_FR); \
|
|
|
|
else \
|
2011-05-10 11:47:18 +01:00
|
|
|
GLOBAL_last_sg_fr = SgFr_previous(SG_FR); \
|
2005-08-04 16:45:56 +01:00
|
|
|
} else { \
|
2011-05-10 11:47:18 +01:00
|
|
|
if ((GLOBAL_first_sg_fr = SgFr_next(SG_FR)) != NULL) \
|
2005-08-04 16:45:56 +01:00
|
|
|
SgFr_previous(SgFr_next(SG_FR)) = NULL; \
|
|
|
|
else \
|
2011-05-10 11:47:18 +01:00
|
|
|
GLOBAL_last_sg_fr = NULL; \
|
2005-08-04 16:45:56 +01:00
|
|
|
} \
|
2011-05-10 12:20:22 +01:00
|
|
|
if (GLOBAL_check_sg_fr == SG_FR) \
|
|
|
|
GLOBAL_check_sg_fr = SgFr_previous(SG_FR)
|
2005-08-04 16:45:56 +01:00
|
|
|
#else
|
|
|
|
#define insert_into_global_sg_fr_list(SG_FR)
|
|
|
|
#define remove_from_global_sg_fr_list(SG_FR)
|
|
|
|
#endif /* LIMIT_TABLING */
|
|
|
|
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2010-04-03 05:58:14 +01:00
|
|
|
/******************************
|
2001-04-09 20:54:03 +01:00
|
|
|
** Inline funcions **
|
2010-04-03 05:58:14 +01:00
|
|
|
******************************/
|
|
|
|
|
2011-12-22 16:50:20 +00:00
|
|
|
#ifdef THREADS
|
2013-03-26 21:11:19 +00:00
|
|
|
#define get_insert_thread_bucket(b, bl) __get_insert_thread_bucket((b), (bl) PASS_REGS)
|
|
|
|
|
|
|
|
static inline void **__get_insert_thread_bucket(void **buckets, lockvar *buckets_lock USES_REGS) {
|
2011-12-22 16:50:20 +00:00
|
|
|
|
|
|
|
/* direct bucket */
|
|
|
|
if (worker_id < THREADS_DIRECT_BUCKETS)
|
|
|
|
return buckets + worker_id;
|
|
|
|
|
|
|
|
/* indirect bucket */
|
|
|
|
buckets = buckets + THREADS_DIRECT_BUCKETS + (worker_id - THREADS_DIRECT_BUCKETS) / THREADS_DIRECT_BUCKETS;
|
|
|
|
if (*buckets)
|
2015-01-20 03:00:42 +00:00
|
|
|
return (void **)((char *)(*buckets) + (worker_id - THREADS_DIRECT_BUCKETS) % THREADS_DIRECT_BUCKETS);
|
2011-12-22 16:50:20 +00:00
|
|
|
|
|
|
|
/* insert indirect bucket */
|
|
|
|
LOCK(*buckets_lock);
|
|
|
|
if (*buckets == NULL)
|
|
|
|
ALLOC_BUCKETS(*buckets, THREADS_DIRECT_BUCKETS);
|
|
|
|
UNLOCK(*buckets_lock);
|
2015-01-20 03:00:42 +00:00
|
|
|
return (void **)((char *)(*buckets) + (worker_id - THREADS_DIRECT_BUCKETS) % THREADS_DIRECT_BUCKETS);
|
2011-12-22 16:50:20 +00:00
|
|
|
}
|
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
#define get_thread_bucket(b) __get_thread_bucket((b) PASS_REGS)
|
2011-12-22 16:50:20 +00:00
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
static inline void **__get_thread_bucket(void **buckets USES_REGS) {
|
2011-12-22 16:50:20 +00:00
|
|
|
|
|
|
|
/* direct bucket */
|
|
|
|
if (worker_id < THREADS_DIRECT_BUCKETS)
|
|
|
|
return buckets + worker_id;
|
|
|
|
|
|
|
|
/* indirect bucket */
|
|
|
|
buckets = buckets + THREADS_DIRECT_BUCKETS + (worker_id - THREADS_DIRECT_BUCKETS) / THREADS_DIRECT_BUCKETS;
|
|
|
|
if (*buckets)
|
2015-01-20 03:00:42 +00:00
|
|
|
return (void **)((char *)(buckets) + (worker_id - THREADS_DIRECT_BUCKETS) % THREADS_DIRECT_BUCKETS);
|
2011-12-22 16:50:20 +00:00
|
|
|
|
|
|
|
/* empty indirect bucket */
|
|
|
|
return buckets;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void abolish_thread_buckets(void **buckets) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* abolish indirect buckets */
|
|
|
|
buckets += THREADS_NUM_BUCKETS;
|
|
|
|
for (i = 0; i < THREADS_INDIRECT_BUCKETS; i++) {
|
|
|
|
if (*--buckets) {
|
|
|
|
FREE_BUCKETS(*buckets);
|
|
|
|
*buckets = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(THREADS_SUBGOAL_SHARING)
|
|
|
|
/* abolish direct buckets */
|
|
|
|
buckets -= THREADS_DIRECT_BUCKETS;
|
|
|
|
FREE_BUCKETS(buckets);
|
|
|
|
#endif /* THREADS_SUBGOAL_SHARING */
|
|
|
|
}
|
|
|
|
#endif /* THREADS */
|
|
|
|
|
|
|
|
|
|
|
|
static inline sg_node_ptr get_insert_subgoal_trie(tab_ent_ptr tab_ent USES_REGS) {
|
|
|
|
#ifdef THREADS_NO_SHARING
|
|
|
|
sg_node_ptr *sg_node_addr = (sg_node_ptr *) get_insert_thread_bucket((void **) &TabEnt_subgoal_trie(tab_ent), &TabEnt_lock(tab_ent));
|
|
|
|
if (*sg_node_addr == NULL) {
|
|
|
|
new_subgoal_trie_node(*sg_node_addr, 0, NULL, NULL, NULL);
|
|
|
|
}
|
|
|
|
return *sg_node_addr;
|
|
|
|
#else
|
|
|
|
return TabEnt_subgoal_trie(tab_ent);
|
|
|
|
#endif /* THREADS_NO_SHARING */
|
|
|
|
}
|
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
#define get_subgoal_trie(te) __get_subgoal_trie((te) PASS_REGS)
|
2011-12-22 16:50:20 +00:00
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
static inline sg_node_ptr __get_subgoal_trie(tab_ent_ptr tab_ent USES_REGS) {
|
2011-12-22 16:50:20 +00:00
|
|
|
#ifdef THREADS_NO_SHARING
|
|
|
|
sg_node_ptr *sg_node_addr = (sg_node_ptr *) get_thread_bucket((void **) &TabEnt_subgoal_trie(tab_ent));
|
|
|
|
return *sg_node_addr;
|
|
|
|
#else
|
|
|
|
return TabEnt_subgoal_trie(tab_ent);
|
|
|
|
#endif /* THREADS_NO_SHARING */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline sg_node_ptr get_subgoal_trie_for_abolish(tab_ent_ptr tab_ent USES_REGS) {
|
|
|
|
#ifdef THREADS_NO_SHARING
|
|
|
|
sg_node_ptr *sg_node_addr = (sg_node_ptr *) get_thread_bucket((void **) &TabEnt_subgoal_trie(tab_ent));
|
|
|
|
sg_node_ptr sg_node = *sg_node_addr;
|
|
|
|
*sg_node_addr = NULL;
|
2012-05-14 19:11:58 +01:00
|
|
|
if (GLOBAL_NOfThreads == 1)
|
2011-12-22 16:50:20 +00:00
|
|
|
abolish_thread_buckets((void **) &TabEnt_subgoal_trie(tab_ent));
|
|
|
|
return sg_node;
|
|
|
|
#else
|
|
|
|
return TabEnt_subgoal_trie(tab_ent);
|
|
|
|
#endif /* THREADS_NO_SHARING */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline sg_fr_ptr *get_insert_subgoal_frame_addr(sg_node_ptr sg_node USES_REGS) {
|
|
|
|
sg_fr_ptr *sg_fr_addr = (sg_fr_ptr *) &TrNode_sg_fr(sg_node);
|
|
|
|
#if defined(THREADS_SUBGOAL_SHARING) || defined(THREADS_FULL_SHARING) || defined(THREADS_CONSUMER_SHARING)
|
|
|
|
if (*sg_fr_addr == NULL) {
|
|
|
|
LOCK_SUBGOAL_NODE(sg_node);
|
|
|
|
if (*sg_fr_addr == NULL) {
|
|
|
|
#if defined(THREADS_SUBGOAL_SHARING)
|
|
|
|
ALLOC_BUCKETS(TrNode_sg_fr(sg_node), THREADS_NUM_BUCKETS);
|
|
|
|
#elif defined(THREADS_FULL_SHARING) || defined(THREADS_CONSUMER_SHARING)
|
|
|
|
sg_ent_ptr sg_ent;
|
|
|
|
new_subgoal_entry(sg_ent);
|
|
|
|
TrNode_sg_ent(sg_node) = (sg_node_ptr) sg_ent;
|
|
|
|
#endif
|
|
|
|
TAG_AS_SUBGOAL_LEAF_NODE(sg_node);
|
|
|
|
}
|
|
|
|
UNLOCK_SUBGOAL_NODE(sg_node);
|
|
|
|
}
|
|
|
|
sg_fr_addr = (sg_fr_ptr *) get_insert_thread_bucket(
|
|
|
|
#if defined(THREADS_SUBGOAL_SHARING)
|
|
|
|
(void **) UNTAG_SUBGOAL_NODE(TrNode_sg_fr(sg_node)),
|
|
|
|
#elif defined(THREADS_FULL_SHARING) || defined(THREADS_CONSUMER_SHARING)
|
2012-01-05 11:41:46 +00:00
|
|
|
(void **) &SgEnt_sg_fr((sg_ent_ptr) UNTAG_SUBGOAL_NODE(TrNode_sg_fr(sg_node))),
|
2011-12-22 16:50:20 +00:00
|
|
|
#endif
|
|
|
|
#ifdef SUBGOAL_TRIE_LOCK_USING_NODE_FIELD
|
|
|
|
&TrNode_lock(sg_node)
|
|
|
|
#elif defined(SUBGOAL_TRIE_LOCK_USING_GLOBAL_ARRAY)
|
|
|
|
&HASH_TRIE_LOCK(sg_node)
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
#endif /* THREADS_SUBGOAL_SHARING || THREADS_FULL_SHARING || THREADS_CONSUMER_SHARING */
|
|
|
|
return sg_fr_addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline sg_fr_ptr get_subgoal_frame(sg_node_ptr sg_node) {
|
|
|
|
#if defined(THREADS_SUBGOAL_SHARING)
|
|
|
|
sg_fr_ptr *sg_fr_addr = (sg_fr_ptr *) get_thread_bucket((void **) UNTAG_SUBGOAL_NODE(TrNode_sg_fr(sg_node)));
|
|
|
|
return *sg_fr_addr;
|
|
|
|
#elif defined(THREADS_FULL_SHARING) || defined(THREADS_CONSUMER_SHARING)
|
2012-01-05 11:41:46 +00:00
|
|
|
sg_fr_ptr *sg_fr_addr = (sg_fr_ptr *) get_thread_bucket((void **) &SgEnt_sg_fr((sg_ent_ptr) UNTAG_SUBGOAL_NODE(TrNode_sg_fr(sg_node))));
|
2011-12-22 16:50:20 +00:00
|
|
|
return *sg_fr_addr;
|
|
|
|
#else
|
2012-01-05 11:41:46 +00:00
|
|
|
return (sg_fr_ptr) UNTAG_SUBGOAL_NODE(TrNode_sg_fr(sg_node));
|
2011-12-22 16:50:20 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline sg_fr_ptr get_subgoal_frame_for_abolish(sg_node_ptr sg_node USES_REGS) {
|
|
|
|
#if defined(THREADS_SUBGOAL_SHARING)
|
|
|
|
sg_fr_ptr *sg_fr_addr = (sg_fr_ptr *) get_thread_bucket((void **) UNTAG_SUBGOAL_NODE(TrNode_sg_fr(sg_node)));
|
|
|
|
sg_fr_ptr sg_fr = *sg_fr_addr;
|
2012-05-14 19:11:58 +01:00
|
|
|
if (GLOBAL_NOfThreads == 1)
|
2011-12-22 16:50:20 +00:00
|
|
|
abolish_thread_buckets((void **) UNTAG_SUBGOAL_NODE(TrNode_sg_fr(sg_node)));
|
|
|
|
else
|
|
|
|
*sg_fr_addr = NULL;
|
|
|
|
return sg_fr;
|
|
|
|
#elif defined(THREADS_FULL_SHARING) || defined(THREADS_CONSUMER_SHARING)
|
2012-01-05 11:41:46 +00:00
|
|
|
sg_fr_ptr *sg_fr_addr = (sg_fr_ptr *) get_thread_bucket((void **) &SgEnt_sg_fr((sg_ent_ptr) UNTAG_SUBGOAL_NODE(TrNode_sg_fr(sg_node))));
|
2011-12-22 16:50:20 +00:00
|
|
|
sg_fr_ptr sg_fr = *sg_fr_addr;
|
2012-05-14 19:11:58 +01:00
|
|
|
if (GLOBAL_NOfThreads == 1)
|
2012-01-05 11:41:46 +00:00
|
|
|
abolish_thread_buckets((void **) &SgEnt_sg_fr((sg_ent_ptr) UNTAG_SUBGOAL_NODE(TrNode_sg_fr(sg_node))));
|
2011-12-22 16:50:20 +00:00
|
|
|
else
|
|
|
|
*sg_fr_addr = NULL;
|
|
|
|
return sg_fr;
|
|
|
|
#else
|
2012-01-05 11:41:46 +00:00
|
|
|
return (sg_fr_ptr) UNTAG_SUBGOAL_NODE(TrNode_sg_fr(sg_node));
|
2011-12-22 16:50:20 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef THREADS_FULL_SHARING
|
2013-03-26 21:11:19 +00:00
|
|
|
#define SgFr_batched_cached_answers_check_insert(s, a) __SgFr_batched_cached_answers_check_insert((s), (a) PASS_REGS)
|
|
|
|
static inline void SgFr_batched_cached_answers_check_insert(sg_fr_ptr sg_fr, ans_node_ptr ans_node USES_REGS) {
|
2011-12-22 16:50:20 +00:00
|
|
|
|
|
|
|
if (SgFr_batched_last_answer(sg_fr) == NULL)
|
|
|
|
SgFr_batched_last_answer(sg_fr) = SgFr_first_answer(sg_fr);
|
|
|
|
else
|
|
|
|
SgFr_batched_last_answer(sg_fr) = TrNode_child(SgFr_batched_last_answer(sg_fr)) ;
|
|
|
|
|
|
|
|
while (SgFr_batched_last_answer(sg_fr) != ans_node) {
|
|
|
|
struct answer_ref_node *ref_node = NULL;
|
|
|
|
if (SgFr_batched_cached_answers(sg_fr) == NULL) {
|
|
|
|
new_answer_ref_node(ref_node, SgFr_batched_last_answer(sg_fr), NULL, NULL);
|
|
|
|
} else {
|
|
|
|
new_answer_ref_node(ref_node, SgFr_batched_last_answer(sg_fr), SgFr_batched_cached_answers(sg_fr), NULL);
|
|
|
|
RefNode_previous(SgFr_batched_cached_answers(sg_fr)) = ref_node;
|
|
|
|
}
|
|
|
|
SgFr_batched_cached_answers(sg_fr) = ref_node;
|
|
|
|
SgFr_batched_last_answer(sg_fr) = TrNode_child(SgFr_batched_last_answer(sg_fr)) ;
|
|
|
|
}
|
|
|
|
if (ans_node != NULL)
|
|
|
|
/* new answer */
|
|
|
|
SgFr_batched_last_answer(sg_fr) = ans_node;
|
|
|
|
else
|
|
|
|
/* repeated answer */
|
|
|
|
SgFr_batched_last_answer(sg_fr) = SgFr_last_answer(sg_fr);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
#define SgFr_batched_cached_answers_check_remove(s, a) __SgFr_batched_cached_answers_check_remove((s), (a) PASS_REGS)
|
|
|
|
|
|
|
|
static inline int __SgFr_batched_cached_answers_check_remove(sg_fr_ptr sg_fr, ans_node_ptr ans_node USES_REgS) {
|
2011-12-22 16:50:20 +00:00
|
|
|
struct answer_ref_node *local_uncons_ans;
|
|
|
|
|
|
|
|
local_uncons_ans = SgFr_batched_cached_answers(sg_fr) ;
|
|
|
|
while ( local_uncons_ans != NULL ) {
|
|
|
|
if ( RefNode_answer(local_uncons_ans) == ans_node )
|
|
|
|
break;
|
|
|
|
local_uncons_ans = RefNode_next(local_uncons_ans) ;
|
|
|
|
}
|
|
|
|
if ( local_uncons_ans == NULL )
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/*remove node from buffer */
|
|
|
|
if (RefNode_previous(local_uncons_ans) == NULL) {
|
|
|
|
SgFr_batched_cached_answers(sg_fr) = RefNode_next(local_uncons_ans) ;
|
|
|
|
if (SgFr_batched_cached_answers(sg_fr) != NULL)
|
|
|
|
RefNode_previous(SgFr_batched_cached_answers(sg_fr)) = NULL;
|
|
|
|
} else{
|
|
|
|
RefNode_next(RefNode_previous(local_uncons_ans)) = RefNode_next(local_uncons_ans);
|
|
|
|
if (RefNode_next(local_uncons_ans) != NULL)
|
|
|
|
RefNode_previous(RefNode_next(local_uncons_ans)) = RefNode_previous(local_uncons_ans);
|
|
|
|
}
|
|
|
|
FREE_ANSWER_REF_NODE(local_uncons_ans);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* THREADS_FULL_SHARING */
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef THREADS_CONSUMER_SHARING
|
2013-03-26 21:11:19 +00:00
|
|
|
|
|
|
|
#define add_to_tdv(w, wd) __add_to_tdv((w), (wd) PASS_REGS)
|
|
|
|
|
|
|
|
static inline void __add_to_tdv(int wid, int wid_dep USES_REGS) {
|
2011-12-22 16:50:20 +00:00
|
|
|
// thread wid next of thread wid_dep
|
|
|
|
/* check before insert */
|
|
|
|
int c_wid = ThDepFr_next(GLOBAL_th_dep_fr(wid));
|
|
|
|
do {
|
|
|
|
if (c_wid == wid_dep)
|
|
|
|
break;
|
|
|
|
c_wid = ThDepFr_next(GLOBAL_th_dep_fr(c_wid));
|
|
|
|
} while( c_wid != wid );
|
|
|
|
if (c_wid == wid_dep)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (wid < wid_dep) {
|
|
|
|
LOCK(ThDepFr_lock(GLOBAL_th_dep_fr(wid)));
|
|
|
|
LOCK(ThDepFr_lock(GLOBAL_th_dep_fr(wid_dep)));
|
|
|
|
} else {
|
|
|
|
LOCK(ThDepFr_lock(GLOBAL_th_dep_fr(wid_dep)));
|
|
|
|
LOCK(ThDepFr_lock(GLOBAL_th_dep_fr(wid)));
|
|
|
|
}
|
|
|
|
|
|
|
|
c_wid = ThDepFr_next(GLOBAL_th_dep_fr(wid));
|
|
|
|
do {
|
|
|
|
if (c_wid == wid_dep)
|
|
|
|
break;
|
|
|
|
c_wid = ThDepFr_next(GLOBAL_th_dep_fr(c_wid));
|
|
|
|
} while( c_wid != wid );
|
|
|
|
if ( c_wid == wid ){
|
|
|
|
// joining the two different SCCs
|
|
|
|
int t = ThDepFr_next(GLOBAL_th_dep_fr(wid));
|
|
|
|
ThDepFr_next(GLOBAL_th_dep_fr(wid)) = ThDepFr_next(GLOBAL_th_dep_fr(wid_dep));
|
|
|
|
ThDepFr_next(GLOBAL_th_dep_fr(wid_dep)) = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
INFO_THREADS("add_to_tdv (2) :tdv_next[%d] = %d tdv_next[%d]= %d", wid, ThDepFr_next(GLOBAL_th_dep_fr(wid)), wid_dep, ThDepFr_next(GLOBAL_th_dep_fr(wid_dep)));
|
|
|
|
|
|
|
|
UNLOCK(ThDepFr_lock(GLOBAL_th_dep_fr(wid)));
|
|
|
|
UNLOCK(ThDepFr_lock(GLOBAL_th_dep_fr(wid_dep)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
#define check_for_deadlock(s) __check_for_deadlock((s) PASS_REGS)
|
2011-12-22 16:50:20 +00:00
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
static inline void __check_for_deadlock(sg_fr_ptr sg_fr USES_REGS) {
|
2011-12-22 16:50:20 +00:00
|
|
|
sg_fr_ptr local_sg_fr = deadlock_detection(sg_fr);
|
|
|
|
|
|
|
|
if (local_sg_fr){
|
|
|
|
LOCK(ThDepFr_lock(GLOBAL_th_dep_fr(worker_id)));
|
|
|
|
if (YOUNGER_CP(DepFr_leader_cp(LOCAL_top_dep_fr),SgFr_gen_cp(local_sg_fr)))
|
|
|
|
DepFr_leader_cp(LOCAL_top_dep_fr) = SgFr_gen_cp(local_sg_fr);
|
|
|
|
|
|
|
|
UNLOCK(ThDepFr_lock(GLOBAL_th_dep_fr(worker_id)));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
#define deadlock_detection(s) __deadlock_detection((s) PASS_REGS)
|
2011-12-22 16:50:20 +00:00
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
static inline sg_fr_ptr __deadlock_detection(sg_fr_ptr sg_fr USES_REGS) {
|
2011-12-22 16:50:20 +00:00
|
|
|
sg_fr_ptr remote_sg_fr = REMOTE_top_sg_fr(SgFr_gen_worker(sg_fr));
|
|
|
|
|
|
|
|
while( SgFr_sg_ent(remote_sg_fr) != SgFr_sg_ent(sg_fr)){
|
|
|
|
sg_fr_ptr local_sg_fr = SgFr_next(LOCAL_top_sg_fr);
|
|
|
|
while(local_sg_fr){
|
|
|
|
if ( SgFr_sg_ent(local_sg_fr) == SgFr_sg_ent(remote_sg_fr) ||
|
|
|
|
( SgFr_gen_worker(remote_sg_fr) != SgFr_gen_worker(sg_fr) && /* jump to other chain */
|
|
|
|
SgFr_gen_worker(remote_sg_fr) != worker_id && /* not jumping to the chain of the thread that is detecting the deadlock */
|
|
|
|
deadlock_detection(remote_sg_fr))){
|
|
|
|
|
|
|
|
sg_fr_ptr leader_remote_sg_fr;
|
|
|
|
do
|
|
|
|
leader_remote_sg_fr = SgFr_next(remote_sg_fr);
|
|
|
|
while(SgFr_sg_ent(leader_remote_sg_fr) != SgFr_sg_ent(sg_fr));
|
|
|
|
LOCK(ThDepFr_lock(GLOBAL_th_dep_fr(SgFr_gen_worker(leader_remote_sg_fr))));
|
|
|
|
|
|
|
|
if (YOUNGER_CP(DepFr_leader_cp(REMOTE_top_dep_fr(SgFr_gen_worker(leader_remote_sg_fr))),SgFr_gen_cp(leader_remote_sg_fr)))
|
|
|
|
DepFr_leader_cp(REMOTE_top_dep_fr(SgFr_gen_worker(leader_remote_sg_fr))) = SgFr_gen_cp(leader_remote_sg_fr);
|
|
|
|
UNLOCK(ThDepFr_lock(GLOBAL_th_dep_fr(SgFr_gen_worker(leader_remote_sg_fr))));
|
|
|
|
|
|
|
|
add_to_tdv(SgFr_gen_worker(local_sg_fr),SgFr_gen_worker(leader_remote_sg_fr));
|
|
|
|
|
|
|
|
return local_sg_fr;
|
|
|
|
}
|
|
|
|
local_sg_fr = SgFr_next(local_sg_fr);
|
|
|
|
}
|
|
|
|
remote_sg_fr = SgFr_next(remote_sg_fr);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif /* THREADS_CONSUMER_SHARING */
|
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
#define freeze_current_cp() __freeze_current_cp( PASS_REGS1 )
|
2011-12-22 16:50:20 +00:00
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
static inline Int __freeze_current_cp(USES_REGS1) {
|
2010-04-19 02:34:08 +01:00
|
|
|
choiceptr freeze_cp = B;
|
|
|
|
|
|
|
|
B_FZ = freeze_cp;
|
|
|
|
H_FZ = freeze_cp->cp_h;
|
|
|
|
TR_FZ = freeze_cp->cp_tr;
|
|
|
|
B = B->cp_b;
|
|
|
|
HB = B->cp_h;
|
2011-05-23 16:19:47 +01:00
|
|
|
return (LOCAL_LocalBase - (ADDR)freeze_cp);
|
2010-04-03 05:58:14 +01:00
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
#define wake_frozen_cp(f) __wake_frozen_cp((f) PASS_REGS)
|
|
|
|
|
|
|
|
#define restore_bindings(u, r) __restore_bindings((u), (r) PASS_REGS)
|
|
|
|
|
|
|
|
static inline void __wake_frozen_cp(Int frozen_offset USES_REGS) {
|
2011-05-23 16:19:47 +01:00
|
|
|
choiceptr frozen_cp = (choiceptr)(LOCAL_LocalBase - frozen_offset);
|
2010-05-18 17:34:52 +01:00
|
|
|
|
2010-04-19 02:34:08 +01:00
|
|
|
restore_bindings(TR, frozen_cp->cp_tr);
|
|
|
|
B = frozen_cp;
|
|
|
|
TR = TR_FZ;
|
|
|
|
TRAIL_LINK(B->cp_tr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
#define abolish_frozen_cps_until(f) __abolish_frozen_cps_until((f) PASS_REGS )
|
|
|
|
|
|
|
|
static inline void __abolish_frozen_cps_until(Int frozen_offset USES_REGS) {
|
2011-05-23 16:19:47 +01:00
|
|
|
choiceptr frozen_cp = (choiceptr)(LOCAL_LocalBase - frozen_offset);
|
2010-05-18 17:34:52 +01:00
|
|
|
|
|
|
|
B_FZ = frozen_cp;
|
|
|
|
H_FZ = frozen_cp->cp_h;
|
|
|
|
TR_FZ = frozen_cp->cp_tr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
#define abolish_frozen_cps_all() __abolish_frozen_cps_all( PASS_REGS1 )
|
2010-05-18 17:34:52 +01:00
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
static inline void __abolish_frozen_cps_all( USES_REGS1 ) {
|
2011-05-23 16:19:47 +01:00
|
|
|
B_FZ = (choiceptr) LOCAL_LocalBase;
|
|
|
|
H_FZ = (CELL *) LOCAL_GlobalBase;
|
|
|
|
TR_FZ = (tr_fr_ptr) LOCAL_TrailBase;
|
2010-04-19 02:34:08 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
#define adjust_freeze_registers() __adjust_freeze_registers( PASS_REGS1 )
|
2010-04-19 02:34:08 +01:00
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
static inline void __adjust_freeze_registers( USES_REGS1 ) {
|
2001-04-09 20:54:03 +01:00
|
|
|
B_FZ = DepFr_cons_cp(LOCAL_top_dep_fr);
|
|
|
|
H_FZ = B_FZ->cp_h;
|
|
|
|
TR_FZ = B_FZ->cp_tr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
#define mark_as_completed(sg) __mark_as_completed((sg) PASS_REGS )
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
static inline void __mark_as_completed(sg_fr_ptr sg_fr USES_REGS) {
|
2012-06-04 19:47:45 +01:00
|
|
|
#if defined(MODE_DIRECTED_TABLING) && !defined(THREADS_FULL_SHARING) && !defined(THREADS_CONSUMER_SHARING)
|
|
|
|
#endif /* MODE_DIRECTED_TABLING && !THREADS_FULL_SHARING && !THREADS_CONSUMER_SHARING */
|
2011-12-22 16:50:20 +00:00
|
|
|
|
2011-12-05 16:54:22 +00:00
|
|
|
LOCK_SG_FR(sg_fr);
|
2011-12-22 16:50:20 +00:00
|
|
|
#if defined(THREADS_FULL_SHARING) || defined(THREADS_CONSUMER_SHARING)
|
|
|
|
INFO_THREADS(" mark_as_completed sgfr=%p ", SgFr_sg_ent(sg_fr));
|
|
|
|
TABLING_ERROR_CHECKING(mark_as_completed, SgFr_sg_ent_state(sg_fr) > complete);
|
|
|
|
SgFr_active_workers(sg_fr)--;
|
|
|
|
SgFr_sg_ent_state(sg_fr) = complete;
|
|
|
|
#endif /* THREADS_FULL_SHARING || THREADS_CONSUMER_SHARING */
|
2011-11-11 18:05:24 +00:00
|
|
|
SgFr_state(sg_fr) = complete;
|
2011-12-05 16:54:22 +00:00
|
|
|
UNLOCK_SG_FR(sg_fr);
|
2011-10-22 16:49:13 +01:00
|
|
|
#ifdef MODE_DIRECTED_TABLING
|
2011-11-11 18:05:24 +00:00
|
|
|
if (SgFr_invalid_chain(sg_fr)) {
|
|
|
|
ans_node_ptr current_node, next_node;
|
|
|
|
/* find first valid answer */
|
|
|
|
current_node = SgFr_first_answer(sg_fr);
|
2011-12-22 16:50:20 +00:00
|
|
|
while (IS_ANSWER_INVALID_NODE(current_node))
|
2011-11-11 18:05:24 +00:00
|
|
|
current_node = TrNode_child(current_node);
|
|
|
|
SgFr_first_answer(sg_fr) = current_node;
|
2011-11-09 11:00:31 +00:00
|
|
|
/* chain next valid answers */
|
2011-11-11 18:05:24 +00:00
|
|
|
next_node = TrNode_child(current_node);
|
|
|
|
while (next_node) {
|
2011-12-22 16:50:20 +00:00
|
|
|
if (! IS_ANSWER_INVALID_NODE(next_node)) {
|
2011-11-11 18:05:24 +00:00
|
|
|
TrNode_child(current_node) = next_node;
|
|
|
|
current_node = next_node;
|
2011-11-09 11:00:31 +00:00
|
|
|
}
|
2011-11-11 18:05:24 +00:00
|
|
|
next_node = TrNode_child(next_node);
|
2011-11-09 11:00:31 +00:00
|
|
|
}
|
2011-11-11 18:05:24 +00:00
|
|
|
SgFr_last_answer(sg_fr) = current_node;
|
2012-06-04 19:27:31 +01:00
|
|
|
#if !defined(THREADS_FULL_SHARING) && !defined(THREADS_CONSUMER_SHARING)
|
2011-11-11 18:05:24 +00:00
|
|
|
/* free invalid answer nodes */
|
|
|
|
current_node = SgFr_invalid_chain(sg_fr);
|
2011-11-09 11:00:31 +00:00
|
|
|
SgFr_invalid_chain(sg_fr) = NULL;
|
2011-11-11 18:05:24 +00:00
|
|
|
while (current_node) {
|
|
|
|
next_node = TrNode_next(current_node);
|
|
|
|
FREE_ANSWER_TRIE_NODE(current_node);
|
|
|
|
current_node = next_node;
|
2011-10-22 16:49:13 +01:00
|
|
|
}
|
2012-06-04 19:27:31 +01:00
|
|
|
#endif /* !THREADS_FULL_SHARING && !THREADS_CONSUMER_SHARING */
|
2011-10-22 16:49:13 +01:00
|
|
|
}
|
2011-11-09 11:00:31 +00:00
|
|
|
#endif /* MODE_DIRECTED_TABLING */
|
2001-04-09 20:54:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
#define unbind_variables(u, e) __unbind_variables((u), (e) PASS_REGS)
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
static inline void __unbind_variables(tr_fr_ptr unbind_tr, tr_fr_ptr end_tr USES_REGS) {
|
2010-04-18 04:59:11 +01:00
|
|
|
TABLING_ERROR_CHECKING(unbind_variables, unbind_tr < end_tr);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* unbind loop */
|
|
|
|
while (unbind_tr != end_tr) {
|
|
|
|
CELL ref = (CELL) TrailTerm(--unbind_tr);
|
|
|
|
/* check for global or local variables */
|
|
|
|
if (IsVarTerm(ref)) {
|
|
|
|
/* unbind variable */
|
|
|
|
RESET_VARIABLE(ref);
|
|
|
|
} else if (IsPairTerm(ref)) {
|
|
|
|
ref = (CELL) RepPair(ref);
|
2011-05-23 16:19:47 +01:00
|
|
|
if (IN_BETWEEN(LOCAL_TrailBase, ref, LOCAL_TrailTop)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
/* avoid frozen segments */
|
|
|
|
unbind_tr = (tr_fr_ptr) ref;
|
2011-05-23 16:19:47 +01:00
|
|
|
TABLING_ERROR_CHECKING(unbind_variables, unbind_tr > (tr_fr_ptr) LOCAL_TrailTop);
|
2010-04-18 04:59:11 +01:00
|
|
|
TABLING_ERROR_CHECKING(unbind_variables, unbind_tr < end_tr);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#ifdef MULTI_ASSIGNMENT_VARIABLES
|
|
|
|
} else {
|
|
|
|
CELL *aux_ptr = RepAppl(ref);
|
2009-05-01 17:53:59 +01:00
|
|
|
--unbind_tr;
|
|
|
|
Term aux_val = TrailVal(unbind_tr);
|
2001-04-09 20:54:03 +01:00
|
|
|
*aux_ptr = aux_val;
|
2003-11-05 16:12:25 +00:00
|
|
|
#endif /* MULTI_ASSIGNMENT_VARIABLES */
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
#define rebind_variables(u, e) __rebind_variables(u, e PASS_REGS)
|
|
|
|
|
|
|
|
static inline void __rebind_variables(tr_fr_ptr rebind_tr, tr_fr_ptr end_tr USES_REGS) {
|
2010-04-18 04:59:11 +01:00
|
|
|
TABLING_ERROR_CHECKING(rebind_variables, rebind_tr < end_tr);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* rebind loop */
|
2014-01-19 21:15:05 +00:00
|
|
|
Yap_NEW_MAHASH((ma_h_inner_struct *)HR PASS_REGS);
|
2001-04-09 20:54:03 +01:00
|
|
|
while (rebind_tr != end_tr) {
|
|
|
|
CELL ref = (CELL) TrailTerm(--rebind_tr);
|
|
|
|
/* check for global or local variables */
|
|
|
|
if (IsVarTerm(ref)) {
|
|
|
|
/* rebind variable */
|
|
|
|
*((CELL *)ref) = TrailVal(rebind_tr);
|
|
|
|
} else if (IsPairTerm(ref)) {
|
|
|
|
ref = (CELL) RepPair(ref);
|
2011-05-23 16:19:47 +01:00
|
|
|
if (IN_BETWEEN(LOCAL_TrailBase, ref, LOCAL_TrailTop)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
/* avoid frozen segments */
|
|
|
|
rebind_tr = (tr_fr_ptr) ref;
|
2011-05-23 16:19:47 +01:00
|
|
|
TABLING_ERROR_CHECKING(rebind_variables, rebind_tr > (tr_fr_ptr) LOCAL_TrailTop);
|
2010-04-18 04:59:11 +01:00
|
|
|
TABLING_ERROR_CHECKING(rebind_variables, rebind_tr < end_tr);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#ifdef MULTI_ASSIGNMENT_VARIABLES
|
|
|
|
} else {
|
|
|
|
CELL *cell_ptr = RepAppl(ref);
|
2011-05-27 17:21:03 +01:00
|
|
|
if (!Yap_lookup_ma_var(cell_ptr PASS_REGS)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
/* first time we found the variable, let's put the new value */
|
|
|
|
*cell_ptr = TrailVal(rebind_tr);
|
|
|
|
}
|
2009-05-01 17:53:59 +01:00
|
|
|
--rebind_tr;
|
2003-11-05 16:12:25 +00:00
|
|
|
#endif /* MULTI_ASSIGNMENT_VARIABLES */
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
2003-11-05 16:12:25 +00:00
|
|
|
return;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
static inline void __restore_bindings(tr_fr_ptr unbind_tr, tr_fr_ptr rebind_tr USES_REGS) {
|
2001-04-09 20:54:03 +01:00
|
|
|
CELL ref;
|
|
|
|
tr_fr_ptr end_tr;
|
|
|
|
|
2010-04-18 04:59:11 +01:00
|
|
|
TABLING_ERROR_CHECKING(restore_variables, unbind_tr < rebind_tr);
|
2001-04-09 20:54:03 +01:00
|
|
|
end_tr = rebind_tr;
|
2014-01-19 21:15:05 +00:00
|
|
|
Yap_NEW_MAHASH((ma_h_inner_struct *)HR PASS_REGS);
|
2001-04-09 20:54:03 +01:00
|
|
|
while (unbind_tr != end_tr) {
|
|
|
|
/* unbind loop */
|
|
|
|
while (unbind_tr > end_tr) {
|
|
|
|
ref = (CELL) TrailTerm(--unbind_tr);
|
|
|
|
if (IsVarTerm(ref)) {
|
|
|
|
RESET_VARIABLE(ref);
|
|
|
|
} else if (IsPairTerm(ref)) {
|
|
|
|
ref = (CELL) RepPair(ref);
|
2011-05-23 16:19:47 +01:00
|
|
|
if (IN_BETWEEN(LOCAL_TrailBase, ref, LOCAL_TrailTop)) {
|
2010-03-12 20:02:32 +00:00
|
|
|
/* avoid frozen segments */
|
2001-04-09 20:54:03 +01:00
|
|
|
unbind_tr = (tr_fr_ptr) ref;
|
2011-05-23 16:19:47 +01:00
|
|
|
TABLING_ERROR_CHECKING(restore_variables, unbind_tr > (tr_fr_ptr) LOCAL_TrailTop);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2009-05-01 17:53:59 +01:00
|
|
|
#ifdef MULTI_ASSIGNMENT_VARIABLES
|
|
|
|
} else if (IsApplTerm(ref)) {
|
|
|
|
CELL *pt = RepAppl(ref);
|
|
|
|
|
|
|
|
/* AbsAppl means */
|
|
|
|
/* multi-assignment variable */
|
2009-10-19 16:39:40 +01:00
|
|
|
/* so that the upper cell is the old value */
|
2009-05-01 17:53:59 +01:00
|
|
|
--unbind_tr;
|
2011-05-27 17:21:03 +01:00
|
|
|
if (!Yap_lookup_ma_var(pt PASS_REGS)) {
|
2009-10-19 16:39:40 +01:00
|
|
|
pt[0] = TrailVal(unbind_tr);
|
|
|
|
}
|
2010-04-18 04:59:11 +01:00
|
|
|
#endif /* MULTI_ASSIGNMENT_VARIABLES */
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* look for end */
|
|
|
|
while (unbind_tr < end_tr) {
|
|
|
|
ref = (CELL) TrailTerm(--end_tr);
|
|
|
|
if (IsPairTerm(ref)) {
|
|
|
|
ref = (CELL) RepPair(ref);
|
2011-05-23 16:19:47 +01:00
|
|
|
if (IN_BETWEEN(LOCAL_TrailBase, ref, LOCAL_TrailTop)) {
|
2010-03-12 20:02:32 +00:00
|
|
|
/* avoid frozen segments */
|
2001-04-09 20:54:03 +01:00
|
|
|
end_tr = (tr_fr_ptr) ref;
|
2011-05-23 16:19:47 +01:00
|
|
|
TABLING_ERROR_CHECKING(restore_variables, end_tr > (tr_fr_ptr) LOCAL_TrailTop);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* rebind loop */
|
|
|
|
while (rebind_tr != end_tr) {
|
|
|
|
ref = (CELL) TrailTerm(--rebind_tr);
|
|
|
|
if (IsVarTerm(ref)) {
|
|
|
|
*((CELL *)ref) = TrailVal(rebind_tr);
|
|
|
|
} else if (IsPairTerm(ref)) {
|
|
|
|
ref = (CELL) RepPair(ref);
|
2011-05-23 16:19:47 +01:00
|
|
|
if (IN_BETWEEN(LOCAL_TrailBase, ref, LOCAL_TrailTop)) {
|
2010-03-12 20:02:32 +00:00
|
|
|
/* avoid frozen segments */
|
2001-04-09 20:54:03 +01:00
|
|
|
rebind_tr = (tr_fr_ptr) ref;
|
2011-05-23 16:19:47 +01:00
|
|
|
TABLING_ERROR_CHECKING(restore_variables, rebind_tr > (tr_fr_ptr) LOCAL_TrailTop);
|
2010-04-18 04:59:11 +01:00
|
|
|
TABLING_ERROR_CHECKING(restore_variables, rebind_tr < end_tr);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2009-05-01 17:53:59 +01:00
|
|
|
#ifdef MULTI_ASSIGNMENT_VARIABLES
|
|
|
|
} else {
|
|
|
|
CELL *cell_ptr = RepAppl(ref);
|
2009-10-19 16:39:40 +01:00
|
|
|
/* first time we found the variable, let's put the new value */
|
|
|
|
*cell_ptr = TrailVal(rebind_tr);
|
2009-05-01 17:53:59 +01:00
|
|
|
--rebind_tr;
|
|
|
|
#endif /* MULTI_ASSIGNMENT_VARIABLES */
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
#define expand_auxiliary_stack(s) __expand_auxiliary_stack((s) PASS_REGS)
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
static inline CELL *__expand_auxiliary_stack(CELL *stack USES_REGS) {
|
2015-01-20 03:00:42 +00:00
|
|
|
char *old_top = (char *)LOCAL_TrailTop;
|
2010-04-19 02:34:08 +01:00
|
|
|
INFORMATION_MESSAGE("Expanding trail in 64 Kbytes");
|
2010-05-11 12:25:49 +01:00
|
|
|
if (! Yap_growtrail(K64, TRUE)) { /* TRUE means 'contiguous_only' */
|
2010-04-19 02:34:08 +01:00
|
|
|
Yap_Error(OUT_OF_TRAIL_ERROR, TermNil, "stack full (STACK_CHECK_EXPAND)");
|
|
|
|
return NULL;
|
|
|
|
} else {
|
2015-01-20 03:00:42 +00:00
|
|
|
UInt diff = (char *)LOCAL_TrailTop - old_top;
|
|
|
|
CELL *new_stack = (CELL *)((char *)stack + diff);
|
|
|
|
memmove((void *)new_stack, stack, old_top - (char *)stack);
|
2010-04-19 02:34:08 +01:00
|
|
|
return new_stack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
#define abolish_incomplete_subgoals(p) __abolish_incomplete_subgoals((p) PASS_REGS)
|
2010-04-19 02:34:08 +01:00
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
|
|
|
|
static inline void __abolish_incomplete_subgoals(choiceptr prune_cp USES_REGS) {
|
2011-05-09 19:36:51 +01:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef YAPOR
|
2010-01-14 15:58:19 +00:00
|
|
|
if (EQUAL_OR_YOUNGER_CP(GetOrFr_node(LOCAL_top_susp_or_fr), prune_cp))
|
2001-04-09 20:54:03 +01:00
|
|
|
pruning_over_tabling_data_structures();
|
|
|
|
#endif /* YAPOR */
|
|
|
|
|
2005-04-07 18:56:58 +01:00
|
|
|
if (EQUAL_OR_YOUNGER_CP(DepFr_cons_cp(LOCAL_top_dep_fr), prune_cp)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef YAPOR
|
2011-06-21 15:19:07 +01:00
|
|
|
if (GLOBAL_parallel_mode == PARALLEL_MODE_RUNNING)
|
2001-04-09 20:54:03 +01:00
|
|
|
pruning_over_tabling_data_structures();
|
|
|
|
#endif /* YAPOR */
|
|
|
|
do {
|
|
|
|
dep_fr_ptr dep_fr = LOCAL_top_dep_fr;
|
|
|
|
LOCAL_top_dep_fr = DepFr_next(dep_fr);
|
|
|
|
FREE_DEPENDENCY_FRAME(dep_fr);
|
2005-04-07 18:56:58 +01:00
|
|
|
} while (EQUAL_OR_YOUNGER_CP(DepFr_cons_cp(LOCAL_top_dep_fr), prune_cp));
|
2001-04-09 20:54:03 +01:00
|
|
|
adjust_freeze_registers();
|
|
|
|
}
|
|
|
|
|
2005-04-07 18:56:58 +01:00
|
|
|
while (LOCAL_top_sg_fr && EQUAL_OR_YOUNGER_CP(SgFr_gen_cp(LOCAL_top_sg_fr), prune_cp)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
sg_fr_ptr sg_fr;
|
|
|
|
#ifdef YAPOR
|
2011-06-21 15:19:07 +01:00
|
|
|
if (GLOBAL_parallel_mode == PARALLEL_MODE_RUNNING)
|
2001-04-09 20:54:03 +01:00
|
|
|
pruning_over_tabling_data_structures();
|
|
|
|
#endif /* YAPOR */
|
|
|
|
sg_fr = LOCAL_top_sg_fr;
|
|
|
|
LOCAL_top_sg_fr = SgFr_next(sg_fr);
|
2011-12-05 16:54:22 +00:00
|
|
|
LOCK_SG_FR(sg_fr);
|
2005-08-04 16:45:56 +01:00
|
|
|
if (SgFr_first_answer(sg_fr) == NULL) {
|
|
|
|
/* no answers --> ready */
|
|
|
|
SgFr_state(sg_fr) = ready;
|
2011-12-05 16:54:22 +00:00
|
|
|
UNLOCK_SG_FR(sg_fr);
|
2008-05-23 19:28:58 +01:00
|
|
|
} else if (SgFr_first_answer(sg_fr) == SgFr_answer_trie(sg_fr)) {
|
|
|
|
/* yes answer --> complete */
|
2009-08-06 02:27:59 +01:00
|
|
|
#ifndef TABLING_EARLY_COMPLETION
|
|
|
|
/* with early completion, at this point the subgoal should be already completed */
|
|
|
|
SgFr_state(sg_fr) = complete;
|
|
|
|
#endif /* TABLING_EARLY_COMPLETION */
|
2011-12-05 16:54:22 +00:00
|
|
|
UNLOCK_SG_FR(sg_fr);
|
2005-07-26 17:28:28 +01:00
|
|
|
} else {
|
2008-05-23 19:28:58 +01:00
|
|
|
/* answers --> incomplete/ready */
|
2005-08-01 16:40:39 +01:00
|
|
|
#ifdef INCOMPLETE_TABLING
|
2008-05-23 19:28:58 +01:00
|
|
|
SgFr_state(sg_fr) = incomplete;
|
2011-12-05 16:54:22 +00:00
|
|
|
UNLOCK_SG_FR(sg_fr);
|
2011-11-11 18:05:24 +00:00
|
|
|
#ifdef MODE_DIRECTED_TABLING
|
|
|
|
if (SgFr_invalid_chain(sg_fr)) {
|
|
|
|
ans_node_ptr current_node, next_node;
|
|
|
|
/* find first valid answer */
|
|
|
|
current_node = SgFr_first_answer(sg_fr);
|
2011-12-22 16:50:20 +00:00
|
|
|
while (IS_ANSWER_INVALID_NODE(current_node))
|
2011-11-11 18:05:24 +00:00
|
|
|
current_node = TrNode_child(current_node);
|
|
|
|
SgFr_first_answer(sg_fr) = current_node;
|
|
|
|
/* chain next valid answers */
|
|
|
|
next_node = TrNode_child(current_node);
|
|
|
|
while (next_node) {
|
2011-12-22 16:50:20 +00:00
|
|
|
if (! IS_ANSWER_INVALID_NODE(next_node)) {
|
2011-11-11 18:05:24 +00:00
|
|
|
TrNode_child(current_node) = next_node;
|
|
|
|
current_node = next_node;
|
|
|
|
}
|
|
|
|
next_node = TrNode_child(next_node);
|
|
|
|
}
|
|
|
|
SgFr_last_answer(sg_fr) = current_node;
|
2012-06-04 19:27:31 +01:00
|
|
|
#if !defined(THREADS_FULL_SHARING) && !defined(THREADS_CONSUMER_SHARING)
|
2011-11-11 18:05:24 +00:00
|
|
|
/* free invalid answer nodes */
|
|
|
|
current_node = SgFr_invalid_chain(sg_fr);
|
|
|
|
SgFr_invalid_chain(sg_fr) = NULL;
|
|
|
|
while (current_node) {
|
2012-06-04 19:27:31 +01:00
|
|
|
next_node = TrNode_next(current_node);
|
2011-11-11 18:05:24 +00:00
|
|
|
FREE_ANSWER_TRIE_NODE(current_node);
|
2012-06-04 19:27:31 +01:00
|
|
|
current_node = next_node;
|
2011-11-11 18:05:24 +00:00
|
|
|
}
|
2012-06-04 19:27:31 +01:00
|
|
|
#endif /* !THREADS_FULL_SHARING && !THREADS_CONSUMER_SHARING */
|
2011-11-11 18:05:24 +00:00
|
|
|
}
|
|
|
|
#endif /* MODE_DIRECTED_TABLING */
|
2005-08-01 16:40:39 +01:00
|
|
|
#else
|
2008-05-23 19:28:58 +01:00
|
|
|
ans_node_ptr node;
|
2012-06-04 19:27:31 +01:00
|
|
|
#if defined(MODE_DIRECTED_TABLING) && !defined(THREADS_FULL_SHARING) && !defined(THREADS_CONSUMER_SHARING)
|
|
|
|
if (SgFr_invalid_chain(sg_fr)) {
|
|
|
|
ans_node_ptr current_node, next_node;
|
|
|
|
/* free invalid answer nodes */
|
|
|
|
current_node = SgFr_invalid_chain(sg_fr);
|
|
|
|
SgFr_invalid_chain(sg_fr) = NULL;
|
|
|
|
while (current_node) {
|
|
|
|
next_node = TrNode_next(current_node);
|
|
|
|
FREE_ANSWER_TRIE_NODE(current_node);
|
|
|
|
current_node = next_node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* MODE_DIRECTED_TABLING && !THREADS_FULL_SHARING && !THREADS_CONSUMER_SHARING */
|
2008-05-23 19:28:58 +01:00
|
|
|
SgFr_state(sg_fr) = ready;
|
2011-12-22 16:50:20 +00:00
|
|
|
#if defined(THREADS_FULL_SHARING) || defined(THREADS_CONSUMER_SHARING)
|
|
|
|
if (SgFr_active_workers(sg_fr) == 0) {
|
|
|
|
SgFr_sg_ent_state(sg_fr) = ready;
|
|
|
|
#endif /* THREADS_FULL_SHARING || THREADS_CONSUMER_SHARING */
|
|
|
|
free_answer_hash_chain(SgFr_hash_chain(sg_fr));
|
|
|
|
SgFr_hash_chain(sg_fr) = NULL;
|
|
|
|
SgFr_first_answer(sg_fr) = NULL;
|
|
|
|
SgFr_last_answer(sg_fr) = NULL;
|
|
|
|
node = TrNode_child(SgFr_answer_trie(sg_fr));
|
|
|
|
TrNode_child(SgFr_answer_trie(sg_fr)) = NULL;
|
|
|
|
UNLOCK_SG_FR(sg_fr);
|
|
|
|
free_answer_trie(node, TRAVERSE_MODE_NORMAL, TRAVERSE_POSITION_FIRST);
|
|
|
|
#ifdef THREADS_FULL_SHARING
|
|
|
|
if (IsMode_Batched(TabEnt_mode(SgFr_tab_ent(sg_fr)))) {
|
|
|
|
SgFr_batched_last_answer(sg_fr) = NULL;
|
|
|
|
struct answer_ref_node *local_uncons_ans = SgFr_batched_cached_answers(sg_fr) ;
|
|
|
|
while ( local_uncons_ans ) {
|
|
|
|
SgFr_batched_cached_answers(sg_fr) = RefNode_next(SgFr_batched_cached_answers(sg_fr));
|
|
|
|
FREE_ANSWER_REF_NODE(local_uncons_ans);
|
|
|
|
local_uncons_ans = SgFr_batched_cached_answers(sg_fr);
|
|
|
|
}
|
|
|
|
}
|
2012-06-04 19:27:31 +01:00
|
|
|
} else { /* SgFr_active_workers(sg_fr) != 0 */
|
2011-12-22 16:50:20 +00:00
|
|
|
if (IsMode_Batched(TabEnt_mode(SgFr_tab_ent(sg_fr)))){
|
|
|
|
SgFr_batched_last_answer(sg_fr) = NULL;
|
|
|
|
if ( worker_id >= ANSWER_LEAF_NODE_MAX_THREADS ) {
|
|
|
|
struct answer_ref_node *local_uncons_ans = SgFr_batched_cached_answers(sg_fr) ;
|
|
|
|
while ( local_uncons_ans ) {
|
|
|
|
SgFr_batched_cached_answers(sg_fr) = RefNode_next(SgFr_batched_cached_answers(sg_fr));
|
|
|
|
FREE_ANSWER_REF_NODE(local_uncons_ans);
|
|
|
|
local_uncons_ans = SgFr_batched_cached_answers(sg_fr);
|
|
|
|
}
|
|
|
|
} else { /* worker_id < ANSWER_LEAF_NODE_MAX_THREADS */
|
|
|
|
ans_node_ptr leaf_ans_trie_node = SgFr_first_answer(sg_fr);
|
|
|
|
while( leaf_ans_trie_node ){
|
|
|
|
ANSWER_LEAF_NODE_DEL_WID(leaf_ans_trie_node,worker_id);
|
|
|
|
leaf_ans_trie_node = TrNode_child(leaf_ans_trie_node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* THREADS_FULL_SHARING */
|
|
|
|
#if defined(THREADS_FULL_SHARING) || defined(THREADS_CONSUMER_SHARING)
|
|
|
|
}
|
|
|
|
#endif /* THREADS_FULL_SHARING || THREADS_CONSUMER_SHARING */
|
2005-08-01 16:40:39 +01:00
|
|
|
#endif /* INCOMPLETE_TABLING */
|
2005-07-26 17:28:28 +01:00
|
|
|
}
|
2005-08-04 16:45:56 +01:00
|
|
|
#ifdef LIMIT_TABLING
|
|
|
|
insert_into_global_sg_fr_list(sg_fr);
|
|
|
|
#endif /* LIMIT_TABLING */
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef YAPOR
|
2010-04-19 02:34:08 +01:00
|
|
|
static inline void pruning_over_tabling_data_structures(void) {
|
2005-05-31 01:49:49 +01:00
|
|
|
Yap_Error(INTERNAL_ERROR, TermNil, "pruning over tabling data structures");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-26 21:11:19 +00:00
|
|
|
#define collect_suspension_frames(o) __collect_suspension_frames((o) PASS_REGS)
|
|
|
|
|
|
|
|
static inline void __collect_suspension_frames(or_fr_ptr or_fr USES_REGS) {
|
2001-04-09 20:54:03 +01:00
|
|
|
int depth;
|
|
|
|
or_fr_ptr *susp_ptr;
|
|
|
|
|
2010-04-18 04:59:11 +01:00
|
|
|
OPTYAP_ERROR_CHECKING(collect_suspension_frames, IS_UNLOCKED(or_fr));
|
|
|
|
OPTYAP_ERROR_CHECKING(collect_suspension_frames, OrFr_suspensions(or_fr) == NULL);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
/* order collected suspension frames by depth */
|
|
|
|
depth = OrFr_depth(or_fr);
|
|
|
|
susp_ptr = & LOCAL_top_susp_or_fr;
|
|
|
|
while (OrFr_depth(*susp_ptr) > depth)
|
|
|
|
susp_ptr = & OrFr_nearest_suspnode(*susp_ptr);
|
|
|
|
OrFr_nearest_suspnode(or_fr) = *susp_ptr;
|
|
|
|
*susp_ptr = or_fr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline
|
|
|
|
#ifdef TIMESTAMP_CHECK
|
|
|
|
susp_fr_ptr suspension_frame_to_resume(or_fr_ptr susp_or_fr, long timestamp) {
|
|
|
|
#else
|
|
|
|
susp_fr_ptr suspension_frame_to_resume(or_fr_ptr susp_or_fr) {
|
|
|
|
#endif /* TIMESTAMP_CHECK */
|
|
|
|
choiceptr top_cp;
|
|
|
|
susp_fr_ptr *susp_ptr, susp_fr;
|
|
|
|
dep_fr_ptr dep_fr;
|
|
|
|
|
2010-01-14 15:58:19 +00:00
|
|
|
top_cp = GetOrFr_node(susp_or_fr);
|
2001-04-09 20:54:03 +01:00
|
|
|
susp_ptr = & OrFr_suspensions(susp_or_fr);
|
|
|
|
susp_fr = *susp_ptr;
|
|
|
|
while (susp_fr) {
|
|
|
|
dep_fr = SuspFr_top_dep_fr(susp_fr);
|
|
|
|
do {
|
2005-04-07 18:56:58 +01:00
|
|
|
if (TrNode_child(DepFr_last_answer(dep_fr))) {
|
2001-04-09 20:54:03 +01:00
|
|
|
/* unconsumed answers in susp_fr */
|
|
|
|
*susp_ptr = SuspFr_next(susp_fr);
|
|
|
|
return susp_fr;
|
|
|
|
}
|
|
|
|
#ifdef TIMESTAMP_CHECK
|
|
|
|
DepFr_timestamp(dep_fr) = timestamp;
|
|
|
|
#endif /* TIMESTAMP_CHECK */
|
|
|
|
dep_fr = DepFr_next(dep_fr);
|
|
|
|
#ifdef TIMESTAMP_CHECK
|
|
|
|
} while (timestamp > DepFr_timestamp(dep_fr) && YOUNGER_CP(DepFr_cons_cp(dep_fr), top_cp));
|
|
|
|
#else
|
|
|
|
} while (YOUNGER_CP(DepFr_cons_cp(dep_fr), top_cp));
|
|
|
|
#endif /* TIMESTAMP_CHECK */
|
|
|
|
susp_ptr = & SuspFr_next(susp_fr);
|
|
|
|
susp_fr = *susp_ptr;
|
|
|
|
}
|
|
|
|
/* no suspension frame with unconsumed answers */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif /* YAPOR */
|
|
|
|
|
|
|
|
|
2005-07-11 20:17:32 +01:00
|
|
|
#ifdef TABLING_INNER_CUTS
|
2010-04-19 02:34:08 +01:00
|
|
|
static inline void CUT_store_tg_answer(or_fr_ptr or_frame, ans_node_ptr ans_node, choiceptr gen_cp, int ltt) {
|
2001-04-09 20:54:03 +01:00
|
|
|
tg_sol_fr_ptr tg_sol_fr, *solution_ptr, next, ltt_next;
|
|
|
|
tg_ans_fr_ptr tg_ans_fr;
|
|
|
|
|
|
|
|
solution_ptr = & OrFr_tg_solutions(or_frame);
|
|
|
|
while (*solution_ptr && YOUNGER_CP(gen_cp, TgSolFr_gen_cp(*solution_ptr))) {
|
|
|
|
solution_ptr = & TgSolFr_next(*solution_ptr);
|
|
|
|
}
|
|
|
|
if (*solution_ptr && gen_cp == TgSolFr_gen_cp(*solution_ptr)) {
|
|
|
|
if (ltt >= TgSolFr_ltt(*solution_ptr)) {
|
|
|
|
while (*solution_ptr && ltt > TgSolFr_ltt(*solution_ptr)) {
|
|
|
|
solution_ptr = & TgSolFr_ltt_next(*solution_ptr);
|
|
|
|
}
|
|
|
|
if (*solution_ptr && ltt == TgSolFr_ltt(*solution_ptr)) {
|
|
|
|
tg_ans_fr = TgSolFr_first(*solution_ptr);
|
|
|
|
if (TgAnsFr_free_slot(tg_ans_fr) == TG_ANSWER_SLOTS) {
|
|
|
|
ALLOC_TG_ANSWER_FRAME(tg_ans_fr);
|
|
|
|
TgAnsFr_free_slot(tg_ans_fr) = 1;
|
|
|
|
TgAnsFr_answer(tg_ans_fr, 0) = ans_node;
|
|
|
|
TgAnsFr_next(tg_ans_fr) = TgSolFr_first(*solution_ptr);
|
|
|
|
TgSolFr_first(*solution_ptr) = tg_ans_fr;
|
|
|
|
} else {
|
|
|
|
TgAnsFr_answer(tg_ans_fr, TgAnsFr_free_slot(tg_ans_fr)) = ans_node;
|
|
|
|
TgAnsFr_free_slot(tg_ans_fr)++;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ltt_next = *solution_ptr;
|
|
|
|
next = NULL;
|
|
|
|
} else {
|
|
|
|
ltt_next = *solution_ptr;
|
|
|
|
next = TgSolFr_next(*solution_ptr);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ltt_next = NULL;
|
|
|
|
next = *solution_ptr;
|
|
|
|
}
|
|
|
|
ALLOC_TG_ANSWER_FRAME(tg_ans_fr);
|
|
|
|
TgAnsFr_free_slot(tg_ans_fr) = 1;
|
|
|
|
TgAnsFr_answer(tg_ans_fr, 0) = ans_node;
|
|
|
|
TgAnsFr_next(tg_ans_fr) = NULL;
|
|
|
|
ALLOC_TG_SOLUTION_FRAME(tg_sol_fr);
|
|
|
|
TgSolFr_gen_cp(tg_sol_fr) = gen_cp;
|
|
|
|
TgSolFr_ltt(tg_sol_fr) = ltt;
|
|
|
|
TgSolFr_first(tg_sol_fr) = tg_ans_fr;
|
|
|
|
TgSolFr_last(tg_sol_fr) = tg_ans_fr;
|
|
|
|
TgSolFr_ltt_next(tg_sol_fr) = ltt_next;
|
|
|
|
TgSolFr_next(tg_sol_fr) = next;
|
|
|
|
*solution_ptr = tg_sol_fr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-19 02:34:08 +01:00
|
|
|
static inline tg_sol_fr_ptr CUT_store_tg_answers(or_fr_ptr or_frame, tg_sol_fr_ptr new_solution, int ltt) {
|
2001-04-09 20:54:03 +01:00
|
|
|
tg_sol_fr_ptr *old_solution_ptr, next_new_solution;
|
|
|
|
choiceptr node, gen_cp;
|
|
|
|
|
|
|
|
old_solution_ptr = & OrFr_tg_solutions(or_frame);
|
2010-01-14 15:58:19 +00:00
|
|
|
node = GetOrFr_node(or_frame);
|
2001-04-09 20:54:03 +01:00
|
|
|
while (new_solution && YOUNGER_CP(node, TgSolFr_gen_cp(new_solution))) {
|
|
|
|
next_new_solution = TgSolFr_next(new_solution);
|
|
|
|
gen_cp = TgSolFr_gen_cp(new_solution);
|
|
|
|
while (*old_solution_ptr && YOUNGER_CP(gen_cp, TgSolFr_gen_cp(*old_solution_ptr))) {
|
|
|
|
old_solution_ptr = & TgSolFr_next(*old_solution_ptr);
|
|
|
|
}
|
|
|
|
if (*old_solution_ptr && gen_cp == TgSolFr_gen_cp(*old_solution_ptr)) {
|
|
|
|
if (ltt >= TgSolFr_ltt(*old_solution_ptr)) {
|
|
|
|
tg_sol_fr_ptr *ltt_next_old_solution_ptr;
|
|
|
|
ltt_next_old_solution_ptr = old_solution_ptr;
|
|
|
|
while (*ltt_next_old_solution_ptr && ltt > TgSolFr_ltt(*ltt_next_old_solution_ptr)) {
|
|
|
|
ltt_next_old_solution_ptr = & TgSolFr_ltt_next(*ltt_next_old_solution_ptr);
|
|
|
|
}
|
|
|
|
if (*ltt_next_old_solution_ptr && ltt == TgSolFr_ltt(*ltt_next_old_solution_ptr)) {
|
|
|
|
TgAnsFr_next(TgSolFr_last(*ltt_next_old_solution_ptr)) = TgSolFr_first(new_solution);
|
|
|
|
TgSolFr_last(*ltt_next_old_solution_ptr) = TgSolFr_last(new_solution);
|
|
|
|
FREE_TG_SOLUTION_FRAME(new_solution);
|
|
|
|
} else {
|
|
|
|
TgSolFr_ltt(new_solution) = ltt;
|
|
|
|
TgSolFr_ltt_next(new_solution) = *ltt_next_old_solution_ptr;
|
|
|
|
TgSolFr_next(new_solution) = NULL;
|
|
|
|
*ltt_next_old_solution_ptr = new_solution;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
TgSolFr_ltt(new_solution) = ltt;
|
|
|
|
TgSolFr_ltt_next(new_solution) = *old_solution_ptr;
|
|
|
|
TgSolFr_next(new_solution) = TgSolFr_next(*old_solution_ptr);
|
|
|
|
*old_solution_ptr = new_solution;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
TgSolFr_ltt(new_solution) = ltt;
|
|
|
|
TgSolFr_ltt_next(new_solution) = NULL;
|
|
|
|
TgSolFr_next(new_solution) = *old_solution_ptr;
|
|
|
|
*old_solution_ptr = new_solution;
|
|
|
|
}
|
|
|
|
old_solution_ptr = & TgSolFr_next(*old_solution_ptr);
|
|
|
|
new_solution = next_new_solution;
|
|
|
|
}
|
|
|
|
return new_solution;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-19 02:34:08 +01:00
|
|
|
static inline void CUT_validate_tg_answers(tg_sol_fr_ptr valid_solutions) {
|
2001-04-09 20:54:03 +01:00
|
|
|
tg_ans_fr_ptr valid_answers, free_answer;
|
|
|
|
tg_sol_fr_ptr ltt_valid_solutions, free_solution;
|
|
|
|
ans_node_ptr first_answer, last_answer, ans_node;
|
|
|
|
sg_fr_ptr sg_fr;
|
|
|
|
int slots;
|
|
|
|
|
|
|
|
while (valid_solutions) {
|
|
|
|
first_answer = last_answer = NULL;
|
2009-08-07 17:29:53 +01:00
|
|
|
#ifdef DETERMINISTIC_TABLING
|
|
|
|
if (IS_DET_GEN_CP(TgSolFr_gen_cp(valid_solutions)))
|
|
|
|
sg_fr = DET_GEN_CP(TgSolFr_gen_cp(valid_solutions))->cp_sg_fr;
|
|
|
|
else
|
|
|
|
#endif /* DETERMINISTIC_TABLING */
|
|
|
|
sg_fr = GEN_CP(TgSolFr_gen_cp(valid_solutions))->cp_sg_fr;
|
2001-04-09 20:54:03 +01:00
|
|
|
ltt_valid_solutions = valid_solutions;
|
|
|
|
valid_solutions = TgSolFr_next(valid_solutions);
|
|
|
|
do {
|
|
|
|
valid_answers = TgSolFr_first(ltt_valid_solutions);
|
|
|
|
do {
|
|
|
|
slots = TgAnsFr_free_slot(valid_answers);
|
|
|
|
do {
|
|
|
|
ans_node = TgAnsFr_answer(valid_answers, --slots);
|
2011-11-15 16:57:51 +00:00
|
|
|
LOCK_ANSWER_TRIE(sg_fr);
|
|
|
|
LOCK_ANSWER_NODE(ans_node);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (! IS_ANSWER_LEAF_NODE(ans_node)) {
|
|
|
|
TAG_AS_ANSWER_LEAF_NODE(ans_node);
|
2005-04-08 20:26:28 +01:00
|
|
|
if (first_answer == NULL)
|
|
|
|
first_answer = ans_node;
|
|
|
|
else
|
2001-04-09 20:54:03 +01:00
|
|
|
TrNode_child(last_answer) = ans_node;
|
2005-04-08 20:26:28 +01:00
|
|
|
last_answer = ans_node;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2011-11-15 16:57:51 +00:00
|
|
|
UNLOCK_ANSWER_NODE(ans_node);
|
|
|
|
UNLOCK_ANSWER_TRIE(sg_fr);
|
2001-04-09 20:54:03 +01:00
|
|
|
} while (slots);
|
|
|
|
free_answer = valid_answers;
|
|
|
|
valid_answers = TgAnsFr_next(valid_answers);
|
|
|
|
FREE_TG_ANSWER_FRAME(free_answer);
|
|
|
|
} while (valid_answers);
|
|
|
|
free_solution = ltt_valid_solutions;
|
|
|
|
ltt_valid_solutions = TgSolFr_ltt_next(ltt_valid_solutions);
|
|
|
|
FREE_TG_SOLUTION_FRAME(free_solution);
|
|
|
|
} while (ltt_valid_solutions);
|
|
|
|
if (first_answer) {
|
2011-12-05 16:54:22 +00:00
|
|
|
LOCK_SG_FR(sg_fr);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (SgFr_first_answer(sg_fr) == NULL) {
|
|
|
|
SgFr_first_answer(sg_fr) = first_answer;
|
|
|
|
} else {
|
|
|
|
TrNode_child(SgFr_last_answer(sg_fr)) = first_answer;
|
|
|
|
}
|
|
|
|
SgFr_last_answer(sg_fr) = last_answer;
|
2011-12-05 16:54:22 +00:00
|
|
|
UNLOCK_SG_FR(sg_fr);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-19 02:34:08 +01:00
|
|
|
static inline void CUT_join_tg_solutions(tg_sol_fr_ptr *old_solution_ptr, tg_sol_fr_ptr new_solution) {
|
2001-04-09 20:54:03 +01:00
|
|
|
tg_sol_fr_ptr next_old_solution, next_new_solution;
|
|
|
|
choiceptr gen_cp;
|
|
|
|
|
|
|
|
do {
|
|
|
|
gen_cp = TgSolFr_gen_cp(new_solution);
|
|
|
|
while (*old_solution_ptr && YOUNGER_CP(gen_cp, TgSolFr_gen_cp(*old_solution_ptr))) {
|
|
|
|
old_solution_ptr = & TgSolFr_next(*old_solution_ptr);
|
|
|
|
}
|
|
|
|
if (*old_solution_ptr) {
|
|
|
|
next_old_solution = *old_solution_ptr;
|
|
|
|
*old_solution_ptr = new_solution;
|
|
|
|
CUT_join_solution_frame_tg_answers(new_solution);
|
|
|
|
if (gen_cp == TgSolFr_gen_cp(next_old_solution)) {
|
|
|
|
tg_sol_fr_ptr free_solution;
|
|
|
|
TgAnsFr_next(TgSolFr_last(new_solution)) = TgSolFr_first(next_old_solution);
|
|
|
|
TgSolFr_last(new_solution) = TgSolFr_last(next_old_solution);
|
|
|
|
free_solution = next_old_solution;
|
|
|
|
next_old_solution = TgSolFr_next(next_old_solution);
|
|
|
|
FREE_TG_SOLUTION_FRAME(free_solution);
|
|
|
|
if (! next_old_solution) {
|
|
|
|
if ((next_new_solution = TgSolFr_next(new_solution))) {
|
|
|
|
CUT_join_solution_frames_tg_answers(next_new_solution);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gen_cp = TgSolFr_gen_cp(next_old_solution);
|
|
|
|
next_new_solution = TgSolFr_next(new_solution);
|
|
|
|
while (next_new_solution && YOUNGER_CP(gen_cp, TgSolFr_gen_cp(next_new_solution))) {
|
|
|
|
new_solution = next_new_solution;
|
|
|
|
next_new_solution = TgSolFr_next(new_solution);
|
|
|
|
CUT_join_solution_frame_tg_answers(new_solution);
|
|
|
|
}
|
|
|
|
old_solution_ptr = & TgSolFr_next(new_solution);
|
|
|
|
TgSolFr_next(new_solution) = next_old_solution;
|
|
|
|
new_solution = next_new_solution;
|
|
|
|
} else {
|
|
|
|
*old_solution_ptr = new_solution;
|
|
|
|
CUT_join_solution_frames_tg_answers(new_solution);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} while (new_solution);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-19 02:34:08 +01:00
|
|
|
static inline void CUT_join_solution_frame_tg_answers(tg_sol_fr_ptr join_solution) {
|
2001-04-09 20:54:03 +01:00
|
|
|
tg_sol_fr_ptr next_solution;
|
|
|
|
|
|
|
|
while ((next_solution = TgSolFr_ltt_next(join_solution))) {
|
|
|
|
TgAnsFr_next(TgSolFr_last(join_solution)) = TgSolFr_first(next_solution);
|
|
|
|
TgSolFr_last(join_solution) = TgSolFr_last(next_solution);
|
|
|
|
TgSolFr_ltt_next(join_solution) = TgSolFr_ltt_next(next_solution);
|
|
|
|
FREE_TG_SOLUTION_FRAME(next_solution);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-19 02:34:08 +01:00
|
|
|
static inline void CUT_join_solution_frames_tg_answers(tg_sol_fr_ptr join_solution) {
|
2001-04-09 20:54:03 +01:00
|
|
|
do {
|
|
|
|
CUT_join_solution_frame_tg_answers(join_solution);
|
|
|
|
join_solution = TgSolFr_next(join_solution);
|
|
|
|
} while (join_solution);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-19 02:34:08 +01:00
|
|
|
static inline void CUT_free_tg_solution_frame(tg_sol_fr_ptr solution) {
|
2001-04-09 20:54:03 +01:00
|
|
|
tg_ans_fr_ptr current_answer, next_answer;
|
|
|
|
|
|
|
|
current_answer = TgSolFr_first(solution);
|
|
|
|
do {
|
|
|
|
next_answer = TgAnsFr_next(current_answer);
|
|
|
|
FREE_TG_ANSWER_FRAME(current_answer);
|
|
|
|
current_answer = next_answer;
|
|
|
|
} while (current_answer);
|
|
|
|
FREE_TG_SOLUTION_FRAME(solution);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-19 02:34:08 +01:00
|
|
|
static inline void CUT_free_tg_solution_frames(tg_sol_fr_ptr current_solution) {
|
2001-04-09 20:54:03 +01:00
|
|
|
tg_sol_fr_ptr ltt_solution, next_solution;
|
|
|
|
|
|
|
|
while (current_solution) {
|
|
|
|
ltt_solution = TgSolFr_ltt_next(current_solution);
|
|
|
|
while (ltt_solution) {
|
|
|
|
next_solution = TgSolFr_ltt_next(ltt_solution);
|
|
|
|
CUT_free_tg_solution_frame(ltt_solution);
|
|
|
|
ltt_solution = next_solution;
|
|
|
|
}
|
|
|
|
next_solution = TgSolFr_next(current_solution);
|
|
|
|
CUT_free_tg_solution_frame(current_solution);
|
|
|
|
current_solution = next_solution;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-19 02:34:08 +01:00
|
|
|
static inline tg_sol_fr_ptr CUT_prune_tg_solution_frames(tg_sol_fr_ptr solutions, int ltt) {
|
2001-04-09 20:54:03 +01:00
|
|
|
tg_sol_fr_ptr ltt_next_solution, return_solution;
|
|
|
|
|
|
|
|
if (! solutions) return NULL;
|
|
|
|
return_solution = CUT_prune_tg_solution_frames(TgSolFr_next(solutions), ltt);
|
|
|
|
while (solutions && ltt > TgSolFr_ltt(solutions)) {
|
|
|
|
ltt_next_solution = TgSolFr_ltt_next(solutions);
|
|
|
|
CUT_free_tg_solution_frame(solutions);
|
|
|
|
solutions = ltt_next_solution;
|
|
|
|
}
|
|
|
|
if (solutions) {
|
|
|
|
TgSolFr_next(solutions) = return_solution;
|
|
|
|
return solutions;
|
|
|
|
} else {
|
|
|
|
return return_solution;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* TABLING_INNER_CUTS */
|