• Tidak ada hasil yang ditemukan

Copy Variable and System Channel Insertion

2.4 Using the Projection Technique

2.4.2 Copy Variable and System Channel Insertion

A variable’s dependency set forms the core of its projection set in DDD, but other syntactic adjust- ments are required to make the transformation formally correct. For example, if a variable appears in the dependency sets of several other variables, copy variables need to be inserted in the sequential code. And if projection is used to pipeline the program, regular assignments such as “x :=a” need to be replaced by distributed assignments: parallel communication statements on newly created system channels, as in “XA!a,XA?x.” This section describes both of these steps.

2.4.2.1 Inserting Copy Variables

If a variable appears in the dependency sets of several other variables, DDD inserts newcopy variables into the program that can each be projected out into a different process. In general, if a variable v appears in the dependency sets of variablesai, then DDD creates a new copy variablevcpand inserts the assignment vcp := v immediately after the original assignment to v. (During projection, the creation of this variable creates a separate copy process for v.) DDD then continues by inserting other assignments of the formvai :=vcp for eachaiafter the assignment tovcp. Finally, to maintain program correctness, DDD replaces each instance ofv on the RHS of the assignment toai with the new copy variablevai.

For example, in the CHP example given below, DDD inserts copy variables acp, ax and aB to transform the programR1 intoR2 before assigning projection sets.

R1 ≡ *[ A?a; x :=a+ 1, B!a;

[a>0−→ Z!f(x) [] else−→ skip ] ]

R2 ≡ *[ A?a; acp :=a;ax :=acp,aB:=acp,aZ :=acp; x :=ax + 1, B!aB; [ aZ >0−→ Z!f(x) [] else−→ skip ] ]

Consider a slightly more complicated scenario where a variable v appears in a guard condition for a selection statement command containing multiple assignments to different variables. A copy ofv can be inserted for each of the assignments, but now they must all be incorporated in the guard condition of the rewritten program. DDD accomplishes this by replacing all instances of “v” in the guard condition with a conjunction of the new copy variables, and all instances of “¬v” with a conjunction of the inverses of the new copy variables. Thus, the process R3 in the example below can be rewritten asR4.

R3 ≡ *[ ... v:=f(w,x); ...

[ v−→ A?a, b:=f(...) [] ¬v −→ skip ] ... ]

R4 ≡ *[ ... v:=f(w,x); vcp:=v;va :=vcp,vb:=vcp;...

[ va∧vb−→ A?a, b:=f(...) [] ¬va∧ ¬vb−→ skip ] ... ]

Thus far, we have been considering communication channels to be the equivalent of regular variables in DDD. There are practical differences that must be acknowledged by our methods. While DDD can copy regular variables to create coherent projection sets, input-communication channels cannot be “copied” or “split.” Instead, DDD rewrites the code so that whenever an input channel is used, the value read in is always stored in the same variable. If this is not the case in the original code, DDD introduces a new input variable to the code for this purpose, and then inserts assignments from this new variable to the original variables immediately afterwards. (This may cause the program to no longer be in DSA form.) The input channel therefore does not directly appear in the dependency or projection sets of any variable other than the new input variable, and no split needs to be considered. In the example below, the processR5is rewritten asR6:

R5 ≡ *[ V?a; x :=f(v) ... ; V?b, W?c; Z!(b+c) ]

R6 ≡ *[ V?vin; a:=vin; x :=f(a) ... ; V?vin, W?c; b:=vin; Z!(b+c) ]

Once the appropriate copy and input variables have been inserted into the sequential program, DDD rewrites all of the dependency sets.

Note that almost all transformations described in this section amount to copy propagation ac- companied by variable renaming. (Variable renaming preserves effective reaching definitions.) The sole exception is the transformation that replaces variables in guard expressions with conjunctions of copies of themselves. In this last transformation, ifx =x1=x2, thenx1∧x2≡x and¬x1∧¬x2≡ ¬x are always true, so the guard replacement never alters the computations performed by the program.

Therefore, the transformations described here fall into the class of operation-preserving transfor- mations and since they preserve effective reaching definitions, by Theorem 1, the new program is equivalent to the original.

Q3≡*[ A?a, B?b, C?c;

Bcp!b, Bcp?bcp, Ccp!c, Ccp?ccp; AX!a, AX?ax,

BX!bcp, BX?bX, By!bcp, By?by, Cd!ccp, Cd?cd, Cy!ccp, Cy?cy; X!(aX +bX);

[cd∧cy −→ D?d; Dy!d, Dy?dy; y :=by+cy+dy

[]¬cd ∧ ¬cy −→ skip ] ]

Figure 2.6: RewritingQ1to create disjoint projection sets.

2.4.2.2 Internal Communication Channel Insertion

For projection to be formally correct, if a decomposed process includes a communications statement then the statement must exist in the original sequential code. Given an original program where variablebdepends directly on variablea, DDD must perform projection so that the value computed in the decomposed process for a is explicitly communicated to the new process for b. This is accomplished by inserting the required communication statements into the sequential program before projection.

If an assignment of the form “ab :=acp” has already been inserted into the program, then DDD can simply rewrite this assignment as “Ab!acp,Ab?ab,” where Ab is a new communication channel created for use internally within the decomposed system. Both the regular assignment and the distributed assignment assign the value ofa toab.

If, on the other hand, DS(b) is the only dependency set in which the variablea appears, then DDD creates a dummy variableab, inserts the concurrent communications statement (as above) into the program immediately following the assignment toa, and replacesaon the RHS of the assignment to b with ab. Figure 2.6 demonstrates the insertion of internal communications by rewriting the process Q1 from Figure 2.5 and creating the projection channels AX, Bcp, BX, By, Ccp, Cd, Cy, andDy.

Again, all transformations in this section are copy propagation transformations accompanied by variable renaming to preserve effective reaching definitions. (Rewriting regular assignments as communication statements can be considered simply another form of copy propagation, using the

new channel as a temporary variable.) By Theorem 1 then, the resulting programs are equivalent to the old.