YAP 7.1.0
opt.init.c
1/************************************************************************
2** **
3** The YapTab/YapOr/OPTYap systems **
4** **
5** YapTab extends the Yap Prolog engine to support sequential tabling **
6** YapOr extends the Yap Prolog engine to support or-parallelism **
7** OPTYap extends the Yap Prolog engine to support or-parallel tabling **
8** **
9** **
10** Yap Prolog was developed at University of Porto, Portugal **
11** **
12************************************************************************/
13
14/***********************
15** Includes **
16***********************/
17
18#include "Yap.h"
19#if defined(YAPOR) || defined(TABLING)
20#define OPT_MAVAR_STATIC
21#include "Yatom.h"
22#include "YapHeap.h"
23#if HAVE_UNISTD_H
24#include <unistd.h>
25#endif
26#include <signal.h>
27#ifdef YAPOR
28#include "or.macros.h"
29#endif /* YAPOR */
30#ifdef TABLING
31#include "tab.macros.h"
32#elif !defined(YAPOR_COW)
33#include "opt.mavar.h"
34#endif /* TABLING */
35#ifdef YAPOR_COW
36#include "sys/wait.h"
37#endif /* YAPOR_COW */
38
39/*********************
40** Macros **
41*********************/
42
43#ifdef USE_PAGES_MALLOC
44#define STRUCTS_PER_PAGE(STR_TYPE) \
45 ((Yap_page_size - ADJUST_SIZE(sizeof(struct page_header))) / \
46 ADJUST_SIZE(sizeof(STR_TYPE)))
47
48#define INIT_GLOBAL_PAGE_ENTRY(PG, STR_TYPE) \
49 INIT_LOCK(PgEnt_lock(PG)); \
50 PgEnt_pages_in_use(PG) = 0; \
51 PgEnt_strs_in_use(PG) = 0; \
52 PgEnt_strs_per_page(PG) = STRUCTS_PER_PAGE(STR_TYPE); \
53 PgEnt_first(PG) = NULL; \
54 PgEnt_last(PG) = NULL;
55#define INIT_LOCAL_PAGE_ENTRY(PG, STR_TYPE) \
56 PgEnt_pages_in_use(PG) = 0; \
57 PgEnt_strs_in_use(PG) = 0; \
58 PgEnt_strs_per_page(PG) = STRUCTS_PER_PAGE(STR_TYPE); \
59 PgEnt_first(PG) = NULL; \
60 PgEnt_last(PG) = NULL;
61#else
62#define INIT_GLOBAL_PAGE_ENTRY(PG, STR_TYPE) PgEnt_strs_in_use(PG) = 0
63#define INIT_LOCAL_PAGE_ENTRY(PG, STR_TYPE) PgEnt_strs_in_use(PG) = 0
64#endif /* USE_PAGES_MALLOC */
65
66/*******************************
67** Global functions **
68*******************************/
69
70void Yap_init_global_optyap_data(int max_table_size, int n_workers,
71 int sch_loop, int delay_load) {
72 int i;
73
74/* global data related to memory management */
75#ifdef USE_PAGES_MALLOC
76 INIT_GLOBAL_PAGE_ENTRY(GLOBAL_pages_alloc, void *);
77 INIT_GLOBAL_PAGE_ENTRY(GLOBAL_pages_void, void *);
78#endif /* USE_PAGES_MALLOC */
79#ifdef TABLING
80 INIT_GLOBAL_PAGE_ENTRY(GLOBAL_pages_tab_ent, struct table_entry);
81#if defined(THREADS_FULL_SHARING) || defined(THREADS_CONSUMER_SHARING)
82 INIT_GLOBAL_PAGE_ENTRY(GLOBAL_pages_sg_ent, struct subgoal_entry);
83#endif
84 INIT_GLOBAL_PAGE_ENTRY(GLOBAL_pages_sg_fr, struct subgoal_frame);
85 INIT_GLOBAL_PAGE_ENTRY(GLOBAL_pages_dep_fr, struct dependency_frame);
86 INIT_GLOBAL_PAGE_ENTRY(GLOBAL_pages_sg_node, struct subgoal_trie_node);
87 INIT_GLOBAL_PAGE_ENTRY(GLOBAL_pages_sg_hash, struct subgoal_trie_hash);
88 INIT_GLOBAL_PAGE_ENTRY(GLOBAL_pages_ans_node, struct answer_trie_node);
89 INIT_GLOBAL_PAGE_ENTRY(GLOBAL_pages_ans_hash, struct answer_trie_hash);
90#if defined(THREADS_FULL_SHARING)
91 INIT_GLOBAL_PAGE_ENTRY(GLOBAL_pages_ans_ref_node, struct answer_ref_node);
92#endif
93 INIT_GLOBAL_PAGE_ENTRY(GLOBAL_pages_gt_node, struct global_trie_node);
94 INIT_GLOBAL_PAGE_ENTRY(GLOBAL_pages_gt_hash, struct global_trie_hash);
95#endif /* TABLING */
96#ifdef YAPOR
97 INIT_GLOBAL_PAGE_ENTRY(GLOBAL_pages_or_fr, struct or_frame);
98 INIT_GLOBAL_PAGE_ENTRY(GLOBAL_pages_qg_sol_fr,
100 INIT_GLOBAL_PAGE_ENTRY(GLOBAL_pages_qg_ans_fr,
102#ifdef TABLING
103 INIT_GLOBAL_PAGE_ENTRY(GLOBAL_pages_susp_fr, struct suspension_frame);
104#endif
105#ifdef TABLING_INNER_CUTS
106 INIT_GLOBAL_PAGE_ENTRY(GLOBAL_pages_tg_sol_fr,
107 struct table_subgoal_solution_frame);
108 INIT_GLOBAL_PAGE_ENTRY(GLOBAL_pages_tg_ans_fr,
109 struct table_subgoal_answer_frame);
110#endif
111#endif /* YAPOR */
112
113#ifdef YAPOR
114 /* global static data */
115 GLOBAL_number_workers = n_workers;
116 GLOBAL_worker_pid(0) = getpid();
117 for (i = 1; i < GLOBAL_number_workers; i++)
118 GLOBAL_worker_pid(i) = 0;
119 GLOBAL_scheduler_loop = sch_loop;
120 GLOBAL_delayed_release_load = delay_load;
121
122 /* global data related to or-parallelism */
123 ALLOC_OR_FRAME(GLOBAL_root_or_fr);
124 BITMAP_clear(GLOBAL_bm_present_workers);
125 for (i = 0; i < GLOBAL_number_workers; i++)
126 BITMAP_insert(GLOBAL_bm_present_workers, i);
127 BITMAP_copy(GLOBAL_bm_idle_workers, GLOBAL_bm_present_workers);
128 BITMAP_clear(GLOBAL_bm_root_cp_workers);
129 BITMAP_clear(GLOBAL_bm_invisible_workers);
130 BITMAP_clear(GLOBAL_bm_requestable_workers);
131 BITMAP_copy(GLOBAL_bm_finished_workers, GLOBAL_bm_present_workers);
132 INIT_LOCK(GLOBAL_locks_bm_idle_workers);
133 INIT_LOCK(GLOBAL_locks_bm_root_cp_workers);
134 INIT_LOCK(GLOBAL_locks_bm_invisible_workers);
135 INIT_LOCK(GLOBAL_locks_bm_requestable_workers);
136 INIT_LOCK(GLOBAL_locks_bm_finished_workers);
137#ifdef TABLING_INNER_CUTS
138 INIT_LOCK(GLOBAL_locks_bm_pruning_workers);
139#endif /* TABLING_INNER_CUTS */
140 GLOBAL_locks_who_locked_heap = MAX_WORKERS;
141 INIT_LOCK(GLOBAL_locks_heap_access);
142 INIT_LOCK(GLOBAL_locks_alloc_block);
143 if (GLOBAL_number_workers == 1)
144 GLOBAL_parallel_mode = PARALLEL_MODE_OFF;
145 else
146 GLOBAL_parallel_mode = PARALLEL_MODE_ON;
147#endif /* YAPOR */
148
149#ifdef TABLING
150 /* global data related to tabling */
151 GLOBAL_root_gt = NULL;
152 GLOBAL_root_tab_ent = NULL;
153#ifdef LIMIT_TABLING
154 if (max_table_size)
155 GLOBAL_max_pages = ((max_table_size - 1) * 1024 * 1024 / SHMMAX + 1) *
156 SHMMAX / Yap_page_size;
157 else
158 GLOBAL_max_pages = -1;
159 GLOBAL_first_sg_fr = NULL;
160 GLOBAL_last_sg_fr = NULL;
161 GLOBAL_check_sg_fr = NULL;
162#endif /* LIMIT_TABLING */
163#ifdef YAPOR
164 new_dependency_frame(GLOBAL_root_dep_fr, FALSE, NULL, NULL, NULL, NULL, FALSE,
165 NULL);
166#endif /* YAPOR */
167 for (i = 0; i < MAX_TABLE_VARS; i++) {
168 CELL *pt = GLOBAL_table_var_enumerator_addr(i);
169 RESET_VARIABLE(pt);
170 }
171#ifdef TRIE_LOCK_USING_GLOBAL_ARRAY
172 for (i = 0; i < TRIE_LOCK_BUCKETS; i++)
173 INIT_LOCK(GLOBAL_trie_locks(i));
174#endif /* TRIE_LOCK_USING_GLOBAL_ARRAY */
175#endif /* TABLING */
176
177 return;
178}
179
180void Yap_init_local_optyap_data(int wid) {
181#if defined(YAPOR_THREADS) || defined(THREADS_CONSUMER_SHARING)
182 CACHE_REGS
183#endif /* YAPOR_THREADS || THREADS_CONSUMER_SHARING */
184
185#if defined(TABLING) && (defined(YAPOR) || defined(THREADS))
186/* local data related to memory management */
187#ifdef YAPOR
188 REMOTE_next_free_ans_node(wid) = NULL;
189#elif THREADS
190#ifdef USE_PAGES_MALLOC
191 INIT_LOCAL_PAGE_ENTRY(REMOTE_pages_void(wid), void *);
192#endif
193 INIT_LOCAL_PAGE_ENTRY(REMOTE_pages_tab_ent(wid), struct table_entry);
194#if defined(THREADS_FULL_SHARING) || defined(THREADS_CONSUMER_SHARING)
195 INIT_LOCAL_PAGE_ENTRY(REMOTE_pages_sg_ent(wid), struct subgoal_entry);
196#endif
197 INIT_LOCAL_PAGE_ENTRY(REMOTE_pages_sg_fr(wid), struct subgoal_frame);
198 INIT_LOCAL_PAGE_ENTRY(REMOTE_pages_dep_fr(wid), struct dependency_frame);
199 INIT_LOCAL_PAGE_ENTRY(REMOTE_pages_sg_node(wid), struct subgoal_trie_node);
200 INIT_LOCAL_PAGE_ENTRY(REMOTE_pages_sg_hash(wid), struct subgoal_trie_hash);
201 INIT_LOCAL_PAGE_ENTRY(REMOTE_pages_ans_node(wid), struct answer_trie_node);
202 INIT_LOCAL_PAGE_ENTRY(REMOTE_pages_ans_hash(wid), struct answer_trie_hash);
203#if defined(THREADS_FULL_SHARING)
204 INIT_LOCAL_PAGE_ENTRY(REMOTE_pages_ans_ref_node(wid), struct answer_ref_node);
205#endif
206 INIT_LOCAL_PAGE_ENTRY(REMOTE_pages_gt_node(wid), struct global_trie_node);
207 INIT_LOCAL_PAGE_ENTRY(REMOTE_pages_gt_hash(wid), struct global_trie_hash);
208#endif
209#endif /* TABLING && (YAPOR || THREADS) */
210
211#ifdef YAPOR
212 /* local data related to or-parallelism */
213 Set_REMOTE_top_cp(wid, (choiceptr)LOCAL_LocalBase);
214 REMOTE_top_or_fr(wid) = GLOBAL_root_or_fr;
215 REMOTE_load(wid) = 0;
216 REMOTE_share_request(wid) = MAX_WORKERS;
217 REMOTE_reply_signal(wid) = worker_ready;
218#ifdef YAPOR_COPY
219 INIT_LOCK(REMOTE_lock_signals(wid));
220#endif /* YAPOR_COPY */
221 Set_REMOTE_prune_request(wid, NULL);
222 INIT_LOCK(REMOTE_lock(wid));
223#endif /* YAPOR */
224
225#ifdef TABLING
226 /* local data related to tabling */
227 REMOTE_top_sg_fr(wid) = NULL;
228 REMOTE_top_dep_fr(wid) = NULL;
229#ifdef YAPOR
230 REMOTE_top_dep_fr(wid) = GLOBAL_root_dep_fr;
231 Set_REMOTE_top_cp_on_stack(wid, (choiceptr)LOCAL_LocalBase); /* ??? */
232 REMOTE_top_susp_or_fr(wid) = GLOBAL_root_or_fr;
233#endif /* YAPOR */
234#ifdef THREADS_CONSUMER_SHARING
235 ThDepFr_terminator(GLOBAL_th_dep_fr(wid)) = 0;
236 ThDepFr_next(GLOBAL_th_dep_fr(wid)) = wid;
237 ThDepFr_state(GLOBAL_th_dep_fr(wid)) = working;
238 INIT_LOCK(ThDepFr_lock(GLOBAL_th_dep_fr(wid)));
239#endif /* THREADS_CONSUMER_SHARING */
240#endif /* TABLING */
241 return;
242}
243
244void Yap_init_root_frames(void) {
245 CACHE_REGS
246
247#ifdef YAPOR
248 /* root or frame */
249 or_fr_ptr or_fr = GLOBAL_root_or_fr;
250 INIT_LOCK(OrFr_lock(or_fr));
251 OrFr_alternative(or_fr) = NULL;
252 BITMAP_copy(OrFr_members(or_fr), GLOBAL_bm_present_workers);
253 SetOrFr_node(or_fr, (choiceptr)LOCAL_LocalBase);
254 OrFr_nearest_livenode(or_fr) = NULL;
255 OrFr_depth(or_fr) = 0;
256 Set_OrFr_pend_prune_cp(or_fr, NULL);
257 OrFr_nearest_leftnode(or_fr) = or_fr;
258 OrFr_qg_solutions(or_fr) = NULL;
259#ifdef TABLING_INNER_CUTS
260 OrFr_tg_solutions(or_fr) = NULL;
261#endif /* TABLING_INNER_CUTS */
262#ifdef TABLING
263 OrFr_owners(or_fr) = GLOBAL_number_workers;
264 OrFr_next_on_stack(or_fr) = NULL;
265 OrFr_suspensions(or_fr) = NULL;
266 OrFr_nearest_suspnode(or_fr) = or_fr;
267#endif /* TABLING */
268 OrFr_next(or_fr) = NULL;
269#endif /* YAPOR */
270
271#ifdef TABLING
272 /* root global trie node */
273 new_global_trie_node(GLOBAL_root_gt, 0, NULL, NULL, NULL);
274/* root dependency frame */
275#ifdef YAPOR
276 DepFr_cons_cp(GLOBAL_root_dep_fr) = B; /* with YAPOR, at that point,
277 LOCAL_top_dep_fr shouldn't be the
278 same as GLOBAL_root_dep_fr ? */
279#else
280 new_dependency_frame(LOCAL_top_dep_fr, FALSE, NULL, NULL, B, NULL, FALSE,
281 NULL);
282#endif /* YAPOR */
283#endif /* TABLING */
284}
285
286void itos(int i, char *s) {
287 int n, r, j;
288 n = 10;
289 while (n <= i)
290 n *= 10;
291 j = 0;
292 while (n > 1) {
293 n = n / 10;
294 r = i / n;
295 i = i - r * n;
296 s[j++] = r + '0';
297 }
298 s[j] = 0;
299 return;
300}
301#endif /* YAPOR || TABLING */
Main definitions.
Definition: tab.structs.h:240
Definition: tab.structs.h:22