00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "debugs/debug_delay.h"
00029
00030
00031
00032 #define unvisit(leader) \
00033 for (csf = leader; csf >= (ComplStackFrame)openreg; csf--) \
00034 compl_visited(csf) = FALSE
00035
00036
00037
00038 #define edge_alloc_chunk_size (1024 * sizeof(struct ascc_edge))
00039
00040 static char *edge_space_chunk_ptr = NULL;
00041
00042 static EPtr free_edges = NULL, free_edge_space = NULL, top_edge_space = NULL;
00043
00044 void abolish_edge_space(void)
00045 {
00046 char *t;
00047
00048 while (edge_space_chunk_ptr) {
00049 t = *(char **)edge_space_chunk_ptr;
00050 mem_dealloc(edge_space_chunk_ptr,edge_alloc_chunk_size+sizeof(Cell),TABLE_SPACE);
00051 edge_space_chunk_ptr = t;
00052 }
00053 free_edges = free_edge_space = top_edge_space = NULL;
00054 }
00055
00056 static EPtr alloc_more_edge_space(void)
00057 {
00058 char *t;
00059
00060 if ((t = (char *)mem_alloc(edge_alloc_chunk_size+sizeof(Cell),TABLE_SPACE)) == NULL)
00061 xsb_abort("No space to allocate more edges for SCC detection");
00062 *(char **)t = edge_space_chunk_ptr;
00063 edge_space_chunk_ptr = t;
00064 free_edge_space = (EPtr)(t+sizeof(Cell));
00065 top_edge_space = (EPtr)(t+edge_alloc_chunk_size+sizeof(Cell));
00066 return free_edge_space++;
00067 }
00068
00069 #define Free_Edge(theEdge) \
00070 next_edge(theEdge) = free_edges; \
00071 free_edges = theEdge
00072
00073 #define New_Edge(theEdge,FromEptr,ToNode) \
00074 if (free_edges) {\
00075 theEdge = free_edges; \
00076 free_edges = next_edge(free_edges); \
00077 } else { \
00078 if (free_edge_space < top_edge_space) { \
00079 theEdge = free_edge_space++; \
00080 } else { \
00081 theEdge = alloc_more_edge_space(); \
00082 } \
00083 } \
00084 edge_to_node(theEdge) = (ComplStackFrame) ToNode; \
00085 next_edge(theEdge) = FromEptr; \
00086 FromEptr = theEdge
00087
00088
00089
00090
00091
00092 #define add_ascc_edges(subgoal,cs_frame,leader_cs_frame) \
00093 csf2 = (ComplStackFrame)subg_compl_stack_ptr(subgoal); \
00094 if (leader_cs_frame >= csf2 && !is_completed(subgoal)) { \
00095 New_Edge(eptr, compl_DGT_edges(cs_frame), csf2); \
00096 New_Edge(eptr, compl_DG_edges(csf2), cs_frame); \
00097 }
00098
00099 static void reclaim_edge_space(ComplStackFrame csf_ptr)
00100 {
00101 EPtr e, eptr;
00102
00103 e = compl_DG_edges(csf_ptr);
00104 while (e != NULL) {
00105 eptr = e;
00106 e = next_edge(e);
00107 Free_Edge(eptr);
00108 }
00109 compl_DG_edges(csf_ptr) = NULL;
00110 e = compl_DGT_edges(csf_ptr);
00111 while (e != NULL) {
00112 eptr = e;
00113 e = next_edge(e);
00114 Free_Edge(eptr);
00115 }
00116 compl_DGT_edges(csf_ptr) = NULL;
00117 }
00118
00119
00120
00121
00122
00123
00124 #define first_consumer(SUBG,CSF) (subg_asf_list_ptr(SUBG))
00125 #define ptcp_of_gen(SUBG,CSF) ((VariantSF)(tcp_ptcp(subg_cp_ptr(SUBG))))
00126 #define ptcp_of_cons(CONS_CP) ((VariantSF)(nlcp_ptcp(CONS_CP)))
00127 #define ptcp_of_csusp(CSUSP_CP) ((VariantSF)(csf_ptcp(CSUSP_CP)))
00128
00129
00130
00131 static inline void construct_dep_graph(CTXTdeclc ComplStackFrame leader_compl_frame)
00132 {
00133 EPtr eptr;
00134 CPtr asf, nsf;
00135 VariantSF p, source_subg;
00136 ComplStackFrame csf1, csf2;
00137
00138 csf1 = leader_compl_frame;
00139 while (csf1 >= (ComplStackFrame)openreg) {
00140 source_subg = compl_subgoal_ptr(csf1);
00141 if (!is_completed(source_subg)) {
00142
00143 if ((p = ptcp_of_gen(source_subg,csf1)) != NULL) {
00144 add_ascc_edges(p, csf1, leader_compl_frame);
00145 }
00146
00147 for (asf = first_consumer(source_subg,csf1);
00148 asf != NULL; asf = nlcp_prevlookup(asf)) {
00149 if ((p = ptcp_of_cons(asf)) != NULL) {
00150 add_ascc_edges(p, csf1, leader_compl_frame);
00151 }
00152 }
00153
00154 for (nsf = subg_compl_susp_ptr(source_subg);
00155 nsf != NULL; nsf = csf_prevcsf(nsf)) {
00156 if ((p = ptcp_of_csusp(nsf)) != NULL) {
00157 add_ascc_edges(p, csf1, leader_compl_frame);
00158 }
00159 }
00160 }
00161 csf1 = (ComplStackFrame)next_compl_frame(csf1);
00162 }
00163 #ifdef VERBOSE_COMPLETION
00164 xsb_dbgmsg((LOG_DEBUG,"! Constructed the edges of the DepGraph"));
00165 #endif
00166 }
00167
00168
00169
00170
00171
00172 static void batched_compute_wfs(CTXTdeclc CPtr leader_compl_frame,
00173 CPtr leader_breg,
00174 VariantSF leader_subg)
00175 {
00176 CPtr ComplStkFrame;
00177 xsbBool sccs_needed;
00178 VariantSF curr_subg;
00179 CPtr cont_breg = leader_breg;
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189 sccs_needed = FALSE;
00190 ComplStkFrame = leader_compl_frame;
00191 while (ComplStkFrame >= openreg) {
00192 curr_subg = compl_subgoal_ptr(ComplStkFrame);
00193 if (is_completed(curr_subg)) {
00194 subg_compl_susp_ptr(curr_subg) = NULL;
00195 } else {
00196 if (subg_compl_susp_ptr(curr_subg) != NULL) sccs_needed = TRUE;
00197 }
00198 ComplStkFrame = next_compl_frame(ComplStkFrame);
00199 }
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229 if (sccs_needed) {
00230 xsbBool found;
00231 CPtr nsf;
00232 CPtr CopyFrame;
00233 ComplStackFrame csf, max_finish_csf;
00234 xsbBool non_lrd_stratified;
00235
00236 #ifdef VERBOSE_COMPLETION
00237 xsb_dbgmsg((LOG_DEBUG,"\t===> SCC detection is needed...(%d subgoals in ASCC)...",
00238 (int)((leader_compl_frame-openreg)/COMPLFRAMESIZE+1)));
00239 print_completion_stack();
00240 #endif
00241
00242 construct_dep_graph(CTXTc (ComplStackFrame)leader_compl_frame);
00243 dbg_print_completion_stack(LOG_COMPLETION);
00244
00245 max_finish_csf = DFS_DGT(CTXTc (ComplStackFrame)leader_compl_frame);
00246 xsb_dbgmsg((LOG_COMPLETION,
00247 "! MAX FINISH_SUBGOAL AT COMPL STACK: %p",max_finish_csf));
00248
00249
00250 unvisit((ComplStackFrame)leader_compl_frame);
00251
00252
00253
00254 find_independent_scc(max_finish_csf);
00255
00256 dbg_print_completion_stack(LOG_COMPLETION);
00257
00258
00259
00260 ComplStkFrame = leader_compl_frame;
00261 non_lrd_stratified = FALSE;
00262
00263 while (ComplStkFrame >= openreg) {
00264 CPtr susp_csf;
00265 VariantSF susp_subgoal;
00266
00267 curr_subg = compl_subgoal_ptr(ComplStkFrame);
00268 if (!is_completed(curr_subg)) {
00269 if (compl_visited(ComplStkFrame) != FALSE) {
00270 curr_subg = compl_subgoal_ptr(ComplStkFrame);
00271 for (nsf = subg_compl_susp_ptr(curr_subg);
00272 nsf != NULL; nsf = csf_prevcsf(nsf)) {
00273 if ((susp_subgoal = ptcp_of_csusp(nsf)) != NULL) {
00274 susp_csf = subg_compl_stack_ptr(susp_subgoal);
00275 if (!is_completed(susp_subgoal) &&
00276 susp_csf <= leader_compl_frame &&
00277 compl_visited(susp_csf) != FALSE) {
00278
00279 mark_delayed(ComplStkFrame, susp_csf, nsf);
00280 non_lrd_stratified = TRUE;
00281 xsb_dbgmsg((LOG_DELAY, "\t Subgoal "));
00282 dbg_print_subgoal(LOG_DELAY,stddbg,(VariantSF)susp_subgoal);
00283 xsb_dbgmsg((LOG_DELAY, " depends negatively on subgoal "));
00284 dbg_print_subgoal(LOG_DELAY, stddbg, curr_subg);
00285 xsb_dbgmsg((LOG_DELAY, "\n"));
00286 }
00287 }
00288 }
00289 }
00290 }
00291 ComplStkFrame = next_compl_frame(ComplStkFrame);
00292 }
00293
00294
00295 #ifdef SERIOUS_DEBUGGING_NEEDED
00296 print_completion_stack();
00297 #endif
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328 if (non_lrd_stratified == FALSE) {
00329 found = FALSE;
00330 for (ComplStkFrame = openreg;
00331 !found && ComplStkFrame <= leader_compl_frame;
00332 ComplStkFrame = prev_compl_frame(ComplStkFrame)) {
00333 if (compl_visited(ComplStkFrame) <= FALSE) {
00334 cont_breg = subg_cp_ptr(compl_subgoal_ptr(ComplStkFrame));
00335 breg = cont_breg;
00336 xsb_dbgmsg((LOG_COMPLETION,
00337 "------ Setting TBreg to %p...", cont_breg));
00338 found = TRUE;
00339 }
00340 }
00341 if (!found) {
00342
00343 cont_breg = tcp_prevbreg(leader_breg);
00344 }
00345 } else {
00346 for (ComplStkFrame = openreg; ComplStkFrame <= leader_compl_frame;
00347 ComplStkFrame = prev_compl_frame(ComplStkFrame)) {
00348 if (compl_visited(ComplStkFrame) != FALSE)
00349 compl_visited(ComplStkFrame) = DELAYED;
00350 }
00351 cont_breg = subg_cp_ptr(compl_subgoal_ptr(leader_compl_frame));
00352 breg = cont_breg;
00353 xsb_dbgmsg((LOG_COMPLETION, "------ Setting TBreg to %p...", cont_breg));
00354 }
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373 {
00374 for (ComplStkFrame = leader_compl_frame; ComplStkFrame >= openreg;
00375 ComplStkFrame = next_compl_frame(ComplStkFrame)) {
00376 if (compl_visited(ComplStkFrame) != FALSE) {
00377 curr_subg = compl_subgoal_ptr(ComplStkFrame);
00378 if (compl_visited(ComplStkFrame) != DELAYED) {
00379 #ifdef CONC_COMPL
00380 if( !is_completed(curr_subg) )
00381 {
00382 mark_as_completed(curr_subg);
00383 WakeDependentThreads(th, curr_subg);
00384 }
00385 #else
00386 mark_as_completed(curr_subg);
00387 #endif
00388 reclaim_incomplete_table_structs(curr_subg);
00389 if (neg_simplif_possible(curr_subg)) {
00390 simplify_neg_fails(CTXTc curr_subg);
00391 }
00392 }
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413 if ((nsf = subg_compl_susp_ptr(curr_subg)) != NULL) {
00414 CPtr min_breg;
00415
00416 set_min(min_breg, breg, bfreg);
00417 if (compl_visited(ComplStkFrame) != DELAYED) {
00418 save_compl_susp_cp(min_breg, cont_breg, nsf);
00419 breg = min_breg;
00420
00421 subg_compl_susp_ptr(curr_subg) = NULL;
00422 #ifdef VERBOSE_COMPLETION
00423 xsb_dbgmsg((LOG_DEBUG,"------ Setting Breg to %p...", breg));
00424 #endif
00425 } else {
00426 CPtr dnsf = NULL, ndnsf = NULL;
00427 CPtr head_dnsf = NULL, head_ndnsf = NULL;
00428 while (nsf != NULL) {
00429 if (csf_neg_loop(nsf) == FALSE) {
00430 if (ndnsf == NULL) head_ndnsf = nsf;
00431 else csf_prevcsf(ndnsf) = nsf;
00432 ndnsf = nsf;
00433 nsf = csf_prevcsf(nsf);
00434 csf_prevcsf(ndnsf) = NULL;
00435 } else {
00436 if (dnsf == NULL) head_dnsf = nsf;
00437 else csf_prevcsf(dnsf) = nsf;
00438 dnsf = nsf;
00439 nsf = csf_prevcsf(nsf);
00440 csf_prevcsf(dnsf) = NULL;
00441 }
00442 }
00443 if (head_dnsf != NULL) {
00444 save_compl_susp_cp(min_breg, cont_breg, head_dnsf);
00445 breg = min_breg;
00446 }
00447 subg_compl_susp_ptr(curr_subg) = head_ndnsf;
00448 }
00449 cont_breg = breg;
00450 }
00451 }
00452 }
00453 }
00454 xsb_dbgmsg((LOG_COMPLETION, "------ Completed the chosen SCC..."));
00455
00456
00457
00458
00459
00460
00461
00462
00463 ComplStkFrame = CopyFrame = leader_compl_frame;
00464 while (ComplStkFrame >= openreg) {
00465 curr_subg = compl_subgoal_ptr(ComplStkFrame);
00466 reclaim_edge_space((ComplStackFrame)ComplStkFrame);
00467 if (!is_completed(curr_subg)) {
00468 subg_compl_stack_ptr(curr_subg) = CopyFrame;
00469
00470 compact_completion_frame(CopyFrame, ComplStkFrame, curr_subg);
00471 } else {
00472 reclaim_incomplete_table_structs(curr_subg);
00473 }
00474 ComplStkFrame = next_compl_frame(ComplStkFrame);
00475 }
00476 openreg = prev_compl_frame(CopyFrame);
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500 tcp_prevbreg(subg_cp_ptr(compl_subgoal_ptr(leader_compl_frame))) =
00501 tcp_prevbreg(subg_cp_ptr(leader_subg));
00502 for (ComplStkFrame = next_compl_frame(leader_compl_frame);
00503 ComplStkFrame >= openreg;
00504 ComplStkFrame = next_compl_frame(ComplStkFrame)){
00505 tcp_prevbreg(subg_cp_ptr(compl_subgoal_ptr(ComplStkFrame))) =
00506 subg_cp_ptr(compl_subgoal_ptr(prev_compl_frame(ComplStkFrame)));
00507 }
00508 }
00509 else {
00510 ComplStkFrame = leader_compl_frame;
00511 while (ComplStkFrame >= openreg) {
00512 curr_subg = compl_subgoal_ptr(ComplStkFrame);
00513 #ifdef CONC_COMPL
00514 if( !is_completed(curr_subg) )
00515 {
00516 mark_as_completed(curr_subg);
00517 WakeDependentThreads(th, curr_subg);
00518 }
00519 #else
00520 mark_as_completed(curr_subg);
00521 #endif
00522 if (neg_simplif_possible(curr_subg)) {
00523 simplify_neg_fails(CTXTc curr_subg);
00524 }
00525 ComplStkFrame = next_compl_frame(ComplStkFrame);
00526 }
00527
00528 ComplStkFrame = leader_compl_frame;
00529 while (ComplStkFrame >= openreg) {
00530 curr_subg = compl_subgoal_ptr(ComplStkFrame);
00531 reclaim_incomplete_table_structs(curr_subg);
00532 ComplStkFrame = next_compl_frame(ComplStkFrame);
00533 }
00534
00535 openreg = prev_compl_frame(leader_compl_frame);
00536 }
00537
00538 xsb_dbgmsg((LOG_COMPLETION, "------ Completed an ASCC..."));
00539 }
00540
00541