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 **
|
|
|
|
** **
|
|
|
|
************************************************************************/
|
2005-05-31 09:24:24 +01:00
|
|
|
|
2010-04-03 05:58:14 +01:00
|
|
|
/***********************
|
2001-04-09 20:54:03 +01:00
|
|
|
** Includes **
|
2010-04-03 05:58:14 +01:00
|
|
|
***********************/
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
#include "Yap.h"
|
|
|
|
#if defined(YAPOR) || defined(TABLING)
|
2005-06-03 19:28:11 +01:00
|
|
|
#define OPT_MAVAR_STATIC
|
2001-04-09 20:54:03 +01:00
|
|
|
#include "Yatom.h"
|
2009-10-23 14:22:17 +01:00
|
|
|
#include "YapHeap.h"
|
2001-04-09 20:54:03 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#ifdef YAPOR
|
|
|
|
#include "or.macros.h"
|
|
|
|
#endif /* YAPOR */
|
|
|
|
#ifdef TABLING
|
|
|
|
#include "tab.macros.h"
|
|
|
|
#endif /* TABLING */
|
|
|
|
#if defined(TABLING) || !defined(ACOW)
|
|
|
|
#ifndef TABLING
|
|
|
|
#include "opt.mavar.h"
|
2005-06-03 19:28:11 +01:00
|
|
|
#endif /* !TABLING */
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef MULTI_ASSIGNMENT_VARIABLES
|
2005-08-05 15:55:03 +01:00
|
|
|
ma_hash_entry Yap_ma_hash_table[MAVARS_HASH_SIZE];
|
|
|
|
UInt Yap_timestamp; /* an unsigned int */
|
|
|
|
ma_h_inner_struct *Yap_ma_h_top;
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif /* MULTI_ASSIGNMENT_VARIABLES */
|
2005-06-03 19:28:11 +01:00
|
|
|
#endif /* TABLING || !ACOW */
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef ACOW
|
|
|
|
#include "sys/wait.h"
|
|
|
|
#endif /* ACOW */
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-04-19 02:34:08 +01:00
|
|
|
|
|
|
|
/************************************************
|
|
|
|
** Global variables are defined here **
|
|
|
|
************************************************/
|
|
|
|
|
|
|
|
#if defined(YAPOR) && ! defined(THREADS)
|
|
|
|
struct worker WORKER;
|
|
|
|
#endif /* YAPOR && ! THREADS */
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-04-03 05:58:14 +01:00
|
|
|
/*********************
|
|
|
|
** Macros **
|
|
|
|
*********************/
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2009-09-27 02:31:31 +01:00
|
|
|
#ifdef SHM_MEMORY_ALLOC_SCHEME
|
2003-11-05 16:12:25 +00:00
|
|
|
#define STRUCTS_PER_PAGE(STR_TYPE) ((Yap_page_size - STRUCT_SIZE(struct page_header)) / STRUCT_SIZE(STR_TYPE))
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
#define INIT_PAGES(PG, STR_TYPE) \
|
|
|
|
INIT_LOCK(Pg_lock(PG)); \
|
2005-07-11 20:17:32 +01:00
|
|
|
Pg_pg_alloc(PG) = 0; \
|
|
|
|
Pg_str_in_use(PG) = 0; \
|
2001-04-09 20:54:03 +01:00
|
|
|
Pg_str_per_pg(PG) = STRUCTS_PER_PAGE(STR_TYPE); \
|
2005-07-11 20:17:32 +01:00
|
|
|
Pg_free_pg(PG) = NULL
|
2009-09-27 02:31:31 +01:00
|
|
|
#else
|
|
|
|
#define INIT_PAGES(PG, STR_TYPE) Pg_str_in_use(PG) = 0
|
|
|
|
#endif /* SHM_MEMORY_ALLOC_SCHEME */
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2010-04-03 05:58:14 +01:00
|
|
|
/*******************************
|
2001-04-09 20:54:03 +01:00
|
|
|
** Global functions **
|
2010-04-03 05:58:14 +01:00
|
|
|
*******************************/
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2005-11-04 01:17:17 +00:00
|
|
|
void Yap_init_global(int max_table_size, int n_workers, int sch_loop, int delay_load) {
|
2001-04-09 20:54:03 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* global data related to memory management */
|
2005-08-04 16:45:56 +01:00
|
|
|
#ifdef LIMIT_TABLING
|
2005-08-10 22:36:34 +01:00
|
|
|
if (max_table_size)
|
|
|
|
GLOBAL_MAX_PAGES = ((max_table_size - 1) * 1024 * 1024 / SHMMAX + 1) * SHMMAX / Yap_page_size;
|
|
|
|
else
|
|
|
|
GLOBAL_MAX_PAGES = -1;
|
2005-08-04 16:45:56 +01:00
|
|
|
#endif /* LIMIT_TABLING */
|
2001-04-09 20:54:03 +01:00
|
|
|
INIT_PAGES(GLOBAL_PAGES_void, void *);
|
|
|
|
#ifdef YAPOR
|
|
|
|
INIT_PAGES(GLOBAL_PAGES_or_fr, struct or_frame);
|
|
|
|
INIT_PAGES(GLOBAL_PAGES_qg_sol_fr, struct query_goal_solution_frame);
|
|
|
|
INIT_PAGES(GLOBAL_PAGES_qg_ans_fr, struct query_goal_answer_frame);
|
|
|
|
#endif /* YAPOR */
|
|
|
|
#ifdef TABLING_INNER_CUTS
|
|
|
|
INIT_PAGES(GLOBAL_PAGES_tg_sol_fr, struct table_subgoal_solution_frame);
|
|
|
|
INIT_PAGES(GLOBAL_PAGES_tg_ans_fr, struct table_subgoal_answer_frame);
|
|
|
|
#endif /* TABLING_INNER_CUTS */
|
|
|
|
#ifdef TABLING
|
|
|
|
INIT_PAGES(GLOBAL_PAGES_tab_ent, struct table_entry);
|
|
|
|
INIT_PAGES(GLOBAL_PAGES_sg_fr, struct subgoal_frame);
|
2010-04-23 04:22:10 +01:00
|
|
|
INIT_PAGES(GLOBAL_PAGES_dep_fr, struct dependency_frame);
|
2001-04-09 20:54:03 +01:00
|
|
|
INIT_PAGES(GLOBAL_PAGES_sg_node, struct subgoal_trie_node);
|
|
|
|
INIT_PAGES(GLOBAL_PAGES_ans_node, struct answer_trie_node);
|
2010-04-23 04:22:10 +01:00
|
|
|
INIT_PAGES(GLOBAL_PAGES_gt_node, struct global_trie_node);
|
2009-09-27 02:31:31 +01:00
|
|
|
INIT_PAGES(GLOBAL_PAGES_sg_hash, struct subgoal_trie_hash);
|
|
|
|
INIT_PAGES(GLOBAL_PAGES_ans_hash, struct answer_trie_hash);
|
2010-04-23 04:22:10 +01:00
|
|
|
INIT_PAGES(GLOBAL_PAGES_gt_hash, struct global_trie_hash);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif /* TABLING */
|
|
|
|
#if defined(YAPOR) && defined(TABLING)
|
|
|
|
INIT_PAGES(GLOBAL_PAGES_susp_fr, struct suspension_frame);
|
|
|
|
#endif /* YAPOR && TABLING */
|
|
|
|
|
|
|
|
#ifdef YAPOR
|
|
|
|
/* global static data */
|
|
|
|
number_workers = n_workers;
|
|
|
|
worker_pid(0) = getpid();
|
|
|
|
for (i = 1; i < number_workers; i++) worker_pid(i) = 0;
|
|
|
|
SCHEDULER_LOOP = sch_loop;
|
|
|
|
DELAYED_RELEASE_LOAD = delay_load;
|
|
|
|
|
|
|
|
/* global data related to or-performance */
|
|
|
|
GLOBAL_number_goals = 0;
|
|
|
|
GLOBAL_best_times(0) = 0;
|
|
|
|
GLOBAL_performance_mode = PERFORMANCE_OFF;
|
|
|
|
|
|
|
|
/* global data related to or-parallelism */
|
|
|
|
BITMAP_clear(GLOBAL_bm_present_workers);
|
|
|
|
for (i = 0; i < number_workers; i++)
|
|
|
|
BITMAP_insert(GLOBAL_bm_present_workers, i);
|
|
|
|
BITMAP_copy(GLOBAL_bm_idle_workers, GLOBAL_bm_present_workers);
|
|
|
|
BITMAP_clear(GLOBAL_bm_root_cp_workers);
|
|
|
|
BITMAP_clear(GLOBAL_bm_invisible_workers);
|
|
|
|
BITMAP_clear(GLOBAL_bm_requestable_workers);
|
|
|
|
BITMAP_clear(GLOBAL_bm_executing_workers);
|
|
|
|
BITMAP_copy(GLOBAL_bm_finished_workers, GLOBAL_bm_present_workers);
|
|
|
|
INIT_LOCK(GLOBAL_LOCKS_bm_idle_workers);
|
|
|
|
INIT_LOCK(GLOBAL_LOCKS_bm_root_cp_workers);
|
|
|
|
INIT_LOCK(GLOBAL_LOCKS_bm_invisible_workers);
|
|
|
|
INIT_LOCK(GLOBAL_LOCKS_bm_requestable_workers);
|
|
|
|
INIT_LOCK(GLOBAL_LOCKS_bm_executing_workers);
|
|
|
|
INIT_LOCK(GLOBAL_LOCKS_bm_finished_workers);
|
|
|
|
#ifdef TABLING_INNER_CUTS
|
|
|
|
INIT_LOCK(GLOBAL_LOCKS_bm_pruning_workers);
|
|
|
|
#endif /* TABLING_INNER_CUTS */
|
|
|
|
GLOBAL_LOCKS_who_locked_heap = MAX_WORKERS;
|
|
|
|
INIT_LOCK(GLOBAL_LOCKS_heap_access);
|
|
|
|
INIT_LOCK(GLOBAL_LOCKS_alloc_block);
|
|
|
|
if (number_workers == 1)
|
|
|
|
PARALLEL_EXECUTION_MODE = FALSE;
|
|
|
|
else
|
|
|
|
PARALLEL_EXECUTION_MODE = TRUE;
|
|
|
|
#endif /* YAPOR */
|
|
|
|
|
|
|
|
#ifdef TABLING
|
|
|
|
/* global data related to tabling */
|
2009-09-27 02:31:31 +01:00
|
|
|
new_global_trie_node(GLOBAL_root_gt, 0, NULL, NULL, NULL);
|
2010-04-23 04:22:10 +01:00
|
|
|
GLOBAL_root_tab_ent = NULL;
|
2005-08-04 16:45:56 +01:00
|
|
|
#ifdef LIMIT_TABLING
|
|
|
|
GLOBAL_first_sg_fr = NULL;
|
|
|
|
GLOBAL_last_sg_fr = NULL;
|
|
|
|
GLOBAL_check_sg_fr = NULL;
|
|
|
|
#endif /* LIMIT_TABLING */
|
2008-04-11 17:30:28 +01:00
|
|
|
GLOBAL_root_dep_fr = NULL;
|
2005-11-04 01:17:17 +00:00
|
|
|
for (i = 0; i < MAX_TABLE_VARS; i++) {
|
2007-04-26 15:13:21 +01:00
|
|
|
CELL *pt = GLOBAL_table_var_enumerator_addr(i);
|
2005-11-04 01:17:17 +00:00
|
|
|
RESET_VARIABLE(pt);
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef TABLE_LOCK_AT_WRITE_LEVEL
|
|
|
|
for (i = 0; i < TABLE_LOCK_BUCKETS; i++)
|
|
|
|
INIT_LOCK(GLOBAL_table_lock(i));
|
|
|
|
#endif /* TABLE_LOCK_AT_WRITE_LEVEL */
|
|
|
|
#endif /* TABLING */
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-19 08:24:38 +00:00
|
|
|
void Yap_init_local(void) {
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef YAPOR
|
|
|
|
/* local data related to or-parallelism */
|
2003-11-05 16:12:25 +00:00
|
|
|
LOCAL = REMOTE + worker_id;
|
2010-03-12 20:02:32 +00:00
|
|
|
Set_LOCAL_top_cp((choiceptr) Yap_LocalBase);
|
2001-04-09 20:54:03 +01:00
|
|
|
LOCAL_top_or_fr = GLOBAL_root_or_fr;
|
|
|
|
LOCAL_load = 0;
|
|
|
|
LOCAL_share_request = MAX_WORKERS;
|
2010-01-15 12:04:01 +00:00
|
|
|
LOCAL_reply_signal = worker_ready;
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef ENV_COPY
|
|
|
|
INIT_LOCK(LOCAL_lock_signals);
|
|
|
|
#endif /* ENV_COPY */
|
2010-01-27 11:01:28 +00:00
|
|
|
Set_LOCAL_prune_request(NULL);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif /* YAPOR */
|
|
|
|
INIT_LOCK(LOCAL_lock);
|
|
|
|
#ifdef TABLING
|
|
|
|
/* local data related to tabling */
|
|
|
|
LOCAL_next_free_ans_node = NULL;
|
|
|
|
LOCAL_top_sg_fr = NULL;
|
|
|
|
LOCAL_top_dep_fr = GLOBAL_root_dep_fr;
|
|
|
|
#ifdef YAPOR
|
2010-03-12 20:02:32 +00:00
|
|
|
Set_LOCAL_top_cp_on_stack((choiceptr) Yap_LocalBase); /* ??? */
|
2001-04-09 20:54:03 +01:00
|
|
|
LOCAL_top_susp_or_fr = GLOBAL_root_or_fr;
|
|
|
|
#endif /* YAPOR */
|
|
|
|
#endif /* TABLING */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void make_root_frames(void) {
|
|
|
|
#ifdef YAPOR
|
|
|
|
/* root or frame */
|
|
|
|
or_fr_ptr or_fr;
|
|
|
|
|
|
|
|
ALLOC_OR_FRAME(or_fr);
|
|
|
|
INIT_LOCK(OrFr_lock(or_fr));
|
|
|
|
OrFr_alternative(or_fr) = NULL;
|
|
|
|
BITMAP_copy(OrFr_members(or_fr), GLOBAL_bm_present_workers);
|
2010-03-12 20:02:32 +00:00
|
|
|
SetOrFr_node(or_fr, (choiceptr) Yap_LocalBase);
|
2001-04-09 20:54:03 +01:00
|
|
|
OrFr_nearest_livenode(or_fr) = NULL;
|
|
|
|
OrFr_depth(or_fr) = 0;
|
2010-02-02 23:42:15 +00:00
|
|
|
Set_OrFr_pend_prune_cp(or_fr, NULL);
|
2001-04-09 20:54:03 +01:00
|
|
|
OrFr_nearest_leftnode(or_fr) = or_fr;
|
|
|
|
OrFr_qg_solutions(or_fr) = NULL;
|
|
|
|
#ifdef TABLING_INNER_CUTS
|
|
|
|
OrFr_tg_solutions(or_fr) = NULL;
|
|
|
|
#endif /* TABLING_INNER_CUTS */
|
|
|
|
#ifdef TABLING
|
|
|
|
OrFr_owners(or_fr) = number_workers;
|
|
|
|
OrFr_next_on_stack(or_fr) = NULL;
|
|
|
|
OrFr_suspensions(or_fr) = NULL;
|
|
|
|
OrFr_nearest_suspnode(or_fr) = or_fr;
|
|
|
|
#endif /* TABLING */
|
|
|
|
OrFr_next(or_fr) = NULL;
|
|
|
|
GLOBAL_root_or_fr = or_fr;
|
|
|
|
#endif /* YAPOR */
|
|
|
|
|
|
|
|
#ifdef TABLING
|
|
|
|
/* root dependency frame */
|
2010-03-15 14:21:16 +00:00
|
|
|
if (!GLOBAL_root_dep_fr) {
|
2008-04-11 17:30:28 +01:00
|
|
|
new_dependency_frame(GLOBAL_root_dep_fr, FALSE, NULL, NULL, NULL, NULL, NULL);
|
2010-03-15 14:21:16 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif /* TABLING */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef YAPOR
|
|
|
|
void init_workers(void) {
|
|
|
|
int proc;
|
2010-01-14 15:58:19 +00:00
|
|
|
#ifdef THREADS
|
|
|
|
return;
|
|
|
|
#endif
|
2002-10-21 23:52:36 +01:00
|
|
|
NOfThreads = number_workers;
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef ACOW
|
|
|
|
if (number_workers > 1) {
|
|
|
|
int son;
|
|
|
|
son = fork();
|
2005-05-31 01:49:49 +01:00
|
|
|
if (son == -1)
|
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "fork error (init_workers)");
|
2001-04-09 20:54:03 +01:00
|
|
|
if (son > 0) {
|
|
|
|
/* I am the father, I must stay here and wait for my children to all die */
|
|
|
|
struct sigaction sigact;
|
|
|
|
|
|
|
|
GLOBAL_master_worker = getpid();
|
|
|
|
sigact.sa_handler = SIG_DFL;
|
|
|
|
sigemptyset(&sigact.sa_mask);
|
|
|
|
sigact.sa_flags = SA_RESTART;
|
|
|
|
sigaction(SIGINT, &sigact, NULL);
|
|
|
|
pause();
|
|
|
|
exit(0);
|
|
|
|
} else worker_pid(0) = getpid();
|
|
|
|
}
|
|
|
|
#endif /* ACOW */
|
|
|
|
for (proc = 1; proc < number_workers; proc++) {
|
|
|
|
int son;
|
|
|
|
son = fork();
|
2005-05-31 01:49:49 +01:00
|
|
|
if (son == -1)
|
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "fork error (init_workers)");
|
2001-04-09 20:54:03 +01:00
|
|
|
if (son == 0) {
|
|
|
|
/* new worker */
|
|
|
|
worker_id = proc;
|
|
|
|
remap_memory();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else worker_pid(proc) = son;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* YAPOR */
|
2010-04-19 02:34:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
void itos(int i, char *s) {
|
|
|
|
int n,r,j;
|
|
|
|
n = 10;
|
|
|
|
while (n <= i) n *= 10;
|
|
|
|
j = 0;
|
|
|
|
while (n > 1) {
|
|
|
|
n = n / 10;
|
|
|
|
r = i / n;
|
|
|
|
i = i - r * n;
|
|
|
|
s[j++] = r + '0';
|
|
|
|
}
|
|
|
|
s[j] = 0;
|
|
|
|
return;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif /* YAPOR || TABLING */
|