260 likes | 386 Views
Code Optimization in Compaan. Problem statement. 1. Why code optimization ? 2. Where to do code optimization? 3. How to do code optimization ?. Peter Held Test (Modified). for i = 1 : 2 : 10, for j = 1 : 2 : 10, if i-3*div(i,3) <= 0, [ a(i,j) ] = funcA(); end
E N D
Problem statement • 1. Why code optimization ? • 2. Where to do code optimization? • 3. How to do code optimization ?
Peter Held Test (Modified) • for i = 1 : 2 : 10, • for j = 1 : 2 : 10, • if i-3*div(i,3) <= 0, • [ a(i,j) ] = funcA(); • end • if j-3*div(j,3) <= 0, • [ ] = funcB( a(i,j) ); • end • end • end Matlab Code:
Peter Held Test • Single Assignment Code (SAC): for i = 1 : 2 : 10, for j = 1 : 2 : 10, if -i+3*div(i,3) >= 0, [out_0]=funcA( ); [a_1(i,j)]=opd(out_0); end if -j+3*div(j,3) >= 0, d3 = div(i+1,2); if -i+2*d3-1 >= 0, d4 = div(j+1,2); if -j+2*d4-1 >= 0, d5 = div(2*d3+2,3); if -I+3*d5-3 >= 0, [in_0]=ipd(a_1(3*d5-3, 2*d4-1)); else %%if i-3*d5+2 >=0, [in_0]=ipd(a(i,j)); end end end [ ]=funcB(in_0); end end end
Parse Tree of SAC for i=1:2:10 for j=1:2:10 if -i+3*div(i,3)>=0 if -j+3*div(j,3)>=0 d3=div(i+1,2) []=funcB(in_0) [out_0]=funcA() [a_1(i,j)]=opd(out_0) if -i+2*d3-1>=0 d4=div(j+1,2) if -j+2*d4-1>=0 d5=div(2*d3+2,3) if -i+3*d5-3>=0 if i-3*d5+2>=0 [in_0]=ipd(a_1(3*d5-3,2*d4-1)) [in_0]=ipd(a(i,j)) high dimension dense polytope lattice ?
Lattice and Z-Polyhedron j j j i i Lattice L Z-Polyhedron Z i Polyhedron P P = { ( i , j ) | 1 i 6, 1 j 9 } L = { ( i , j ) | 2 i + 1, 2 j + 1 } Z = P L Z = { ( 2 i + 1 , 2 j +1 ) | 0 2 i 5, 0 2 j 8 }
Top-down Analysis <1> : For-loop j i for i = 1 : 2 : 10, for j = 1 : 2 : 10, for i = 1 : 1 : 10, for j = 1 : 1 : 10, d0 = div(i,2); if i-2*d0-1 >= 0, d1 = div(j,2); if j-2*d1-1 >= 0, for i = 1 : 1 : 10, for j = 1 : 1 : 10, if mod(i-1,2) == 0, if mod(j-1,2) == 0,
Top-down Analysis <2> : for loop + “if” for i = 1 : 1 : 10, for j = 1 : 1 : 10, d0 = div(i,2); if i-2*d0-1 >= 0, d1 = div(j,2); if j-2*d1-1 >= 0, for i = 1 : 1 : 10, for j = 1 : 1 : 10, if mod(i-1,2) == 0, if mod(j-3,6) == 0, d2 = div(j,3); if -j+3*d2 >= 0,
Top-down Analysis <3> Redundant! i j Domain of dependency: d3 = div(i+1,2); if -i+2*d3-1 >= 0, d4 = div(j+1,2); if -j+2*d4-1 >= 0, d5 = div(2*d3+2,3); if -i+3*d5-3 >= 0, [in_0] = ipd( a_1(3*d5-3, 2*d4-1) ); if mod(i-3,6) == 0, if mod(j+1,2) == 0, [in_0] =ipd(a_1(i,j));
Top-down Analysis <4> : Put it together ? Redundant! for i = 1 : 1 : 10, for j = 1 : 1 : 10, if mod(i-1,2) == 0, if mod(j-3,6) == 0, if mod(i-3,6) == 0, if mod(j+1,2) == 0, [in_0]=ipd(a_1(i,j));
Holy Cup Lattice Intersection for i = 1 : 1 : 10, for j = 1 : 1 : 10, if mod(i-3,6) == 0, if mod(j-3,6) == 0, [in_0]=ipd(a_1(i,j));
How to get the lattice? lattice? for i = 1 : 1 : 10, for j = 1 : 1 : 10, d0 = div(i,2); if i-2*d0-1 >= 0, d1 = div(j,2); if j-2*d1-1 >= 0, d2 = div(j,3); if -j+3*d2 >= 0, d3 = div(i+1,2); if -i+2*d3-1 >= 0, d4 = div(j+1,2); if -j+2*d4-1 >= 0, d5 = div(2*d3+2,3); if -i+3*d5-3 >= 0, [in_0] = ipd( a_1(3*d5-3, 2*d4-1) ); else if i-3*d5+2 >= 0, [in_0] = ipd( a(i,j) ); end end end [] = funcB( in_0 ); end end end
System of inequalities div if
Solution Particular solution not in the null space • 1. N has a null space of dimension 2, so there exists 2 linearly independent vectors ( c1, c2 ) or C2 such that : N ( C2Kf ) == 0 • So, if is a solution, then + C2 Kf is also • a solution. • 2. Hermite Normal Form ( HNF ) • N C = [ H 0 ] => N [ C1 C2 ] =0 => N C2 Kf = 0 • So, I = C21 Kf + O
Solution (cont.) So O=C11Kb Bounded by(*) & (**).
Sum up • So, the lattice corresponding to the polytope is defined by: • I = C21 Kf + O & O = C11 Kb • b1 <= N C1 Kb <= b2 • Nq C1 Kb <= bq • In our example, • C21 = 6 0 & O = 9 • 0 6 3
Else statement O= 9 ipd(a_1(i,j)) 3 O= 7 ipd(a(i,j)) 3 O= 5 ipd(a(i,j)) 3 … … d5 = div(2*d3+2,3); if -i+3*d5-3 >= 0, [in_0]=ipd(a_1(3*d5-3, 2*d4-1)); else %%if i-3*d5+2 >= 0, [in_0]=ipd(a(i,j)); end … …
Altogether for i = 1 : 1 : 10, for j = 1 : 1 : 10, if mod(i-3,6) == 0, if mod(j-1,2) == 0, [out_0]=funcA(); [a_1(i,j)]=opd(out_0); if mod(j-3,6) == 0, if mod(i-3,6) == 0, [in_0]=ipd(a_1(i,j)); else if mod(i-5,6) == 0, [in_0]=ipd(a(i,j)); else if mod(i-7,6) == 0, [in_0]=ipd(a(i,j)); end [ ] = funcB(in_0); end end end
Conclusion <1> • By introducing lattice, we can reduce the complexity of the program. The improvements are as follows: • No control variables ; • Less div and if statements ; • The data dependency becomes explicit.
Is that the whole story? d = div(i-3, 6); if i-3-6*d <= 0, • Observation : • We have to express those “mod”s with “div”s + “if”s : if mod(i-3, 6) == 0
Further analysis Z-Polyhedron Polyhedral image ?
Further analysis (cont.) Polyhedral preimage
Further analysis (cont.) So, if we set O = [ -3 -3 ], which gives us the same system, we can express the mod with a div and a if: d = div(i+3, 6); if i+3-6*d <= 0, if mod(i+3, 6) == 0
Conclusion <2> • Providing that the lattice is invertable, we can get a dense polyhedron by preimaging the Z-polyhedron. The integer points in the dense polyhedron have a one-to-one affine mapping to the integer points in the Z-polyhedron.
Things to be done • 1. Compute the offset. Pip ? or … … • 2. Rewrite the program in a reduced form using lattice ; • 3. Testing.