schedrev_xsb_i.h

00001 /* File:      schedrev_xsb_i.h
00002 ** Author(s): Juliana Freire
00003 ** Contact:   xsb-contact@cs.sunysb.edu
00004 ** 
00005 ** Copyright (C) The Research Foundation of SUNY, 1986, 1993-1998
00006 ** Copyright (C) ECRC, Germany, 1990
00007 ** 
00008 ** XSB is free software; you can redistribute it and/or modify it under the
00009 ** terms of the GNU Library General Public License as published by the Free
00010 ** Software Foundation; either version 2 of the License, or (at your option)
00011 ** any later version.
00012 ** 
00013 ** XSB is distributed in the hope that it will be useful, but WITHOUT ANY
00014 ** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00015 ** FOR A PARTICULAR PURPOSE.  See the GNU Library General Public License for
00016 ** more details.
00017 ** 
00018 ** You should have received a copy of the GNU Library General Public License
00019 ** along with XSB; if not, write to the Free Software Foundation,
00020 ** Inc, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00021 **
00022 ** $Id: schedrev_xsb_i.h,v 1.18 2006/05/22 20:47:45 tswift Exp $
00023 ** 
00024 */
00025 
00026 /*-------------------------------------------------------------------------*/
00027 
00028 /*
00029  * Schedules consumers to which at least some answers haven't been
00030  * returned.  Does so by linking them through their "previous breg" choice
00031  * point fields.  The value returned is the scheduling chain of such
00032  * consumers (NULL if no such consumers of this producer exist).
00033  * sched_answers() schedules all consumers for a given subgoal in an
00034  * SCC; it calls ScheduleConsumers which links each single consumer
00035  * into the scheduling chain.
00036  */
00037 
00038 #define ScheduleConsumer(Consumer,First,Last) {         \
00039    if ( IsNonNULL(Last) )                               \
00040      nlcp_prevbreg(Last) = Consumer;                    \
00041    else  /* record first consumer to backtrack to */    \
00042      First = Consumer;                                  \
00043    Last = Consumer;                                     \
00044  }
00045 
00046 #define ANSWER_TEMPLATE nlcp_template(consumer_cpf)
00047 
00048 static CPtr sched_answers(CTXTdeclc VariantSF producer_sf, CPtr *last_consumer)
00049 {
00050 
00051   CPtr first_sched_cons, last_sched_cons, consumer_cpf;
00052 
00053 
00054 #ifdef PROFILE
00055   subinst_table[SCHED_ANSWERS][1]++;
00056 #endif  
00057   xsb_dbgmsg((LOG_COMPLETION,"scheduling answers for %x\n",
00058               subg_cp_ptr(producer_sf)));
00059   first_sched_cons = last_sched_cons = NULL;
00060   consumer_cpf = subg_asf_list_ptr(producer_sf);
00061 
00062   /**** The producer has answers and consuming calls ****/  
00063   if ( has_answers(producer_sf) && IsNonNULL(consumer_cpf) 
00064        && !subg_is_reclaimed(producer_sf)) {
00065     /**** Check each consumer for unresolved answers ****/
00066     if ( IsSubsumptiveProducer(producer_sf) )
00067       while ( IsNonNULL(consumer_cpf) ) {
00068         SubConsSF consumer_sf;
00069         ALNptr answer_continuation;
00070         BTNptr next_answer;
00071 
00072         consumer_sf = (SubConsSF)nlcp_subgoal_ptr(consumer_cpf);
00073 
00074         table_pending_answer( nlcp_trie_return(consumer_cpf),
00075                               answer_continuation,
00076                               next_answer,
00077                               consumer_sf,
00078                               (SubProdSF)producer_sf,
00079                               ANSWER_TEMPLATE,
00080                               switch_envs(consumer_cpf),
00081                               TPA_NoOp );
00082         if ( IsNonNULL(answer_continuation) )
00083           ScheduleConsumer(consumer_cpf,first_sched_cons,last_sched_cons);
00084         consumer_cpf = nlcp_prevlookup(consumer_cpf);
00085       }
00086     else
00087       while ( IsNonNULL(consumer_cpf) ) {
00088 #ifdef CONC_COMPL
00089         if( int_val(nlcp_tid(consumer_cpf)) != th->tid )
00090                 ;
00091         else
00092 #endif
00093           if ( IsNonNULL(ALN_Next(nlcp_trie_return(consumer_cpf))) ) {
00094             xsb_dbgmsg((LOG_SCHED,"Scheduling Answer for: consumer_cpf=%x\n",
00095                (int)consumer_cpf));
00096             ScheduleConsumer(consumer_cpf,first_sched_cons,last_sched_cons); 
00097           }
00098         consumer_cpf = nlcp_prevlookup(consumer_cpf);
00099       }
00100 
00101     if( last_consumer )
00102       *last_consumer = last_sched_cons;
00103     if( last_sched_cons ) {
00104       /* by default the schain points to the leader */
00105       nlcp_prevbreg(last_sched_cons) = breg ;
00106       xsb_dbgmsg((LOG_SCHED, 
00107                   "scheduling fixed point base for last %x at %x @breg %x\n",
00108                   last_sched_cons,breg,*tcp_pcreg(breg)));
00109     }
00110   } /* if any answers and active nodes */
00111 
00112   return first_sched_cons;
00113 }
00114 
00115 /*-------------------------------------------------------------------------*/
00116 
00117 /* returns 0 if reached fixpoint, otherwise, returns the next breg 
00118  * for batched.  Essentially this routine performs a sched_answer()
00119  * for each subgoal in the (A)SCC except the leader.  This
00120  * sched_answer() has already been performed earlier in the
00121  * check_complete instruction.
00122  */
00123 
00124 static CPtr find_fixpoint(CTXTdeclc CPtr leader_csf, CPtr producer_cpf) 
00125 {
00126 
00127   VariantSF currSubg;
00128   CPtr complFrame; /* completion frame for currSubg */
00129   CPtr last_cons = 0; /* last consumer scheduled */
00130   CPtr sched_chain = 0, prev_sched = 0, tmp_sched = 0; /* build sched chain */
00131 
00132 #ifdef PROFILE
00133   subinst_table[OUTER_FIXPOINT][1]++;
00134 #endif
00135   xsb_dbgmsg((LOG_COMPLETION,"starting fp: %x\n",
00136               subg_cp_ptr(compl_subgoal_ptr(leader_csf))));
00137 
00138   complFrame = openreg;
00139   /* for each subgoal in the ASCC, from youngest to leader there is no
00140    * need to include the leader.  This is because sched_answers() is
00141    * done for each subgoal whenever it executes a check_complete
00142    * operation. Thus, scheduling for the leader has already been done.
00143    */
00144 #ifdef CONC_COMPL
00145   InitThreadDepList(&th->TDL);
00146 
00147   while(complFrame <= leader_csf) {
00148 #else
00149   while(complFrame < leader_csf) {
00150 #endif
00151 #ifdef PROFILE
00152     subinst_table[ITER_FIXPOINT][1]++;
00153 #endif
00154     currSubg = compl_subgoal_ptr(complFrame);
00155     xsb_dbgmsg((LOG_COMPLETION,"iterating: %x\n",subg_cp_ptr(currSubg)));
00156     /* check if all answers have been resolved for this subgoal */
00157 
00158     /* if there are unresolved answers for currSubg */
00159 #ifdef CONC_COMPL
00160     if ( IsNonNULL(compl_ext_cons(complFrame)) )
00161         last_cons=tmp_sched=sched_external(CTXTc compl_ext_cons(complFrame));
00162     else
00163         tmp_sched = sched_answers(CTXTc currSubg, &last_cons) ;
00164     if (tmp_sched) {
00165 #else
00166     if ((tmp_sched = sched_answers(CTXTc currSubg, &last_cons))) {
00167 #endif
00168       if (prev_sched) { /* if there is a prev subgoal scheduled */
00169         /* link new node to the previous one */
00170         nlcp_prevbreg(prev_sched) = tmp_sched;
00171       }
00172       else {
00173         sched_chain = tmp_sched; /* first node in the chain */
00174       }
00175       if( last_cons == NULL ) printf("lc NULL");
00176       prev_sched = last_cons;
00177     }
00178     complFrame = prev_compl_frame(complFrame);  
00179   }  /* while */
00180 
00181   if (prev_sched)  /* if anything has been scheduled */
00182     /* the first generator should backtrack to leader */
00183     nlcp_prevbreg(prev_sched) = producer_cpf;  
00184 
00185   xsb_dbgmsg((LOG_COMPLETION,"ending fp: %x\n",
00186               subg_cp_ptr(compl_subgoal_ptr(leader_csf))));
00187 
00188   return sched_chain;
00189 }

Generated on Wed Jul 26 13:30:42 2006 for XSB by  doxygen 1.4.5