On Combining Linear-Based Strategies for Tabled Evaluation of Logic Programs
Tabled evaluation is a recognized and powerful technique that overcomes some limitations of traditional Prolog systems in dealing with recursion and redundant sub-computations. We can distinguish two main categories of tabling mechanisms: suspension-…
Authors: Miguel Areias, Ricardo Rocha
Under c onsider ation for public ation in The ory and Pr actic e of L o gic Pr o gr amming 1 On Combining Line ar-Base d Str ate gies for T able d Evaluation of L o gic Pr o gr ams MIGUEL AREIAS and RICARDO R OCHA CRACS & INESC-Porto LA, F aculty of Scienc es, University of Porto Rua do Camp o Ale gr e, 1021/1055, 4169-007 Porto, Portugal ( e-mail: { miguel-areias,ricroc } @dcc.fc.up.pt ) submitte d 1 January 2003; r evise d 1 January 2003; ac c epte d 1 January 2003 Abstract T abled ev aluation is a recognized and pow erful technique that ov ercomes some limitations of traditional Prolog systems in dealing with recursion and redundant sub-computations. W e can distinguish tw o main categories of tabling mechanisms: susp ension-based tabling and linear tabling. While susp ension-based mec hanisms are considered to obtain b etter results in general, they ha ve more memory space requiremen ts and are more complex and harder to implement than linear tabling mec hanisms. Arguably , the SLDT and DRA strategies are the tw o most successful extensions to standard linear tabled ev aluation. In this work, we prop ose a new strategy , named DRS, and w e present a framework, on top of the Y ap system, that supp orts the combination of all these three strategies. Our imple- men tation shares the underlying execution environmen t and most of the data structures used to implemen t tabling in Y ap. W e th us argue that all these common features allows us to make a first and fair comparison b etw een these different linear tabling strategies and, therefore, b etter understand the adv antages and w eaknesses of each, when used solely or com bined with the others. KEYWORDS : Linear T abling, Integration, Implemen tation. 1 In tro duction The op erational semantics of Prolog is giv en by SLD resolution (Llo yd 1987), an ev aluation strategy particularly simple that matches current stac k based machines particularly well, but that suffers from fundamental limitations, such as in dealing with recursion and redundan t sub-computations. T abled ev aluation (T amaki and Sato 1986; Chen and W arren 1996) is a recognized and p ow erful technique that can considerably reduce the search space, av oid looping and ha ve better termination prop erties than SLD resolution. T abling consists of storing intermediate solutions for subgoals so that they can b e reused when a repeated subgoal app ears during the resolution pro cess. Imple- men tations of tabling are curren tly a v ailable in systems like XSB Prolog (Sagonas and Swift 1998), Y ap Prolog (Ro cha et al. 2000), B-Prolog (Zhou et al. 2000), ALS- Prolog (Guo and Gupta 2001), Mercury (Somogyi and Sagonas 2006) and Ciao 2 Miguel Ar eias and R ic ar do R o cha Prolog (de Guzm´ an et al. 2009). In these implementations, we can distinguish tw o main categories of tabling mec hanisms: susp ension-b ase d tabling and line ar tabling . Susp ension-based tabling mechanisms need to preserve the computation state of susp ended tabled subgoals in order to ensure that all solutions are correctly com- puted. A tabled ev aluation can be seen as a sequence of sub-computations that susp end and later resume. Linear tabling mec hanisms use iterative computations of tabled subgoals to compute fix-p oints and for that they maintain a single execu- tion tree without requiring susp ension and resumption of sub-computations. While susp ension-based mechanisms are considered to obtain b etter results in general, they ha ve more memory space requiremen ts and are more complex and harder to implemen t than linear tabling mechanisms. Arguably , the SLDT (Zhou et al. 2000) and DRA (Guo and Gupta 2001; Areias and Ro c ha 2010) strategies are the t w o most successful extensions to standard linear tabling ev aluation. As these strategies optimize differen t asp ects of the ev aluation, they are, in principle, orthogonal to each other and thus it should b e p ossible to com bine b oth in the same system. Ho wev er, to the b est of our knowledge, no single Prolog system supports b oth strategies sim ultaneously and th us, understanding the adv an tages and w eaknesses of eac h cannot be fully disso ciated from the base Prolog system on top of whic h they are implemented. In this work, we prop ose a new strategy , named Dynamic R e or dering of Solu- tions (DRS) , and we presen t a framework, on top of the Y ap Prolog system, that in tegrates and supp orts the combination of the SLDT, DRA and DRS strategies. Our implementation shares the underlying execution environmen t and most of the data structures used to implement tabling in Y ap (Ro cha et al. 2000). In partic- ular, w e to ok adv an tage of Y ap’s efficien t table space data structures based on tries (Ramakrishnan et al. 1999), which w e used with minimal modifications. W e th us argue that all these common support features allo ws us to mak e a first and fair comparison betw een these different linear tabling strategies and, therefore, b etter understand the adv antages and weaknesses of eac h, when used solely or com bined with the others. The remainder of the pap er is organized as follows. First, we briefly introduce the basics of tabling and describ e the execution mo del for standard linear tabled ev aluation. Next, we present the SLDT, DRA and DRS strategies and discuss how they can b e used to optimize differen t asp ects of the ev aluation. W e then pro vide some implemen tation details regarding the integration of the three strategies on top of the Y ap engine. Finally , we present some exp erimen tal results and w e end b y outlining some conclusions. 2 Standard Linear T abled Ev aluation T abling w orks by storing in termediate solutions for tabled subgoals so that they can b e reused when a rep eated call app ears 1 . In a n utshell, first calls to tabled subgoals 1 A subgoal call repeats a previous call if they are the same up to v ariable renaming. On Combining Line ar-Base d Str ate gies for T able d Evaluation 3 are considered gener ators and are ev aluated as usual, using SLD resolution, but their solutions are stored in a global data space, called the table sp ac e . Rep eated calls to tabled subgoals are considered c onsumers and are not re-ev aluated against the program clauses b ecause they can p otentially lead to infinite lo ops, instead they are resolved by consuming the solutions already stored for the corresp onding generator. During this pro cess, as further new solutions are found, we need to ensure that they will b e consumed by all the consumers, as otherwise w e ma y miss parts of the computation and not fully explore the search space. A generator call C thus keeps trying its matc hing clauses un til a fix-p oint is reac hed. If no new solutions are found during one cycle of trying the matc hing clauses, then we hav e reached a fix-p oint and we can say that C is completely ev al- uated. Ho w ever, if a n umber of subgoal calls is mutually dep endent, th us forming a Str ongly Conne cte d Comp onent (SCC) , then completion is more complex and we can only complete the calls in a SCC together (Sagonas and Swift 1998). SCCs are usually represen ted by the le ader c al l , i.e., the generator call which do es not dep end on older generators. A leader call defines the next completion p oint, i.e., if no new solutions are found during one cycle of trying the matching clauses for the leader call, then we hav e reached a fix-point and w e can say that all subgoal calls in the SCC are completely ev aluated. W e next illustrate in Fig. 1 the standard execution mo del for linear tabling. At the top, the figure shows the program co de (the left b ox) and the final state of the table space (the right b ox). The program defines t wo tabled predicates, a/1 and b/1 , eac h defined by tw o clauses (clauses c1 to c4 ). The b ottom sub-figure sho ws the ev aluation sequence for the query goal a(X) . Generator calls are depicted b y blac k ov al b oxes and consumer calls by white ov al b oxes. c3 c4 c1 c2 8,18,28: fix-point check 5: fix-point check :- table a/1, b/1. a(X):- b(X). (c1) a(2). (c2) b(X):- a(X). (c3) b(1). (c4) 1: a(X) 2: b(X) 4: X=1 12: X=2 28: complete Call Solutions 1: a(X) 6: X=1 7: X=2 28: complete 7: X=2 2: b(X) 4: X=1 3: a(X) 2: b(X) 6: X=1 c3 c4 14: fix-point check 9: b(X) 13: X=1 (repeated) 10: a(X) 11: X=1 (repeated) 12: X=2 9: b(X) 15: X=1 (repeated) 16: X=2 (repeated) c2 17: X=2 (repeated) c1 19-27: ... Fig. 1. A standard linear tabled ev aluation The ev aluation starts by inserting a new entry in the table space representing 4 Miguel Ar eias and R ic ar do R o cha the generator call a(X) (step 1). Then, a(X) is resolv ed against its first matching clause, clause c1 , calling b(X) in the contin uation. As this is a first call to b(X) , we insert a new en try in the table space represen ting b(X) and proceed as shown in the b ottom left tree (step 2). Subgoal b(X) is also resolved against its first matching clause, clause c3 , calling again a(X) in the con tinuation (step 3). Since a(X) is a rep eated call, we try to consume solutions from the table space, but at this stage no solutions are a v ailable, so execution fails. W e then try the second matching clause for b(X) , clause c4 , and a first solution for b(X) , X=1 , is found and added to the table space (step 4). W e then follo w a lo c al sche duling strategy and execution fails (F reire et al. 1996). With lo cal sc heduling, new solutions are only returned to the calling en vironmen t when all program clauses w ere explored. The execution thus fails back to no de 2 and we chec k for a fix-p oint (step 5), but b(X) is not a leader call b ecause it has a dep endency (consumer no de 3) to an older call, a(X) . Remem b er that we reac h a fix-point when no new solutions are found during the last cycle of trying the matching clauses for the leader call. Next, as w e are following a local schedu ling strategy , the solution for b(X) should no w b e propagated to the con text of the previous call. W e th us propagate the solution X=1 to the con text of the generator call for a(X) , whic h originates a first solution for a(X) , X=1 (step 6). Then, we try the second matching clause for a(X) and a second solution, X=2 , is found and added to the table space (step 7). W e then bac ktrack again to the generator call for a(X) and because w e hav e already explored all matching clauses, w e chec k for a fix-p oin t (step 8). W e hav e found new solutions for b oth a(X) and b(X) , thus the current SCC is scheduled for re-ev aluation. The ev aluation then rep eats the same sequence as in steps 2 and 3 (now steps 9 and 10), but at this time the consumer call for a(X) has solutions in the table. Solution X=1 is first forw arded to it, whic h originates a rep eated solution for b(X) (step 11) and th us execution fails. Then, solution X=2 is also forw ard to it and a new solution for b(X) is found. In the con tinuation, w e find another rep eated solution for b(X) (step 13) and we fail a second time in the fix-point chec k for b(X) (step 14). Again, as w e are following a lo cal scheduling strategy , the solutions for b(X) are propagated to the con text of the generator call for a(X) , but only rep eated solutions are found (steps 15 and 16). Clause c2 is then explored, but without any further dev elopments (step 17). W e then backtrac k one more time to the generator call for a(X) and b ecause w e ha ve found a new solution for b(X) during the last iteration, the current SCC is sc heduled again for re-ev aluation (step 18). The re-ev aluation of the SCC do es not find new solutions for both a(X) and b(X) (steps 19 to 27). Th us, when bac ktracking again to a(X) we ha ve reached a fix-p oint and b ecause a(X) is a leader call, we can declare the t wo subgoal calls to b e completed (step 28). 3 Linear T abling Strategies The standard linear tabling mechanism uses a naive approach to ev aluate tabled logic programs. Every time a new solution is found during the last round of ev alua- tion, the complete search space for the current SCC is scheduled for re-ev aluation. On Combining Line ar-Base d Str ate gies for T able d Evaluation 5 Ho wev er, some branches of the SCC can b e av oided, since it is p ossible to know b eforehand that they will only lead to rep eated computations, hence not finding an y new solutions. Next, we will present three different strategies for optimizing the standard linear tabled ev aluation. The common goal of these strategies is to minimize the n umber of branches to be explored, thus reducing the searc h space, and each strategy tries to fo cus on different asp ects of the ev aluation to achiev e it. 3.1 Dynamic R e or dering of Exe cution The first optimization, that we call Dynamic R e or dering of Exe cution (DRE) , is based on the original SLDT strategy , as prop osed b y Zhou et al. (Zhou et al. 2000). The key idea of the DRE strategy is to give priorit y to the program clauses instead of consuming answers, and to achiev e that it lets rep eated calls to tabled subgoals execute from the b acktr acking clause of the former c al l . A first call to a tabled subgoal is called a pione er and rep eated calls are called fol lowers of the pioneer. When backtrac king to a pioneer or a follow er, we use the same strategy , first we explore the remaining clauses and only then w e try to consume solutions. The fix- p oin t chec k op eration is still only p erformed b y pioneer calls. Figure 2 uses the same example from Fig. 1 to illustrate how DRE ev aluation works. c1 c2 10,19: fix-point check :- table a/1, b/1. a(X):- b(X). (c1) a(2). (c2) b(X):- a(X). (c3) b(1). (c4) 1: a(X) 2: b(X) 5: X=2 6: X=1 19: complete Call Solutions 1: a(X) 4: X=2 9: X=1 19: complete 4: X=2 2: b(X) 8: X=2 (repeated) 9: X=1 c3 c4 7: fix-point check 2: b(X) 6: X=1 3: a(X) 3: a(X) 11-18: ... 5: X=2 Fig. 2. Using the DRE strategy to ev aluate the program in Fig. 1 As for the standard strategy , the ev aluation starts with first (pioneer) calls to a(X) (step 1) and b(X) (step 2), and then, in the con tin uation, a(X) is called rep eatedly (step 3). But no w with DRE ev aluation, a(X) is considered a follow er and thus we ste al the backtrac king clause of the former call at node 1, i.e., the second matching clause for a(X) , clause c2 . The ev aluation then pro ceeds as for a generator call (right upper tree in Fig. 2), which means that new solutions can b e generated for a(X) . W e thus try c2 , and a first solution for a(X) , X=2 , is found and added to the table space (step 4). W e then follow a lo cal sc heduling strategy and execution fails backtrac king to the follow er no de. As b oth matching clauses for 6 Miguel Ar eias and R ic ar do R o cha a(X) were already taken, we try to consume solutions. The solution X=2 is then propagated to the context of b(X) whic h originates the solution X=2 (step 5). Next, in step 6 we find the second solution for b(X) and in step 7 we chec k for a fix-p oint, but b(X) is not a leader call because it has a dep endency (follow er node 3) to an older call, a(X) . The solutions for b(X) are then propagated to the context of the pioneer call for a(X) , which originates a second solution for a(X) , X=1 (step 9). W e then backtrac k to the pioneer call for a(X) and b ecause we hav e already explored the matching clause c2 in the follow er no de 3, we c heck for a fix-p oin t. Since we ha ve found new solutions during the last iteration, the current SCC is scheduled for re-ev aluation (step 10). The re-ev aluation of the SCC do es not find any further solutions (steps 11 to 18), and th us the ev aluation can b e completed at step 19. 3.2 Dynamic R e or dering of Alternatives The key idea of the Dynamic R e or dering of Alternatives (DRA) strategy , as origi- nally proposed b y Guo and Gupta (Guo and Gupta 2001), is to memoize the clauses (or alternatives) leading to consumer calls, the lo oping alternatives , in such a w a y that when sc heduling an SCC for re-ev aluation, instead of trying the full set of matc hing clauses, we only try the lo oping alternatives. Initially , a generator call C explores the matching clauses as in standard linear tabled ev aluation and, if a consumer call is found, the current clause for C is memoized as a lo oping alternative. After exploring all the matching clauses, C en ters the lo oping state and from this p oint on, it only tries the lo oping alternatives un til a fix-p oint is reac hed. Figure 3 uses again the same example from Fig. 1 to illustrate ho w DRA ev aluation works. c3 c4 c1 c2 8,16,24: fix-point check 5: fix-point check :- table a/1, b/1. a(X):- b(X). (c1) a(2). (c2) b(X):- a(X). (c3) b(1). (c4) 1: a(X) 7: X=2 2: b(X) 4: X=1 3: a(X) 2: b(X) c3 13: fix-point check 9: b(X) 10: a(X) 11: X=1 (repeated) 12: X=2 9: b(X) 14: X=1 (repeated) 15: X=2 (repeated) c1 17-23: ... 1: a(X) 2: b(X) 4: X=1 12: X=2 24: complete Call Solutions 6: X=1 7: X=2 24: complete Looping Alternatives 3: c1 3: c3 6: X=1 Fig. 3. Using the DRA strategy to ev aluate the program in Fig. 1 The ev aluation sequence for the first SCC round (steps 2 to 7) is identical to the On Combining Line ar-Base d Str ate gies for T able d Evaluation 7 standard ev aluation of Fig. 1. The difference is that this round is also used to detect the alternativ es leading to consumers calls. W e only ha v e one consumer call at node 3 for a(X) . The clauses in ev aluation up to the corresponding generator, call a(X) at no de 1, are th us marked as looping alternativ es and added to the resp ective table entries. This includes alternative c3 for b(X) and alternative c1 for a(X) . As for the standard strategy , the SCC is then scheduled for tw o extra re-ev aluation rounds (steps 9 to 15 and steps 17 to 23), but now only the lo oping alternatives are ev aluated, which means that the clauses c2 and c4 are ignored. 3.3 Dynamic R e or dering of Solutions The last optimization, that we named Dynamic R e or dering of Solutions (DRS) , is a new prop osal that can b e seen as a v ariant of the DRA strategy , but applied to the consumption of solutions. The key idea of the DRS strategy is to memoize the solutions leading to consumer calls, the lo oping solutions . When a non-leader gen- erator call C consumes solutions to propagate them to the context of the previous call, if a consumer call is found, the current solution for C is memoized as a lo oping solution. Later, if C is sc heduled for re-ev aluation, instead of trying the full set of solutions, it only tries the lo oping solutions plus the new solutions found during the current round. In eac h round, the new solutions leading to consumer calls are added to the previous set of lo oping solutions. In Fig. 4, we use again the same example from Fig. 1 to illustrate ho w DRS ev aluation works. c3 c4 c1 c2 8,17,25: fix-point check 5: fix-point check :- table a/1, b/1. a(X):- b(X). (c1) a(2). (c2) b(X):- a(X). (c3) b(1). (c4) 1: a(X) 7: X=2 2: b(X) 4: X=1 3: a(X) 2: b(X) c3 c4 14: fix-point check 9: b(X) 13: X=1 (repeated) 10: a(X) 11: X=1 (repeated) 12: X=2 9: b(X) 15: X=2 (repeated) c2 16: X=2 (repeated) c1 18-24: ... 1: a(X) 2: b(X) 4: X=1 12: X=2 25: complete Call Solutions 6: X=1 7: X=2 25: complete Looping Solutions 6: X=1 Fig. 4. Using the DRS strategy to ev aluate the program in Fig. 1 In this example, w e only hav e one non-leader generator call, b(X) , whic h is called once for each ev aluation round o ver the SCC (steps 2, 9 and 18 in Fig. 4). By follo wing the ev aluation, it is p ossible to v erify that no solutions are mark ed as lo oping solutions, and thus, on eac h round, b(X) only consumes the new solutions 8 Miguel Ar eias and R ic ar do R o cha found during the round. This means that solution X=1 only is consumed on the first round (step 6), solution X=2 only is consumed on the second round (step 15) and no solution is consumed on the last round. 4 Implemen tation Details This section describ es some implementation details regarding the integration of the three strategies on top of the Y ap engine, with particular fo cus on the table space data structures and on the tabling op erations. 4.1 T able Sp ac e T o implement the table space, Y ap uses tries which is considered a very efficien t wa y to implement the table space (Ramakrishnan et al. 1999). T ries are trees in whic h common prefixes are represented only once. T ries provide complete discrimination for terms and p ermit lo ok up and insertion to b e done in a single pass. Figure 5 details the table space organization for the example used on the previous sections. subgoal trie table entry for a/1 VAR0 subgoal frame for a(VAR0) solution trie 1 2 subgoal trie table entry for b/1 VAR0 subgoal frame for b(VAR0) solution trie 1 2 Table Space Fig. 5. T able space organization As other tabling engines, Y ap uses tw o lev els of tries: one for the subgoal calls and other for the com- puted solutions. A tabled predicate ac- cesses the table space through a sp ecific ta- ble entry data struc- ture. Eac h differen t subgoal call is rep- resen ted as a unique path in the sub go al trie and each differ- en t solution is represen ted as a unique path in the solution trie . A k ey data structure in this organization is the sub go al fr ame . Subgoal frames are used to store informa- tion about each tabled subgoal call, namely: the en try point to the solution trie; the state of the subgoal ( r e ady , evaluating or c omplete ); supp ort to detect if the subgoal is a leader call; and support to detect if new solutions were found during the last round of ev aluation. The DRE, DRA and DRS strategies extend the subgoal frame data structure with the follo wing extra information: DRE: the pioneer call; and the backtrac king clause of the former call. DRA: support to detect, store and load lo oping alternatives; and tw o new states, lo op r e ady and lo op evaluating , used to detect, resp ectiv ely , generator and con- sumer calls in re-ev aluating rounds. DRS: support to detect, store and load lo oping solutions. On Combining Line ar-Base d Str ate gies for T able d Evaluation 9 As these extensions are sp ecific to eac h strategy , as we will see next, they can b e com bined without ma jor ov erheads. 4.2 T abling Op er ations W e next introduce the pseudo-code for the main tabling operations required to supp ort the integration of the three strategies on top of Y ap. W e start with Fig. 6 showing the pseudo-co de for the new solution op eration. Initially , the op eration simply inserts the given solution SOL in the solution trie structure for the given subgoal frame SF and, if the solution is new, it up dates the SgFr new solutions subgoal frame field to TRUE . If DRS mode is enabled for the subgoal, it also marks the newest solution found during the current round. W e then implemen t a lo cal scheduling strategy and alwa ys fail at the end. new_solution(solution SOL, subgoal frame SF) { if (solution_check_insert(SOL,SF) == TRUE) { // new solution SgFr_new_solutions(SF) = TRUE if (DRS_mode(SF) && first_solution_in_current_round(SF) == NULL) first_solution_in_current_round(SF) = SOL } fail() // local scheduling } Fig. 6. Pseudo-code for the new solution op eration Figure 7 shows the pseudo-co de for the table d c al l op eration. New calls to tabled subgoals are inserted into the table space by allo cating the necessary data struc- tures. This includes allo cating and initializing a new subgoal frame to represent the giv en subgoal call (this is the case where the state of SF is ready ). In such case, the tabled call op eration then up dates the state of SF to evaluating ; stores a new generator no de 2 ; and pro ceeds b y executing the current alternative. On the other hand, if the subgoal call is a rep eated call, then the subgoal frame is already in the table space, and three different situations may o ccur. First, if the call is already ev aluated (this is the case where the state of SF is complete ), the op eration consumes the a v ailable solutions b y implemen ting the c omplete d table optimization whic h executes compiled co de directly from the solution trie structure asso ciated with the completed call (Ramakrishnan et al. 1999). Second, if the call is a first call in a re-ev aluating round (this is the case where the state of SF is loop ready ), the op eration up dates the state of SF to loop evaluating ; stores a new generator no de; and pro ceeds by re-executing the first looping alter- nativ e or the first matching alternative, according to whether the DRA mo de is enabled or disabled for the subgoal. Third, if the call is a consumer call (this is the case where the state of SF is evaluating or loop evaluating ), the op eration first marks the current branch as 2 Generator, consumer and follow er no des are implemen ted as W AM choice points extended with some extra fields. 10 Miguel Ar eias and R ic ar do R o cha tabled_call(subgoal call SC) { SF = subgoal_check_insert(SC) // SF is the subgoal frame for SC if (SgFr_state(SF) == ready) { // first round SgFr_state(SF) = evaluating store_generator_node() goto execute(current_alternative()) } else if (SgFr_state(SF) == loop_ready) { // re-evaluation round SgFr_state(SF) = loop_evaluating store_generator_node() if (DRA_mode(SF)) goto execute(first_looping_alternative()) else goto execute(first_alternative()) } else if (SgFr_state(SF) == evaluating or // first round SgFr_state(SF) == loop_evaluating) { // re-evaluation round mark_current_branch_as_a_non_leader_branch(SF) if (DRA_mode(SF) or DRS_mode(SF)) mark_current_branch_as_a_looping_branch(SF) if (DRE_mode(SF) && has_unexploited_alternatives(SF)) { store_follower_node() if (DRA_mode(SF) and SgFr_state(SF) == loop_evaluating) goto execute(next_looping_alternative()) else goto execute(next_alternative()) } else { store_consumer_node() goto consume_solutions(SF) } } else if (SgFr_state(SF) == complete) // already evaluated goto completed_table_optimization(SF) } Fig. 7. Pseudo-code for the table d c al l op eration a non-leader branch and, if in DRA or DRS mo de, it also marks the current branch as a looping branc h. Next, if DRE mode is enabled and there are unexploited alternativ es (i.e., there is a bac ktracking clause for the former call), it stores a follo wer no de and pro ceeds by re-executing the next lo oping alternative or the next matc hing alternativ e, according to whether the DRA mo de is enabled or disabled for the subgoal. Otherwise, it stores a new consumer no de and starts consuming the a v ailable solutions. T o mark the current branc h as a non-leader branc h, w e follow all in termediate generator calls in ev aluation up to the generator call for frame SF and we mark them as non-leader calls (note that the call at hand defines a new dep endency for the curren t SCC). T o mark the curren t branch as a looping branch, w e follow all in termediate generator calls in ev aluation up to the generator call for frame SF and w e mark the alternatives b eing ev aluated or the solutions b eing consumed b y each call, resp ectiv ely , as lo oping alternatives or lo oping solutions. Finally , we discuss in more detail how completion is detected. Remember that after exploring the last matching clause for a tabled call, w e execute the fix-p oint che ck op eration. Figure 8 sho ws the pseudo-co de for its implementation. The fix-point c hec k op eration starts b y c hecking if the subgoal at hand is a leader call. If it is leader and has found new solutions during the curren t round, then the On Combining Line ar-Base d Str ate gies for T able d Evaluation 11 fix-point_check(subgoal frame SF) { if (SgFr_is_leader(SF)){ if (SgFr_new_solutions(SF)) { // start a new round SgFr_new_solutions(SF) = FALSE for each (subgoal in current SCC) SgFr_state(subgoal) = loop_ready SgFr_state(SF) = loop_evaluating if (DRA_mode(SF)) goto execute(first_looping_alternative()) else goto execute(first_alternative()) } else { // complete subgoals in current SCC for each (subgoal in current SCC) SgFr_state(subgoal) = complete goto completed_table_optimization(SF) // local scheduling } else { // not a leader call if (SgFr_new_solutions(SF)) // propagate new solutions SgFr_new_solutions(current_leader(SF)) = TRUE SgFr_new_solutions(SF) = FALSE // reset new solutions // local scheduling if (DRS_mode(SF)) goto consume_looping_solutions_and_solutions_in_current_round(SF) else goto consume_solutions(SF) } } Fig. 8. Pseudo-code for the fix-p oint che ck operation curren t SCC is scheduled for a re-ev aluation. If it is leader but no new solutions w ere found during the curren t round, then w e ha v e reac hed a fix-point and th us, the subgoals in the current SCC are marked as completed and the ev aluation pro ceeds with the completed table optimization. Otherwise, if the subgoal is not a leader call, then it propagates the new solutions information to the curren t leader of the SCC and starts consuming the a v ailable solutions. If DRS mo de is enabled, it only consumes the lo oping solutions and the solutions found during the current round, otherwise it consumes all solutions. 5 Exp erimen tal Results T o the b est of our kno wledge, Y ap is no w the first tabling engine that in tegrates and supp orts the combination of different linear tabling strategies. W e ha ve th us the conditions to better understand the adv antages and w eaknesses of eac h strategy when used solely or com bined with the others. In what follo ws, we present initial exp erimen ts comparing linear tabled ev aluation with and without support for the DRE, DRA and DRS strategies. The environmen t for our exp eriments was a PC with a 2.83 GHz Intel(R) Core(TM)2 Quad CPU and 4 GBytes of memory running the Lin ux kernel 2.6.32-27-generic-pae with Y ap 6.0.7. T o put the p erformance results in persp ective, we used t wo right recursive defi- nitions of the well-kno wn path/ 2 predicate, that computes the transitive closure in a graph, com bined with sev eral differen t configurations of edg e/ 2 facts. One path definition has the recursiv e clause first and the other has the recursive clause last. 12 Miguel Ar eias and R ic ar do R o cha path_first(X,Z) :- sld1, edge(X,Y), path_first(Y,Z), sld2. path_first(X,Z) :- sld3, edge(X,Z), sld4. path_last(X,Z) :- sld3, edge(X,Z), sld4. path_last(X,Z) :- sld1, edge(X,Y), path_last(Y,Z), sld2. Cycle (depth 3) Grid (depth 3) Pyramid (depth 3) Fig. 9. An example of edge configura- tions Regarding the edge facts, we used three configurations: a pyramid, a cycle and a grid configuration (Fig. 9 sho ws an ex- ample for each configuration). W e exp er- imen ted the pyramid and cycle configu- rations with depths 1000, 2000 and 3000 and the grid configuration with depths 20, 30 and 40. All exp eriments find all the solutions for the problem. W e c hose these exp erimen ts b ecause the path/ 2 predicate implemen ts a relativ ely easy to under- stand pattern of computation and its righ t recursive definition creates several in ter- dep endencies b et ween tabled subgoals. Notice also that in the definitions ab ov e we included four extra SLD (non-tabled) predicates (the sld1/0 , sld2/0 , sld3/0 and sld4/0 predicates) in order to measure how the mixing with SLD computations can affect the base p erformance. First, in T able 1, we sho w the execution time, in milliseconds, for standard linear tabled ev aluation with lo cal scheduling and the ratios comparing standard linear tabling against DRE, DRA and DRS solely and combined strategies ( A l l means DRE+DRA+DRS) for the t w o definitions of the path/ 2 predicate without including the four extra SLD computations. Ratios higher than 1.00 mean that the respective strategies ha ve a positive impact on the execution time. The results obtained are the a verage of ten runs for each configuration. In addition to the results presen ted in T able 1, we also collected several statistics regarding imp ortant asp ects of the ev aluation (not fully presen ted here due to lack of space). In T able 2, we show some of these statistics for standard linear tabled ev aluation and the ratios against the several strategies for the particular ev aluation of the grid configuration with depth 40. The Alts column sho ws the n umber of alternativ es explored during the ev aluation, the Sols column sho ws the n umber of solutions consumed b y generator no des corresponding to non-leader subgoals, and the SLD columns show the num ber of times each extra SLD predicate is executed. Analyzing the general picture of T able 1, the results show that, for most of these exp erimen ts, DRE ev aluation has no significant impact in the execution time. On the other hand, the results indicate that the DRA and DRS strategies are able to effectiv ely reduce the execution time for most of the experiments, when compared with standard ev aluation, and that by combining both strategies it is p ossible to obtain ev en b etter results. W e next discuss in more detail each strategy . DRE: for most of these configurations, DRE has no significant impact. F or the configurations with the recursiv e clause last and the configurations without lo ops (i.e., without inter-dependencies betw een subgoals), like the pyramid configura- On Combining Line ar-Base d Str ate gies for T able d Evaluation 13 T able 1. Execution time, in milliseconds, for standard linear tabling with lo cal sc heduling and the resp ective ratios against the several strategies using the righ t recursiv e definition of the path problem (ratios in b old mean that the use of the resp ectiv e strategies is b etter than not using some or all of them) Strategy Pyramid Cycle Grid 1000 2000 3000 1000 2000 3000 20 30 40 Recursiv e Clause First Standard 664 2,669 6,040 377 1,522 3,400 386 2,714 10,689 DRE 1.02 1.01 1.02 1.00 1.01 1.01 1.02 1.00 1.00 DRA 1.55 1.51 1.51 1.22 1.23 1.21 1.14 1.09 1.10 DRS 1.01 1.00 1.01 1.21 1.23 1.22 1.23 1.27 1.31 DRE+DRA 1.52 1.51 1.50 1.24 1.23 1.20 1.15 1.10 1.06 DRE+DRS 1.01 1.01 1.00 1.22 1.23 1.22 1.22 1.23 1.23 DRA+DRS 1.54 1.52 1.51 1.56 1.57 1.52 1.42 1.42 1.43 All 1.56 1.53 1.50 1.55 1.57 1.52 1.38 1.39 1.37 Recursiv e Clause Last Standard 673 2,775 6,216 382 1,542 3,487 365 2,602 10,403 DRE 0.99 1.01 1.01 1.01 1.01 1.01 1.02 1.03 1.03 DRA 1.47 1.49 1.47 1.24 1.22 1.22 1.15 1.13 1.11 DRS 0.99 0.99 1.01 1.20 1.21 1.23 1.21 1.27 1.30 DRE+DRA 1.49 1.34 1.43 1.24 1.22 1.22 1.14 1.12 1.10 DRE+DRS 1.00 0.99 1.01 1.23 1.22 1.23 1.22 1.27 1.30 DRA+DRS 1.47 1.47 1.46 1.55 1.54 1.53 1.42 1.43 1.43 All 1.49 1.48 1.09 1.48 1.56 1.55 1.42 1.44 1.45 tions, it is not applicable and thus no follow ers no des are ever stored. F or the cycle configurations the num ber of follow ers is also very reduced, maximum 3 follo wers, and thus its impact is insignificant. F or the grid configurations with the recursiv e clause first, the results obtained are the most in teresting. F or exam- ple, in T able 2 for the recursive clause first, DRE executes less alternatives (ratio 1.05) and consumes less solutions on non-leader generator nodes (ratio 1.04) than standard ev aluation, but even so the impact on the execution time is minimal. DRA: the results for DRA ev aluation sho w that the strategy of a v oiding the ex- ploration of non-lo oping alternatives in re-ev aluation rounds is quite effective in general. The results also show that DRA is more effective for programs without lo ops (th us without lo oping alternativ es), lik e the p yramid configurations, than for programs with larger SCCs, like the cycle and grid configurations. F or the p yramid and grid configurations, the total n umber of alternativ es explored by the other strategies is around 2 times the total num b er of alternatives explored with DRA. F or the cycle configurations, this num b er is around 1.5 times the n umber with DRA ev aluation. F or example, in T able 2, we can observe that standard ev aluation explores 1.91 times more alternatives (resp ectively 33,601 and 32,002 14 Miguel Ar eias and R ic ar do R o cha T able 2. Statistics for standard linear tabling and the resp ective ratios against the sev eral strategies for the grid configuration with depth 40 (ratios in b old mean that the use of the resp ectiv e strategies is b etter than not using some or all of them) Strategy Alts Sols SLD Computations sld1/0 sld2/0 sld3/0 sld4/0 Recursiv e Clause First Standard 70,403 50,015,215 35,202 200,974,309 35,201 149,757 DRE 1.05 1.04 1.05 1.04 1.05 1.04 DRA 1.91 1.00 1.00 1.05 21.99 12.00 DRS 1.00 19.55 1.00 1.29 1.00 1.00 DRE+DRA 1.06 1.04 1.05 1.10 1.07 1.11 DRE+DRS 1.05 19.55 1.05 1.33 1.05 1.04 DRA+DRS 1.91 19.55 1.00 1.38 21.99 12.00 All 1.06 19.55 1.05 1.43 1.07 1.11 Recursiv e Clause Last Standard 67,204 48,080,300 48,602 352,277,129 48,602 205,920 DRE 1.00 1.00 1.00 1.00 1.00 1.00 DRA 1.91 1.00 1.00 1.05 20.99 11.50 DRS 1.00 18.79 1.00 1.29 1.00 1.00 DRE+DRA 1.91 1.00 1.00 1.05 20.99 11.50 DRE+DRS 1.00 18.79 1.00 1.29 1.00 1.00 DRA+DRS 1.91 18.79 1.00 1.38 20.99 11.50 All 1.91 18.79 1.00 1.38 20.99 11.50 more alternatives for the recursiv e clause first and last) than DRA ev aluation for the grid configuration with depth 40. DRS: the results for DRS ev aluation sho w that the strategy of a v oiding the con- sumption of non-lo oping solutions in re-ev aluation rounds is quite effective for programs that can b enefit from it, lik e the cycle and grid configurations, and do not introduces relev an t costs for programs that cannot b enefit from it, like the p yramid configurations. Notice that the pyramid configurations only execute one re-ev aluation round per SCC and that we only take adv antage of DRS ev aluation starting from the second re-ev aluation round. F or the cycle and grid configura- tion, DRS optimization is used several times b ecause these configurations create a single huge SCC including all subgoal calls. F or example, in T able 2, DRS con- sumes 47,456,815 (ratio 19.55) and 45,521,900 (ratio 18.79) less solutions than standard ev aluation for the recursive clause first and last, resp ectively . Regarding the combination of the strategies, in general, our statistics show that the b est of eac h world is alwa ys presen t in the com bination. By analyzing the results in T able 1, w e can conclude that, for these exp erimen ts, by combining the DRA and DRS strategies it is p ossible to reduce even further the execution time of the ev aluation, and in most cases this reduction is higher than the sum of the reductions obtained with each strategy individually . In particular, T able 2 shows On Combining Line ar-Base d Str ate gies for T able d Evaluation 15 the same num b er of solutions and alternativ es for DRA+DRS that the resp ective DRS and DRA strategies obtain when used solely . This clearly shows the p otential of our framework and suggests that the o verhead asso ciated with this combination is negligible. When DRE is presen t, the results are, in general, w orse than the results obtained with the DRA/DRS strategies solely . In T able 2 we can observe that, for the DRE+DRA com bination, the n um b er of the alternativ es explored is far more higher than the DRA used solely and that, for the DRE+DRS combination, the non consumed solutions for DRE used s olely are included on the non consumed solutions of the DRS optimization. So, for this particular configurations, DRE is not fully orthogonal to DRA and DRS. T able 2 also shows the num ber of times each extra SLD predicate is executed for the grid configuration with depth 40. W e can read these num b ers as an estimation of the p erformance ratios that w e will obtain if the execution time of the corresp onding SLD predicate clearly ov erw eights the execution time of the other tabled and non- tabled computations. In particular, the sld2/0 predicate (placed at the end of the recursiv e clause) is the one that can potentially hav e a greater influence in the p erformance ratios as it clearly exceeds all the others in the num b er of executions. In general, these ratios show that by mixing tabled with non-tabled computations, our framework can achiev e similar and, for some cases, even b etter results than the ones presented in T able 1. In particular, the ratios for the sld2/0 predicate (the one with greater influence) are very similar to the ratios in T able 1 and for DRA ev aluation, the ratios for the sld3/0 and sld4/0 predicates are excellen t (around 22 and 12, resp ectively), showing that in tert wining SLD computations with linear tabling can affect p ositiv ely the base p erformance. 6 Conclusions W e presented a new strategy for linear tabled ev aluation of logic programs, named DRS, and a framework that in tegrates and supp orts the combination of DRS with t wo other (DRE and DRA) linear tabling strategies. W e discussed how these strate- gies can optimize different aspects of a tabled ev aluation and w e presented the relev an t implementation details for their integration on top of the Y ap system. Our exp eriments for DRS ev aluation show ed that the strategy of av oiding the consumption of non-lo oping solutions in re-ev aluation rounds can b e quite effec- tiv e for programs that can b enefit from it, with insignificant costs for the other programs. Preliminary results for the combined framework w ere also very promis- ing. In particular, the combination of DRA with DRS show ed the potential of our framew ork to reduce even further the execution time of a linear tabled ev aluation. F urther work will include adding new strategies/optimizations to our framework, suc h as the ones prop osed in (Zhou et al. 2008), and exploring the impact of applying our strategies to more complex problems, seeking real-w orld experimental results, allo wing us to improv e and consolidate our current implementation. 16 Miguel Ar eias and R ic ar do R o cha Ac knowledgmen ts This work has b een partially supp orted by the FCT researc h pro jects HORUS (PTDC/EIA-EIA/100897/2008) and LEAP (PTDC/EIA-CCO/112158/2009). References Areias, M. and Rocha, R. 2010. An Efficient Implemen tation of Linear T abling Based on Dynamic Reordering of Alternatives. In International Symp osium on Pr actic al Asp e cts of Declar ative L anguages . Number 5937 in LNCS. Springer-V erlag, 279–293. Chen, W. and W arren, D. S. 1996. T abled Ev aluation with Delaying for General Logic Programs. Journal of the ACM 43, 1, 20–74. de Guzm ´ an, P. C. , Carro, M. , and Hermenegildo, M. V. 2009. T o wards a Complete Sc heme for T abled Execution Based on Program T ransformation. In International Sym- p osium on Pr actic al Asp e cts of De clar ative L anguages . Num ber 5418 in LNCS. Springer- V erlag, 224–238. Freire, J. , Swift, T. , and W arren, D. S. 1996. Beyond Depth-First: Improving T abled Logic Programs through Alternative Sc heduling Strategies. In International Symp osium on Pro gr amming L anguage Implementation and L o gic Pr o gr amming . Number 1140 in LNCS. Springer-V erlag, 243–258. Guo, H.-F. and Gupt a, G. 2001. A Simple Scheme for Implementing T abled Logic Programming Systems Based on Dynamic Reordering of Alternatives. In International Confer enc e on L o gic Pr o gr amming . Number 2237 in LNCS. Springer-V erlag, 181–196. Lloyd, J. W. 1987. F oundations of L o gic Pr o gr amming . Springer-V erlag. Ramakrishnan, I. V. , Rao, P. , Sa gonas, K. , Swift, T. , and W arren, D. S. 1999. Efficien t Access Mechanisms for T abled Logic Programs. Journal of Lo gic Pr o gr am- ming 38, 1, 31–54. R ocha, R. , Sil v a, F. , and Santos Cost a, V. 2000. Y apT ab: A T abling Engine Designed to Supp ort Parallelism. In Confer enc e on T abulation in Parsing and De duction . 77–87. Sagonas, K. and Swift, T. 1998. An Abstract Mac hine for T abled Execution of Fixed- Order Stratified Logic Programs. A CM T r ansactions on Pr o gr amming L anguages and Systems 20, 3, 586–634. Somogyi, Z. and Sagonas, K. 2006. T abling in Mercury: Design and Implementation. In International Symp osium on Pr actic al Asp e cts of Declar ative Languages . Number 3819 in LNCS. Springer-V erlag, 150–167. T amaki, H. and Sa to, T. 1986. OLDT Resolution with T abulation. In International Confer enc e on L o gic Pr o gr amming . Number 225 in LNCS. Springer-V erlag, 84–98. Zhou, N.-F. , Sa to, T. , and Shen, Y.-D. 2008. Linear T abling Strategies and Optimiza- tions. The ory and Practic e of L o gic Pr o gr amming 8, 1, 81–109. Zhou, N.-F. , Shen, Y.-D. , Yuan, L.-Y. , and You, J.-H. 2000. Implementation of a Linear T abling Mechanism. In Pr actical Asp e cts of De clar ative L anguages . Number 1753 in LNCS. Springer-V erlag, 109–123.
Original Paper
Loading high-quality paper...
Comments & Academic Discussion
Loading comments...
Leave a Comment