File: opt.init.c

package info (click to toggle)
yap 5.1.1-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 16,124 kB
  • ctags: 14,650
  • sloc: ansic: 122,796; perl: 22,545; sh: 3,768; java: 1,277; makefile: 1,191; xml: 739; tcl: 624; lisp: 142; awk: 9
file content (298 lines) | stat: -rw-r--r-- 8,828 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/**********************************************************************
                                                               
                       The OPTYap Prolog system                
  OPTYap extends the Yap Prolog system to support or-parallel tabling
                                                               
  Copyright:   R. Rocha and NCC - University of Porto, Portugal
  File:        opt.init.c  
  version:     $Id: opt.init.c,v 1.14 2005/11/16 01:55:03 vsc Exp $   
                                                                     
**********************************************************************/

/* ------------------ **
**      Includes      **
** ------------------ */

#include "Yap.h"
#if defined(YAPOR) || defined(TABLING)
#define OPT_MAVAR_STATIC
#include "Yatom.h"
#include "Heap.h"
#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"
#endif /* !TABLING */
#ifdef MULTI_ASSIGNMENT_VARIABLES
ma_hash_entry Yap_ma_hash_table[MAVARS_HASH_SIZE];
UInt Yap_timestamp;    /* an unsigned int */
ma_h_inner_struct *Yap_ma_h_top;
#endif /* MULTI_ASSIGNMENT_VARIABLES */
#endif /* TABLING || !ACOW */
#ifdef ACOW
#include "sys/wait.h"
#endif /* ACOW */



/* ---------------------- **
**      Local macros      **
** ---------------------- */

#define STRUCTS_PER_PAGE(STR_TYPE)  ((Yap_page_size - STRUCT_SIZE(struct page_header)) / STRUCT_SIZE(STR_TYPE))

#define INIT_PAGES(PG, STR_TYPE)                         \
        INIT_LOCK(Pg_lock(PG));                          \
        Pg_pg_alloc(PG) = 0;                             \
        Pg_str_in_use(PG) = 0;                           \
        Pg_str_per_pg(PG) = STRUCTS_PER_PAGE(STR_TYPE);  \
        Pg_free_pg(PG) = NULL



#if YAP_MEMORY_ALLOC_SCHEME
char *
Yap_get_yap_space(int sz)
{
  char *ptr = Yap_AllocCodeSpace(sz+sizeof(CELL));
  if (ptr) {
    *ptr = 'y';
    return ptr+sizeof(CELL);
  }
  ptr = (char *)malloc(sz+sizeof(CELL));
  if (ptr) {
    *ptr = 'm';
    return ptr+sizeof(CELL);
  }
  Yap_Error(OUT_OF_HEAP_ERROR, TermNil, "Yap_AllocCodeSpace error (ALLOC_STRUCT), when allocating %d B", sz);
  return NULL;
}

void
Yap_free_yap_space(char *ptr)
{
  ptr -= sizeof(CELL);
  if (ptr[0] == 'y') {
    Yap_FreeCodeSpace(ptr);
  } else {
    free((void *)ptr);
  }
}
#endif

/* -------------------------- **
**      Global functions      **
** -------------------------- */

void Yap_init_global(int max_table_size, int n_workers, int sch_loop, int delay_load) {
  int i;

  /* global data related to memory management */
#ifdef LIMIT_TABLING
  if (max_table_size)
    GLOBAL_MAX_PAGES = ((max_table_size - 1) * 1024 * 1024 / SHMMAX + 1) * SHMMAX / Yap_page_size;
  else
    GLOBAL_MAX_PAGES = -1;
#endif /* LIMIT_TABLING */
  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);
  INIT_PAGES(GLOBAL_PAGES_sg_node, struct subgoal_trie_node);
  INIT_PAGES(GLOBAL_PAGES_ans_node, struct answer_trie_node);
  INIT_PAGES(GLOBAL_PAGES_sg_hash, struct subgoal_hash);
  INIT_PAGES(GLOBAL_PAGES_ans_hash, struct answer_hash);
  INIT_PAGES(GLOBAL_PAGES_dep_fr, struct dependency_frame);
#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 defined(YAPOR_ERRORS) || defined(TABLING_ERRORS)
  INIT_LOCK(GLOBAL_LOCKS_stderr_messages);
#endif /* YAPOR_ERRORS || TABLING_ERRORS */
  if (number_workers == 1)
    PARALLEL_EXECUTION_MODE = FALSE;
  else
    PARALLEL_EXECUTION_MODE = TRUE;
#endif /* YAPOR */

#ifdef TABLING
  /* global data related to tabling */
  GLOBAL_root_tab_ent = NULL;
#ifdef LIMIT_TABLING
  GLOBAL_first_sg_fr = NULL;
  GLOBAL_last_sg_fr = NULL;
  GLOBAL_check_sg_fr = NULL;
#endif /* LIMIT_TABLING */
  for (i = 0; i < MAX_TABLE_VARS; i++) {
    CELL *pt = ADDR_GLOBAL_table_var_enumerator(i);

    RESET_VARIABLE(pt);
  }
#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;
}


void init_local(void) {
#ifdef YAPOR
  /* local data related to or-parallelism */
  LOCAL = REMOTE + worker_id;
  LOCAL_top_cp = B_BASE;
  LOCAL_top_or_fr = GLOBAL_root_or_fr;
  LOCAL_load = 0;
  LOCAL_share_request = MAX_WORKERS;
  LOCAL_reply_signal = ready;
#ifdef ENV_COPY
  INIT_LOCK(LOCAL_lock_signals);
#endif /* ENV_COPY */
  LOCAL_prune_request = NULL;
#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
  LOCAL_top_cp_on_stack = B_BASE; /* ??? */
  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);
  OrFr_node(or_fr) = B_BASE;
  OrFr_nearest_livenode(or_fr) = NULL;
  OrFr_depth(or_fr) = 0;
  OrFr_pend_prune_cp(or_fr) = NULL;
  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 */
  new_dependency_frame(GLOBAL_root_dep_fr, FALSE, NULL, NULL, NULL, NULL, NULL);
#endif /* TABLING */
}


#ifdef YAPOR
void init_workers(void) {
  int proc;
  NOfThreads = number_workers;
#ifdef ACOW
  if (number_workers > 1) {
    int son;
    son = fork();
    if (son == -1)
      Yap_Error(FATAL_ERROR, TermNil, "fork error (init_workers)");
    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();
    if (son == -1)
      Yap_Error(FATAL_ERROR, TermNil, "fork error (init_workers)");
    if (son == 0) { 
      /* new worker */
      worker_id = proc;
      remap_memory();
      break;
    }
    else worker_pid(proc) = son;
  }
}
#endif /* YAPOR */
#endif /* YAPOR || TABLING */